diff --git a/agent/src/com/cloud/agent/Agent.java b/agent/src/com/cloud/agent/Agent.java index 25b1df31d69..ffc85dde3f6 100755 --- a/agent/src/com/cloud/agent/Agent.java +++ b/agent/src/com/cloud/agent/Agent.java @@ -111,6 +111,7 @@ public class Agent implements HandlerFactory, IAgentControl { AtomicInteger _inProgress = new AtomicInteger(); StartupTask _startup = null; + boolean _reconnectAllowed = true; // for simulator use only public Agent(IAgentShell shell) { @@ -347,6 +348,9 @@ public class Agent implements HandlerFactory, IAgentControl { } protected void reconnect(final Link link) { + if (!_reconnectAllowed) { + return; + } synchronized(this) { if (_startup != null) { _startup.cancel(); @@ -456,6 +460,12 @@ public class Agent implements HandlerFactory, IAgentControl { } 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 AgentControlCommand) { answer = null; synchronized(_controlListeners) { diff --git a/api/src/com/cloud/agent/api/ShutdownCommand.java b/api/src/com/cloud/agent/api/ShutdownCommand.java index 42144812b33..a161593e9c4 100644 --- a/api/src/com/cloud/agent/api/ShutdownCommand.java +++ b/api/src/com/cloud/agent/api/ShutdownCommand.java @@ -25,14 +25,17 @@ public class ShutdownCommand extends Command { public static final String Requested = "sig.kill"; public static final String Update = "update"; public static final String Unknown = "unknown"; + public static final String DeleteHost = "deleteHost"; private String reason; private String detail; protected ShutdownCommand() { + super(); } public ShutdownCommand(String reason, String detail) { + super(); this.reason = reason; this.detail = detail; } @@ -50,6 +53,6 @@ public class ShutdownCommand extends Command { @Override public boolean executeInSequence() { - return false; + return true; } } diff --git a/server/src/com/cloud/agent/manager/AgentAttache.java b/server/src/com/cloud/agent/manager/AgentAttache.java index cb01c678010..9703ac1666e 100644 --- a/server/src/com/cloud/agent/manager/AgentAttache.java +++ b/server/src/com/cloud/agent/manager/AgentAttache.java @@ -40,6 +40,7 @@ import com.cloud.agent.api.MaintainCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.ReadyCommand; +import com.cloud.agent.api.ShutdownCommand; import com.cloud.agent.api.StartCommand; import com.cloud.agent.api.StopCommand; import com.cloud.agent.api.storage.CreateCommand; @@ -98,7 +99,7 @@ public abstract class AgentAttache { protected boolean _maintenance; public final static String[] s_commandsAllowedInMaintenanceMode = - new String[] { MaintainCommand.class.toString(), MigrateCommand.class.toString(), StopCommand.class.toString(), CheckVirtualMachineCommand.class.toString(), PingTestCommand.class.toString(), CheckHealthCommand.class.toString(), ReadyCommand.class.toString() }; + new String[] { MaintainCommand.class.toString(), MigrateCommand.class.toString(), StopCommand.class.toString(), CheckVirtualMachineCommand.class.toString(), PingTestCommand.class.toString(), CheckHealthCommand.class.toString(), ReadyCommand.class.toString(), ShutdownCommand.class.toString() }; protected final static String[] s_commandsNotAllowedInConnectingMode = new String[] { StartCommand.class.toString(), CreateCommand.class.toString() }; static { diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 56eb4d4ffd8..1302daeb338 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -972,47 +972,57 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, + host.getGuid()); } - if (host.getType() == Type.Routing - && host.getHypervisorType() == HypervisorType.XenServer) { - if (host.getClusterId() != null) { - List hosts = _hostDao.listBy(Type.Routing, - host.getClusterId(), host.getPodId(), - host.getDataCenterId()); - hosts.add(host); - boolean success = true; - for (HostVO thost : hosts) { - long thostId = thost.getId(); - PoolEjectCommand eject = new PoolEjectCommand( - host.getGuid()); - Answer answer = easySend(thostId, eject); - if (answer != null && answer.getResult()) { - s_logger.debug("Eject Host: " + hostId + " from " - + thostId + " Succeed"); - success = true; - break; - } else { - success = false; - s_logger.debug("Eject Host: " - + hostId - + " from " - + thostId - + " failed due to " - + (answer != null ? answer.getDetails() - : "no answer")); - } - } - if (!success) { - String msg = "Unable to eject host " - + host.getGuid() - + " due to there is no host up in this cluster, please execute xe pool-eject host-uuid=" - + host.getGuid() + "in this host " - + host.getPrivateIpAddress(); - s_logger.info(msg); - _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST, - host.getDataCenterId(), host.getPodId(), - "Unable to eject host " + host.getGuid(), msg); - } - } + if (host.getType() == Type.Routing) { + if (host.getHypervisorType() == HypervisorType.XenServer) { + if (host.getClusterId() != null) { + List hosts = _hostDao.listBy(Type.Routing, + host.getClusterId(), host.getPodId(), + host.getDataCenterId()); + hosts.add(host); + boolean success = true; + for (HostVO thost : hosts) { + long thostId = thost.getId(); + PoolEjectCommand eject = new PoolEjectCommand( + host.getGuid()); + Answer answer = easySend(thostId, eject); + if (answer != null && answer.getResult()) { + s_logger.debug("Eject Host: " + hostId + " from " + + thostId + " Succeed"); + success = true; + break; + } else { + success = false; + s_logger.debug("Eject Host: " + + hostId + + " from " + + thostId + + " failed due to " + + (answer != null ? answer.getDetails() + : "no answer")); + } + } + if (!success) { + String msg = "Unable to eject host " + + host.getGuid() + + " due to there is no host up in this cluster, please execute xe pool-eject host-uuid=" + + host.getGuid() + "in this host " + + host.getPrivateIpAddress(); + s_logger.info(msg); + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST, + host.getDataCenterId(), host.getPodId(), + "Unable to eject host " + host.getGuid(), msg); + } + } + } else if (host.getHypervisorType() == HypervisorType.KVM) { + try { + ShutdownCommand cmd = new ShutdownCommand(ShutdownCommand.DeleteHost, null); + send(host.getId(), cmd); + } catch (AgentUnavailableException e) { + s_logger.debug("Sending ShutdownCommand failed: " + e.toString()); + } catch (OperationTimedoutException e) { + s_logger.debug("Sending ShutdownCommand failed: " + e.toString()); + } + } } txn.start();