Search in sources :

Example 66 with Pair

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);
}
Also used : ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) Pair(com.cloud.legacymodel.utils.Pair)

Example 67 with Pair

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;
}
Also used : ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) ZoneHostInfo(com.cloud.consoleproxy.RunningHostInfoAgregator.ZoneHostInfo) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) Pair(com.cloud.legacymodel.utils.Pair)

Example 68 with Pair

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;
}
Also used : Account(com.cloud.legacymodel.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) ExecutionException(java.util.concurrent.ExecutionException) Pair(com.cloud.legacymodel.utils.Pair) DB(com.cloud.utils.db.DB)

Example 69 with Pair

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);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Inet6Address(java.net.Inet6Address) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) InetAddress(java.net.InetAddress) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Pair(com.cloud.legacymodel.utils.Pair) NameValuePair(org.apache.http.NameValuePair)

Example 70 with Pair

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());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) HashMap(java.util.HashMap) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) VolumeTO(com.cloud.legacymodel.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.legacymodel.utils.Pair) NicTO(com.cloud.legacymodel.to.NicTO) SR(com.xensource.xenapi.SR) MigrateWithStorageAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageAnswer) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Aggregations

Pair (com.cloud.legacymodel.utils.Pair)139 ArrayList (java.util.ArrayList)87 List (java.util.List)64 Account (com.cloud.legacymodel.user.Account)49 Filter (com.cloud.utils.db.Filter)48 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)38 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)27 HashMap (java.util.HashMap)27 Ternary (com.cloud.legacymodel.utils.Ternary)23 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)22 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)20 SSHKeyPair (com.cloud.legacymodel.user.SSHKeyPair)16 TemplateFilter (com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter)13 Map (java.util.Map)13 DB (com.cloud.utils.db.DB)11 DomainVO (com.cloud.domain.DomainVO)10 VolumeVO (com.cloud.storage.VolumeVO)10 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)9 Network (com.cloud.legacymodel.network.Network)9 ResourceTagVO (com.cloud.tags.ResourceTagVO)9