diff --git a/agent/bindir/cloud-setup-agent.in b/agent/bindir/cloud-setup-agent.in index 4fabf7a3b22..9c5913b6926 100755 --- a/agent/bindir/cloud-setup-agent.in +++ b/agent/bindir/cloud-setup-agent.in @@ -37,16 +37,14 @@ backupdir = "@SHAREDSTATEDIR@/@AGENTPATH@/etcbackup" #=================== the magic happens here ==================== -stderr("Welcome to the Cloud Agent setup") -stderr("") try: # parse cmd line - opts, args = getopt.getopt(sys.argv[1:], "a", ["host=", "zone=", "pod=", "no-kvm"]) + opts, args = getopt.getopt(sys.argv[1:], "a", ["host=", "zone=", "pod=", "no-kvm", "guid="]) host=None zone=None pod=None - stderr(str(opts)) + guid=None autoMode=False do_check_kvm = True for opt, arg in opts: @@ -59,11 +57,19 @@ try: elif opt == "--pod": if arg != "": pod = arg + elif opt == "--guid": + if arg != "": + guid = arg elif opt == "--no-kvm": do_check_kvm = False elif opt == "-a": autoMode=True + if autoMode: + cloud_utils.setLogFile("/var/log/cloud/setupAgent.log") + + stderr("Welcome to the Cloud Agent setup") + stderr("") # pre-flight checks for things that the administrator must fix try: for f,n in cloud_utils.preflight_checks( @@ -106,7 +112,7 @@ try: stderr(str(e)) bail(cloud_utils.E_SETUPFAILED,"Cloud Agent setup failed") - setup_agent_config(configfile, host, zone, pod) + setup_agent_config(configfile, host, zone, pod, guid) stderr("Enabling and starting the Cloud Agent") stop_service(servicename) enable_service(servicename) diff --git a/agent/src/com/cloud/agent/resource/DummyResource.java b/agent/src/com/cloud/agent/resource/DummyResource.java index c269c64284d..9ff60e1220d 100755 --- a/agent/src/com/cloud/agent/resource/DummyResource.java +++ b/agent/src/com/cloud/agent/resource/DummyResource.java @@ -32,6 +32,7 @@ import com.cloud.resource.ServerResource; @Local(value={ServerResource.class}) public class DummyResource implements ServerResource { + private boolean _isRemoteAgent = false; String _name; Host.Type _type; boolean _negative; @@ -101,4 +102,12 @@ public class DummyResource implements ServerResource { public void setAgentControl(IAgentControl agentControl) { _agentControl = agentControl; } + + public boolean IsRemoteAgent() { + return _isRemoteAgent; + } + + public void setRemoteAgent(boolean remote) { + _isRemoteAgent = remote; + } } diff --git a/client/tomcatconf/components.xml.in b/client/tomcatconf/components.xml.in index a10c9c2e9cd..986ac065f6d 100755 --- a/client/tomcatconf/components.xml.in +++ b/client/tomcatconf/components.xml.in @@ -149,7 +149,7 @@ - + diff --git a/console-proxy/bindir/cloud-setup-console-proxy.in b/console-proxy/bindir/cloud-setup-console-proxy.in index b550407d593..9bf55bc2f09 100755 --- a/console-proxy/bindir/cloud-setup-console-proxy.in +++ b/console-proxy/bindir/cloud-setup-console-proxy.in @@ -14,6 +14,7 @@ for pythonpath in ( if os.path.isdir(pythonpath): sys.path.insert(0,pythonpath) # ---- End snippet of code ---- import cloud_utils +from cloud_utils import stderr E_GENERIC= 1 E_NOKVM = 2 @@ -27,13 +28,6 @@ E_CPRECONFIGFAILED = 9 E_CPFAILEDTOSTART = 10 E_NOFQDN = 11 - -def stderr(msgfmt,*args): - msgfmt += "\n" - if args: sys.stderr.write(msgfmt%args) - else: sys.stderr.write(msgfmt) - sys.stderr.flush() - def bail(errno=E_GENERIC,message=None,*args): if message: stderr(message,*args) stderr("Cloud Console Proxy setup aborted") @@ -133,10 +127,11 @@ CentOS = os.path.exists("/etc/centos-release") or ( os.path.exists("/etc/redhat- def main(): # parse cmd line - opts, args = getopt.getopt(sys.argv[1:], "", ["host=", "zone=", "pod="]) + opts, args = getopt.getopt(sys.argv[1:], "a", ["host=", "zone=", "pod="]) host=None zone=None pod=None + autoMode=False do_check_kvm = True for opt, arg in opts: if opt == "--host": @@ -148,8 +143,13 @@ def main(): elif opt == "--pod": if arg != "": pod = arg + elif opt == "-a": + autoMode=True servicename = "@PACKAGE@-console-proxy" + if autoMode: + cloud_utils.setLogFile("/var/log/cloud/setupConsoleProxy.log") + stderr("Welcome to the Cloud Console Proxy setup") stderr("") diff --git a/core/src/com/cloud/hypervisor/kvm/resource/KvmDummyResourceBase.java b/core/src/com/cloud/hypervisor/kvm/resource/KvmDummyResourceBase.java new file mode 100644 index 00000000000..78451bced48 --- /dev/null +++ b/core/src/com/cloud/hypervisor/kvm/resource/KvmDummyResourceBase.java @@ -0,0 +1,70 @@ +package com.cloud.hypervisor.kvm.resource; + +import java.util.HashMap; +import java.util.Map; + +import javax.naming.ConfigurationException; + +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.host.Host.Type; +import com.cloud.hypervisor.Hypervisor; +import com.cloud.hypervisor.xen.resource.CitrixResourceBase; +import com.cloud.resource.ServerResource; +import com.cloud.resource.ServerResourceBase; +import com.cloud.vm.State; + +public class KvmDummyResourceBase extends ServerResourceBase implements ServerResource { + private String _zoneId; + private String _podId; + private String _guid; + private String _agentIp; + @Override + public Type getType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public StartupCommand[] initialize() { + StartupRoutingCommand cmd = new StartupRoutingCommand(0, 0, 0, 0, null, Hypervisor.Type.KVM, new HashMap(), new HashMap()); + cmd.setDataCenter(_zoneId); + cmd.setPod(_podId); + cmd.setGuid(_guid); + cmd.setName(_agentIp); + cmd.setPrivateIpAddress(_agentIp); + cmd.setStorageIpAddress(_agentIp); + cmd.setVersion(KvmDummyResourceBase.class.getPackage().getImplementationVersion()); + return new StartupCommand[] { cmd }; + } + + @Override + public PingCommand getCurrentStatus(long id) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Answer executeRequest(Command cmd) { + // TODO Auto-generated method stub + return null; + } + + @Override + protected String getDefaultScriptsDir() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean configure(final String name, final Map params) throws ConfigurationException { + _zoneId = (String)params.get("zone"); + _podId = (String)params.get("pod"); + _guid = (String)params.get("guid"); + _agentIp = (String)params.get("agentIp"); + return true; + } +} diff --git a/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index 0df9c7df36c..ea44e249591 100644 --- a/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -23,7 +23,7 @@ import com.cloud.resource.ServerResource; import com.cloud.storage.resource.StoragePoolResource; public class VmwareResource implements StoragePoolResource, ServerResource { - + private boolean _isRemoteAgent = false; @Override public DownloadAnswer execute(PrimaryStorageDownloadCommand cmd) { // TODO Auto-generated method stub @@ -119,4 +119,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource { // TODO Auto-generated method stub return false; } + + public boolean IsRemoteAgent() { + return _isRemoteAgent; + } + + public void setRemoteAgent(boolean remote) { + _isRemoteAgent = remote; + } } diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index 1f5ca0f6827..0dd5d95ca16 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -223,6 +223,7 @@ public abstract class CitrixResourceBase implements StoragePoolResource, ServerR protected String _guestNetworkName; protected int _wait; protected IAgentControl _agentControl; + protected boolean _isRemoteAgent = false; protected final XenServerHost _host = new XenServerHost(); @@ -6072,6 +6073,13 @@ public abstract class CitrixResourceBase implements StoragePoolResource, ServerR _agentControl = agentControl; } + public boolean IsRemoteAgent() { + return _isRemoteAgent; + } + + public void setRemoteAgent(boolean remote) { + _isRemoteAgent = remote; + } protected Answer execute(PoolEjectCommand cmd) { diff --git a/core/src/com/cloud/resource/ServerResource.java b/core/src/com/cloud/resource/ServerResource.java index b056a478016..75a37155361 100755 --- a/core/src/com/cloud/resource/ServerResource.java +++ b/core/src/com/cloud/resource/ServerResource.java @@ -71,4 +71,8 @@ public interface ServerResource extends Manager { public IAgentControl getAgentControl(); public void setAgentControl(IAgentControl agentControl); + + public boolean IsRemoteAgent(); + + public void setRemoteAgent(boolean remote); } diff --git a/core/src/com/cloud/resource/ServerResourceBase.java b/core/src/com/cloud/resource/ServerResourceBase.java index c10da164984..a60849c0772 100755 --- a/core/src/com/cloud/resource/ServerResourceBase.java +++ b/core/src/com/cloud/resource/ServerResourceBase.java @@ -48,6 +48,7 @@ public abstract class ServerResourceBase implements ServerResource { protected NetworkInterface _storageNic; protected NetworkInterface _storageNic2; protected IAgentControl _agentControl; + protected boolean _isRemoteAgent = false; @Override public String getName() { @@ -301,4 +302,12 @@ public abstract class ServerResourceBase implements ServerResource { public boolean stop() { return true; } + + public boolean IsRemoteAgent() { + return _isRemoteAgent; + } + + public void setRemoteAgent(boolean remote) { + _isRemoteAgent = remote; + } } diff --git a/python/lib/cloud_utils.py b/python/lib/cloud_utils.py index 6df111c58c6..1434372d548 100644 --- a/python/lib/cloud_utils.py +++ b/python/lib/cloud_utils.py @@ -45,9 +45,11 @@ elif os.path.exists("/etc/redhat-release") and not os.path.exists("/etc/fedora-r elif os.path.exists("/etc/legal") and "Ubuntu" in file("/etc/legal").read(-1): distro = Ubuntu else: distro = Unknown - +logFileName=None # ================== LIBRARY UTILITY CODE============= - +def setLogFile(logFile): + global logFileName + logFileName=logFile def read_properties(propfile): if not hasattr(propfile,"read"): propfile = file(propfile) properties = propfile.read().splitlines() @@ -63,9 +65,10 @@ def read_properties(propfile): def stderr(msgfmt,*args): """Print a message to stderr, optionally interpolating the arguments into it""" msgfmt += "\n" + if logFileName != None: + sys.stderr = open(logFileName, 'a+') if args: sys.stderr.write(msgfmt%args) else: sys.stderr.write(msgfmt) - sys.stderr.flush() def exit(errno=E_GENERIC,message=None,*args): """Exit with an error status code, printing a message to stderr if specified""" @@ -907,7 +910,7 @@ def prompt_for_hostpods(zonespods): # this configures the agent -def setup_agent_config(configfile, host, zone, pod): +def setup_agent_config(configfile, host, zone, pod, guid): stderr("Examining Agent configuration") fn = configfile text = file(fn).read(-1) @@ -915,9 +918,12 @@ def setup_agent_config(configfile, host, zone, pod): confopts = dict([ m.split("=",1) for m in lines if "=" in m and not m.startswith("#") ]) confposes = dict([ (m.split("=",1)[0],n) for n,m in enumerate(lines) if "=" in m and not m.startswith("#") ]) - if not "guid" in confopts: - stderr("Generating GUID for this Agent") - confopts['guid'] = uuidgen().stdout.strip() + if guid != None: + confopts['guid'] = guid + else: + if not "guid" in confopts: + stderr("Generating GUID for this Agent") + confopts['guid'] = uuidgen().stdout.strip() if host == None: try: host = confopts["host"] diff --git a/scripts/vm/hypervisor/kvm/setup_agent.sh b/scripts/vm/hypervisor/kvm/setup_agent.sh index 5302e8f71f8..7697aaa0d0b 100755 --- a/scripts/vm/hypervisor/kvm/setup_agent.sh +++ b/scripts/vm/hypervisor/kvm/setup_agent.sh @@ -1,6 +1,6 @@ #! /bin/bash # Did cloud-agent installed -set -x +#set -x install_cloud_agent() { local dev=$1 local retry=10 @@ -126,6 +126,7 @@ cloud_agent_setup() { local host=$1 local zone=$2 local pod=$3 + local guid=$4 # disable selinux selenabled=`cat /selinux/enforce` if [ "$selenabled" == "1" ] @@ -133,21 +134,22 @@ cloud_agent_setup() { sed -i 's/\(SELINUX\)\(.*\)/\1=permissive/' /etc/selinux/config setenforce 0 fi - cloud-setup-agent --host=$host --zone=$zone --pod=$pod -a + cloud-setup-agent --host=$host --zone=$zone --pod=$pod --guid=$guid -a > /dev/null } cloud_consoleP_setup() { local host=$1 local zone=$2 local pod=$3 - cloud-setup-console-proxy --host=$host --zone=$zone --pod=$pod + cloud-setup-console-proxy --host=$host --zone=$zone --pod=$pod -a > /dev/null } host= zone= pod= +guid= dflag= -while getopts 'h:z:p:d' OPTION +while getopts 'h:z:p:u:d' OPTION do case $OPTION in h) @@ -159,6 +161,9 @@ do p) pod="$OPTARG" ;; + u) + guid="$OPTARG" + ;; d) dflag=1 ;; @@ -166,7 +171,7 @@ do esac done -install_cloud_agent $dflag -install_cloud_consoleP $dflag -cloud_agent_setup $host $zone $pod +#install_cloud_agent $dflag +#install_cloud_consoleP $dflag +cloud_agent_setup $host $zone $pod $guid cloud_consoleP_setup $host $zone $pod diff --git a/server/.classpath b/server/.classpath index 52f2d3852ad..cefa0653a40 100644 --- a/server/.classpath +++ b/server/.classpath @@ -20,5 +20,6 @@ + diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index a34cd13381f..bf89138060b 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -100,6 +100,7 @@ import com.cloud.host.Status.Event; import com.cloud.host.dao.DetailsDao; import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor; +import com.cloud.hypervisor.kvm.resource.KvmDummyResourceBase; import com.cloud.maid.StackMaid; import com.cloud.maint.UpgradeManager; import com.cloud.network.IPAddressVO; @@ -460,11 +461,12 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory { if (server == null) { return null; } - + long id = server.getId(); AgentAttache attache = createAttache(id, server, resource); - notifyMonitorsOfConnection(attache, startup); + if (!resource.IsRemoteAgent()) + notifyMonitorsOfConnection(attache, startup); return attache; } @@ -1550,7 +1552,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory { protected AgentAttache createAttache(long id, HostVO server, ServerResource resource) { s_logger.debug("Adding directly connect host for " + id); - if (resource instanceof DummySecondaryStorageResource) { + if (resource instanceof DummySecondaryStorageResource || resource instanceof KvmDummyResourceBase) { return new DummyAttache(id, false); } final DirectAgentAttache attache = new DirectAgentAttache(id, resource, server.getStatus() == Status.Maintenance diff --git a/server/src/com/cloud/hypervisor/kvm/discoverer/KvmServerDiscoverer.java b/server/src/com/cloud/hypervisor/kvm/discoverer/KvmServerDiscoverer.java index b16b57a85b4..19c1aea8bee 100644 --- a/server/src/com/cloud/hypervisor/kvm/discoverer/KvmServerDiscoverer.java +++ b/server/src/com/cloud/hypervisor/kvm/discoverer/KvmServerDiscoverer.java @@ -1,8 +1,16 @@ package com.cloud.hypervisor.kvm.discoverer; +import java.net.InetAddress; import java.net.URI; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.apache.log4j.Logger; import com.cloud.agent.Listener; import com.cloud.agent.api.AgentControlAnswer; @@ -10,16 +18,32 @@ import com.cloud.agent.api.AgentControlCommand; import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; import com.cloud.agent.api.StartupCommand; +import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.exception.DiscoveryException; import com.cloud.host.HostVO; import com.cloud.host.Status; +import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.kvm.resource.KvmDummyResourceBase; +import com.cloud.hypervisor.xen.resource.CitrixResourceBase; import com.cloud.resource.Discoverer; import com.cloud.resource.DiscovererBase; import com.cloud.resource.ServerResource; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.component.Inject; +import com.cloud.utils.script.Script; +import com.trilead.ssh2.SCPClient; +import com.trilead.ssh2.Session; +@Local(value=Discoverer.class) public class KvmServerDiscoverer extends DiscovererBase implements Discoverer, Listener { - + private static final Logger s_logger = Logger.getLogger(KvmServerDiscoverer.class); + private String _setupAgentPath; + private ConfigurationDao _configDao; + private String _hostIp; + private int _waitTime = 10; + @Inject HostDao _hostDao = null; + @Override public boolean processAnswer(long agentId, long seq, Answer[] answers) { // TODO Auto-generated method stub @@ -73,14 +97,101 @@ public class KvmServerDiscoverer extends DiscovererBase implements Discoverer, public Map> find(long dcId, Long podId, Long clusterId, URI uri, String username, String password) throws DiscoveryException { - // TODO Auto-generated method stub + Map> resources = new HashMap>(); + Map details = new HashMap(); + if (!uri.getScheme().equals("http")) { + String msg = "urlString is not http so we're not taking care of the discovery for this: " + uri; + s_logger.debug(msg); + return null; + } + com.trilead.ssh2.Connection sshConnection = null; + Session sshSession = null; + String agentIp = null; + try { + + String hostname = uri.getHost(); + InetAddress ia = InetAddress.getByName(hostname); + agentIp = ia.getHostAddress(); + String guid = UUID.nameUUIDFromBytes(agentIp.getBytes()).toString(); + sshConnection = new com.trilead.ssh2.Connection(agentIp, 22); + + sshConnection.connect(null, 60000, 60000); + if (!sshConnection.authenticateWithPassword(username, password)) { + throw new Exception("Unable to authenticate"); + } + SCPClient scp = new SCPClient(sshConnection); + scp.put(_setupAgentPath, "/usr/bin", "0755"); + sshSession = sshConnection.openSession(); + /*running setup script in background, because we will restart agent network, that may cause connection lost*/ + s_logger.debug("/usr/bin/setup_agent.sh " + " -h " + _hostIp + " -z " + dcId + " -p " + podId + " -u " + guid); + sshSession.execCommand("/usr/bin/setup_agent.sh " + " -h " + _hostIp + " -z " + dcId + " -p " + podId + " -u " + guid + " 1>&2" ); + + KvmDummyResourceBase kvmResource = new KvmDummyResourceBase(); + Map params = new HashMap(); + + params.put("zone", Long.toString(dcId)); + params.put("pod", Long.toString(podId)); + params.put("guid", guid); + params.put("agentIp", agentIp); + kvmResource.configure("kvm agent", params); + kvmResource.setRemoteAgent(true); + resources.put(kvmResource, details); + return resources; + } catch (Exception e) { + String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage(); + s_logger.warn(msg); + } finally { + if (sshSession != null) + sshSession.close(); + + if (sshConnection != null) + sshConnection.close(); + } + return null; } @Override - public void postDiscovery(List hosts, long msId) { - // TODO Auto-generated method stub + public void postDiscovery(List hosts, long msId) throws DiscoveryException { + /*Wait for agent coming back*/ + if (hosts.isEmpty()) { + return; + } + HostVO host = hosts.get(0); + for (int i = 0 ; i < _waitTime; i++) { + + if (host.getStatus() != Status.Up) { + try { + Thread.sleep(60000); + } catch (InterruptedException e) { + s_logger.debug("Failed to sleep: " + e.toString()); + } + } else { + return; + } + } + /*Timeout, throw warning msg to user*/ + throw new DiscoveryException("Agent " + host.getId() + ":" + host.getPublicIpAddress() + " does not come back, It may connect to server later, if not, please check the agent log"); } - + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + ComponentLocator locator = ComponentLocator.getCurrentLocator(); + _configDao = locator.getDao(ConfigurationDao.class); + _setupAgentPath = Script.findScript(getPatchPath(), "setup_agent.sh"); + + if (_setupAgentPath == null) { + throw new ConfigurationException("Can't find setup_agent.sh"); + } + _hostIp = _configDao.getValue("host"); + if (_hostIp == null) { + throw new ConfigurationException("Can't get host IP"); + } + return true; + } + + protected String getPatchPath() { + return "scripts/vm/hypervisor/kvm/"; + } } diff --git a/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java b/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java index ecdf216f5df..8b0e7bd280f 100644 --- a/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java +++ b/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java @@ -450,7 +450,7 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L } @Override - public void postDiscovery(List hosts, long msId) { + public void postDiscovery(List hosts, long msId) throws DiscoveryException{ //do nothing } diff --git a/server/src/com/cloud/resource/Discoverer.java b/server/src/com/cloud/resource/Discoverer.java index 1d82d9701f9..a162a417afd 100644 --- a/server/src/com/cloud/resource/Discoverer.java +++ b/server/src/com/cloud/resource/Discoverer.java @@ -40,5 +40,5 @@ public interface Discoverer extends Adapter { */ Map> find(long dcId, Long podId, Long clusterId, URI uri, String username, String password) throws DiscoveryException; - void postDiscovery(List hosts, long msId); + void postDiscovery(List hosts, long msId) throws DiscoveryException; }