diff --git a/agent/src/com/cloud/agent/Agent.java b/agent/src/com/cloud/agent/Agent.java index 5dac32c2a4f..76fafe48ff6 100755 --- a/agent/src/com/cloud/agent/Agent.java +++ b/agent/src/com/cloud/agent/Agent.java @@ -117,6 +117,8 @@ public class Agent implements HandlerFactory, IAgentControl { AtomicInteger _inProgress = new AtomicInteger(); StartupTask _startup = null; + long _startupWaitDefault = 180000; + long _startupWait = _startupWaitDefault; boolean _reconnectAllowed = true; //For time sentitive task, e.g. PingTask private ThreadPoolExecutor _ugentTaskPool; @@ -282,7 +284,7 @@ public class Agent implements HandlerFactory, IAgentControl { s_logger.debug("Adding a watch list"); } final WatchTask task = new WatchTask(link, request, this); - _timer.schedule(task, delay, period); + _timer.schedule(task, 0, period); _watchList.add(task); } } @@ -315,7 +317,7 @@ public class Agent implements HandlerFactory, IAgentControl { } synchronized (this) { _startup = new StartupTask(link); - _timer.schedule(_startup, 180000); + _timer.schedule(_startup, _startupWait); } try { link.send(request.toBytes()); @@ -793,6 +795,7 @@ public class Agent implements HandlerFactory, IAgentControl { // TimerTask.cancel may fail depends on the calling context if (!cancelled) { cancelled = true; + _startupWait = _startupWaitDefault; s_logger.debug("Startup task cancelled"); return super.cancel(); } @@ -807,6 +810,7 @@ public class Agent implements HandlerFactory, IAgentControl { } cancelled = true; _startup = null; + _startupWait = _startupWaitDefault *2; reconnect(_link); } } diff --git a/agent/src/com/cloud/agent/AgentShell.java b/agent/src/com/cloud/agent/AgentShell.java index d877fe98223..c33af22caa3 100644 --- a/agent/src/com/cloud/agent/AgentShell.java +++ b/agent/src/com/cloud/agent/AgentShell.java @@ -35,6 +35,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.UUID; import javax.naming.ConfigurationException; @@ -351,7 +352,7 @@ public class AgentShell implements IAgentShell { if (!developer) { throw new ConfigurationException("Unable to find the guid"); } - _guid = MacAddress.getMacAddress().toString(":"); + _guid = UUID.randomUUID().toString(); } return true; @@ -529,8 +530,12 @@ public class AgentShell implements IAgentShell { init(args); String instance = getProperty(null, "instance"); - if (instance == null) { - instance = ""; + if (instance == null) { + if (Boolean.parseBoolean(getProperty(null, "developer"))) { + instance = UUID.randomUUID().toString(); + } else { + instance = ""; + } } else { instance += "."; } diff --git a/agent/src/com/cloud/agent/resource/DummyResource.java b/agent/src/com/cloud/agent/resource/DummyResource.java index 4cbbf0d3963..72ce1aef0bf 100755 --- a/agent/src/com/cloud/agent/resource/DummyResource.java +++ b/agent/src/com/cloud/agent/resource/DummyResource.java @@ -17,7 +17,11 @@ */ package com.cloud.agent.resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.UUID; import javax.ejb.Local; @@ -26,9 +30,17 @@ import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; import com.cloud.agent.api.PingCommand; import com.cloud.agent.api.StartupCommand; +import com.cloud.agent.api.StartupRoutingCommand; +import com.cloud.agent.api.StartupStorageCommand; +import com.cloud.agent.api.StoragePoolInfo; +import com.cloud.agent.api.StartupRoutingCommand.VmState; import com.cloud.host.Host; import com.cloud.host.Host.Type; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.network.Networks.RouterPrivateIpStrategy; import com.cloud.resource.ServerResource; +import com.cloud.storage.Storage; +import com.cloud.storage.Storage.StoragePoolType; @Local(value={ServerResource.class}) public class DummyResource implements ServerResource { @@ -36,6 +48,7 @@ public class DummyResource implements ServerResource { Host.Type _type; boolean _negative; IAgentControl _agentControl; + private Map _params; @Override public void disconnected() { @@ -58,10 +71,98 @@ public class DummyResource implements ServerResource { public Type getType() { return _type; } + + protected String getConfiguredProperty(String key, String defaultValue) { + String val = (String)_params.get(key); + return val==null?defaultValue:val; + } + + protected Long getConfiguredProperty(String key, Long defaultValue) { + String val = (String)_params.get(key); + + if (val != null) { + Long result = Long.parseLong(val); + return result; + } + return defaultValue; + } + protected List getHostInfo() { + final ArrayList info = new ArrayList(); + long speed = getConfiguredProperty("cpuspeed", 4000L) ; + long cpus = getConfiguredProperty("cpus", 4L); + long ram = getConfiguredProperty("memory", 16000L*1024L*1024L); + long dom0ram = Math.min(ram/10, 768*1024*1024L); + + + String cap = getConfiguredProperty("capabilities", "hvm"); + info.add((int)cpus); + info.add(speed); + info.add(ram); + info.add(cap); + info.add(dom0ram); + return info; + + } + + protected void fillNetworkInformation(final StartupCommand cmd) { + + cmd.setPrivateIpAddress((String)getConfiguredProperty("private.ip.address", "127.0.0.1")); + cmd.setPrivateMacAddress((String)getConfiguredProperty("private.mac.address", "8A:D2:54:3F:7C:C3")); + cmd.setPrivateNetmask((String)getConfiguredProperty("private.ip.netmask", "255.255.255.0")); + + cmd.setStorageIpAddress((String)getConfiguredProperty("private.ip.address", "127.0.0.1")); + cmd.setStorageMacAddress((String)getConfiguredProperty("private.mac.address", "8A:D2:54:3F:7C:C3")); + cmd.setStorageNetmask((String)getConfiguredProperty("private.ip.netmask", "255.255.255.0")); + cmd.setGatewayIpAddress((String)getConfiguredProperty("gateway.ip.address", "127.0.0.1")); + + } + + private Map getVersionStrings() { + Map result = new HashMap(); + String hostOs = (String) _params.get("Host.OS"); + String hostOsVer = (String) _params.get("Host.OS.Version"); + String hostOsKernVer = (String) _params.get("Host.OS.Kernel.Version"); + result.put("Host.OS", hostOs==null?"Fedora":hostOs); + result.put("Host.OS.Version", hostOsVer==null?"14":hostOsVer); + result.put("Host.OS.Kernel.Version", hostOsKernVer==null?"2.6.35.6-45.fc14.x86_64":hostOsKernVer); + return result; + } + + protected StoragePoolInfo initializeLocalStorage() { + String hostIp = (String)getConfiguredProperty("private.ip.address", "127.0.0.1"); + String localStoragePath = (String)getConfiguredProperty("local.storage.path", "/mnt"); + String lh = hostIp + localStoragePath; + String uuid = UUID.nameUUIDFromBytes(lh.getBytes()).toString(); + + String capacity = (String)getConfiguredProperty("local.storage.capacity", "1000000000"); + String available = (String)getConfiguredProperty("local.storage.avail", "10000000"); + + return new StoragePoolInfo(uuid, hostIp, localStoragePath, + localStoragePath, StoragePoolType.Filesystem, + Long.parseLong(capacity), Long.parseLong(available)); + + } + @Override public StartupCommand[] initialize() { - return new StartupCommand[] {new StartupCommand(Host.Type.Storage)}; + Map changes = null; + + + final List info = getHostInfo(); + + final StartupRoutingCommand cmd = new StartupRoutingCommand((Integer)info.get(0), (Long)info.get(1), (Long)info.get(2), (Long)info.get(4), (String)info.get(3), HypervisorType.KVM, RouterPrivateIpStrategy.HostLocal, changes); + fillNetworkInformation(cmd); + cmd.getHostDetails().putAll(getVersionStrings()); + cmd.setCluster(getConfiguredProperty("cluster", "1")); + StoragePoolInfo pi = initializeLocalStorage(); + StartupStorageCommand sscmd = new StartupStorageCommand(); + sscmd.setPoolInfo(pi); + sscmd.setGuid(pi.getUuid()); + sscmd.setDataCenter((String)_params.get("zone")); + sscmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL); + + return new StartupCommand[]{cmd, sscmd}; } @Override @@ -73,9 +174,13 @@ public class DummyResource implements ServerResource { value = (String)params.get("negative.reply"); _negative = Boolean.parseBoolean(value); - + setParams(params); return true; } + + public void setParams(Map _params) { + this._params = _params; + } @Override public String getName() { diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 8d368ee5fd2..4866eb94568 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -1993,8 +1993,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { } catch (ClosedChannelException e) { s_logger.debug("Failed to send startupanswer: " + e.toString()); return null; - } - + } if (attache == null) { return null; } diff --git a/server/src/com/cloud/network/SshKeysDistriMonitor.java b/server/src/com/cloud/network/SshKeysDistriMonitor.java index ea774f4180e..f7f59ba8d34 100755 --- a/server/src/com/cloud/network/SshKeysDistriMonitor.java +++ b/server/src/com/cloud/network/SshKeysDistriMonitor.java @@ -23,14 +23,18 @@ import java.util.Map; import org.apache.log4j.Logger; +import com.cloud.agent.AgentManager; import com.cloud.agent.Listener; import com.cloud.agent.api.AgentControlAnswer; import com.cloud.agent.api.AgentControlCommand; import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; +import com.cloud.agent.api.ModifySshKeysCommand; import com.cloud.agent.api.StartupCommand; import com.cloud.agent.api.StartupRoutingCommand; +import com.cloud.agent.manager.Commands; import com.cloud.configuration.dao.ConfigurationDao; +import com.cloud.exception.AgentUnavailableException; import com.cloud.exception.ConnectionException; import com.cloud.host.HostVO; import com.cloud.host.Status; @@ -42,11 +46,11 @@ import com.cloud.network.router.VirtualNetworkApplianceManager; public class SshKeysDistriMonitor implements Listener { private static final Logger s_logger = Logger.getLogger(SshKeysDistriMonitor.class); - private final VirtualNetworkApplianceManager _routerMgr; + AgentManager _agentMgr; private final HostDao _hostDao; private ConfigurationDao _configDao; - public SshKeysDistriMonitor(VirtualNetworkApplianceManager mgr, HostDao host, ConfigurationDao config) { - this._routerMgr = mgr; + public SshKeysDistriMonitor(AgentManager mgr, HostDao host, ConfigurationDao config) { + this._agentMgr = mgr; _hostDao = host; _configDao = config; } @@ -81,9 +85,13 @@ public class SshKeysDistriMonitor implements Listener { Map configs = _configDao.getConfiguration("management-server", new HashMap()); String pubKey = configs.get("ssh.publickey"); String prvKey = configs.get("ssh.privatekey"); - if (!_routerMgr.sendSshKeysToHost(host.getId(), pubKey, prvKey)) { + + try { + ModifySshKeysCommand cmds = new ModifySshKeysCommand(pubKey, prvKey); + Commands c = new Commands(cmds); + _agentMgr.send(host.getId(), c, this); + } catch (AgentUnavailableException e) { s_logger.debug("Failed to send keys to agent: " + host.getId()); - throw new ConnectionException(true, "Unable to send keys to the agent"); } } } diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index afe99480041..1177339d9a0 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -627,7 +627,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new ConfigurationException("Unable to get " + UserStatisticsDao.class.getName()); } - _agentMgr.registerForHostEvents(new SshKeysDistriMonitor(this, _hostDao, _configDao), true, false, false); + _agentMgr.registerForHostEvents(new SshKeysDistriMonitor(_agentMgr, _hostDao, _configDao), true, false, false); _itMgr.registerGuru(VirtualMachine.Type.DomainRouter, this); boolean useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key()));