use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class SecondaryStorageManagerImpl method scanPool.
@Override
public Pair<AfterScanAction, Object> scanPool(final Long pool) {
logger.info("Scanning secondary storage pool {}", pool.toString());
final long dataCenterId = pool.longValue();
final List<SecondaryStorageVmVO> ssVms = this._secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVmRole.templateProcessor, dataCenterId, Running, Migrating, Starting, Stopped, Stopping);
final int vmSize = (ssVms == null) ? 0 : ssVms.size();
final List<DataStore> ssStores = this._dataStoreMgr.getImageStoresByScope(new ZoneScope(dataCenterId));
final int storeSize = (ssStores == null) ? 0 : ssStores.size();
if (storeSize > vmSize) {
final int requiredVMs = storeSize - vmSize;
logger.info("Found less ({}) secondary storage VMs than image stores ({}) in dcId={}, starting {} new VMs", vmSize, storeSize, dataCenterId, requiredVMs);
return new Pair<>(AfterScanAction.expand(requiredVMs), SecondaryStorageVmRole.templateProcessor);
} else {
final String standByCapacity = this._configDao.getValue(Config.SecStorageCapacityStandby.toString());
final String maxPerVm = this._configDao.getValue(Config.SecStorageSessionMax.toString());
final int requiredCapacity = new SecondaryStorageCapacityCalculator().calculateRequiredCapacity(Integer.parseInt(standByCapacity), Integer.parseInt(maxPerVm));
if (requiredCapacity > vmSize) {
final int requiredVMs = requiredCapacity - vmSize;
logger.info("Found less ({}) secondary storage VMs than required ({}) in dcId={}, starting {} new VMs", vmSize, requiredCapacity, dataCenterId, requiredVMs);
return new Pair<>(AfterScanAction.expand(requiredVMs), SecondaryStorageVmRole.templateProcessor);
}
}
return new Pair<>(AfterScanAction.nop(), SecondaryStorageVmRole.templateProcessor);
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class SecondaryStorageManagerImpl method isZoneReady.
public boolean isZoneReady(final Map<Long, ZoneHostInfo> zoneHostInfoMap, final long dataCenterId) {
final ZoneHostInfo zoneHostInfo = zoneHostInfoMap.get(dataCenterId);
if (zoneHostInfo != null && (zoneHostInfo.getFlags() & RunningHostInfoAgregator.ZoneHostInfo.ROUTING_HOST_MASK) != 0) {
final VMTemplateVO template = this._templateDao.findSystemVMReadyTemplate(dataCenterId, HypervisorType.Any);
if (template == null) {
logger.debug("System vm template is not ready at data center " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
return false;
}
final List<DataStore> stores = this._dataStoreMgr.getImageStoresByScope(new ZoneScope(dataCenterId));
if (stores.size() < 1) {
logger.debug("No image store added in zone " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
return false;
}
final DataStore store = this.templateMgr.getImageStore(dataCenterId, template.getId());
if (store == null) {
logger.debug("No secondary storage available in zone " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
return false;
}
boolean useLocalStorage = false;
final Boolean useLocal = ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId);
if (useLocal != null) {
useLocalStorage = useLocal.booleanValue();
}
final List<Pair<Long, Integer>> l = this._storagePoolHostDao.getDatacenterStoragePoolHostInfo(dataCenterId, !useLocalStorage);
if (l != null && l.size() > 0 && l.get(0).second().intValue() > 0) {
return true;
} else {
logger.debug("Primary storage is not ready, wait until it is ready to launch secondary storage vm. dcId: " + dataCenterId + ", " + ConfigurationManagerImpl.SystemVMUseLocalStorage.key() + ": " + useLocalStorage + ". " + "If you want to use local storage to start SSVM, need to set " + ConfigurationManagerImpl.SystemVMUseLocalStorage.key() + " to true");
}
}
return false;
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class HypervisorTemplateAdapter method delete.
@Override
@DB
public boolean delete(final TemplateProfile profile) {
boolean success = true;
final VMTemplateVO template = profile.getTemplate();
// find all eligible image stores for this template
final List<DataStore> imageStores = this.templateMgr.getImageStoreByTemplate(template.getId(), profile.getZoneId());
if (imageStores == null || imageStores.size() == 0) {
// already destroyed on image stores
s_logger.info("Unable to find image store still having template: " + template.getName() + ", so just mark the template removed");
} else {
// Make sure the template is downloaded to all found image stores
for (final DataStore store : imageStores) {
final long storeId = store.getId();
final List<TemplateDataStoreVO> templateStores = this._tmpltStoreDao.listByTemplateStore(template.getId(), storeId);
for (final TemplateDataStoreVO templateStore : templateStores) {
if (templateStore.getDownloadState() == VMTemplateStatus.DOWNLOAD_IN_PROGRESS) {
final String errorMsg = "Please specify a template that is not currently being downloaded.";
s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + store.getName() + "; cant' delete it.");
throw new CloudRuntimeException(errorMsg);
}
}
}
for (final DataStore imageStore : imageStores) {
// publish zone-wide usage event
final Long sZoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
s_logger.info("Delete template from image store: " + imageStore.getName());
final AsyncCallFuture<TemplateApiResult> future = this.imageService.deleteTemplateAsync(this.imageFactory.getTemplate(template.getId(), imageStore));
try {
final TemplateApiResult result = future.get();
success = result.isSuccess();
if (!success) {
s_logger.warn("Failed to delete the template " + template + " from the image store: " + imageStore.getName() + " due to: " + result.getResult());
break;
}
// remove from template_zone_ref
final List<VMTemplateZoneVO> templateZones = this.templateZoneDao.listByZoneTemplate(sZoneId, template.getId());
if (templateZones != null) {
for (final VMTemplateZoneVO templateZone : templateZones) {
this.templateZoneDao.remove(templateZone.getId());
}
}
// mark all the occurrences of this template in the given store as destroyed.
this.templateDataStoreDao.removeByTemplateStore(template.getId(), imageStore.getId());
} catch (final InterruptedException | ExecutionException e) {
s_logger.debug("delete template Failed", e);
throw new CloudRuntimeException("delete template Failed", e);
}
}
}
if (success) {
if ((imageStores.size() > 1) && (profile.getZoneId() != null)) {
// if template is stored in more than one image stores, and the zone id is not null, then don't delete other templates.
return success;
}
// delete all cache entries for this template
final List<TemplateInfo> cacheTmpls = this.imageFactory.listTemplateOnCache(template.getId());
for (final TemplateInfo tmplOnCache : cacheTmpls) {
s_logger.info("Delete template from image cache store: " + tmplOnCache.getDataStore().getName());
tmplOnCache.delete();
}
// find all eligible image stores for this template
final List<DataStore> iStores = this.templateMgr.getImageStoreByTemplate(template.getId(), null);
if (iStores == null || iStores.size() == 0) {
// Mark template as Inactive.
template.setState(VirtualMachineTemplate.State.Inactive);
this._tmpltDao.update(template.getId(), template);
// Decrement the number of templates and total secondary storage
// space used by the account
final Account account = this._accountDao.findByIdIncludingRemoved(template.getAccountId());
this._resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
this._resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
}
// remove its related ACL permission
final Pair<Class<?>, Long> tmplt = new Pair<>(VirtualMachineTemplate.class, template.getId());
this._messageBus.publish(this._name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
}
return success;
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class UriUtils method validateUrl.
public static Pair<String, Integer> validateUrl(final String format, final String url) throws IllegalArgumentException {
try {
final URI uri = new URI(url);
if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) {
throw new IllegalArgumentException("Unsupported scheme for url: " + url);
}
int port = uri.getPort();
if (!(port == 80 || port == 8080 || port == 443 || port == -1)) {
throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed");
}
if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) {
port = 443;
} else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) {
port = 80;
}
final String host = uri.getHost();
try {
final InetAddress hostAddr = InetAddress.getByName(host);
if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) {
throw new IllegalArgumentException("Illegal host specified in url");
}
if (hostAddr instanceof Inet6Address) {
throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")");
}
} catch (final UnknownHostException uhe) {
throw new IllegalArgumentException("Unable to resolve " + host);
}
// verify format
if (format != null) {
final String uripath = uri.getPath();
checkFormat(format, uripath);
}
return new Pair<>(host, port);
} catch (final URISyntaxException use) {
throw new IllegalArgumentException("Invalid URL: " + url);
}
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class XenServer610MigrateWithStorageCommandWrapper method execute.
@Override
public Answer execute(final MigrateWithStorageCommand command, final XenServer610Resource xenServer610Resource) {
final Connection connection = xenServer610Resource.getConnection();
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final List<Pair<VolumeTO, StorageFilerTO>> volumeToFiler = command.getVolumeToFilerAsList();
final String vmName = vmSpec.getName();
Task task = null;
final XsHost xsHost = xenServer610Resource.getHost();
final String uuid = xsHost.getUuid();
try {
xenServer610Resource.prepareISO(connection, vmName, null, null);
// Get the list of networks and recreate VLAN, if required.
for (final NicTO nicTo : vmSpec.getNics()) {
xenServer610Resource.getNetwork(connection, nicTo);
}
final Map<String, String> other = new HashMap<>();
other.put("live", "true");
final XsLocalNetwork nativeNetworkForTraffic = xenServer610Resource.getNativeNetworkForTraffic(connection, TrafficType.Storage, null);
final Network networkForSm = nativeNetworkForTraffic.getNetwork();
// Create the vif map. The vm stays in the same cluster so we have to pass an empty vif map.
final Map<VIF, Network> vifMap = new HashMap<>();
final Map<VDI, SR> vdiMap = new HashMap<>();
for (final Pair<VolumeTO, StorageFilerTO> entry : volumeToFiler) {
final VolumeTO volume = entry.first();
final StorageFilerTO sotrageFiler = entry.second();
vdiMap.put(xenServer610Resource.getVDIbyUuid(connection, volume.getPath()), xenServer610Resource.getStorageRepository(connection, sotrageFiler.getUuid()));
}
// Get the vm to migrate.
final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
final VM vmToMigrate = vms.iterator().next();
// Check migration with storage is possible.
final Host host = Host.getByUuid(connection, uuid);
final Map<String, String> token = host.migrateReceive(connection, networkForSm, other);
task = vmToMigrate.assertCanMigrateAsync(connection, token, true, vdiMap, vifMap, other);
try {
// poll every 1 seconds
final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
xenServer610Resource.waitForTask(connection, task, 1000, timeout);
xenServer610Resource.checkForSuccess(connection, task);
} catch (final Types.HandleInvalid e) {
s_logger.error("Error while checking if vm " + vmName + " can be migrated to the destination host " + host, e);
throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated to the " + "destination host " + host, e);
}
// Migrate now.
task = vmToMigrate.migrateSendAsync(connection, token, true, vdiMap, vifMap, other);
try {
// poll every 1 seconds.
final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
xenServer610Resource.waitForTask(connection, task, 1000, timeout);
xenServer610Resource.checkForSuccess(connection, task);
} catch (final Types.HandleInvalid e) {
s_logger.error("Error while migrating vm " + vmName + " to the destination host " + host, e);
throw new CloudRuntimeException("Error while migrating vm " + vmName + " to the destination host " + host, e);
}
// Volume paths would have changed. Return that information.
final List<VolumeObjectTO> volumeToList = xenServer610Resource.getUpdatedVolumePathsOfMigratedVm(connection, vmToMigrate, vmSpec.getDisks());
vmToMigrate.setAffinity(connection, host);
return new MigrateWithStorageAnswer(command, volumeToList);
} catch (final Exception e) {
s_logger.warn("Catch Exception " + e.getClass().getName() + ". Storage motion failed due to " + e.toString(), e);
return new MigrateWithStorageAnswer(command, e);
} finally {
if (task != null) {
try {
task.destroy(connection);
} catch (final Exception e) {
s_logger.debug("Unable to destroy task " + task.toString() + " on host " + uuid + " due to " + e.toString());
}
}
}
}
Aggregations