Search in sources :

Example 26 with SecondaryStorageVmVO

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

the class NetworkServiceImpl method addTrafficTypeToPhysicalNetwork.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TRAFFIC_TYPE_CREATE, eventDescription = "Creating Physical Network TrafficType", create = true)
public PhysicalNetworkTrafficType addTrafficTypeToPhysicalNetwork(Long physicalNetworkId, String trafficTypeStr, String isolationMethod, String xenLabel, String kvmLabel, String vmwareLabel, String simulatorLabel, String vlan, String hypervLabel, String ovm3Label) {
    // verify input parameters
    PhysicalNetworkVO network = _physicalNetworkDao.findById(physicalNetworkId);
    if (network == null) {
        throw new InvalidParameterValueException("Physical Network id=" + physicalNetworkId + "doesn't exist in the system");
    }
    Networks.TrafficType trafficType = null;
    if (trafficTypeStr != null && !trafficTypeStr.isEmpty()) {
        try {
            trafficType = Networks.TrafficType.valueOf(trafficTypeStr);
        } catch (IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve trafficType '" + trafficTypeStr + "' to a supported value");
        }
    }
    if (_pNTrafficTypeDao.isTrafficTypeSupported(physicalNetworkId, trafficType)) {
        throw new CloudRuntimeException("This physical network already supports the traffic type: " + trafficType);
    }
    if (TrafficType.isSystemNetwork(trafficType) || TrafficType.Public.equals(trafficType) || TrafficType.Storage.equals(trafficType)) {
        if (!_physicalNetworkDao.listByZoneAndTrafficType(network.getDataCenterId(), trafficType).isEmpty()) {
            throw new CloudRuntimeException("Fail to add the traffic type to physical network because Zone already has a physical network with this traffic type: " + trafficType);
        }
    }
    if (TrafficType.Storage.equals(trafficType)) {
        List<SecondaryStorageVmVO> ssvms = _stnwMgr.getSSVMWithNoStorageNetwork(network.getDataCenterId());
        if (!ssvms.isEmpty()) {
            StringBuilder sb = new StringBuilder("Cannot add " + trafficType + " traffic type as there are below secondary storage vm still running. Please stop them all and add Storage traffic type again, then destory them all to allow CloudStack recreate them with storage network(If you have added storage network ip range)");
            sb.append("SSVMs:");
            for (SecondaryStorageVmVO ssvm : ssvms) {
                sb.append(ssvm.getInstanceName()).append(":").append(ssvm.getState());
            }
            throw new CloudRuntimeException(sb.toString());
        }
    }
    try {
        // Create the new traffic type in the database
        if (xenLabel == null) {
            xenLabel = getDefaultXenNetworkLabel(trafficType);
        }
        PhysicalNetworkTrafficTypeVO pNetworktrafficType = new PhysicalNetworkTrafficTypeVO(physicalNetworkId, trafficType, xenLabel, kvmLabel, vmwareLabel, simulatorLabel, vlan, hypervLabel, ovm3Label);
        pNetworktrafficType = _pNTrafficTypeDao.persist(pNetworktrafficType);
        // each broadcast type will individually need to be qualified for support of public traffic
        if (TrafficType.Public.equals(trafficType)) {
            List<String> isolationMethods = network.getIsolationMethods();
            if ((isolationMethods.size() == 1 && isolationMethods.get(0).toLowerCase().equals("vxlan")) || (isolationMethod != null && isolationMethods.contains(isolationMethod) && isolationMethod.toLowerCase().equals("vxlan"))) {
                // find row in networks table that is defined as 'Public', created when zone was deployed
                NetworkVO publicNetwork = _networksDao.listByZoneAndTrafficType(network.getDataCenterId(), TrafficType.Public).get(0);
                if (publicNetwork != null) {
                    s_logger.debug("setting public network " + publicNetwork + " to broadcast type vxlan");
                    publicNetwork.setBroadcastDomainType(BroadcastDomainType.Vxlan);
                    _networksDao.persist(publicNetwork);
                }
            }
        }
        return pNetworktrafficType;
    } catch (Exception ex) {
        s_logger.warn("Exception: ", ex);
        throw new CloudRuntimeException("Fail to add a traffic type to physical network");
    }
}
Also used : SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) PhysicalNetworkTrafficTypeVO(com.cloud.network.dao.PhysicalNetworkTrafficTypeVO) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) TrafficType(com.cloud.network.Networks.TrafficType) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 27 with SecondaryStorageVmVO

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

the class StoragePoolAutomationImpl method maintain.

@Override
public boolean maintain(DataStore store) {
    Long userId = CallContext.current().getCallingUserId();
    User user = _userDao.findById(userId);
    Account account = CallContext.current().getCallingAccount();
    StoragePoolVO pool = primaryDataStoreDao.findById(store.getId());
    try {
        List<StoragePoolVO> spes = null;
        // if the storage is ZONE wide then we pass podid and cluster id as null as they will be empty for ZWPS
        if (pool.getScope() == ScopeType.ZONE) {
            spes = primaryDataStoreDao.listBy(pool.getDataCenterId(), null, null, ScopeType.ZONE);
        } else {
            spes = primaryDataStoreDao.listBy(pool.getDataCenterId(), pool.getPodId(), pool.getClusterId(), ScopeType.CLUSTER);
        }
        for (StoragePoolVO sp : spes) {
            if (sp.getStatus() == StoragePoolStatus.PrepareForMaintenance) {
                throw new CloudRuntimeException("Only one storage pool in a cluster can be in PrepareForMaintenance mode, " + sp.getId() + " is already in  PrepareForMaintenance mode ");
            }
        }
        StoragePool storagePool = (StoragePool) store;
        //Handeling the Zone wide and cluster wide primay storage
        List<HostVO> hosts = new ArrayList<HostVO>();
        //TODO: if it's zone wide, this code will list a lot of hosts in the zone, which may cause performance/OOM issue.
        if (pool.getScope().equals(ScopeType.ZONE)) {
            if (HypervisorType.Any.equals(pool.getHypervisor())) {
                hosts = _resourceMgr.listAllUpAndEnabledHostsInOneZone(pool.getDataCenterId());
            } else {
                hosts = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByHypervisor(pool.getHypervisor(), pool.getDataCenterId());
            }
        } else {
            hosts = _resourceMgr.listHostsInClusterByStatus(pool.getClusterId(), Status.Up);
        }
        if (hosts == null || hosts.size() == 0) {
            pool.setStatus(StoragePoolStatus.Maintenance);
            primaryDataStoreDao.update(pool.getId(), pool);
            return true;
        } else {
            // set the pool state to prepare for maintenance
            pool.setStatus(StoragePoolStatus.PrepareForMaintenance);
            primaryDataStoreDao.update(pool.getId(), pool);
        }
        // remove heartbeat
        for (HostVO host : hosts) {
            ModifyStoragePoolCommand cmd = new ModifyStoragePoolCommand(false, storagePool);
            final Answer answer = agentMgr.easySend(host.getId(), cmd);
            if (answer == null || !answer.getResult()) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("ModifyStoragePool false failed due to " + ((answer == null) ? "answer null" : answer.getDetails()));
                }
            } else {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("ModifyStoragePool false succeeded");
                }
            }
        }
        // check to see if other ps exist
        // if they do, then we can migrate over the system vms to them
        // if they dont, then just stop all vms on this one
        List<StoragePoolVO> upPools = primaryDataStoreDao.listByStatusInZone(pool.getDataCenterId(), StoragePoolStatus.Up);
        boolean restart = true;
        if (upPools == null || upPools.size() == 0) {
            restart = false;
        }
        // 2. Get a list of all the ROOT volumes within this storage pool
        List<VolumeVO> allVolumes = volumeDao.findByPoolId(pool.getId());
        // 3. Enqueue to the work queue
        for (VolumeVO volume : allVolumes) {
            VMInstanceVO vmInstance = vmDao.findById(volume.getInstanceId());
            if (vmInstance == null) {
                continue;
            }
            // enqueue sp work
            if (vmInstance.getState().equals(State.Running) || vmInstance.getState().equals(State.Starting) || vmInstance.getState().equals(State.Stopping)) {
                try {
                    StoragePoolWorkVO work = new StoragePoolWorkVO(vmInstance.getId(), pool.getId(), false, false, server.getId());
                    _storagePoolWorkDao.persist(work);
                } catch (Exception e) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Work record already exists, re-using by re-setting values");
                    }
                    StoragePoolWorkVO work = _storagePoolWorkDao.findByPoolIdAndVmId(pool.getId(), vmInstance.getId());
                    work.setStartedAfterMaintenance(false);
                    work.setStoppedForMaintenance(false);
                    work.setManagementServerId(server.getId());
                    _storagePoolWorkDao.update(work.getId(), work);
                }
            }
        }
        // 4. Process the queue
        List<StoragePoolWorkVO> pendingWork = _storagePoolWorkDao.listPendingWorkForPrepareForMaintenanceByPoolId(pool.getId());
        for (StoragePoolWorkVO work : pendingWork) {
            // shut down the running vms
            VMInstanceVO vmInstance = vmDao.findById(work.getVmId());
            if (vmInstance == null) {
                continue;
            }
            // proxy
            if (vmInstance.getType().equals(VirtualMachine.Type.ConsoleProxy)) {
                // call the consoleproxymanager
                ConsoleProxyVO consoleProxy = _consoleProxyDao.findById(vmInstance.getId());
                vmMgr.advanceStop(consoleProxy.getUuid(), false);
                // update work status
                work.setStoppedForMaintenance(true);
                _storagePoolWorkDao.update(work.getId(), work);
                if (restart) {
                    vmMgr.advanceStart(consoleProxy.getUuid(), null, null);
                    // update work status
                    work.setStartedAfterMaintenance(true);
                    _storagePoolWorkDao.update(work.getId(), work);
                }
            }
            // if the instance is of type uservm, call the user vm manager
            if (vmInstance.getType() == VirtualMachine.Type.User) {
                UserVmVO userVm = userVmDao.findById(vmInstance.getId());
                vmMgr.advanceStop(userVm.getUuid(), false);
                // update work status
                work.setStoppedForMaintenance(true);
                _storagePoolWorkDao.update(work.getId(), work);
            }
            // secondary storage vm manager
            if (vmInstance.getType().equals(VirtualMachine.Type.SecondaryStorageVm)) {
                SecondaryStorageVmVO secStrgVm = _secStrgDao.findById(vmInstance.getId());
                vmMgr.advanceStop(secStrgVm.getUuid(), false);
                // update work status
                work.setStoppedForMaintenance(true);
                _storagePoolWorkDao.update(work.getId(), work);
                if (restart) {
                    vmMgr.advanceStart(secStrgVm.getUuid(), null, null);
                    // update work status
                    work.setStartedAfterMaintenance(true);
                    _storagePoolWorkDao.update(work.getId(), work);
                }
            }
            // manager
            if (vmInstance.getType().equals(VirtualMachine.Type.DomainRouter)) {
                DomainRouterVO domR = _domrDao.findById(vmInstance.getId());
                vmMgr.advanceStop(domR.getUuid(), false);
                // update work status
                work.setStoppedForMaintenance(true);
                _storagePoolWorkDao.update(work.getId(), work);
                if (restart) {
                    vmMgr.advanceStart(domR.getUuid(), null, null);
                    // update work status
                    work.setStartedAfterMaintenance(true);
                    _storagePoolWorkDao.update(work.getId(), work);
                }
            }
        }
    } catch (Exception e) {
        s_logger.error("Exception in enabling primary storage maintenance:", e);
        pool.setStatus(StoragePoolStatus.ErrorInMaintenance);
        primaryDataStoreDao.update(pool.getId(), pool);
        throw new CloudRuntimeException(e.getMessage());
    }
    return true;
}
Also used : Account(com.cloud.user.Account) SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) UserVmVO(com.cloud.vm.UserVmVO) User(com.cloud.user.User) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO) ModifyStoragePoolCommand(com.cloud.agent.api.ModifyStoragePoolCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Answer(com.cloud.agent.api.Answer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) ConsoleProxyVO(com.cloud.vm.ConsoleProxyVO) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Aggregations

SecondaryStorageVmVO (com.cloud.vm.SecondaryStorageVmVO)27 HostVO (com.cloud.host.HostVO)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 Answer (com.cloud.agent.api.Answer)7 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)6 CheckSshAnswer (com.cloud.agent.api.check.CheckSshAnswer)5 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)5 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)5 SecStorageSetupAnswer (com.cloud.agent.api.SecStorageSetupAnswer)4 ArrayList (java.util.ArrayList)4 ConfigurationException (javax.naming.ConfigurationException)4 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3 UnableDeleteHostException (com.cloud.resource.UnableDeleteHostException)3 SecStorageVmAlertEventArgs (com.cloud.storage.secondary.SecStorageVmAlertEventArgs)3 Account (com.cloud.user.Account)3 Pair (com.cloud.utils.Pair)3 DB (com.cloud.utils.db.DB)3 NicProfile (com.cloud.vm.NicProfile)3 URISyntaxException (java.net.URISyntaxException)3