diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index e08836bb5d0..db718ed34c8 100755 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -1240,37 +1240,19 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe protected MaintainAnswer execute(MaintainCommand cmd) { Connection conn = getConnection(); try { - Pool pool = Pool.getByUuid(conn, _host.pool); - Pool.Record poolr = pool.getRecord(conn); - - Host.Record hostr = poolr.master.getRecord(conn); - if (!_host.uuid.equals(hostr.uuid)) { - s_logger.debug("Not the master node so just return ok: " + _host.ip); - return new MaintainAnswer(cmd); - } - Map hostMap = Host.getAllRecords(conn); - if (hostMap.size() == 1) { - s_logger.debug("There is the last host in pool " + poolr.uuid ); - return new MaintainAnswer(cmd); - } - Host newMaster = null; - Host.Record newMasterRecord = null; - for (Map.Entry entry : hostMap.entrySet()) { - if (!_host.uuid.equals(entry.getValue().uuid)) { - newMaster = entry.getKey(); - newMasterRecord = entry.getValue(); - s_logger.debug("New master for the XenPool is " + newMasterRecord.uuid + " : " + newMasterRecord.address); - try { - _connPool.switchMaster(_host.ip, _host.pool, conn, newMaster, _username, _password, _wait); - return new MaintainAnswer(cmd, "New Master is " + newMasterRecord.address); - } catch (XenAPIException e) { - s_logger.warn("Unable to switch the new master to " + newMasterRecord.uuid + ": " + newMasterRecord.address + " Trying again..."); - } catch (XmlRpcException e) { - s_logger.warn("Unable to switch the new master to " + newMasterRecord.uuid + ": " + newMasterRecord.address + " Trying again..."); - } + + Host host = Host.getByUuid(conn, _host.uuid); + // remove all tags cloud stack + Host.Record hr = host.getRecord(conn); + Iterator it = hr.tags.iterator(); + while (it.hasNext()) { + String tag = it.next(); + if (tag.contains("cloud")) { + it.remove(); } } - return new MaintainAnswer(cmd, false, "Unable to find an appropriate host to set as the new master"); + host.setTags(conn, hr.tags); + return new MaintainAnswer(cmd); } catch (XenAPIException e) { s_logger.warn("Unable to put server in maintainence mode", e); return new MaintainAnswer(cmd, false, e.getMessage()); @@ -6396,37 +6378,58 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe Connection conn = getConnection(); String hostuuid = cmd.getHostuuid(); try { - Map hostrs = Host.getAllRecords(conn); - boolean found = false; - for( Host.Record hr : hostrs.values() ) { - if( hr.uuid.equals(hostuuid)) { - found = true; - } - } - if( ! found) { + Host host = Host.getByUuid(conn, hostuuid); + if( isRefNull(host) ) { s_logger.debug("host " + hostuuid + " has already been ejected from pool " + _host.pool); return new Answer(cmd); } - - Pool pool = Pool.getAll(conn).iterator().next(); - Pool.Record poolr = pool.getRecord(conn); - - Host.Record masterRec = poolr.master.getRecord(conn); - if (hostuuid.equals(masterRec.uuid)) { - s_logger.debug("This is last host to eject, so don't need to eject: " + hostuuid); - return new Answer(cmd); - } - - Host host = Host.getByUuid(conn, hostuuid); // remove all tags cloud stack add before eject Host.Record hr = host.getRecord(conn); Iterator it = hr.tags.iterator(); while (it.hasNext()) { String tag = it.next(); - if (tag.startsWith("vmops-version-")) { + if (tag.contains("cloud")) { it.remove(); } } + host.setTags(conn, hr.tags); + Pool pool = Pool.getByUuid(conn, _host.pool); + Pool.Record poolr = pool.getRecord(conn); + + Host.Record hostr = poolr.master.getRecord(conn); + if (_host.uuid.equals(hostr.uuid)) { + boolean mastermigrated = false; + Map hostMap = Host.getAllRecords(conn); + if (hostMap.size() != 1) { + Host newMaster = null; + Host.Record newMasterRecord = null; + for (Map.Entry entry : hostMap.entrySet()) { + if (_host.uuid.equals(entry.getValue().uuid)) { + continue; + } + newMaster = entry.getKey(); + newMasterRecord = entry.getValue(); + s_logger.debug("New master for the XenPool is " + newMasterRecord.uuid + " : " + newMasterRecord.address); + try { + _connPool.switchMaster(_host.ip, _host.pool, conn, newMaster, _username, _password, _wait); + mastermigrated = true; + break; + } catch (Exception e) { + s_logger.warn("Unable to switch the new master to " + newMasterRecord.uuid + ": " + newMasterRecord.address + " due to " + e.toString()); + } + } + } else { + s_logger.debug("This is last host to eject, so don't need to eject: " + hostuuid); + return new Answer(cmd); + } + if ( !mastermigrated ) { + String msg = "this host is master, and cannot designate a new master"; + s_logger.debug(msg); + return new Answer(cmd, false, msg); + + } + } + // eject from pool try { Pool.eject(conn, host); @@ -6440,12 +6443,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe host.destroy(conn); } return new Answer(cmd); - } catch (XenAPIException e) { - String msg = "XenAPIException Unable to destroy host " + _host.uuid + " in xenserver database due to " + e.toString(); - s_logger.warn(msg, e); - return new Answer(cmd, false, msg); } catch (Exception e) { - String msg = "Exception Unable to destroy host " + _host.uuid + " in xenserver database due to " + e.getMessage(); + String msg = "Exception Unable to destroy host " + _host.uuid + " in xenserver database due to " + e.toString(); s_logger.warn(msg, e); return new Answer(cmd, false, msg); } diff --git a/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java b/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java index 8ffe9970ccb..0f408c5eef0 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java +++ b/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java @@ -20,14 +20,9 @@ package com.cloud.hypervisor.xen.resource; import java.io.File; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; - import javax.ejb.Local; -import javax.naming.ConfigurationException; - import org.apache.log4j.Logger; import org.apache.xmlrpc.XmlRpcException; @@ -39,21 +34,14 @@ import com.cloud.agent.api.FenceAnswer; import com.cloud.agent.api.FenceCommand; import com.cloud.agent.api.NetworkUsageAnswer; import com.cloud.agent.api.NetworkUsageCommand; -import com.cloud.agent.api.PoolEjectCommand; import com.cloud.agent.api.StartupCommand; import com.cloud.resource.ServerResource; -import com.cloud.utils.NumbersUtil; -import com.cloud.utils.Pair; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; -import com.xensource.xenapi.Bond; import com.xensource.xenapi.Connection; import com.xensource.xenapi.Host; import com.xensource.xenapi.Network; -import com.xensource.xenapi.PBD; import com.xensource.xenapi.PIF; -import com.xensource.xenapi.SR; -import com.xensource.xenapi.Types; import com.xensource.xenapi.Types.IpConfigurationMode; import com.xensource.xenapi.Types.XenAPIException; import com.xensource.xenapi.VLAN; @@ -67,8 +55,6 @@ public class XenServer56Resource extends CitrixResourceBase { public Answer executeRequest(Command cmd) { if (cmd instanceof FenceCommand) { return execute((FenceCommand) cmd); - } else if (cmd instanceof PoolEjectCommand) { - return execute((PoolEjectCommand) cmd); } else if (cmd instanceof NetworkUsageCommand) { return execute((NetworkUsageCommand) cmd); } else { @@ -180,36 +166,6 @@ public class XenServer56Resource extends CitrixResourceBase { return answer; } - @Override - protected Answer execute(PoolEjectCommand cmd) { - Connection conn = getConnection(); - String hostuuid = cmd.getHostuuid(); - try { - Host host = Host.getByUuid(conn, hostuuid); - // remove all tags cloud stack add before eject - Host.Record hr = host.getRecord(conn); - Iterator it = hr.tags.iterator(); - while (it.hasNext()) { - String tag = it.next(); - if (tag.contains("cloud-heartbeat-")) { - it.remove(); - } - } - return super.execute(cmd); - - } catch (XenAPIException e) { - String msg = "Unable to eject host " + _host.uuid + " due to " + e.toString(); - s_logger.warn(msg, e); - return new Answer(cmd, false, msg); - } catch (Exception e) { - s_logger.warn("Unable to eject host " + _host.uuid, e); - String msg = "Unable to eject host " + _host.uuid + " due to " + e.getMessage(); - s_logger.warn(msg, e); - return new Answer(cmd, false, msg); - } - - } - protected FenceAnswer execute(FenceCommand cmd) { Connection conn = getConnection(); try { diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 4e85be16890..00ad5556d27 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -1639,6 +1639,13 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { public boolean maintain(final long hostId) throws AgentUnavailableException { HostVO host = _hostDao.findById(hostId); Status state; + + state = host.getStatus(); + if (state != Status.Up) { + String msg = "Unable to put host " + hostId + " in matinenance mode because it is not in UP status"; + s_logger.debug(msg); + throw new AgentUnavailableException(msg, hostId); + } MaintainAnswer answer = (MaintainAnswer) easySend(hostId, new MaintainCommand()); if (answer == null || !answer.getResult()) { @@ -1646,20 +1653,12 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { return false; } - // Let's put this guy in maintenance state - do { - host = _hostDao.findById(hostId); - if (host == null) { - s_logger.debug("Unable to find host " + hostId); - return false; - } - state = host.getStatus(); - if (state == Status.Disconnected || state == Status.Updating) { - s_logger.debug("Unable to put host " + hostId + " in matinenance mode because it is currently in " + state); - throw new AgentUnavailableException("Agent is in " + state + " state. Please wait for it to become Alert state try again.", hostId); - } - } while (!_hostDao.updateStatus(host, Event.MaintenanceRequested, _nodeId)); - + // Let's put this guy in maintenance state + if (!_hostDao.updateStatus(host, Event.MaintenanceRequested, _nodeId)) { + String msg = "Unable to update host " + hostId + " to preparematinenance mode"; + s_logger.debug(msg); + return false; + } AgentAttache attache = findAttache(hostId); if (attache != null) { attache.setMaintenanceMode(true);