Search in sources :

Example 6 with MaintainAnswer

use of com.cloud.agent.api.MaintainAnswer in project cloudstack by apache.

the class ResourceManagerImpl method doMaintain.

private boolean doMaintain(final long hostId) {
    final HostVO host = _hostDao.findById(hostId);
    s_logger.info("Maintenance: attempting maintenance of host " + host.getUuid());
    ResourceState hostState = host.getResourceState();
    if (!ResourceState.canAttemptMaintenance(hostState)) {
        throw new CloudRuntimeException("Cannot perform maintain when resource state is " + hostState + ", hostId = " + hostId);
    }
    final MaintainAnswer answer = (MaintainAnswer) _agentMgr.easySend(hostId, new MaintainCommand());
    if (answer == null || !answer.getResult()) {
        s_logger.warn("Unable to send MaintainCommand to host: " + hostId);
        return false;
    }
    try {
        resourceStateTransitTo(host, ResourceState.Event.AdminAskMaintenance, _nodeId);
    } catch (final NoTransitionException e) {
        final String err = String.format("Cannot transit resource state of %s to %s", host, ResourceState.Maintenance);
        s_logger.debug(err, e);
        throw new CloudRuntimeException(err + e.getMessage());
    }
    ActionEventUtils.onStartedActionEvent(CallContext.current().getCallingUserId(), CallContext.current().getCallingAccountId(), EventTypes.EVENT_MAINTENANCE_PREPARE, "starting maintenance for host " + hostId, true, 0);
    _agentMgr.pullAgentToMaintenance(hostId);
    /* TODO: move below to listener */
    if (host.getType() == Host.Type.Routing) {
        final List<VMInstanceVO> vms = _vmDao.listByHostId(hostId);
        if (vms.size() == 0) {
            return true;
        }
        List<HostVO> hosts = listAllUpAndEnabledHosts(Host.Type.Routing, host.getClusterId(), host.getPodId(), host.getDataCenterId());
        if (CollectionUtils.isEmpty(hosts)) {
            s_logger.warn("Unable to find a host for vm migration in cluster: " + host.getClusterId());
            if (!isClusterWideMigrationPossible(host, vms, hosts)) {
                return false;
            }
        }
        for (final VMInstanceVO vm : vms) {
            if (hosts == null || hosts.isEmpty() || !answer.getMigrate() || _serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.vgpuType.toString()) != null) {
                handleVmForLastHostOrWithVGpu(host, vm);
            } else if (HypervisorType.LXC.equals(host.getHypervisorType()) && VirtualMachine.Type.User.equals(vm.getType())) {
                // Migration is not supported for LXC Vms. Schedule restart instead.
                _haMgr.scheduleRestart(vm, false);
            } else if (userVmManager.isVMUsingLocalStorage(vm)) {
                if (isMaintenanceLocalStrategyForceStop()) {
                    _haMgr.scheduleStop(vm, hostId, WorkType.ForceStop);
                } else if (isMaintenanceLocalStrategyMigrate()) {
                    migrateAwayVmWithVolumes(host, vm);
                } else if (!isMaintenanceLocalStrategyDefault()) {
                    String logMessage = String.format("Unsupported host.maintenance.local.storage.strategy: %s. Please set a strategy according to the global settings description: " + "'Error', 'Migration', or 'ForceStop'.", HOST_MAINTENANCE_LOCAL_STRATEGY.value().toString());
                    s_logger.error(logMessage);
                    throw new CloudRuntimeException("There are active VMs using the host's local storage pool. Please stop all VMs on this host that use local storage.");
                }
            } else {
                s_logger.info("Maintenance: scheduling migration of VM " + vm.getUuid() + " from host " + host.getUuid());
                _haMgr.scheduleMigration(vm);
            }
        }
    }
    return true;
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VMInstanceVO(com.cloud.vm.VMInstanceVO) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO)

Example 7 with MaintainAnswer

use of com.cloud.agent.api.MaintainAnswer in project cloudstack by apache.

the class CitrixMaintainCommandWrapper method execute.

@Override
public Answer execute(final MaintainCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final XsHost xsHost = citrixResourceBase.getHost();
        final String uuid = xsHost.getUuid();
        final Host host = Host.getByUuid(conn, uuid);
        // remove all tags cloud stack
        final Host.Record hr = host.getRecord(conn);
        // Adding this check because could not get the mock to work. Will push the code and fix it afterwards.
        if (hr == null) {
            s_logger.warn("Host.Record is null.");
            return new MaintainAnswer(command, false, "Host.Record is null");
        }
        final Iterator<String> it = hr.tags.iterator();
        while (it.hasNext()) {
            final String tag = it.next();
            if (tag.contains("cloud")) {
                it.remove();
            }
        }
        host.setTags(conn, hr.tags);
        return new MaintainAnswer(command);
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    }
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

MaintainAnswer (com.cloud.agent.api.MaintainAnswer)7 MaintainCommand (com.cloud.agent.api.MaintainCommand)5 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)3 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)3 Answer (com.cloud.agent.api.Answer)3 Command (com.cloud.agent.api.Command)3 CronCommand (com.cloud.agent.api.CronCommand)3 PingCommand (com.cloud.agent.api.PingCommand)3 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)3 StartupAnswer (com.cloud.agent.api.StartupAnswer)3 StartupCommand (com.cloud.agent.api.StartupCommand)3 Response (com.cloud.agent.transport.Response)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ReadyCommand (com.cloud.agent.api.ReadyCommand)2 HostVO (com.cloud.host.HostVO)2 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)2 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2