use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareManagerImpl method removeVmwareDatacenter.
@Override
public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUseException {
Long zoneId = cmd.getZoneId();
// Validate zone
validateZone(zoneId);
// Zone validation to check if the zone already has resources.
// Association of VMware DC to zone is not allowed if zone already has resources added.
validateZoneWithResources(zoneId, "remove VMware datacenter to zone");
// Get DC associated with this zone
VmwareDatacenterVO vmwareDatacenter;
String vmwareDcName;
String vCenterHost;
String userName;
String password;
DatacenterMO dcMo = null;
final VmwareDatacenterZoneMapVO vmwareDcZoneMap = _vmwareDcZoneMapDao.findByZoneId(zoneId);
// Check if zone is associated with VMware DC
if (vmwareDcZoneMap == null) {
throw new CloudRuntimeException("Zone " + zoneId + " is not associated with any VMware datacenter.");
}
final long vmwareDcId = vmwareDcZoneMap.getVmwareDcId();
vmwareDatacenter = _vmwareDcDao.findById(vmwareDcId);
vmwareDcName = vmwareDatacenter.getVmwareDatacenterName();
vCenterHost = vmwareDatacenter.getVcenterHost();
userName = vmwareDatacenter.getUser();
password = vmwareDatacenter.getPassword();
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
// Remove the VMware datacenter entry in table vmware_data_center
_vmwareDcDao.remove(vmwareDcId);
// Remove the map entry in table vmware_data_center_zone_map
_vmwareDcZoneMapDao.remove(vmwareDcZoneMap.getId());
}
});
// Construct context
VmwareContext context = null;
try {
context = VmwareContextFactory.create(vCenterHost, userName, password);
// Check if DC exists on vCenter
try {
dcMo = new DatacenterMO(context, vmwareDcName);
} catch (Throwable t) {
String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost;
s_logger.error(msg);
throw new DiscoveryException(msg);
}
assert (dcMo != null);
// Reset custom field property cloud.zone over this DC
dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "false");
s_logger.info("Sucessfully reset custom field property cloud.zone over DC " + vmwareDcName);
} catch (Exception e) {
String msg = "Unable to reset custom field property cloud.zone over DC " + vmwareDcName + " due to : " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
throw new CloudRuntimeException(msg);
} finally {
if (context != null) {
context.close();
}
context = null;
}
return true;
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(RevertToVMSnapshotCommand cmd) {
try {
VmwareContext context = getServiceContext();
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
return mgr.getStorageManager().execute(this, cmd);
} catch (Exception e) {
e.printStackTrace();
return new RevertToVMSnapshotAnswer(cmd, false, "");
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(CreateStoragePoolCommand cmd) {
if (cmd.getCreateDatastore()) {
try {
VmwareContext context = getServiceContext();
_storageProcessor.prepareManagedDatastore(context, getHyperHost(context), cmd.getDetails().get(CreateStoragePoolCommand.DATASTORE_NAME), cmd.getDetails().get(CreateStoragePoolCommand.IQN), cmd.getDetails().get(CreateStoragePoolCommand.STORAGE_HOST), Integer.parseInt(cmd.getDetails().get(CreateStoragePoolCommand.STORAGE_PORT)));
} catch (Exception ex) {
return new Answer(cmd, false, "Issue creating datastore");
}
}
return new Answer(cmd, true, "success");
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method prepareNetworkElementCommand.
private ExecutionResult prepareNetworkElementCommand(IpAssocCommand cmd) {
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
IpAddressTO[] ips = cmd.getIpAddresses();
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String controlIp = VmwareResource.getRouterSshControlIp(cmd);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
// the check and will try to find it within cluster
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(routerName);
}
}
if (vmMo == null) {
String msg = "Router " + routerName + " no longer exists to execute IPAssoc command";
s_logger.error(msg);
throw new Exception(msg);
}
for (IpAddressTO ip : ips) {
/**
* TODO support other networks
*/
URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri());
}
String vlanId = BroadcastDomainType.getValue(broadcastUri);
String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first());
}
boolean addVif = false;
if (ip.isAdd() && publicNicInfo.first().intValue() == -1) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Plug new NIC to associate" + controlIp + " to " + ip.getPublicIp());
}
addVif = true;
}
if (addVif) {
plugPublicNic(vmMo, vlanId, ip.getVifMacAddress());
publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (publicNicInfo.first().intValue() >= 0) {
networkUsage(controlIp, "addVif", "eth" + publicNicInfo.first());
}
}
if (publicNicInfo.first().intValue() < 0) {
String msg = "Failed to find DomR VIF to associate/disassociate IP with.";
s_logger.error(msg);
throw new InternalErrorException(msg);
}
ip.setNicDevId(publicNicInfo.first().intValue());
ip.setNewNic(addVif);
}
} catch (Throwable e) {
s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
return new ExecutionResult(false, e.toString());
}
return new ExecutionResult(true, null);
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method getServiceContext.
@Override
public VmwareContext getServiceContext(Command cmd) {
VmwareContext context = null;
if (s_serviceContext.get() != null) {
context = s_serviceContext.get();
String poolKey = VmwareContextPool.composePoolKey(_vCenterAddress, _username);
// Before re-using the thread local context, ensure it corresponds to the right vCenter API session and that it is valid to make calls.
if (context.getPoolKey().equals(poolKey)) {
if (context.validate()) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("ThreadLocal context is still valid, just reuse");
}
return context;
} else {
s_logger.info("Validation of the context failed, dispose and use a new one");
invalidateServiceContext(context);
}
} else {
// Exisitng ThreadLocal context corresponds to a different vCenter API session. Why has it not been recycled?
s_logger.warn("ThreadLocal VMware context: " + poolKey + " doesn't correspond to the right vCenter. Expected VMware context: " + context.getPoolKey());
}
}
try {
context = VmwareContextFactory.getContext(_vCenterAddress, _username, _password);
s_serviceContext.set(context);
} catch (Exception e) {
s_logger.error("Unable to connect to vSphere server: " + _vCenterAddress, e);
throw new CloudRuntimeException("Unable to connect to vSphere server: " + _vCenterAddress);
}
return context;
}
Aggregations