mirror of https://github.com/apache/cloudstack.git
simulator managers commit to simulator db
All the simulator related managers will persist to a seperate database from `cloud`. Moving related transaction code. Transction responsibilty will be at simulator agent's dao layer
This commit is contained in:
parent
4bf16f8d31
commit
efc58cdea3
|
|
@ -1,89 +0,0 @@
|
|||
-- Licensed to the Apache Software Foundation (ASF) under one
|
||||
-- or more contributor license agreements. See the NOTICE file
|
||||
-- distributed with this work for additional information
|
||||
-- regarding copyright ownership. The ASF licenses this file
|
||||
-- to you under the Apache License, Version 2.0 (the
|
||||
-- "License"); you may not use this file except in compliance
|
||||
-- with the License. You may obtain a copy of the License at
|
||||
--
|
||||
-- http://www.apache.org/licenses/LICENSE-2.0
|
||||
--
|
||||
-- Unless required by applicable law or agreed to in writing,
|
||||
-- software distributed under the License is distributed on an
|
||||
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
-- KIND, either express or implied. See the License for the
|
||||
-- specific language governing permissions and limitations
|
||||
-- under the License.
|
||||
DROP TABLE IF EXISTS `cloud`.`mockhost`;
|
||||
DROP TABLE IF EXISTS `cloud`.`mocksecstorage`;
|
||||
DROP TABLE IF EXISTS `cloud`.`mockstoragepool`;
|
||||
DROP TABLE IF EXISTS `cloud`.`mockvm`;
|
||||
DROP TABLE IF EXISTS `cloud`.`mockvolume`;
|
||||
|
||||
CREATE TABLE `cloud`.`mockhost` (
|
||||
`id` bigint unsigned NOT NULL auto_increment,
|
||||
`name` varchar(255) NOT NULL,
|
||||
`private_ip_address` char(40),
|
||||
`private_mac_address` varchar(17),
|
||||
`private_netmask` varchar(15),
|
||||
`storage_ip_address` char(40),
|
||||
`storage_netmask` varchar(15),
|
||||
`storage_mac_address` varchar(17),
|
||||
`public_ip_address` char(40),
|
||||
`public_netmask` varchar(15),
|
||||
`public_mac_address` varchar(17),
|
||||
`guid` varchar(255) UNIQUE,
|
||||
`version` varchar(40) NOT NULL,
|
||||
`data_center_id` bigint unsigned NOT NULL,
|
||||
`pod_id` bigint unsigned,
|
||||
`cluster_id` bigint unsigned COMMENT 'foreign key to cluster',
|
||||
`cpus` int(10) unsigned,
|
||||
`speed` int(10) unsigned,
|
||||
`ram` bigint unsigned,
|
||||
`capabilities` varchar(255) COMMENT 'host capabilities in comma separated list',
|
||||
`vm_id` bigint unsigned,
|
||||
`resource` varchar(255) DEFAULT NULL COMMENT 'If it is a local resource, this is the class name',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `cloud`.`mocksecstorage` (
|
||||
`id` bigint unsigned NOT NULL auto_increment,
|
||||
`url` varchar(255),
|
||||
`capacity` bigint unsigned,
|
||||
`mount_point` varchar(255),
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `cloud`.`mockstoragepool` (
|
||||
`id` bigint unsigned NOT NULL auto_increment,
|
||||
`guid` varchar(255),
|
||||
`mount_point` varchar(255),
|
||||
`capacity` bigint,
|
||||
`pool_type` varchar(40),
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
|
||||
CREATE TABLE `cloud`.`mockvm` (
|
||||
`id` bigint unsigned NOT NULL auto_increment,
|
||||
`name` varchar(255),
|
||||
`host_id` bigint unsigned,
|
||||
`type` varchar(40),
|
||||
`state` varchar(40),
|
||||
`vnc_port` bigint unsigned,
|
||||
`memory` bigint unsigned,
|
||||
`cpu` bigint unsigned,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
|
||||
CREATE TABLE `cloud`.`mockvolume` (
|
||||
`id` bigint unsigned NOT NULL auto_increment,
|
||||
`name` varchar(255),
|
||||
`size` bigint unsigned,
|
||||
`path` varchar(255),
|
||||
`pool_id` bigint unsigned,
|
||||
`type` varchar(40),
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
|
|
@ -33,28 +33,34 @@ import com.cloud.simulator.MockHost;
|
|||
import com.cloud.utils.component.Manager;
|
||||
|
||||
public interface MockAgentManager extends Manager {
|
||||
public static final long DEFAULT_HOST_MEM_SIZE = 8 * 1024 * 1024 * 1024L; // 8G, unit of
|
||||
// Mbytes
|
||||
public static final int DEFAULT_HOST_CPU_CORES = 4; // 2 dual core CPUs (2 x
|
||||
// 2)
|
||||
public static final int DEFAULT_HOST_SPEED_MHZ = 8000; // 1 GHz CPUs
|
||||
boolean configure(String name, Map<String, Object> params) throws ConfigurationException;
|
||||
public static final long DEFAULT_HOST_MEM_SIZE = 8 * 1024 * 1024 * 1024L; // 8G,
|
||||
// unit
|
||||
// of
|
||||
// Mbytes
|
||||
public static final int DEFAULT_HOST_CPU_CORES = 4; // 2 dual core CPUs (2 x
|
||||
// 2)
|
||||
public static final int DEFAULT_HOST_SPEED_MHZ = 8000; // 1 GHz CPUs
|
||||
|
||||
Map<AgentResourceBase, Map<String, String>> createServerResources(Map<String, Object> params);
|
||||
boolean configure(String name, Map<String, Object> params) throws ConfigurationException;
|
||||
|
||||
boolean handleSystemVMStart(long vmId, String privateIpAddress, String privateMacAddress, String privateNetMask, long dcId, long podId, String name, String vmType, String url);
|
||||
Map<AgentResourceBase, Map<String, String>> createServerResources(Map<String, Object> params);
|
||||
|
||||
boolean handleSystemVMStop(long vmId);
|
||||
boolean handleSystemVMStart(long vmId, String privateIpAddress, String privateMacAddress, String privateNetMask,
|
||||
long dcId, long podId, String name, String vmType, String url);
|
||||
|
||||
GetHostStatsAnswer getHostStatistic(GetHostStatsCommand cmd);
|
||||
Answer checkHealth(CheckHealthCommand cmd);
|
||||
Answer pingTest(PingTestCommand cmd);
|
||||
|
||||
Answer prepareForMigrate(PrepareForMigrationCommand cmd);
|
||||
|
||||
MockHost getHost(String guid);
|
||||
boolean handleSystemVMStop(long vmId);
|
||||
|
||||
Answer maintain(MaintainCommand cmd);
|
||||
GetHostStatsAnswer getHostStatistic(GetHostStatsCommand cmd);
|
||||
|
||||
Answer checkHealth(CheckHealthCommand cmd);
|
||||
|
||||
Answer pingTest(PingTestCommand cmd);
|
||||
|
||||
Answer prepareForMigrate(PrepareForMigrationCommand cmd);
|
||||
|
||||
MockHost getHost(String guid);
|
||||
|
||||
Answer maintain(MaintainCommand cmd);
|
||||
|
||||
Answer checkNetworkCommand(CheckNetworkCommand cmd);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,321 +61,408 @@ import com.cloud.utils.Pair;
|
|||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.concurrency.NamedThreadFactory;
|
||||
import com.cloud.utils.db.DB;
|
||||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.utils.net.NetUtils;
|
||||
|
||||
@Local(value = { MockAgentManager.class })
|
||||
public class MockAgentManagerImpl implements MockAgentManager {
|
||||
private static final Logger s_logger = Logger.getLogger(MockAgentManagerImpl.class);
|
||||
@Inject HostPodDao _podDao = null;
|
||||
@Inject MockHostDao _mockHostDao = null;
|
||||
@Inject MockVMDao _mockVmDao = null;
|
||||
@Inject SimulatorManager _simulatorMgr = null;
|
||||
@Inject AgentManager _agentMgr = null;
|
||||
@Inject MockStorageManager _storageMgr = null;
|
||||
@Inject ResourceManager _resourceMgr;
|
||||
private SecureRandom random;
|
||||
private Map<String, AgentResourceBase> _resources = new ConcurrentHashMap<String, AgentResourceBase>();
|
||||
private ThreadPoolExecutor _executor;
|
||||
private static final Logger s_logger = Logger.getLogger(MockAgentManagerImpl.class);
|
||||
@Inject
|
||||
HostPodDao _podDao = null;
|
||||
@Inject
|
||||
MockHostDao _mockHostDao = null;
|
||||
@Inject
|
||||
MockVMDao _mockVmDao = null;
|
||||
@Inject
|
||||
SimulatorManager _simulatorMgr = null;
|
||||
@Inject
|
||||
AgentManager _agentMgr = null;
|
||||
@Inject
|
||||
MockStorageManager _storageMgr = null;
|
||||
@Inject
|
||||
ResourceManager _resourceMgr;
|
||||
private SecureRandom random;
|
||||
private Map<String, AgentResourceBase> _resources = new ConcurrentHashMap<String, AgentResourceBase>();
|
||||
private ThreadPoolExecutor _executor;
|
||||
|
||||
private Pair<String, Long> getPodCidr(long podId, long dcId) {
|
||||
try {
|
||||
|
||||
HashMap<Long, List<Object>> podMap = _podDao
|
||||
.getCurrentPodCidrSubnets(dcId, 0);
|
||||
List<Object> cidrPair = podMap.get(podId);
|
||||
String cidrAddress = (String) cidrPair.get(0);
|
||||
Long cidrSize = (Long)cidrPair.get(1);
|
||||
return new Pair<String, Long>(cidrAddress, cidrSize);
|
||||
} catch (PatternSyntaxException e) {
|
||||
s_logger.error("Exception while splitting pod cidr");
|
||||
return null;
|
||||
} catch(IndexOutOfBoundsException e) {
|
||||
s_logger.error("Invalid pod cidr. Please check");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Pair<String, Long> getPodCidr(long podId, long dcId) {
|
||||
try {
|
||||
|
||||
private String getIpAddress(long instanceId, long dcId, long podId) {
|
||||
Pair<String, Long> cidr = this.getPodCidr(podId, dcId);
|
||||
return NetUtils.long2Ip(NetUtils.ip2Long(cidr.first()) + instanceId);
|
||||
}
|
||||
|
||||
private String getMacAddress(long dcId, long podId, long clusterId, int instanceId) {
|
||||
return NetUtils.long2Mac((dcId << 40 + podId << 32 + clusterId << 24 + instanceId));
|
||||
}
|
||||
public synchronized int getNextAgentId(long cidrSize) {
|
||||
return random.nextInt((int)cidrSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
@DB
|
||||
public Map<AgentResourceBase, Map<String, String>> createServerResources(
|
||||
Map<String, Object> params) {
|
||||
|
||||
Map<String, String> args = new HashMap<String, String>();
|
||||
Map<AgentResourceBase, Map<String,String>> newResources = new HashMap<AgentResourceBase, Map<String,String>>();
|
||||
AgentResourceBase agentResource;
|
||||
long cpuCore = Long.parseLong((String)params.get("cpucore"));
|
||||
long cpuSpeed = Long.parseLong((String)params.get("cpuspeed"));
|
||||
long memory = Long.parseLong((String)params.get("memory"));
|
||||
long localStorageSize = Long.parseLong((String)params.get("localstorage"));
|
||||
synchronized (this) {
|
||||
long dataCenterId = Long.parseLong((String)params.get("zone"));
|
||||
long podId = Long.parseLong((String)params.get("pod"));
|
||||
long clusterId = Long.parseLong((String)params.get("cluster"));
|
||||
long cidrSize = getPodCidr(podId, dataCenterId).second();
|
||||
HashMap<Long, List<Object>> podMap = _podDao.getCurrentPodCidrSubnets(dcId, 0);
|
||||
List<Object> cidrPair = podMap.get(podId);
|
||||
String cidrAddress = (String) cidrPair.get(0);
|
||||
Long cidrSize = (Long) cidrPair.get(1);
|
||||
return new Pair<String, Long>(cidrAddress, cidrSize);
|
||||
} catch (PatternSyntaxException e) {
|
||||
s_logger.error("Exception while splitting pod cidr");
|
||||
return null;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
s_logger.error("Invalid pod cidr. Please check");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
int agentId = getNextAgentId(cidrSize);
|
||||
String ipAddress = getIpAddress(agentId, dataCenterId, podId);
|
||||
String macAddress = getMacAddress(dataCenterId, podId, clusterId, agentId);
|
||||
MockHostVO mockHost = new MockHostVO();
|
||||
mockHost.setDataCenterId(dataCenterId);
|
||||
mockHost.setPodId(podId);
|
||||
mockHost.setClusterId(clusterId);
|
||||
mockHost.setCapabilities("hvm");
|
||||
mockHost.setCpuCount(cpuCore);
|
||||
mockHost.setCpuSpeed(cpuSpeed);
|
||||
mockHost.setMemorySize(memory);
|
||||
String guid = UUID.randomUUID().toString();
|
||||
mockHost.setGuid(guid);
|
||||
mockHost.setName("SimulatedAgent." + guid);
|
||||
mockHost.setPrivateIpAddress(ipAddress);
|
||||
mockHost.setPublicIpAddress(ipAddress);
|
||||
mockHost.setStorageIpAddress(ipAddress);
|
||||
mockHost.setPrivateMacAddress(macAddress);
|
||||
mockHost.setPublicMacAddress(macAddress);
|
||||
mockHost.setStorageMacAddress(macAddress);
|
||||
mockHost.setVersion(this.getClass().getPackage().getImplementationVersion());
|
||||
mockHost.setResource("com.cloud.agent.AgentRoutingResource");
|
||||
mockHost = _mockHostDao.persist(mockHost);
|
||||
|
||||
_storageMgr.getLocalStorage(guid, localStorageSize);
|
||||
private String getIpAddress(long instanceId, long dcId, long podId) {
|
||||
Pair<String, Long> cidr = this.getPodCidr(podId, dcId);
|
||||
return NetUtils.long2Ip(NetUtils.ip2Long(cidr.first()) + instanceId);
|
||||
}
|
||||
|
||||
agentResource = new AgentRoutingResource();
|
||||
if (agentResource != null) {
|
||||
try {
|
||||
params.put("guid", mockHost.getGuid());
|
||||
agentResource.start();
|
||||
agentResource.configure(mockHost.getName(),
|
||||
params);
|
||||
private String getMacAddress(long dcId, long podId, long clusterId, int instanceId) {
|
||||
return NetUtils.long2Mac((dcId << 40 + podId << 32 + clusterId << 24 + instanceId));
|
||||
}
|
||||
|
||||
newResources.put(agentResource, args);
|
||||
} catch (ConfigurationException e) {
|
||||
s_logger
|
||||
.error("error while configuring server resource"
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
return newResources;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params)
|
||||
throws ConfigurationException {
|
||||
try {
|
||||
random = SecureRandom.getInstance("SHA1PRNG");
|
||||
_executor = new ThreadPoolExecutor(1, 5, 1, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("Simulator-Agent-Mgr"));
|
||||
//ComponentLocator locator = ComponentLocator.getCurrentLocator();
|
||||
//_simulatorMgr = (SimulatorManager) locator.getComponent(SimulatorManager.Name);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
s_logger.debug("Failed to initialize random:" + e.toString());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleSystemVMStart(long vmId, String privateIpAddress, String privateMacAddress, String privateNetMask, long dcId, long podId, String name, String vmType, String url) {
|
||||
_executor.execute(new SystemVMHandler(vmId, privateIpAddress, privateMacAddress, privateNetMask, dcId, podId, name, vmType, _simulatorMgr, url));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleSystemVMStop(long vmId) {
|
||||
_executor.execute(new SystemVMHandler(vmId));
|
||||
return true;
|
||||
}
|
||||
|
||||
private class SystemVMHandler implements Runnable {
|
||||
private long vmId;
|
||||
private String privateIpAddress;
|
||||
private String privateMacAddress;
|
||||
private String privateNetMask;
|
||||
private long dcId;
|
||||
private long podId;
|
||||
private String guid;
|
||||
private String name;
|
||||
private String vmType;
|
||||
private SimulatorManager mgr;
|
||||
private String mode;
|
||||
private String url;
|
||||
public SystemVMHandler(long vmId, String privateIpAddress, String privateMacAddress, String privateNetMask, long dcId, long podId, String name, String vmType,
|
||||
SimulatorManager mgr, String url) {
|
||||
this.vmId = vmId;
|
||||
this.privateIpAddress = privateIpAddress;
|
||||
this.privateMacAddress = privateMacAddress;
|
||||
this.privateNetMask = privateNetMask;
|
||||
this.dcId = dcId;
|
||||
this.guid = "SystemVM-" + UUID.randomUUID().toString();
|
||||
this.name = name;
|
||||
this.vmType = vmType;
|
||||
this.mgr = mgr;
|
||||
this.mode = "Start";
|
||||
this.url = url;
|
||||
this.podId = podId;
|
||||
}
|
||||
|
||||
public SystemVMHandler(long vmId) {
|
||||
this.vmId = vmId;
|
||||
this.mode = "Stop";
|
||||
}
|
||||
|
||||
@Override
|
||||
@DB
|
||||
public void run() {
|
||||
if (this.mode.equalsIgnoreCase("Stop")) {
|
||||
MockHost host = _mockHostDao.findByVmId(this.vmId);
|
||||
if (host != null) {
|
||||
String guid = host.getGuid();
|
||||
if (guid != null) {
|
||||
AgentResourceBase res = _resources.get(guid);
|
||||
if (res != null) {
|
||||
res.stop();
|
||||
_resources.remove(guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
String resource = null;
|
||||
if (vmType.equalsIgnoreCase("secstorage")) {
|
||||
resource = "com.cloud.agent.AgentStorageResource";
|
||||
}
|
||||
MockHostVO mockHost = new MockHostVO();
|
||||
mockHost.setDataCenterId(this.dcId);
|
||||
mockHost.setPodId(this.podId);
|
||||
mockHost.setCpuCount(DEFAULT_HOST_CPU_CORES);
|
||||
mockHost.setCpuSpeed(DEFAULT_HOST_SPEED_MHZ);
|
||||
mockHost.setMemorySize(DEFAULT_HOST_MEM_SIZE);
|
||||
mockHost.setGuid(this.guid);
|
||||
mockHost.setName(name);
|
||||
mockHost.setPrivateIpAddress(this.privateIpAddress);
|
||||
mockHost.setPublicIpAddress(this.privateIpAddress);
|
||||
mockHost.setStorageIpAddress(this.privateIpAddress);
|
||||
mockHost.setPrivateMacAddress(this.privateMacAddress);
|
||||
mockHost.setPublicMacAddress(this.privateMacAddress);
|
||||
mockHost.setStorageMacAddress(this.privateMacAddress);
|
||||
mockHost.setVersion(this.getClass().getPackage().getImplementationVersion());
|
||||
mockHost.setResource(resource);
|
||||
mockHost.setVmId(vmId);
|
||||
mockHost = _mockHostDao.persist(mockHost);
|
||||
|
||||
if (vmType.equalsIgnoreCase("secstorage")) {
|
||||
AgentStorageResource storageResource = new AgentStorageResource();
|
||||
try {
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
Map<String, String> details = new HashMap<String, String>();
|
||||
params.put("guid", this.guid);
|
||||
details.put("guid", this.guid);
|
||||
storageResource.configure("secondaryStorage", params);
|
||||
storageResource.start();
|
||||
//on the simulator the ssvm is as good as a direct agent
|
||||
_resourceMgr.addHost(mockHost.getDataCenterId(), storageResource, Host.Type.SecondaryStorageVM, details);
|
||||
_resources.put(this.guid, storageResource);
|
||||
} catch (ConfigurationException e) {
|
||||
s_logger.debug("Failed to load secondary storage resource: " + e.toString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public synchronized int getNextAgentId(long cidrSize) {
|
||||
return random.nextInt((int) cidrSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockHost getHost(String guid) {
|
||||
return _mockHostDao.findByGuid(guid);
|
||||
}
|
||||
@Override
|
||||
@DB
|
||||
public Map<AgentResourceBase, Map<String, String>> createServerResources(Map<String, Object> params) {
|
||||
|
||||
@Override
|
||||
public GetHostStatsAnswer getHostStatistic(GetHostStatsCommand cmd) {
|
||||
String hostGuid = cmd.getHostGuid();
|
||||
MockHost host = _mockHostDao.findByGuid(hostGuid);
|
||||
if (host == null) {
|
||||
return null;
|
||||
}
|
||||
List<MockVMVO> vms = _mockVmDao.findByHostId(host.getId());
|
||||
double usedMem = 0.0;
|
||||
double usedCpu = 0.0;
|
||||
for (MockVMVO vm : vms) {
|
||||
usedMem += vm.getMemory();
|
||||
usedCpu += vm.getCpu();
|
||||
}
|
||||
|
||||
HostStatsEntry hostStats = new HostStatsEntry();
|
||||
hostStats.setTotalMemoryKBs(host.getMemorySize());
|
||||
hostStats.setFreeMemoryKBs(host.getMemorySize() - usedMem);
|
||||
hostStats.setNetworkReadKBs(32768);
|
||||
hostStats.setNetworkWriteKBs(16384);
|
||||
hostStats.setCpuUtilization(usedCpu/(host.getCpuCount() * host.getCpuSpeed()));
|
||||
hostStats.setEntityType("simulator-host");
|
||||
hostStats.setHostId(cmd.getHostId());
|
||||
return new GetHostStatsAnswer(cmd, hostStats);
|
||||
}
|
||||
Map<String, String> args = new HashMap<String, String>();
|
||||
Map<AgentResourceBase, Map<String, String>> newResources = new HashMap<AgentResourceBase, Map<String, String>>();
|
||||
AgentResourceBase agentResource;
|
||||
long cpuCore = Long.parseLong((String) params.get("cpucore"));
|
||||
long cpuSpeed = Long.parseLong((String) params.get("cpuspeed"));
|
||||
long memory = Long.parseLong((String) params.get("memory"));
|
||||
long localStorageSize = Long.parseLong((String) params.get("localstorage"));
|
||||
synchronized (this) {
|
||||
long dataCenterId = Long.parseLong((String) params.get("zone"));
|
||||
long podId = Long.parseLong((String) params.get("pod"));
|
||||
long clusterId = Long.parseLong((String) params.get("cluster"));
|
||||
long cidrSize = getPodCidr(podId, dataCenterId).second();
|
||||
|
||||
int agentId = getNextAgentId(cidrSize);
|
||||
String ipAddress = getIpAddress(agentId, dataCenterId, podId);
|
||||
String macAddress = getMacAddress(dataCenterId, podId, clusterId, agentId);
|
||||
MockHostVO mockHost = new MockHostVO();
|
||||
mockHost.setDataCenterId(dataCenterId);
|
||||
mockHost.setPodId(podId);
|
||||
mockHost.setClusterId(clusterId);
|
||||
mockHost.setCapabilities("hvm");
|
||||
mockHost.setCpuCount(cpuCore);
|
||||
mockHost.setCpuSpeed(cpuSpeed);
|
||||
mockHost.setMemorySize(memory);
|
||||
String guid = UUID.randomUUID().toString();
|
||||
mockHost.setGuid(guid);
|
||||
mockHost.setName("SimulatedAgent." + guid);
|
||||
mockHost.setPrivateIpAddress(ipAddress);
|
||||
mockHost.setPublicIpAddress(ipAddress);
|
||||
mockHost.setStorageIpAddress(ipAddress);
|
||||
mockHost.setPrivateMacAddress(macAddress);
|
||||
mockHost.setPublicMacAddress(macAddress);
|
||||
mockHost.setStorageMacAddress(macAddress);
|
||||
mockHost.setVersion(this.getClass().getPackage().getImplementationVersion());
|
||||
mockHost.setResource("com.cloud.agent.AgentRoutingResource");
|
||||
|
||||
@Override
|
||||
public Answer checkHealth(CheckHealthCommand cmd) {
|
||||
return new Answer(cmd);
|
||||
}
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
mockHost = _mockHostDao.persist(mockHost);
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
s_logger.error("Error while configuring mock agent " + ex.getMessage());
|
||||
throw new CloudRuntimeException("Error configuring agent", ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
|
||||
_storageMgr.getLocalStorage(guid, localStorageSize);
|
||||
|
||||
@Override
|
||||
public Answer pingTest(PingTestCommand cmd) {
|
||||
return new Answer(cmd);
|
||||
}
|
||||
agentResource = new AgentRoutingResource();
|
||||
if (agentResource != null) {
|
||||
try {
|
||||
params.put("guid", mockHost.getGuid());
|
||||
agentResource.start();
|
||||
agentResource.configure(mockHost.getName(), params);
|
||||
|
||||
newResources.put(agentResource, args);
|
||||
} catch (ConfigurationException e) {
|
||||
s_logger.error("error while configuring server resource" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
return newResources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrepareForMigrationAnswer prepareForMigrate(PrepareForMigrationCommand cmd) {
|
||||
VirtualMachineTO vm = cmd.getVirtualMachine();
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Preparing host for migrating " + vm);
|
||||
}
|
||||
return new PrepareForMigrationAnswer(cmd);
|
||||
}
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
try {
|
||||
random = SecureRandom.getInstance("SHA1PRNG");
|
||||
_executor = new ThreadPoolExecutor(1, 5, 1, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(),
|
||||
new NamedThreadFactory("Simulator-Agent-Mgr"));
|
||||
// ComponentLocator locator = ComponentLocator.getCurrentLocator();
|
||||
// _simulatorMgr = (SimulatorManager)
|
||||
// locator.getComponent(SimulatorManager.Name);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
s_logger.debug("Failed to initialize random:" + e.toString());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleSystemVMStart(long vmId, String privateIpAddress, String privateMacAddress,
|
||||
String privateNetMask, long dcId, long podId, String name, String vmType, String url) {
|
||||
_executor.execute(new SystemVMHandler(vmId, privateIpAddress, privateMacAddress, privateNetMask, dcId, podId,
|
||||
name, vmType, _simulatorMgr, url));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean handleSystemVMStop(long vmId) {
|
||||
_executor.execute(new SystemVMHandler(vmId));
|
||||
return true;
|
||||
}
|
||||
|
||||
private class SystemVMHandler implements Runnable {
|
||||
private long vmId;
|
||||
private String privateIpAddress;
|
||||
private String privateMacAddress;
|
||||
private String privateNetMask;
|
||||
private long dcId;
|
||||
private long podId;
|
||||
private String guid;
|
||||
private String name;
|
||||
private String vmType;
|
||||
private SimulatorManager mgr;
|
||||
private String mode;
|
||||
private String url;
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
public SystemVMHandler(long vmId, String privateIpAddress, String privateMacAddress, String privateNetMask,
|
||||
long dcId, long podId, String name, String vmType, SimulatorManager mgr, String url) {
|
||||
this.vmId = vmId;
|
||||
this.privateIpAddress = privateIpAddress;
|
||||
this.privateMacAddress = privateMacAddress;
|
||||
this.privateNetMask = privateNetMask;
|
||||
this.dcId = dcId;
|
||||
this.guid = "SystemVM-" + UUID.randomUUID().toString();
|
||||
this.name = name;
|
||||
this.vmType = vmType;
|
||||
this.mgr = mgr;
|
||||
this.mode = "Start";
|
||||
this.url = url;
|
||||
this.podId = podId;
|
||||
}
|
||||
|
||||
public SystemVMHandler(long vmId) {
|
||||
this.vmId = vmId;
|
||||
this.mode = "Stop";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.getClass().getSimpleName();
|
||||
}
|
||||
@Override
|
||||
@DB
|
||||
public void run() {
|
||||
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
if (this.mode.equalsIgnoreCase("Stop")) {
|
||||
txn.start();
|
||||
MockHost host = _mockHostDao.findByVmId(this.vmId);
|
||||
if (host != null) {
|
||||
String guid = host.getGuid();
|
||||
if (guid != null) {
|
||||
AgentResourceBase res = _resources.get(guid);
|
||||
if (res != null) {
|
||||
res.stop();
|
||||
_resources.remove(guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
txn.commit();
|
||||
return;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("Unable to get host " + guid + " due to " + ex.getMessage(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MaintainAnswer maintain(com.cloud.agent.api.MaintainCommand cmd) {
|
||||
return new MaintainAnswer(cmd);
|
||||
}
|
||||
String resource = null;
|
||||
if (vmType.equalsIgnoreCase("secstorage")) {
|
||||
resource = "com.cloud.agent.AgentStorageResource";
|
||||
}
|
||||
MockHostVO mockHost = new MockHostVO();
|
||||
mockHost.setDataCenterId(this.dcId);
|
||||
mockHost.setPodId(this.podId);
|
||||
mockHost.setCpuCount(DEFAULT_HOST_CPU_CORES);
|
||||
mockHost.setCpuSpeed(DEFAULT_HOST_SPEED_MHZ);
|
||||
mockHost.setMemorySize(DEFAULT_HOST_MEM_SIZE);
|
||||
mockHost.setGuid(this.guid);
|
||||
mockHost.setName(name);
|
||||
mockHost.setPrivateIpAddress(this.privateIpAddress);
|
||||
mockHost.setPublicIpAddress(this.privateIpAddress);
|
||||
mockHost.setStorageIpAddress(this.privateIpAddress);
|
||||
mockHost.setPrivateMacAddress(this.privateMacAddress);
|
||||
mockHost.setPublicMacAddress(this.privateMacAddress);
|
||||
mockHost.setStorageMacAddress(this.privateMacAddress);
|
||||
mockHost.setVersion(this.getClass().getPackage().getImplementationVersion());
|
||||
mockHost.setResource(resource);
|
||||
mockHost.setVmId(vmId);
|
||||
Transaction simtxn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
simtxn.start();
|
||||
mockHost = _mockHostDao.persist(mockHost);
|
||||
simtxn.commit();
|
||||
} catch (Exception ex) {
|
||||
simtxn.rollback();
|
||||
throw new CloudRuntimeException("Unable to persist host " + mockHost.getGuid() + " due to "
|
||||
+ ex.getMessage(), ex);
|
||||
} finally {
|
||||
simtxn.close();
|
||||
simtxn = Transaction.open(Transaction.CLOUD_DB);
|
||||
simtxn.close();
|
||||
}
|
||||
|
||||
if (vmType.equalsIgnoreCase("secstorage")) {
|
||||
AgentStorageResource storageResource = new AgentStorageResource();
|
||||
try {
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
Map<String, String> details = new HashMap<String, String>();
|
||||
params.put("guid", this.guid);
|
||||
details.put("guid", this.guid);
|
||||
storageResource.configure("secondaryStorage", params);
|
||||
storageResource.start();
|
||||
// on the simulator the ssvm is as good as a direct
|
||||
// agent
|
||||
_resourceMgr.addHost(mockHost.getDataCenterId(), storageResource, Host.Type.SecondaryStorageVM,
|
||||
details);
|
||||
_resources.put(this.guid, storageResource);
|
||||
} catch (ConfigurationException e) {
|
||||
s_logger.debug("Failed to load secondary storage resource: " + e.toString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockHost getHost(String guid) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
MockHost _host = _mockHostDao.findByGuid(guid);
|
||||
txn.commit();
|
||||
if (_host != null) {
|
||||
return _host;
|
||||
} else {
|
||||
s_logger.error("Host with guid " + guid + " was not found");
|
||||
return null;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("Unable to get host " + guid + " due to " + ex.getMessage(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GetHostStatsAnswer getHostStatistic(GetHostStatsCommand cmd) {
|
||||
String hostGuid = cmd.getHostGuid();
|
||||
MockHost host = null;
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
host = _mockHostDao.findByGuid(hostGuid);
|
||||
txn.commit();
|
||||
if (host == null) {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("Unable to get host " + hostGuid + " due to " + ex.getMessage(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
|
||||
Transaction vmtxn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
vmtxn.start();
|
||||
List<MockVMVO> vms = _mockVmDao.findByHostId(host.getId());
|
||||
vmtxn.commit();
|
||||
double usedMem = 0.0;
|
||||
double usedCpu = 0.0;
|
||||
for (MockVMVO vm : vms) {
|
||||
usedMem += vm.getMemory();
|
||||
usedCpu += vm.getCpu();
|
||||
}
|
||||
|
||||
HostStatsEntry hostStats = new HostStatsEntry();
|
||||
hostStats.setTotalMemoryKBs(host.getMemorySize());
|
||||
hostStats.setFreeMemoryKBs(host.getMemorySize() - usedMem);
|
||||
hostStats.setNetworkReadKBs(32768);
|
||||
hostStats.setNetworkWriteKBs(16384);
|
||||
hostStats.setCpuUtilization(usedCpu / (host.getCpuCount() * host.getCpuSpeed()));
|
||||
hostStats.setEntityType("simulator-host");
|
||||
hostStats.setHostId(cmd.getHostId());
|
||||
return new GetHostStatsAnswer(cmd, hostStats);
|
||||
} catch (Exception ex) {
|
||||
vmtxn.rollback();
|
||||
throw new CloudRuntimeException("Unable to get Vms on host " + host.getGuid() + " due to "
|
||||
+ ex.getMessage(), ex);
|
||||
} finally {
|
||||
vmtxn.close();
|
||||
vmtxn = Transaction.open(Transaction.CLOUD_DB);
|
||||
vmtxn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer checkHealth(CheckHealthCommand cmd) {
|
||||
return new Answer(cmd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer pingTest(PingTestCommand cmd) {
|
||||
return new Answer(cmd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrepareForMigrationAnswer prepareForMigrate(PrepareForMigrationCommand cmd) {
|
||||
VirtualMachineTO vm = cmd.getVirtualMachine();
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Preparing host for migrating " + vm);
|
||||
}
|
||||
return new PrepareForMigrationAnswer(cmd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MaintainAnswer maintain(com.cloud.agent.api.MaintainCommand cmd) {
|
||||
return new MaintainAnswer(cmd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer checkNetworkCommand(CheckNetworkCommand cmd) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Checking if network name setup is done on the resource");
|
||||
}
|
||||
return new CheckNetworkAnswer(cmd, true , "Network Setup check by names is done");
|
||||
s_logger.debug("Checking if network name setup is done on the resource");
|
||||
}
|
||||
return new CheckNetworkAnswer(cmd, true, "Network Setup check by names is done");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -75,6 +75,8 @@ import com.cloud.simulator.dao.MockVMDao;
|
|||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.Ternary;
|
||||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.vm.VirtualMachine.State;
|
||||
|
||||
@Local(value = { MockVmManager.class })
|
||||
|
|
@ -85,7 +87,7 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
@Inject MockAgentManager _mockAgentMgr = null;
|
||||
@Inject MockHostDao _mockHostDao = null;
|
||||
@Inject MockSecurityRulesDao _mockSecurityDao = null;
|
||||
private Map<String, Map<String, Ternary<String,Long,Long>>> _securityRules = new ConcurrentHashMap<String, Map<String, Ternary<String, Long, Long>>>();
|
||||
private Map<String, Map<String, Ternary<String, Long, Long>>> _securityRules = new ConcurrentHashMap<String, Map<String, Ternary<String, Long, Long>>>();
|
||||
|
||||
public MockVmManagerImpl() {
|
||||
}
|
||||
|
|
@ -101,12 +103,27 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
int cpuHz, long ramSize,
|
||||
String bootArgs, String hostGuid) {
|
||||
|
||||
MockHost host = _mockHostDao.findByGuid(hostGuid);
|
||||
if (host == null) {
|
||||
return "can't find host";
|
||||
}
|
||||
|
||||
MockVm vm = _mockVmDao.findByVmName(vmName);
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
MockHost host = null;
|
||||
MockVm vm = null;
|
||||
try {
|
||||
txn.start();
|
||||
host = _mockHostDao.findByGuid(hostGuid);
|
||||
if (host == null) {
|
||||
return "can't find host";
|
||||
}
|
||||
|
||||
vm = _mockVmDao.findByVmName(vmName);
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("Unable to start VM " + vmName, ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
|
||||
if(vm == null) {
|
||||
int vncPort = 0;
|
||||
if(vncPort < 0)
|
||||
|
|
@ -127,11 +144,35 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
} else if (vmName.startsWith("i-")) {
|
||||
vm.setType("User");
|
||||
}
|
||||
vm = _mockVmDao.persist((MockVMVO)vm);
|
||||
txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
vm = _mockVmDao.persist((MockVMVO) vm);
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to save vm to db " + vm.getName(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
} else {
|
||||
if(vm.getState() == State.Stopped) {
|
||||
vm.setState(State.Running);
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO)vm);
|
||||
txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO)vm);
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to update vm " + vm.getName(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -179,37 +220,73 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
}
|
||||
|
||||
public boolean rebootVM(String vmName) {
|
||||
MockVm vm = _mockVmDao.findByVmName(vmName);
|
||||
if(vm != null) {
|
||||
vm.setState(State.Running);
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO)vm);
|
||||
}
|
||||
return true;
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
MockVm vm = _mockVmDao.findByVmName(vmName);
|
||||
if (vm != null) {
|
||||
vm.setState(State.Running);
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO) vm);
|
||||
|
||||
}
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to reboot vm " + vmName, ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, MockVMVO> getVms(String hostGuid) {
|
||||
List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid);
|
||||
Map<String, MockVMVO> vmMap = new HashMap<String, MockVMVO>();
|
||||
for (MockVMVO vm : vms) {
|
||||
vmMap.put(vm.getName(), vm);
|
||||
public Map<String, MockVMVO> getVms(String hostGuid) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid);
|
||||
Map<String, MockVMVO> vmMap = new HashMap<String, MockVMVO>();
|
||||
for (MockVMVO vm : vms) {
|
||||
vmMap.put(vm.getName(), vm);
|
||||
}
|
||||
txn.commit();
|
||||
return vmMap;
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to fetch vms from host " + hostGuid, ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
return vmMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, State> getVmStates(String hostGuid) {
|
||||
Map<String, State> states = new HashMap<String, State>();
|
||||
List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid);
|
||||
if (vms.isEmpty()) {
|
||||
return states;
|
||||
public Map<String, State> getVmStates(String hostGuid) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
Map<String, State> states = new HashMap<String, State>();
|
||||
List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid);
|
||||
if (vms.isEmpty()) {
|
||||
txn.commit();
|
||||
return states;
|
||||
}
|
||||
for (MockVm vm : vms) {
|
||||
states.put(vm.getName(), vm.getState());
|
||||
}
|
||||
txn.commit();
|
||||
return states;
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to fetch vms from host " + hostGuid, ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
|
||||
for(MockVm vm : vms) {
|
||||
states.put(vm.getName(), vm.getState());
|
||||
}
|
||||
|
||||
return states;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -243,14 +320,26 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CheckVirtualMachineAnswer checkVmState(CheckVirtualMachineCommand cmd) {
|
||||
MockVMVO vm = _mockVmDao.findByVmName(cmd.getVmName());
|
||||
if (vm == null) {
|
||||
return new CheckVirtualMachineAnswer(cmd, "can't find vm:" + cmd.getVmName());
|
||||
}
|
||||
|
||||
return new CheckVirtualMachineAnswer(cmd, vm.getState(), vm.getVncPort());
|
||||
}
|
||||
public CheckVirtualMachineAnswer checkVmState(CheckVirtualMachineCommand cmd) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
MockVMVO vm = _mockVmDao.findByVmName(cmd.getVmName());
|
||||
if (vm == null) {
|
||||
return new CheckVirtualMachineAnswer(cmd, "can't find vm:" + cmd.getVmName());
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
return new CheckVirtualMachineAnswer(cmd, vm.getState(), vm.getVncPort());
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to fetch vm state " + cmd.getVmName(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer startVM(StartCommand cmd, SimulatorInfo info) {
|
||||
|
|
@ -290,22 +379,34 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public MigrateAnswer Migrate(MigrateCommand cmd, SimulatorInfo info) {
|
||||
String vmName = cmd.getVmName();
|
||||
String destGuid = cmd.getHostGuid();
|
||||
MockVMVO vm = _mockVmDao.findByVmNameAndHost(vmName, info.getHostUuid());
|
||||
if (vm == null) {
|
||||
return new MigrateAnswer(cmd, false, "can;t find vm:" + vmName + " on host:" + info.getHostUuid(), null);
|
||||
}
|
||||
|
||||
MockHost destHost = _mockHostDao.findByGuid(destGuid);
|
||||
if (destHost == null) {
|
||||
return new MigrateAnswer(cmd, false, "can;t find host:" + info.getHostUuid(), null);
|
||||
}
|
||||
vm.setHostId(destHost.getId());
|
||||
_mockVmDao.update(vm.getId(), vm);
|
||||
return new MigrateAnswer(cmd, true,null, 0);
|
||||
}
|
||||
public MigrateAnswer Migrate(MigrateCommand cmd, SimulatorInfo info) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
String vmName = cmd.getVmName();
|
||||
String destGuid = cmd.getHostGuid();
|
||||
MockVMVO vm = _mockVmDao.findByVmNameAndHost(vmName, info.getHostUuid());
|
||||
if (vm == null) {
|
||||
return new MigrateAnswer(cmd, false, "can;t find vm:" + vmName + " on host:" + info.getHostUuid(), null);
|
||||
}
|
||||
|
||||
MockHost destHost = _mockHostDao.findByGuid(destGuid);
|
||||
if (destHost == null) {
|
||||
return new MigrateAnswer(cmd, false, "can;t find host:" + info.getHostUuid(), null);
|
||||
}
|
||||
vm.setHostId(destHost.getId());
|
||||
_mockVmDao.update(vm.getId(), vm);
|
||||
txn.commit();
|
||||
return new MigrateAnswer(cmd, true, null, 0);
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to migrate vm " + cmd.getVmName(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer IpAssoc(IpAssocCommand cmd) {
|
||||
|
|
@ -328,37 +429,77 @@ public class MockVmManagerImpl implements MockVmManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Answer CleanupNetworkRules(CleanupNetworkRulesCmd cmd, SimulatorInfo info) {
|
||||
List<MockSecurityRulesVO> rules = _mockSecurityDao.findByHost(info.getHostUuid());
|
||||
for (MockSecurityRulesVO rule : rules) {
|
||||
MockVMVO vm = _mockVmDao.findByVmNameAndHost(rule.getVmName(), info.getHostUuid());
|
||||
if (vm == null) {
|
||||
_mockSecurityDao.remove(rule.getId());
|
||||
}
|
||||
}
|
||||
return new Answer(cmd);
|
||||
}
|
||||
public Answer CleanupNetworkRules(CleanupNetworkRulesCmd cmd, SimulatorInfo info) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
List<MockSecurityRulesVO> rules = _mockSecurityDao.findByHost(info.getHostUuid());
|
||||
for (MockSecurityRulesVO rule : rules) {
|
||||
MockVMVO vm = _mockVmDao.findByVmNameAndHost(rule.getVmName(), info.getHostUuid());
|
||||
if (vm == null) {
|
||||
_mockSecurityDao.remove(rule.getId());
|
||||
}
|
||||
}
|
||||
txn.commit();
|
||||
return new Answer(cmd);
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to clean up rules", ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer stopVM(StopCommand cmd) {
|
||||
String vmName = cmd.getVmName();
|
||||
MockVm vm = _mockVmDao.findByVmName(vmName);
|
||||
if(vm != null) {
|
||||
vm.setState(State.Stopped);
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO)vm);
|
||||
}
|
||||
public Answer stopVM(StopCommand cmd) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
String vmName = cmd.getVmName();
|
||||
MockVm vm = _mockVmDao.findByVmName(vmName);
|
||||
if (vm != null) {
|
||||
vm.setState(State.Stopped);
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO) vm);
|
||||
}
|
||||
|
||||
if (vmName.startsWith("s-")) {
|
||||
_mockAgentMgr.handleSystemVMStop(vm.getId());
|
||||
}
|
||||
|
||||
return new StopAnswer(cmd, null, new Integer(0), true);
|
||||
}
|
||||
if (vmName.startsWith("s-")) {
|
||||
_mockAgentMgr.handleSystemVMStop(vm.getId());
|
||||
}
|
||||
txn.commit();
|
||||
return new StopAnswer(cmd, null, new Integer(0), true);
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to stop vm " + cmd.getVmName(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer rebootVM(RebootCommand cmd) {
|
||||
return new RebootAnswer(cmd, "Rebooted "+cmd.getVmName(), false);
|
||||
}
|
||||
public Answer rebootVM(RebootCommand cmd) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
MockVm vm = _mockVmDao.findByVmName(cmd.getVmName());
|
||||
if (vm != null) {
|
||||
vm.setState(State.Running);
|
||||
_mockVmDao.update(vm.getId(), (MockVMVO) vm);
|
||||
}
|
||||
txn.commit();
|
||||
return new RebootAnswer(cmd, "Rebooted " + cmd.getVmName(), true);
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("unable to stop vm " + cmd.getVmName(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer getVncPort(GetVncPortCommand cmd) {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import com.cloud.agent.api.CheckHealthCommand;
|
|||
import com.cloud.agent.api.CheckNetworkCommand;
|
||||
import com.cloud.agent.api.CheckVirtualMachineCommand;
|
||||
import com.cloud.agent.api.CleanupNetworkRulesCmd;
|
||||
import com.cloud.agent.api.ClusterSyncAnswer;
|
||||
import com.cloud.agent.api.ClusterSyncCommand;
|
||||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.ComputeChecksumCommand;
|
||||
|
|
@ -111,11 +112,11 @@ public class SimulatorManagerImpl implements SimulatorManager {
|
|||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
/*
|
||||
try {
|
||||
Connection conn = Transaction.getStandaloneConnectionWithException();
|
||||
Connection conn = Transaction.getStandaloneSimulatorConnection();
|
||||
conn.setAutoCommit(true);
|
||||
_concierge = new ConnectionConcierge("SimulatorConnection", conn, true);
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to get a db connection", e);
|
||||
throw new CloudRuntimeException("Unable to get a db connection to simulator", e);
|
||||
}
|
||||
*/
|
||||
return true;
|
||||
|
|
@ -295,17 +296,20 @@ public class SimulatorManagerImpl implements SimulatorManager {
|
|||
return _mockVmMgr.getDomRVersion((GetDomRVersionCmd)cmd);
|
||||
} else if (cmd instanceof ClusterSyncCommand) {
|
||||
return new Answer(cmd);
|
||||
//return new ClusterSyncAnswer(((ClusterSyncCommand) cmd).getClusterId(), this.getVmStates(hostGuid));
|
||||
} else if (cmd instanceof CopyVolumeCommand) {
|
||||
return _mockStorageMgr.CopyVolume((CopyVolumeCommand)cmd);
|
||||
} else {
|
||||
return Answer.createUnsupportedCommandAnswer(cmd);
|
||||
}
|
||||
} catch(Exception e) {
|
||||
s_logger.debug("Failed execute cmd: " + e.toString());
|
||||
s_logger.error("Failed execute cmd: " + e.toString());
|
||||
txn.rollback();
|
||||
return new Answer(cmd, false, e.toString());
|
||||
} finally {
|
||||
txn.transitToAutoManagedConnection(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
txn = Transaction.open(Transaction.CLOUD_DB);
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -315,53 +319,50 @@ public class SimulatorManagerImpl implements SimulatorManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean configureSimulator(Long zoneId, Long podId, Long clusterId, Long hostId, String command, String values) {
|
||||
MockConfigurationVO config = _mockConfigDao.findByCommand(zoneId, podId, clusterId, hostId, command);
|
||||
if (config == null) {
|
||||
config = new MockConfigurationVO();
|
||||
config.setClusterId(clusterId);
|
||||
config.setDataCenterId(zoneId);
|
||||
config.setPodId(podId);
|
||||
config.setHostId(hostId);
|
||||
config.setName(command);
|
||||
config.setValues(values);
|
||||
_mockConfigDao.persist(config);
|
||||
} else {
|
||||
config.setValues(values);
|
||||
_mockConfigDao.update(config.getId(), config);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@DB
|
||||
public Map<String, State> getVmStates(String hostGuid) {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.transitToUserManagedConnection(_concierge.conn());
|
||||
try {
|
||||
return _mockVmMgr.getVmStates(hostGuid);
|
||||
} finally {
|
||||
txn.transitToAutoManagedConnection(Transaction.CLOUD_DB);
|
||||
}
|
||||
return _mockVmMgr.getVmStates(hostGuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
@DB
|
||||
public Map<String, MockVMVO> getVms(String hostGuid) {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.transitToUserManagedConnection(_concierge.conn());
|
||||
try {
|
||||
return _mockVmMgr.getVms(hostGuid);
|
||||
} finally {
|
||||
txn.transitToAutoManagedConnection(Transaction.CLOUD_DB);
|
||||
}
|
||||
return _mockVmMgr.getVms(hostGuid);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HashMap<String, Pair<Long, Long>> syncNetworkGroups(String hostGuid) {
|
||||
SimulatorInfo info = new SimulatorInfo();
|
||||
info.setHostUuid(hostGuid);
|
||||
return _mockVmMgr.syncNetworkGroups(info);
|
||||
return _mockVmMgr.syncNetworkGroups(info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configureSimulator(Long zoneId, Long podId, Long clusterId, Long hostId, String command,
|
||||
String values) {
|
||||
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
|
||||
try {
|
||||
txn.start();
|
||||
MockConfigurationVO config = _mockConfigDao.findByCommand(zoneId, podId, clusterId, hostId, command);
|
||||
if (config == null) {
|
||||
config = new MockConfigurationVO();
|
||||
config.setClusterId(clusterId);
|
||||
config.setDataCenterId(zoneId);
|
||||
config.setPodId(podId);
|
||||
config.setHostId(hostId);
|
||||
config.setName(command);
|
||||
config.setValues(values);
|
||||
_mockConfigDao.persist(config);
|
||||
txn.commit();
|
||||
} else {
|
||||
config.setValues(values);
|
||||
_mockConfigDao.update(config.getId(), config);
|
||||
txn.commit();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
txn.rollback();
|
||||
throw new CloudRuntimeException("Unable to configure simulator because of " + ex.getMessage(), ex);
|
||||
} finally {
|
||||
txn.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
package com.cloud.simulator;
|
||||
|
||||
import com.cloud.utils.SerialVersionUID;
|
||||
import com.cloud.utils.exception.RuntimeCloudException;
|
||||
|
||||
/**
|
||||
* wrap exceptions that you know there's no point in dealing with.
|
||||
*/
|
||||
public class SimulatorRuntimeException extends RuntimeCloudException {
|
||||
|
||||
private static final long serialVersionUID = SerialVersionUID.CloudRuntimeException;
|
||||
|
||||
public SimulatorRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SimulatorRuntimeException(String message, Throwable th) {
|
||||
super(message, th);
|
||||
}
|
||||
|
||||
protected SimulatorRuntimeException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue