Search in sources :

Example 16 with Hypervisor

use of com.cloud.hypervisor.Hypervisor in project cloudstack by apache.

the class SystemVmTemplateRegistration method performTemplateRegistrationOperations.

private Long performTemplateRegistrationOperations(Pair<Hypervisor.HypervisorType, String> hypervisorAndTemplateName, String url, String checksum, ImageFormat format, long guestOsId, Long storeId, Long templateId, String filePath, boolean updateTmpltDetails) {
    Hypervisor.HypervisorType hypervisor = hypervisorAndTemplateName.first();
    String templateName = UUID.randomUUID().toString();
    Date created = new Date(DateUtil.currentGMTTime().getTime());
    SystemVMTemplateDetails details = new SystemVMTemplateDetails(templateName, hypervisorAndTemplateName.second(), created, url, checksum, format, (int) guestOsId, hypervisor, storeId);
    if (templateId == null) {
        VMTemplateVO template = createTemplateObjectInDB(details);
        if (template == null) {
            throw new CloudRuntimeException(String.format("Failed to register template for hypervisor: %s", hypervisor.name()));
        }
        templateId = template.getId();
        createCrossZonesTemplateZoneRefEntries(template);
    }
    details.setId(templateId);
    String destTempFolderName = String.valueOf(templateId);
    String destTempFolder = filePath + PARTIAL_TEMPLATE_FOLDER + destTempFolderName;
    details.setInstallPath(PARTIAL_TEMPLATE_FOLDER + destTempFolderName + File.separator + templateName + "." + hypervisorImageFormat.get(hypervisor).getFileExtension());
    createTemplateStoreRefEntry(details);
    setupTemplate(templateName, hypervisorAndTemplateName, destTempFolder);
    readTemplateProperties(destTempFolder + "/template.properties", details);
    details.setUpdated(new Date(DateUtil.currentGMTTime().getTime()));
    updateTemplateDetails(details, updateTmpltDetails);
    return templateId;
}
Also used : Hypervisor(com.cloud.hypervisor.Hypervisor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.sql.Date)

Example 17 with Hypervisor

use of com.cloud.hypervisor.Hypervisor in project cloudstack by apache.

the class SystemVmTemplateRegistration method updateSystemVmTemplates.

public void updateSystemVmTemplates(final Connection conn) {
    LOGGER.debug("Updating System Vm template IDs");
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>();
            try {
                hypervisorsListInUse = clusterDao.getDistictAvailableHypervisorsAcrossClusters();
            } catch (final Exception e) {
                LOGGER.error("updateSystemVmTemplates: Exception caught while getting hypervisor types from clusters: " + e.getMessage());
                throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting hypervisor types from clusters", e);
            }
            for (final Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()) {
                LOGGER.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms");
                Long templateId = getRegisteredTemplateId(new Pair<>(hypervisorAndTemplateName.getKey(), hypervisorAndTemplateName.getValue()));
                try {
                    // change template type to SYSTEM
                    if (templateId != null) {
                        updateRegisteredTemplateDetails(templateId, hypervisorAndTemplateName);
                    } else {
                        if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())) {
                            try {
                                registerTemplates(hypervisorsListInUse);
                                break;
                            } catch (final Exception e) {
                                throw new CloudRuntimeException(String.format("%s %s SystemVm template not found. Cannot upgrade system Vms", getSystemVmTemplateVersion(), hypervisorAndTemplateName.getKey()));
                            }
                        } else {
                            LOGGER.warn(String.format("%s %s SystemVm template not found. Cannot upgrade system Vms hypervisor is not used, so not failing upgrade", getSystemVmTemplateVersion(), hypervisorAndTemplateName.getKey()));
                            // Update the latest template URLs for corresponding hypervisor
                            VMTemplateVO templateVO = vmTemplateDao.findLatestTemplateByTypeAndHypervisor(hypervisorAndTemplateName.getKey(), Storage.TemplateType.SYSTEM);
                            if (templateVO != null) {
                                updateTemplateUrlAndChecksum(templateVO, hypervisorAndTemplateName);
                            }
                        }
                    }
                } catch (final Exception e) {
                    String errMsg = "updateSystemVmTemplates:Exception while getting ids of templates";
                    LOGGER.error(errMsg, e);
                    throw new CloudRuntimeException(errMsg, e);
                }
            }
            LOGGER.debug("Updating System Vm Template IDs Complete");
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Hypervisor(com.cloud.hypervisor.Hypervisor) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Pair(com.cloud.utils.Pair)

Example 18 with Hypervisor

use of com.cloud.hypervisor.Hypervisor in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method discoverCluster.

@DB
@Override
public List<? extends Cluster> discoverCluster(final AddClusterCmd cmd) throws IllegalArgumentException, DiscoveryException, ResourceInUseException {
    final ResourceChecker resourceChecker = ResourceChecker.builder().dataCenterDao(_dcDao).accountManager(_accountMgr).hostPodDao(_podDao).build();
    final long dcId = cmd.getZoneId();
    final long podId = cmd.getPodId();
    final String clusterName = cmd.getClusterName();
    String url = cmd.getUrl();
    final String username = cmd.getUsername();
    final String password = cmd.getPassword();
    if (url != null) {
        url = URLDecoder.decode(url);
    }
    final URI uri;
    final DataCenterVO zone = resourceChecker.checkIfDataCenterExists(dcId);
    final Account account = CallContext.current().getCallingAccount();
    resourceChecker.checkIfDataCenterIsUsable(zone, account);
    final HostPodVO pod = _podDao.findById(podId);
    if (pod == null) {
        throw new InvalidParameterValueException("Can't find pod with specified podId " + podId);
    }
    // Check if the pod exists in the system
    if (_podDao.findById(podId) == null) {
        throw new InvalidParameterValueException("Can't find pod by id " + podId);
    }
    // check if pod belongs to the zone
    if (!Long.valueOf(pod.getDataCenterId()).equals(dcId)) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Pod with specified id doesn't belong to the zone " + dcId);
        ex.addProxyObject(pod.getUuid(), "podId");
        ex.addProxyObject(zone.getUuid(), "dcId");
        throw ex;
    }
    // Verify cluster information and create a new cluster if needed
    if (clusterName == null || clusterName.isEmpty()) {
        throw new InvalidParameterValueException("Please specify cluster name");
    }
    if (cmd.getHypervisor() == null || cmd.getHypervisor().isEmpty()) {
        throw new InvalidParameterValueException("Please specify a hypervisor");
    }
    final Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.getType(cmd.getHypervisor());
    if (hypervisorType == null) {
        s_logger.error("Unable to resolve " + cmd.getHypervisor() + " to a valid supported hypervisor type");
        throw new InvalidParameterValueException("Unable to resolve " + cmd.getHypervisor() + " to a supported ");
    }
    Cluster.ClusterType clusterType = null;
    if (cmd.getClusterType() != null && !cmd.getClusterType().isEmpty()) {
        clusterType = Cluster.ClusterType.valueOf(cmd.getClusterType());
    }
    if (clusterType == null) {
        clusterType = Cluster.ClusterType.CloudManaged;
    }
    AllocationState allocationState = null;
    if (cmd.getAllocationState() != null && !cmd.getAllocationState().isEmpty()) {
        try {
            allocationState = AllocationState.valueOf(cmd.getAllocationState());
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve Allocation State '" + cmd.getAllocationState() + "' to a supported state");
        }
    }
    if (allocationState == null) {
        allocationState = AllocationState.Enabled;
    }
    final Discoverer discoverer = getMatchingDiscover(hypervisorType);
    if (discoverer == null) {
        throw new InvalidParameterValueException("Could not find corresponding resource manager for " + cmd.getHypervisor());
    }
    final List<ClusterVO> result = new ArrayList<>();
    ClusterVO cluster = new ClusterVO(dcId, podId, clusterName);
    cluster.setHypervisorType(hypervisorType.toString());
    cluster.setClusterType(clusterType);
    cluster.setAllocationState(allocationState);
    try {
        cluster = _clusterDao.persist(cluster);
    } catch (final Exception e) {
        // no longer tolerate exception during the cluster creation phase
        final CloudRuntimeException ex = new CloudRuntimeException("Unable to create cluster " + clusterName + " in pod and data center with specified ids", e);
        // Get the pod VO object's table name.
        ex.addProxyObject(pod.getUuid(), "podId");
        ex.addProxyObject(zone.getUuid(), "dcId");
        throw ex;
    }
    result.add(cluster);
    if (clusterType == Cluster.ClusterType.CloudManaged) {
        final Map<String, String> details = new HashMap<>();
        details.put("cpuOvercommitRatio", CapacityManager.CpuOverprovisioningFactor.value().toString());
        details.put("memoryOvercommitRatio", CapacityManager.MemOverprovisioningFactor.value().toString());
        _clusterDetailsDao.persist(cluster.getId(), details);
        return result;
    }
    // save cluster details for later cluster/host cross-checking
    final Map<String, String> details = new HashMap<>();
    details.put("url", url);
    details.put("username", username);
    details.put("password", password);
    details.put("cpuOvercommitRatio", CapacityManager.CpuOverprovisioningFactor.value().toString());
    details.put("memoryOvercommitRatio", CapacityManager.MemOverprovisioningFactor.value().toString());
    _clusterDetailsDao.persist(cluster.getId(), details);
    boolean success = false;
    try {
        try {
            uri = new URI(UriUtils.encodeURIComponent(url));
            if (uri.getScheme() == null) {
                throw new InvalidParameterValueException("uri.scheme is null " + url + ", add http:// as a prefix");
            } else if (uri.getScheme().equalsIgnoreCase("http")) {
                if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("")) {
                    throw new InvalidParameterValueException("Your host and/or path is wrong.  Make sure it's of the format http://hostname/path");
                }
            }
        } catch (final URISyntaxException e) {
            throw new InvalidParameterValueException(url + " is not a valid uri");
        }
        final List<HostVO> hosts = new ArrayList<>();
        final Map<? extends ServerResource, Map<String, String>> resources;
        resources = discoverer.find(dcId, podId, cluster.getId(), uri, username, password, null);
        if (resources != null) {
            for (final Map.Entry<? extends ServerResource, Map<String, String>> entry : resources.entrySet()) {
                final ServerResource resource = entry.getKey();
                final HostVO host = (HostVO) createHostAndAgent(resource, entry.getValue(), true, null, false);
                if (host != null) {
                    hosts.add(host);
                }
                discoverer.postDiscovery(hosts, _nodeId);
            }
            s_logger.info("External cluster has been successfully discovered by " + discoverer.getName());
            success = true;
            return result;
        }
        s_logger.warn("Unable to find the server resources at " + url);
        throw new DiscoveryException("Unable to add the external cluster");
    } finally {
        if (!success) {
            _clusterDetailsDao.deleteDetails(cluster.getId());
            _clusterDao.remove(cluster.getId());
        }
    }
}
Also used : Account(com.cloud.user.Account) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HostPodVO(com.cloud.dc.HostPodVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DiscoveryException(com.cloud.exception.DiscoveryException) DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) Hypervisor(com.cloud.hypervisor.Hypervisor) PodCluster(com.cloud.dc.PodCluster) Cluster(com.cloud.org.Cluster) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) SshException(com.cloud.utils.ssh.SshException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) AllocationState(com.cloud.model.enumeration.AllocationState) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Map(java.util.Map) HashMap(java.util.HashMap) DB(com.cloud.utils.db.DB)

Example 19 with Hypervisor

use of com.cloud.hypervisor.Hypervisor in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method updateCluster.

@Override
@DB
public Cluster updateCluster(final Cluster clusterToUpdate, final String clusterType, final String hypervisor, final String allocationState, final String managedstate) {
    final ClusterVO cluster = (ClusterVO) clusterToUpdate;
    // Verify cluster information and update the cluster if needed
    boolean doUpdate = false;
    if (hypervisor != null && !hypervisor.isEmpty()) {
        final Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.getType(hypervisor);
        if (hypervisorType == null) {
            s_logger.error("Unable to resolve " + hypervisor + " to a valid supported hypervisor type");
            throw new InvalidParameterValueException("Unable to resolve " + hypervisor + " to a supported type");
        } else {
            cluster.setHypervisorType(hypervisor);
            doUpdate = true;
        }
    }
    final Cluster.ClusterType newClusterType;
    if (clusterType != null && !clusterType.isEmpty()) {
        try {
            newClusterType = Cluster.ClusterType.valueOf(clusterType);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve " + clusterType + " to a supported type");
        }
        if (newClusterType == null) {
            s_logger.error("Unable to resolve " + clusterType + " to a valid supported cluster type");
            throw new InvalidParameterValueException("Unable to resolve " + clusterType + " to a supported type");
        } else {
            cluster.setClusterType(newClusterType);
            doUpdate = true;
        }
    }
    final AllocationState newAllocationState;
    if (allocationState != null && !allocationState.isEmpty()) {
        try {
            newAllocationState = AllocationState.valueOf(allocationState);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve Allocation State '" + allocationState + "' to a supported state");
        }
        if (newAllocationState == null) {
            s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State");
            throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state");
        } else {
            cluster.setAllocationState(newAllocationState);
            doUpdate = true;
        }
    }
    Managed.ManagedState newManagedState = null;
    final Managed.ManagedState oldManagedState = cluster.getManagedState();
    if (managedstate != null && !managedstate.isEmpty()) {
        try {
            newManagedState = Managed.ManagedState.valueOf(managedstate);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve Managed State '" + managedstate + "' to a supported state");
        }
        if (newManagedState == null) {
            s_logger.error("Unable to resolve Managed State '" + managedstate + "' to a supported state");
            throw new InvalidParameterValueException("Unable to resolve Managed State '" + managedstate + "' to a supported state");
        } else {
            doUpdate = true;
        }
    }
    if (doUpdate) {
        _clusterDao.update(cluster.getId(), cluster);
    }
    if (newManagedState != null && !newManagedState.equals(oldManagedState)) {
        if (newManagedState.equals(Managed.ManagedState.Unmanaged)) {
            boolean success = false;
            try {
                cluster.setManagedState(Managed.ManagedState.PrepareUnmanaged);
                _clusterDao.update(cluster.getId(), cluster);
                List<HostVO> hosts = listAllUpAndEnabledHosts(Host.Type.Routing, cluster.getId(), cluster.getPodId(), cluster.getDataCenterId());
                for (final HostVO host : hosts) {
                    if (host.getType().equals(Host.Type.Routing) && !host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Up) && !host.getStatus().equals(Status.Alert)) {
                        final String msg = "host " + host.getPrivateIpAddress() + " should not be in " + host.getStatus().toString() + " status";
                        throw new CloudRuntimeException("PrepareUnmanaged Failed due to " + msg);
                    }
                }
                for (final HostVO host : hosts) {
                    if (host.getStatus().equals(Status.Up)) {
                        umanageHost(host.getId());
                    }
                }
                final int retry = 40;
                boolean lsuccess;
                for (int i = 0; i < retry; i++) {
                    lsuccess = true;
                    try {
                        Thread.sleep(5 * 1000);
                    } catch (final Exception e) {
                    }
                    hosts = listAllUpAndEnabledHosts(Host.Type.Routing, cluster.getId(), cluster.getPodId(), cluster.getDataCenterId());
                    for (final HostVO host : hosts) {
                        if (!host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Alert)) {
                            lsuccess = false;
                            break;
                        }
                    }
                    if (lsuccess == true) {
                        success = true;
                        break;
                    }
                }
                if (success == false) {
                    throw new CloudRuntimeException("PrepareUnmanaged Failed due to some hosts are still in UP status after 5 Minutes, please try later ");
                }
            } finally {
                cluster.setManagedState(success ? Managed.ManagedState.Unmanaged : Managed.ManagedState.PrepareUnmanagedError);
                _clusterDao.update(cluster.getId(), cluster);
            }
        } else if (newManagedState.equals(Managed.ManagedState.Managed)) {
            cluster.setManagedState(Managed.ManagedState.Managed);
            _clusterDao.update(cluster.getId(), cluster);
        }
    }
    return cluster;
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) Hypervisor(com.cloud.hypervisor.Hypervisor) PodCluster(com.cloud.dc.PodCluster) Cluster(com.cloud.org.Cluster) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) SshException(com.cloud.utils.ssh.SshException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) AllocationState(com.cloud.model.enumeration.AllocationState) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Managed(com.cloud.org.Managed) DB(com.cloud.utils.db.DB)

Aggregations

Hypervisor (com.cloud.hypervisor.Hypervisor)19 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)18 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)7 HostVO (com.cloud.host.HostVO)7 DB (com.cloud.utils.db.DB)7 URISyntaxException (java.net.URISyntaxException)7 ArrayList (java.util.ArrayList)7 ClusterVO (com.cloud.dc.ClusterVO)6 DiscoveryException (com.cloud.exception.DiscoveryException)6 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6 ResourceInUseException (com.cloud.exception.ResourceInUseException)6 Cluster (com.cloud.org.Cluster)6 Grouping (com.cloud.org.Grouping)6 VMTemplateVO (com.cloud.storage.VMTemplateVO)6 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)6 HashSet (java.util.HashSet)6