Skip to content

Commit fe667e9

Browse files
Edison SuEdison Su
authored andcommitted
fix Maintenance releated issues with kvm:
1. put host into Maintenance, will send a Maintenance command to host, tell host that do not reconnect to mgt server 2. cancel Maintenance, will ssh into kvm host, and restart cloud-agent, which will reconnect to host
1 parent c6b1961 commit fe667e9

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

agent/src/com/cloud/agent/Agent.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import com.cloud.agent.api.Answer;
4444
import com.cloud.agent.api.Command;
4545
import com.cloud.agent.api.CronCommand;
46+
import com.cloud.agent.api.MaintainAnswer;
47+
import com.cloud.agent.api.MaintainCommand;
4648
import com.cloud.agent.api.ModifySshKeysCommand;
4749
import com.cloud.agent.api.PingCommand;
4850
import com.cloud.agent.api.ShutdownCommand;
@@ -476,6 +478,11 @@ protected void processRequest(final Request request, final Link link) {
476478
cancelTasks();
477479
_reconnectAllowed = false;
478480
answer = new Answer(cmd, true, null);
481+
} else if (cmd instanceof MaintainCommand) {
482+
s_logger.debug("Received maintainCommand" );
483+
cancelTasks();
484+
_reconnectAllowed = false;
485+
answer = new MaintainAnswer((MaintainCommand)cmd);
479486
} else if (cmd instanceof AgentControlCommand) {
480487
answer = null;
481488
synchronized (_controlListeners) {

server/src/com/cloud/hypervisor/kvm/discoverer/KvmServerDiscoverer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId,
242242
cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString());
243243
_clusterDao.update(clusterId, cluster);
244244
}
245+
246+
//save user name and password
247+
_hostDao.loadDetails(connectedHost);
248+
Map<String, String> hostDetails = connectedHost.getDetails();
249+
hostDetails.put("password", password);
250+
hostDetails.put("username", username);
251+
_hostDao.saveDetails(connectedHost);
245252
return resources;
246253
} catch (DiscoveredWithErrorException e){
247254
throw e;

server/src/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package com.cloud.resource;
1919

20+
import java.io.IOException;
2021
import java.net.URI;
2122
import java.net.URISyntaxException;
2223
import java.net.URLDecoder;
@@ -138,6 +139,8 @@
138139
import com.cloud.utils.fsm.NoTransitionException;
139140
import com.cloud.utils.net.Ip;
140141
import com.cloud.utils.net.NetUtils;
142+
import com.cloud.utils.ssh.SSHCmdHelper;
143+
import com.cloud.utils.ssh.sshException;
141144
import com.cloud.vm.VMInstanceVO;
142145
import com.cloud.vm.VirtualMachine.State;
143146
import com.cloud.vm.VirtualMachineManager;
@@ -1040,8 +1043,7 @@ private boolean doMaintain(final long hostId) {
10401043
HostVO host = _hostDao.findById(hostId);
10411044
MaintainAnswer answer = (MaintainAnswer) _agentMgr.easySend(hostId, new MaintainCommand());
10421045
if (answer == null || !answer.getResult()) {
1043-
s_logger.warn("Unable to put host in maintainance mode: " + hostId);
1044-
return false;
1046+
s_logger.warn("Unable to send MaintainCommand to host: " + hostId);
10451047
}
10461048

10471049
try {
@@ -1805,6 +1807,29 @@ private boolean doCancelMaintenance(long hostId) {
18051807
try {
18061808
resourceStateTransitTo(host, ResourceState.Event.AdminCancelMaintenance, _nodeId);
18071809
_agentMgr.pullAgentOutMaintenance(hostId);
1810+
1811+
//for kvm, need to log into kvm host, restart cloud-agent
1812+
if (host.getHypervisorType() == HypervisorType.KVM) {
1813+
_hostDao.loadDetails(host);
1814+
String password = host.getDetail("password");
1815+
String username = host.getDetail("username");
1816+
if (password == null || username == null) {
1817+
s_logger.debug("Can't find password/username");
1818+
return false;
1819+
}
1820+
com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection(host.getPrivateIpAddress(), 22, username, password);
1821+
if (connection == null) {
1822+
s_logger.debug("Failed to connect to host: " + host.getPrivateIpAddress());
1823+
return false;
1824+
}
1825+
1826+
try {
1827+
SSHCmdHelper.sshExecuteCmdOneShot(connection, "service cloud-agent restart");
1828+
} catch (sshException e) {
1829+
return false;
1830+
}
1831+
}
1832+
18081833
return true;
18091834
} catch (NoTransitionException e) {
18101835
s_logger.debug("Cannot transmit host " + host.getId() + "to Enabled state", e);

0 commit comments

Comments
 (0)