Search in sources :

Example 1 with MaintainAnswer

use of com.cloud.agent.api.MaintainAnswer in project CloudStack-archive by CloudStack-extras.

the class Agent method processRequest.

protected void processRequest(final Request request, final Link link) {
    boolean requestLogged = false;
    Response response = null;
    try {
        final Command[] cmds = request.getCommands();
        final Answer[] answers = new Answer[cmds.length];
        for (int i = 0; i < cmds.length; i++) {
            final Command cmd = cmds[i];
            Answer answer;
            try {
                if (s_logger.isDebugEnabled()) {
                    // this is a hack to make sure we do NOT log the ssh keys
                    if ((cmd instanceof ModifySshKeysCommand)) {
                        s_logger.debug("Received the request for command: ModifySshKeysCommand");
                    } else {
                        if (// ensures request is logged only once per method call
                        !requestLogged) {
                            s_logger.debug("Request:" + request.toString());
                            requestLogged = true;
                        }
                    }
                    s_logger.debug("Processing command: " + cmd.toString());
                }
                if (cmd instanceof CronCommand) {
                    final CronCommand watch = (CronCommand) cmd;
                    scheduleWatch(link, request, watch.getInterval() * 1000, watch.getInterval() * 1000);
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof UpgradeCommand) {
                    final UpgradeCommand upgrade = (UpgradeCommand) cmd;
                    answer = upgradeAgent(upgrade.getUpgradeUrl(), upgrade);
                } else if (cmd instanceof ShutdownCommand) {
                    ShutdownCommand shutdown = (ShutdownCommand) cmd;
                    s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                    cancelTasks();
                    _reconnectAllowed = false;
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof MaintainCommand) {
                    s_logger.debug("Received maintainCommand");
                    cancelTasks();
                    _reconnectAllowed = false;
                    answer = new MaintainAnswer((MaintainCommand) cmd);
                } else if (cmd instanceof AgentControlCommand) {
                    answer = null;
                    synchronized (_controlListeners) {
                        for (IAgentControlListener listener : _controlListeners) {
                            answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                            if (answer != null) {
                                break;
                            }
                        }
                    }
                    if (answer == null) {
                        s_logger.warn("No handler found to process cmd: " + cmd.toString());
                        answer = new AgentControlAnswer(cmd);
                    }
                } else {
                    _inProgress.incrementAndGet();
                    try {
                        answer = _resource.executeRequest(cmd);
                    } finally {
                        _inProgress.decrementAndGet();
                    }
                    if (answer == null) {
                        s_logger.debug("Response: unsupported command" + cmd.toString());
                        answer = Answer.createUnsupportedCommandAnswer(cmd);
                    }
                }
            } catch (final Throwable th) {
                s_logger.warn("Caught: ", th);
                final StringWriter writer = new StringWriter();
                th.printStackTrace(new PrintWriter(writer));
                answer = new Answer(cmd, false, writer.toString());
            }
            answers[i] = answer;
            if (!answer.getResult() && request.stopOnError()) {
                for (i++; i < cmds.length; i++) {
                    answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
                }
                break;
            }
        }
        response = new Response(request, answers);
    } finally {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(response != null ? response.toString() : "response is null");
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) UpgradeCommand(com.cloud.agent.api.UpgradeCommand) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CronCommand(com.cloud.agent.api.CronCommand) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) UpgradeAnswer(com.cloud.agent.api.UpgradeAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) StringWriter(java.io.StringWriter) MaintainCommand(com.cloud.agent.api.MaintainCommand) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) PingCommand(com.cloud.agent.api.PingCommand) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) UpgradeCommand(com.cloud.agent.api.UpgradeCommand) CronCommand(com.cloud.agent.api.CronCommand) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) PrintWriter(java.io.PrintWriter)

Example 2 with MaintainAnswer

use of com.cloud.agent.api.MaintainAnswer in project cosmic by MissionCriticalCloud.

the class Agent method processRequest.

protected void processRequest(final Request request, final Link link) {
    boolean requestLogged = false;
    Response response = null;
    try {
        final Command[] cmds = request.getCommands();
        final Answer[] answers = new Answer[cmds.length];
        for (int i = 0; i < cmds.length; i++) {
            final Command cmd = cmds[i];
            Answer answer;
            if (cmd.getContextParam("logid") != null) {
                MDC.put("logcontextid", cmd.getContextParam("logid"));
            }
            if (// ensures request is logged only once per method call
            !requestLogged) {
                final String requestMsg = request.toString();
                if (requestMsg != null) {
                    logger.debug("Request:" + requestMsg);
                }
                requestLogged = true;
            }
            logger.debug("Processing command: " + cmd.toString());
            if (cmd instanceof CronCommand) {
                final CronCommand watch = (CronCommand) cmd;
                scheduleWatch(link, request, (long) watch.getInterval() * 1000, watch.getInterval() * 1000);
                answer = new Answer(cmd, true, null);
            } else if (cmd instanceof ShutdownCommand) {
                final ShutdownCommand shutdown = (ShutdownCommand) cmd;
                logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                cancelTasks();
                _reconnectAllowed = false;
                answer = new Answer(cmd, true, null);
            } else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
                logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
                System.exit(1);
                return;
            } else if (cmd instanceof MaintainCommand) {
                logger.debug("Received maintainCommand");
                cancelTasks();
                _reconnectAllowed = false;
                answer = new MaintainAnswer((MaintainCommand) cmd);
            } else if (cmd instanceof AgentControlCommand) {
                answer = null;
                synchronized (_controlListeners) {
                    for (final IAgentControlListener listener : _controlListeners) {
                        answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                        if (answer != null) {
                            break;
                        }
                    }
                }
                if (answer == null) {
                    logger.warn("No handler found to process cmd: " + cmd.toString());
                    answer = new AgentControlAnswer(cmd);
                }
            } else {
                if (cmd instanceof ReadyCommand) {
                    processReadyCommand(cmd);
                }
                _inProgress.incrementAndGet();
                try {
                    answer = resource.executeRequest(cmd);
                } finally {
                    _inProgress.decrementAndGet();
                }
                if (answer == null) {
                    logger.debug("Response: unsupported command" + cmd.toString());
                    answer = Answer.createUnsupportedCommandAnswer(cmd);
                }
            }
            answers[i] = answer;
            if (!answer.getResult() && request.stopOnError()) {
                for (i++; i < cmds.length; i++) {
                    answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
                }
                break;
            }
        }
        response = new Response(request, answers);
    } finally {
        if (logger.isDebugEnabled()) {
            final String responseMsg = response.toString();
            if (responseMsg != null) {
                logger.debug(response.toString());
            }
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CronCommand(com.cloud.agent.api.CronCommand) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) StartupAnswer(com.cloud.agent.api.StartupAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) MaintainCommand(com.cloud.agent.api.MaintainCommand) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) PingCommand(com.cloud.agent.api.PingCommand) ReadyCommand(com.cloud.agent.api.ReadyCommand) CronCommand(com.cloud.agent.api.CronCommand) ReadyCommand(com.cloud.agent.api.ReadyCommand) IAgentControlListener(com.cloud.agent.IAgentControlListener)

Example 3 with MaintainAnswer

use of com.cloud.agent.api.MaintainAnswer in project cosmic by MissionCriticalCloud.

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)

Example 4 with MaintainAnswer

use of com.cloud.agent.api.MaintainAnswer in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method doMaintain.

private boolean doMaintain(final long hostId) {
    final HostVO host = _hostDao.findById(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.AdminAskMaintenace, _nodeId);
    } catch (final NoTransitionException e) {
        final String err = "Cannot transmit resource state of host " + host.getId() + " to " + 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;
        }
        final List<HostVO> hosts = listAllUpAndEnabledHosts(Host.Type.Routing, host.getClusterId(), host.getPodId(), host.getDataCenterId());
        for (final VMInstanceVO vm : vms) {
            if (hosts == null || hosts.isEmpty() || !answer.getMigrate() || _serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.vgpuType.toString()) != null) {
                // Migration is not supported for VGPU Vms so stop them.
                // for the last host in this cluster, stop all the VMs
                _haMgr.scheduleStop(vm, hostId, WorkType.ForceStop);
            } else {
                _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 5 with MaintainAnswer

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

the class Agent method processRequest.

protected void processRequest(final Request request, final Link link) {
    boolean requestLogged = false;
    Response response = null;
    try {
        final Command[] cmds = request.getCommands();
        final Answer[] answers = new Answer[cmds.length];
        for (int i = 0; i < cmds.length; i++) {
            final Command cmd = cmds[i];
            Answer answer;
            try {
                if (cmd.getContextParam("logid") != null) {
                    MDC.put("logcontextid", cmd.getContextParam("logid"));
                }
                if (s_logger.isDebugEnabled()) {
                    if (// ensures request is logged only once per method call
                    !requestLogged) {
                        final String requestMsg = request.toString();
                        if (requestMsg != null) {
                            s_logger.debug("Request:" + requestMsg);
                        }
                        requestLogged = true;
                    }
                    s_logger.debug("Processing command: " + cmd.toString());
                }
                if (cmd instanceof CronCommand) {
                    final CronCommand watch = (CronCommand) cmd;
                    scheduleWatch(link, request, (long) watch.getInterval() * 1000, watch.getInterval() * 1000);
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof ShutdownCommand) {
                    final ShutdownCommand shutdown = (ShutdownCommand) cmd;
                    s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                    cancelTasks();
                    if (shutdown.isRemoveHost()) {
                        cleanupAgentZoneProperties();
                    }
                    _reconnectAllowed = false;
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
                    s_logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
                    System.exit(1);
                    return;
                } else if (cmd instanceof MaintainCommand) {
                    s_logger.debug("Received maintainCommand, do not cancel current tasks");
                    answer = new MaintainAnswer((MaintainCommand) cmd);
                } else if (cmd instanceof AgentControlCommand) {
                    answer = null;
                    synchronized (_controlListeners) {
                        for (final IAgentControlListener listener : _controlListeners) {
                            answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                            if (answer != null) {
                                break;
                            }
                        }
                    }
                    if (answer == null) {
                        s_logger.warn("No handler found to process cmd: " + cmd.toString());
                        answer = new AgentControlAnswer(cmd);
                    }
                } else if (cmd instanceof SetupKeyStoreCommand && ((SetupKeyStoreCommand) cmd).isHandleByAgent()) {
                    answer = setupAgentKeystore((SetupKeyStoreCommand) cmd);
                } else if (cmd instanceof SetupCertificateCommand && ((SetupCertificateCommand) cmd).isHandleByAgent()) {
                    answer = setupAgentCertificate((SetupCertificateCommand) cmd);
                    if (Host.Type.Routing.equals(_resource.getType())) {
                        scheduleServicesRestartTask();
                    }
                } else if (cmd instanceof SetupMSListCommand) {
                    answer = setupManagementServerList((SetupMSListCommand) cmd);
                } else {
                    if (cmd instanceof ReadyCommand) {
                        processReadyCommand(cmd);
                    }
                    _inProgress.incrementAndGet();
                    try {
                        answer = _resource.executeRequest(cmd);
                    } finally {
                        _inProgress.decrementAndGet();
                    }
                    if (answer == null) {
                        s_logger.debug("Response: unsupported command" + cmd.toString());
                        answer = Answer.createUnsupportedCommandAnswer(cmd);
                    }
                }
            } catch (final Throwable th) {
                s_logger.warn("Caught: ", th);
                final StringWriter writer = new StringWriter();
                th.printStackTrace(new PrintWriter(writer));
                answer = new Answer(cmd, false, writer.toString());
            }
            answers[i] = answer;
            if (!answer.getResult() && request.stopOnError()) {
                for (i++; i < cmds.length; i++) {
                    answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
                }
                break;
            }
        }
        response = new Response(request, answers);
    } finally {
        if (s_logger.isDebugEnabled()) {
            final String responseMsg = response.toString();
            if (responseMsg != null) {
                s_logger.debug(response.toString());
            }
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CronCommand(com.cloud.agent.api.CronCommand) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeystoreAnswer(org.apache.cloudstack.ca.SetupKeystoreAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) SetupMSListAnswer(org.apache.cloudstack.agent.lb.SetupMSListAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) SetupCertificateAnswer(org.apache.cloudstack.ca.SetupCertificateAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) StringWriter(java.io.StringWriter) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PingCommand(com.cloud.agent.api.PingCommand) PostCertificateRenewalCommand(org.apache.cloudstack.ca.PostCertificateRenewalCommand) MaintainCommand(com.cloud.agent.api.MaintainCommand) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) ReadyCommand(com.cloud.agent.api.ReadyCommand) CronCommand(com.cloud.agent.api.CronCommand) ReadyCommand(com.cloud.agent.api.ReadyCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PrintWriter(java.io.PrintWriter)

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