Search in sources :

Example 6 with Hypervisor

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

the class ResourceManagerImpl method updateCluster.

@Override
@DB
public Cluster updateCluster(UpdateClusterCmd cmd) {
    ClusterVO cluster = (ClusterVO) getCluster(cmd.getId());
    String clusterType = cmd.getClusterType();
    String hypervisor = cmd.getHypervisor();
    String allocationState = cmd.getAllocationState();
    String managedstate = cmd.getManagedstate();
    String name = cmd.getClusterName();
    // Verify cluster information and update the cluster if needed
    boolean doUpdate = false;
    if (StringUtils.isNotBlank(name)) {
        if (cluster.getHypervisorType() == HypervisorType.VMware) {
            throw new InvalidParameterValueException("Renaming VMware cluster is not supported as it could cause problems if the updated  cluster name is not mapped on VCenter.");
        }
        s_logger.debug("Updating Cluster name to: " + name);
        cluster.setName(name);
        doUpdate = true;
    }
    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;
        }
    }
    Cluster.ClusterType newClusterType = null;
    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;
        }
    }
    Grouping.AllocationState newAllocationState = null;
    if (allocationState != null && !allocationState.isEmpty()) {
        try {
            newAllocationState = Grouping.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 = listAllHosts(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 = true;
                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) Grouping(com.cloud.org.Grouping) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) 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.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Managed(com.cloud.org.Managed) DB(com.cloud.utils.db.DB)

Example 7 with Hypervisor

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

the class SystemVmTemplateRegistration method parseMetadataFile.

/**
 * This method parses the metadata file consisting of the systemVM templates information
 * @return the version of the systemvm template that is to be used. This is done to in order
 * to fallback on the latest available version of the systemVM template when there does not
 * exist a template corresponding to the current code version.
 */
public static String parseMetadataFile() {
    try {
        Ini ini = new Ini();
        ini.load(new FileReader(METADATA_FILE));
        for (Hypervisor.HypervisorType hypervisorType : hypervisorList) {
            String hypervisor = hypervisorType.name().toLowerCase(Locale.ROOT);
            Ini.Section section = ini.get(hypervisor);
            NewTemplateNameList.put(hypervisorType, section.get("templatename"));
            FileNames.put(hypervisorType, section.get("filename"));
            NewTemplateChecksum.put(hypervisorType, section.get("checksum"));
            NewTemplateUrl.put(hypervisorType, section.get("downloadurl"));
        }
        Ini.Section section = ini.get("default");
        return section.get("version");
    } catch (Exception e) {
        String errMsg = String.format("Failed to parse systemVM template metadata file: %s", METADATA_FILE);
        LOGGER.error(errMsg, e);
        throw new CloudRuntimeException(errMsg, e);
    }
}
Also used : Ini(org.ini4j.Ini) Hypervisor(com.cloud.hypervisor.Hypervisor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) FileReader(java.io.FileReader) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException)

Example 8 with Hypervisor

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

the class SystemVmTemplateRegistration method registerTemplates.

public void registerTemplates(Set<Hypervisor.HypervisorType> hypervisorsInUse) {
    GlobalLock lock = GlobalLock.getInternLock("UpgradeDatabase-Lock");
    try {
        LOGGER.info("Grabbing lock to register templates.");
        if (!lock.lock(LOCK_WAIT_TIMEOUT)) {
            throw new CloudRuntimeException("Unable to acquire lock to register SystemVM template.");
        }
        try {
            validateTemplates(hypervisorsInUse);
            // Perform Registration if templates not already registered
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    List<Long> zoneIds = getEligibleZoneIds();
                    for (Long zoneId : zoneIds) {
                        String filePath = null;
                        try {
                            filePath = Files.createTempDirectory(TEMPORARY_SECONDARY_STORE).toString();
                            if (filePath == null) {
                                throw new CloudRuntimeException("Failed to create temporary file path to mount the store");
                            }
                            Pair<String, Long> storeUrlAndId = getNfsStoreInZone(zoneId);
                            mountStore(storeUrlAndId.first(), filePath);
                            List<String> hypervisorList = fetchAllHypervisors(zoneId);
                            for (String hypervisor : hypervisorList) {
                                Hypervisor.HypervisorType name = Hypervisor.HypervisorType.getType(hypervisor);
                                String templateName = NewTemplateNameList.get(name);
                                Pair<Hypervisor.HypervisorType, String> hypervisorAndTemplateName = new Pair<Hypervisor.HypervisorType, String>(name, templateName);
                                Long templateId = getRegisteredTemplateId(hypervisorAndTemplateName);
                                if (templateId != null) {
                                    VMTemplateVO templateVO = vmTemplateDao.findById(templateId);
                                    TemplateDataStoreVO templateDataStoreVO = templateDataStoreDao.findByTemplate(templateId, DataStoreRole.Image);
                                    String installPath = templateDataStoreVO.getInstallPath();
                                    if (validateIfSeeded(storeUrlAndId.first(), installPath)) {
                                        continue;
                                    } else if (templateVO != null) {
                                        registerTemplate(hypervisorAndTemplateName, storeUrlAndId, templateVO, filePath);
                                        continue;
                                    }
                                }
                                registerTemplate(hypervisorAndTemplateName, storeUrlAndId, filePath);
                            }
                            unmountStore(filePath);
                        } catch (Exception e) {
                            unmountStore(filePath);
                            throw new CloudRuntimeException("Failed to register systemVM template. Upgrade Failed");
                        }
                    }
                }
            });
        } catch (Exception e) {
            throw new CloudRuntimeException("Failed to register systemVM template. Upgrade Failed");
        }
    } finally {
        lock.unlock();
        lock.releaseRef();
    }
}
Also used : Hypervisor(com.cloud.hypervisor.Hypervisor) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) GlobalLock(com.cloud.utils.db.GlobalLock) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 9 with Hypervisor

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

the class SystemVmTemplateRegistration method validateTemplates.

private void validateTemplates(Set<Hypervisor.HypervisorType> hypervisorsInUse) {
    Set<String> hypervisors = hypervisorsInUse.stream().map(Enum::name).map(name -> name.toLowerCase(Locale.ROOT)).map(this::getHypervisorName).collect(Collectors.toSet());
    List<String> templates = new ArrayList<>();
    for (Hypervisor.HypervisorType hypervisorType : hypervisorsInUse) {
        templates.add(FileNames.get(hypervisorType));
    }
    boolean templatesFound = true;
    for (String hypervisor : hypervisors) {
        String matchedTemplate = templates.stream().filter(x -> x.contains(hypervisor)).findAny().orElse(null);
        if (matchedTemplate == null) {
            templatesFound = false;
            break;
        }
        File tempFile = new File(TEMPLATES_PATH + matchedTemplate);
        String templateChecksum = DigestHelper.calculateChecksum(tempFile);
        if (!templateChecksum.equals(NewTemplateChecksum.get(getHypervisorType(hypervisor)))) {
            LOGGER.error(String.format("Checksum mismatch: %s != %s ", templateChecksum, NewTemplateChecksum.get(getHypervisorType(hypervisor))));
            templatesFound = false;
            break;
        }
    }
    if (!templatesFound) {
        String errMsg = "SystemVm template not found. Cannot upgrade system Vms";
        LOGGER.error(errMsg);
        throw new CloudRuntimeException(errMsg);
    }
}
Also used : Hypervisor(com.cloud.hypervisor.Hypervisor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) File(java.io.File)

Example 10 with Hypervisor

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

the class SystemVmTemplateRegistration method registerTemplate.

public void registerTemplate(Pair<Hypervisor.HypervisorType, String> hypervisorAndTemplateName, Pair<String, Long> storeUrlAndId, String filePath) {
    Long templateId = null;
    try {
        Hypervisor.HypervisorType hypervisor = hypervisorAndTemplateName.first();
        templateId = performTemplateRegistrationOperations(hypervisorAndTemplateName, NewTemplateUrl.get(hypervisor), NewTemplateChecksum.get(hypervisor), hypervisorImageFormat.get(hypervisor), hypervisorGuestOsMap.get(hypervisor), storeUrlAndId.second(), null, filePath, true);
        Map<String, String> configParams = new HashMap<>();
        configParams.put(RouterTemplateConfigurationNames.get(hypervisorAndTemplateName.first()), hypervisorAndTemplateName.second());
        configParams.put("minreq.sysvmtemplate.version", getSystemVmTemplateVersion());
        updateConfigurationParams(configParams);
        updateSystemVMEntries(templateId, hypervisorAndTemplateName.first());
    } catch (Exception e) {
        String errMsg = String.format("Failed to register template for hypervisor: %s", hypervisorAndTemplateName.first());
        LOGGER.error(errMsg, e);
        if (templateId != null) {
            updateTemplateTablesOnFailure(templateId);
            cleanupStore(templateId, filePath);
        }
        throw new CloudRuntimeException(errMsg, e);
    }
}
Also used : Hypervisor(com.cloud.hypervisor.Hypervisor) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException)

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