use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareStorageProcessor method removeVmfsDatastore.
private void removeVmfsDatastore(VmwareHypervisorHost hyperHost, String datastoreName, String storageIpAddress, int storagePortNumber, String iqn) throws Exception {
// hyperHost.unmountDatastore(datastoreName);
VmwareContext context = hostService.getServiceContext(null);
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO cluster = new ClusterMO(context, morCluster);
List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
HostInternetScsiHbaStaticTarget target = new HostInternetScsiHbaStaticTarget();
target.setAddress(storageIpAddress);
target.setPort(storagePortNumber);
target.setIScsiName(iqn);
final List<HostInternetScsiHbaStaticTarget> lstTargets = new ArrayList<HostInternetScsiHbaStaticTarget>();
lstTargets.add(target);
addRemoveInternetScsiTargetsToAllHosts(context, false, lstTargets, lstHosts);
rescanAllHosts(context, lstHosts);
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
@Override
public PrimaryStorageDownloadAnswer execute(PrimaryStorageDownloadCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource PrimaryStorageDownloadCommand: " + _gson.toJson(cmd));
}
try {
VmwareContext context = getServiceContext();
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
return (PrimaryStorageDownloadAnswer) mgr.getStorageManager().execute(this, cmd);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String msg = "PrimaryStorageDownloadCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new PrimaryStorageDownloadAnswer(msg);
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareStorageProcessor method getControllerFromConfigurationSetting.
private String getControllerFromConfigurationSetting() throws Exception {
String diskController = null;
VmwareContext context = null;
try {
context = hostService.getServiceContext(null);
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
diskController = mgr.getDataDiskController();
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(context);
}
String details = "Failed to connect to vCenter due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(details, e);
}
return diskController;
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareStorageProcessor method backupSnapshot.
@Override
public Answer backupSnapshot(CopyCommand cmd) {
SnapshotObjectTO srcSnapshot = (SnapshotObjectTO) cmd.getSrcTO();
DataStoreTO primaryStore = srcSnapshot.getDataStore();
SnapshotObjectTO destSnapshot = (SnapshotObjectTO) cmd.getDestTO();
DataStoreTO destStore = destSnapshot.getDataStore();
if (!(destStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
NfsTO destNfsStore = (NfsTO) destStore;
String secondaryStorageUrl = destNfsStore.getUrl();
String snapshotUuid = srcSnapshot.getPath();
String prevSnapshotUuid = srcSnapshot.getParentSnapshotPath();
String prevBackupUuid = destSnapshot.getParentSnapshotPath();
VirtualMachineMO workerVm = null;
String workerVMName = null;
String volumePath = srcSnapshot.getVolume().getPath();
ManagedObjectReference morDs = null;
DatastoreMO dsMo = null;
// By default assume failure
String details = null;
boolean success = false;
String snapshotBackupUuid = null;
boolean hasOwnerVm = false;
Ternary<String, String, String[]> backupResult = null;
VmwareContext context = hostService.getServiceContext(cmd);
VirtualMachineMO vmMo = null;
String vmName = srcSnapshot.getVmName();
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
CopyCmdAnswer answer = null;
try {
if (vmName != null) {
vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
}
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
}
}
if (vmMo == null) {
dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
workerVMName = hostService.getWorkerName(context, cmd, 0);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName);
if (vmMo == null) {
throw new Exception("Failed to find the newly create or relocated VM. vmName: " + workerVMName);
}
workerVm = vmMo;
// attach volume to worker VM
String datastoreVolumePath = dsMo.getDatastorePath(volumePath + ".vmdk");
vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
} else {
s_logger.info("Using owner VM " + vmName + " for snapshot operation");
hasOwnerVm = true;
}
if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + srcSnapshot.getName(), false, false)) {
throw new Exception("Failed to take snapshot " + srcSnapshot.getName() + " on vm: " + vmName);
}
backupResult = backupSnapshotToSecondaryStorage(vmMo, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1), _nfsVersion);
snapshotBackupUuid = backupResult.first();
success = (snapshotBackupUuid != null);
if (!success) {
details = "Failed to backUp the snapshot with uuid: " + snapshotUuid + " to secondary storage.";
answer = new CopyCmdAnswer(details);
} else {
details = "Successfully backedUp the snapshot with Uuid: " + snapshotUuid + " to secondary storage.";
// Get snapshot physical size
long physicalSize = 0l;
String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, _nfsVersion);
String snapshotDir = destSnapshot.getPath() + "/" + snapshotBackupUuid;
File[] files = new File(secondaryMountPoint + "/" + snapshotDir).listFiles();
if (files != null) {
for (File file : files) {
String fileName = file.getName();
if (fileName.toLowerCase().startsWith(snapshotBackupUuid) && fileName.toLowerCase().endsWith(".vmdk")) {
physicalSize = new File(secondaryMountPoint + "/" + snapshotDir + "/" + fileName).length();
break;
}
}
}
SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(snapshotDir + "/" + snapshotBackupUuid);
newSnapshot.setPhysicalSize(physicalSize);
answer = new CopyCmdAnswer(newSnapshot);
}
} finally {
if (vmMo != null) {
ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
if (snapshotMor != null) {
vmMo.removeSnapshot(snapshotUuid, false);
// in the middle
if (backupResult != null && hasOwnerVm) {
s_logger.info("Check if we have disk consolidation after snapshot operation");
boolean chainConsolidated = false;
for (String vmdkDsFilePath : backupResult.third()) {
s_logger.info("Validate disk chain file:" + vmdkDsFilePath);
if (vmMo.getDiskDevice(vmdkDsFilePath) == null) {
s_logger.info("" + vmdkDsFilePath + " no longer exists, consolidation detected");
chainConsolidated = true;
break;
} else {
s_logger.info("" + vmdkDsFilePath + " is found still in chain");
}
}
if (chainConsolidated) {
String topVmdkFilePath = null;
try {
topVmdkFilePath = vmMo.getDiskCurrentTopBackingFileInChain(backupResult.second());
} catch (Exception e) {
s_logger.error("Unexpected exception", e);
}
s_logger.info("Disk has been consolidated, top VMDK is now: " + topVmdkFilePath);
if (topVmdkFilePath != null) {
DatastoreFile file = new DatastoreFile(topVmdkFilePath);
SnapshotObjectTO snapshotInfo = (SnapshotObjectTO) answer.getNewData();
VolumeObjectTO vol = new VolumeObjectTO();
vol.setUuid(srcSnapshot.getVolume().getUuid());
vol.setPath(file.getFileBaseName());
snapshotInfo.setVolume(vol);
} else {
s_logger.error("Disk has been consolidated, but top VMDK is not found ?!");
}
}
}
} else {
s_logger.error("Can not find the snapshot we just used ?!");
}
}
try {
if (workerVm != null) {
// detach volume and destroy worker vm
workerVm.detachAllDisks();
workerVm.destroy();
}
} catch (Throwable e) {
s_logger.warn("Failed to destroy worker VM: " + workerVMName);
}
}
return answer;
} catch (Throwable e) {
if (e instanceof RemoteException) {
hostService.invalidateServiceContext(context);
}
s_logger.error("Unexpecpted exception ", e);
details = "backup snapshot exception: " + VmwareHelper.getExceptionMessage(e);
return new CopyCmdAnswer(details);
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareSecondaryStorageContextFactory method create.
public static VmwareContext create(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
assert (vCenterAddress != null);
assert (vCenterUserName != null);
assert (vCenterPassword != null);
String serviceUrl = "https://" + vCenterAddress + "/sdk/vimService";
VmwareClient vimClient = new VmwareClient(vCenterAddress + "-" + s_seq++);
vimClient.setVcenterSessionTimeout(s_vCenterSessionTimeout);
vimClient.connect(serviceUrl, vCenterUserName, vCenterPassword);
VmwareContext context = new VmwareContext(vimClient, vCenterAddress);
assert (context != null);
context.setPoolInfo(s_pool, VmwareContextPool.composePoolKey(vCenterAddress, vCenterUserName));
return context;
}
Aggregations