Search in sources :

Example 46 with SecondaryStorageVmVO

use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.

the class SecondaryStorageManagerImpl method allocCapacity.

public void allocCapacity(long dataCenterId, SecondaryStorageVm.Role role) {
    s_logger.debug(String.format("Allocate secondary storage VM standby capacity for zone [%s].", dataCenterId));
    if (!isSecondaryStorageVmRequired(dataCenterId)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(String.format("Secondary storage VM not required in zone [%s] according to zone config.", dataCenterId));
        }
        return;
    }
    SecondaryStorageVmVO secStorageVm = null;
    String errorString = null;
    try {
        boolean secStorageVmFromStoppedPool = false;
        secStorageVm = assignSecStorageVmFromStoppedPool(dataCenterId, role);
        if (secStorageVm == null) {
            if (s_logger.isInfoEnabled()) {
                s_logger.info("No stopped secondary storage VM is available, need to allocate a new secondary storage VM.");
            }
            if (_allocLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC_IN_SECONDS)) {
                try {
                    secStorageVm = startNew(dataCenterId, role);
                } finally {
                    _allocLock.unlock();
                }
            } else {
                if (s_logger.isInfoEnabled()) {
                    s_logger.info("Unable to acquire synchronization lock for secondary storage VM allocation, wait for next scan.");
                }
                return;
            }
        } else {
            if (s_logger.isInfoEnabled()) {
                s_logger.info(String.format("Found a stopped secondary storage %s, starting it.", secStorageVm.toString()));
            }
            secStorageVmFromStoppedPool = true;
        }
        if (secStorageVm != null) {
            long secStorageVmId = secStorageVm.getId();
            GlobalLock secStorageVmLock = GlobalLock.getInternLock(getSecStorageVmLockName(secStorageVmId));
            try {
                if (secStorageVmLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC_IN_SECONDS)) {
                    try {
                        secStorageVm = startSecStorageVm(secStorageVmId);
                    } finally {
                        secStorageVmLock.unlock();
                    }
                } else {
                    if (s_logger.isInfoEnabled()) {
                        s_logger.info(String.format("Unable to acquire synchronization lock for starting secondary storage %s.", secStorageVm.toString()));
                    }
                    return;
                }
            } finally {
                secStorageVmLock.releaseRef();
            }
            if (secStorageVm == null) {
                if (s_logger.isInfoEnabled()) {
                    s_logger.info(String.format("Unable to start secondary storage VM [%s] for standby capacity, it will be recycled and will start a new one.", secStorageVmId));
                }
                if (secStorageVmFromStoppedPool) {
                    destroySecStorageVm(secStorageVmId);
                }
            } else {
                SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this, new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_UP, dataCenterId, secStorageVmId, secStorageVm, null));
                if (s_logger.isInfoEnabled()) {
                    s_logger.info(String.format("Secondary storage %s was started.", secStorageVm.toString()));
                }
            }
        }
    } catch (Exception e) {
        errorString = String.format("Unable to allocate capacity on zone [%s] due to [%s].", dataCenterId, errorString);
        throw e;
    } finally {
        if (secStorageVm == null || secStorageVm.getState() != State.Running)
            SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this, new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_CREATE_FAILURE, dataCenterId, 0l, null, errorString));
    }
}
Also used : GlobalLock(com.cloud.utils.db.GlobalLock) SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) SecStorageVmAlertEventArgs(com.cloud.storage.secondary.SecStorageVmAlertEventArgs) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) URISyntaxException(java.net.URISyntaxException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException)

Example 47 with SecondaryStorageVmVO

use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.

the class SecondaryStorageManagerImpl method createSecStorageVmInstance.

protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, SecondaryStorageVm.Role role) {
    DataStore secStore = _dataStoreMgr.getImageStoreWithFreeCapacity(dataCenterId);
    if (secStore == null) {
        String msg = String.format("No secondary storage available in zone %s, cannot create secondary storage VM.", dataCenterId);
        s_logger.warn(msg);
        throw new CloudRuntimeException(msg);
    }
    long id = _secStorageVmDao.getNextInSequence(Long.class, "id");
    String name = VirtualMachineName.getSystemVmName(id, _instance, "s").intern();
    Account systemAcct = _accountMgr.getSystemAccount();
    DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
    DataCenter dc = _dcDao.findById(plan.getDataCenterId());
    NetworkVO defaultNetwork = getDefaultNetworkForCreation(dc);
    List<? extends NetworkOffering> offerings = null;
    if (_sNwMgr.isStorageIpRangeAvailable(dataCenterId)) {
        offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork, NetworkOffering.SystemStorageNetwork);
    } else {
        offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork);
    }
    LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<>(offerings.size() + 1);
    NicProfile defaultNic = new NicProfile();
    defaultNic.setDefaultNic(true);
    defaultNic.setDeviceId(2);
    try {
        networks.put(_networkMgr.setupNetwork(systemAcct, _networkOfferingDao.findById(defaultNetwork.getNetworkOfferingId()), plan, null, null, false).get(0), new ArrayList<>(Arrays.asList(defaultNic)));
        for (NetworkOffering offering : offerings) {
            networks.put(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false).get(0), new ArrayList<>());
        }
    } catch (ConcurrentOperationException e) {
        s_logger.error(String.format("Unable to setup networks on %s due [%s].", dc.toString(), e.getMessage()), e);
        return new HashMap<>();
    }
    HypervisorType availableHypervisor = _resourceMgr.getAvailableHypervisor(dataCenterId);
    VMTemplateVO template = _templateDao.findSystemVMReadyTemplate(dataCenterId, availableHypervisor);
    if (template == null) {
        throw new CloudRuntimeException(String.format("Unable to find the system templates or it was not downloaded in %s.", dc.toString()));
    }
    ServiceOfferingVO serviceOffering = _serviceOffering;
    if (serviceOffering == null) {
        serviceOffering = _offeringDao.findDefaultSystemOffering(ServiceOffering.ssvmDefaultOffUniqueName, ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId));
    }
    SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), systemAcct.getId(), _accountMgr.getSystemUser().getId(), role, serviceOffering.isOfferHA());
    secStorageVm.setDynamicallyScalable(template.isDynamicallyScalable());
    secStorageVm = _secStorageVmDao.persist(secStorageVm);
    try {
        _itMgr.allocate(name, template, serviceOffering, networks, plan, null);
        secStorageVm = _secStorageVmDao.findById(secStorageVm.getId());
    } catch (InsufficientCapacityException e) {
        String errorMessage = String.format("Unable to allocate secondary storage VM [%s] due to [%s].", name, e.getMessage());
        s_logger.warn(errorMessage, e);
        throw new CloudRuntimeException(errorMessage, e);
    }
    Map<String, Object> context = new HashMap<>();
    context.put("secStorageVmId", secStorageVm.getId());
    return context;
}
Also used : Account(com.cloud.user.Account) SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) VMTemplateVO(com.cloud.storage.VMTemplateVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) LinkedHashMap(java.util.LinkedHashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) Network(com.cloud.network.Network) ArrayList(java.util.ArrayList) List(java.util.List) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NetworkVO(com.cloud.network.dao.NetworkVO) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) NetworkOffering(com.cloud.offering.NetworkOffering) NicProfile(com.cloud.vm.NicProfile) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) DataCenter(com.cloud.dc.DataCenter)

Example 48 with SecondaryStorageVmVO

use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.

the class SecondaryStorageManagerImpl method startSecStorageVm.

@Override
public SecondaryStorageVmVO startSecStorageVm(long secStorageVmId) {
    try {
        SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId);
        _itMgr.advanceStart(secStorageVm.getUuid(), null, null);
        return _secStorageVmDao.findById(secStorageVm.getId());
    } catch (ConcurrentOperationException | InsufficientCapacityException | OperationTimedoutException | ResourceUnavailableException e) {
        s_logger.warn(String.format("Unable to start secondary storage VM [%s] due to [%s].", secStorageVmId, e.getMessage()), e);
        return null;
    }
}
Also used : SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Example 49 with SecondaryStorageVmVO

use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.

the class SecondaryStorageManagerImpl method generateVMSetupCommand.

@Override
public boolean generateVMSetupCommand(Long ssAHostId) {
    HostVO ssAHost = _hostDao.findById(ssAHostId);
    if (ssAHost.getType() != Host.Type.SecondaryStorageVM) {
        return false;
    }
    String ssvmName = ssAHost.getName();
    SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(ssvmName);
    if (secStorageVm == null) {
        s_logger.warn(String.format("Secondary storage VM [%s] does not exist.", ssvmName));
        return false;
    }
    SecStorageVMSetupCommand setupCmd = new SecStorageVMSetupCommand();
    if (_allowedInternalSites != null) {
        List<String> allowedCidrs = new ArrayList<>();
        String[] cidrs = _allowedInternalSites.split(",");
        for (String cidr : cidrs) {
            if (NetUtils.isValidIp4Cidr(cidr) || NetUtils.isValidIp4(cidr) || !cidr.startsWith("0.0.0.0")) {
                allowedCidrs.add(cidr);
            }
        }
        setupCmd.setAllowedInternalSites(allowedCidrs.toArray(new String[allowedCidrs.size()]));
    }
    String copyPasswd = _configDao.getValue("secstorage.copy.password");
    setupCmd.setCopyPassword(copyPasswd);
    setupCmd.setCopyUserName(TemplateConstants.DEFAULT_HTTP_AUTH_USER);
    Answer answer = _agentMgr.easySend(ssAHostId, setupCmd);
    if (answer != null && answer.getResult()) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(String.format("Successfully set HTTP auth into secondary storage VM [%s].", ssvmName));
        }
        return true;
    } else {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(String.format("Failed to set HTTP auth into secondary storage VM [%s] due to [%s].", ssvmName, answer == null ? "answer null" : answer.getDetails()));
        }
        return false;
    }
}
Also used : SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) Answer(com.cloud.agent.api.Answer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) SecStorageVMSetupCommand(com.cloud.agent.api.SecStorageVMSetupCommand) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO)

Example 50 with SecondaryStorageVmVO

use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.

the class SecondaryStorageManagerImpl method finalizeStart.

@Override
public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) {
    CheckSshAnswer answer = (CheckSshAnswer) cmds.getAnswer("checkSsh");
    if (!answer.getResult()) {
        s_logger.warn(String.format("Unable to connect via SSH to the VM [%s] due to [%s] ", profile.toString(), answer.getDetails()));
        return false;
    }
    try {
        _rulesMgr.getSystemIpAndEnableStaticNatForVm(profile.getVirtualMachine(), false);
        IPAddressVO ipaddr = _ipAddressDao.findByAssociatedVmId(profile.getVirtualMachine().getId());
        if (ipaddr != null && ipaddr.getSystem()) {
            SecondaryStorageVmVO secVm = _secStorageVmDao.findById(profile.getId());
            secVm.setPublicIpAddress(ipaddr.getAddress().addr());
            _secStorageVmDao.update(secVm.getId(), secVm);
        }
    } catch (InsufficientAddressCapacityException ex) {
        s_logger.error(String.format("Failed to get system IP and enable static NAT for the VM [%s] due to [%s].", profile.toString(), ex.getMessage()), ex);
        return false;
    }
    return true;
}
Also used : CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Aggregations

SecondaryStorageVmVO (com.cloud.vm.SecondaryStorageVmVO)53 HostVO (com.cloud.host.HostVO)18 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)16 Answer (com.cloud.agent.api.Answer)14 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)11 CheckSshAnswer (com.cloud.agent.api.check.CheckSshAnswer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)9 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)9 SecStorageSetupAnswer (com.cloud.agent.api.SecStorageSetupAnswer)8 ArrayList (java.util.ArrayList)8 Account (com.cloud.user.Account)7 DB (com.cloud.utils.db.DB)6 NicProfile (com.cloud.vm.NicProfile)6 ConfigurationException (javax.naming.ConfigurationException)6 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)6 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)5 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)5 Pair (com.cloud.utils.Pair)5 ModifyStoragePoolCommand (com.cloud.agent.api.ModifyStoragePoolCommand)4 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)4