Search in sources :

Example 6 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixResourceBase method setupServer.

/* return : if setup is needed */
public boolean setupServer(final Connection conn, final Host host) {
    final String packageVersion = CitrixResourceBase.class.getPackage().getImplementationVersion();
    final String version = this.getClass().getName() + "-" + (packageVersion == null ? Long.toString(System.currentTimeMillis()) : packageVersion);
    try {
        /* push patches to XenServer */
        final Host.Record hr = host.getRecord(conn);
        final Iterator<String> it = hr.tags.iterator();
        while (it.hasNext()) {
            final String tag = it.next();
            if (tag.startsWith("vmops-version-")) {
                if (tag.contains(version)) {
                    s_logger.info(logX(host, "Host " + hr.address + " is already setup."));
                    return false;
                } else {
                    it.remove();
                }
            }
        }
        final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(hr.address, 22);
        try {
            sshConnection.connect(null, 60000, 60000);
            if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
                throw new CloudRuntimeException("Unable to authenticate");
            }
            final String cmd = "mkdir -p /opt/cloud/bin /var/log/cloud";
            if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
                throw new CloudRuntimeException("Cannot create directory /opt/cloud/bin on XenServer hosts");
            }
            final SCPClient scp = new SCPClient(sshConnection);
            final List<File> files = getPatchFiles();
            if (files == null || files.isEmpty()) {
                throw new CloudRuntimeException("Can not find patch file");
            }
            for (final File file : files) {
                final String path = file.getParentFile().getAbsolutePath() + "/";
                final Properties props = PropertiesUtil.loadFromFile(file);
                for (final Map.Entry<Object, Object> entry : props.entrySet()) {
                    final String k = (String) entry.getKey();
                    final String v = (String) entry.getValue();
                    assert k != null && k.length() > 0 && v != null && v.length() > 0 : "Problems with " + k + "=" + v;
                    final String[] tokens = v.split(",");
                    String f = null;
                    if (tokens.length == 3 && tokens[0].length() > 0) {
                        if (tokens[0].startsWith("/")) {
                            f = tokens[0];
                        } else if (tokens[0].startsWith("~")) {
                            final String homedir = System.getenv("HOME");
                            f = homedir + tokens[0].substring(1) + k;
                        } else {
                            f = path + tokens[0] + '/' + k;
                        }
                    } else {
                        f = path + k;
                    }
                    final String directoryPath = tokens[tokens.length - 1];
                    f = f.replace('/', File.separatorChar);
                    String permissions = "0755";
                    if (tokens.length == 3) {
                        permissions = tokens[1];
                    } else if (tokens.length == 2) {
                        permissions = tokens[0];
                    }
                    if (!new File(f).exists()) {
                        s_logger.warn("We cannot locate " + f);
                        continue;
                    }
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Copying " + f + " to " + directoryPath + " on " + hr.address + " with permission " + permissions);
                    }
                    if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "mkdir -m 700 -p " + directoryPath)) {
                        s_logger.debug("Unable to create destination path: " + directoryPath + " on " + hr.address + ".");
                    }
                    try {
                        scp.put(f, directoryPath, permissions);
                    } catch (final IOException e) {
                        final String msg = "Unable to copy file " + f + " to path " + directoryPath + " with permissions  " + permissions;
                        s_logger.debug(msg);
                        throw new CloudRuntimeException("Unable to setup the server: " + msg, e);
                    }
                }
            }
        } catch (final IOException e) {
            throw new CloudRuntimeException("Unable to setup the server correctly", e);
        } finally {
            sshConnection.close();
        }
        hr.tags.add("vmops-version-" + version);
        host.setTags(conn, hr.tags);
        return true;
    } catch (final XenAPIException e) {
        final String msg = "XenServer setup failed due to " + e.toString();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException("Unable to get host information " + e.toString(), e);
    } catch (final XmlRpcException e) {
        final String msg = "XenServer setup failed due to " + e.getMessage();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException("Unable to get host information ", e);
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) IOException(java.io.IOException) Properties(java.util.Properties) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIObject(com.xensource.xenapi.XenAPIObject) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 7 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixResourceBase method createLocalIsoSR.

public SR createLocalIsoSR(final Connection conn, final String srName) throws XenAPIException, XmlRpcException {
    // if config drive sr already exists then return
    SR sr = getSRByNameLabelandHost(conn, _configDriveSRName + _host.getIp());
    if (sr != null) {
        s_logger.debug("Config drive SR already exist, returing it");
        return sr;
    }
    try {
        final Map<String, String> deviceConfig = new HashMap<String, String>();
        final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
        try {
            sshConnection.connect(null, 60000, 60000);
            if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
                throw new CloudRuntimeException("Unable to authenticate");
            }
            final String cmd = "mkdir -p " + _configDriveIsopath;
            if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
                throw new CloudRuntimeException("Cannot create directory configdrive_iso on XenServer hosts");
            }
        } catch (final IOException e) {
            throw new CloudRuntimeException("Unable to create iso folder", e);
        } finally {
            sshConnection.close();
        }
        s_logger.debug("Created the config drive SR " + srName + " folder path " + _configDriveIsopath);
        deviceConfig.put("location", _configDriveIsopath);
        deviceConfig.put("legacy_mode", "true");
        final Host host = Host.getByUuid(conn, _host.getUuid());
        final String type = SRType.ISO.toString();
        sr = SR.create(conn, host, deviceConfig, new Long(0), _configDriveIsopath, "iso", type, "iso", false, new HashMap<String, String>());
        sr.setNameLabel(conn, srName);
        sr.setNameDescription(conn, deviceConfig.get("location"));
        sr.scan(conn);
        s_logger.debug("Config drive ISO SR at the path " + _configDriveIsopath + " got created in host " + _host);
        return sr;
    } catch (final XenAPIException e) {
        final String msg = "createLocalIsoSR failed! mountpoint " + e.toString();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException(msg, e);
    } catch (final Exception e) {
        final String msg = "createLocalIsoSR failed! mountpoint:  due to " + e.getMessage();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException(msg, e);
    }
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) IOException(java.io.IOException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 8 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixResourceBase method createIsoSRbyURI.

protected SR createIsoSRbyURI(final Connection conn, final URI uri, final String vmName, final boolean shared) {
    try {
        final Map<String, String> deviceConfig = new HashMap<String, String>();
        String path = uri.getPath();
        path = path.replace("//", "/");
        deviceConfig.put("location", uri.getHost() + ":" + path);
        final Host host = Host.getByUuid(conn, _host.getUuid());
        final SR sr = SR.create(conn, host, deviceConfig, new Long(0), uri.getHost() + path, "iso", "iso", "iso", shared, new HashMap<String, String>());
        sr.setNameLabel(conn, vmName + "-ISO");
        sr.setNameDescription(conn, deviceConfig.get("location"));
        sr.scan(conn);
        return sr;
    } catch (final XenAPIException e) {
        final String msg = "createIsoSRbyURI failed! mountpoint: " + uri.getHost() + uri.getPath() + " due to " + e.toString();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException(msg, e);
    } catch (final Exception e) {
        final String msg = "createIsoSRbyURI failed! mountpoint: " + uri.getHost() + uri.getPath() + " due to " + e.getMessage();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException(msg, e);
    }
}
Also used : HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SR(com.xensource.xenapi.SR)

Example 9 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixResourceBase method callHostPluginAsync.

protected String callHostPluginAsync(final Connection conn, final String plugin, final String cmd, final int wait, final Map<String, String> params) {
    final int timeout = wait * 1000;
    final Map<String, String> args = new HashMap<String, String>();
    Task task = null;
    try {
        for (final Map.Entry<String, String> entry : params.entrySet()) {
            args.put(entry.getKey(), entry.getValue());
        }
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("callHostPlugin executing for command " + cmd + " with " + getArgsString(args));
        }
        final Host host = Host.getByUuid(conn, _host.getUuid());
        task = host.callPluginAsync(conn, plugin, cmd, args);
        // poll every 1 seconds
        waitForTask(conn, task, 1000, timeout);
        checkForSuccess(conn, task);
        final String result = task.getResult(conn);
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("callHostPlugin Result: " + result);
        }
        return result.replace("<value>", "").replace("</value>", "").replace("\n", "");
    } catch (final Types.HandleInvalid e) {
        s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to HandleInvalid clazz:" + e.clazz + ", handle:" + e.handle);
    } catch (final Exception e) {
        s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.toString(), e);
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (final Exception e1) {
                s_logger.debug("unable to destroy task(" + task.toString() + ") on host(" + _host.getUuid() + ") due to " + e1.toString());
            }
        }
    }
    return null;
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) HashMap(java.util.HashMap) Host(com.xensource.xenapi.Host) Map(java.util.Map) HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with Host

use of com.xensource.xenapi.Host in project cloudstack by apache.

the class CitrixResourceBase method callHostPlugin.

public String callHostPlugin(final Connection conn, final String plugin, final String cmd, final String... params) {
    final Map<String, String> args = new HashMap<String, String>();
    String msg;
    try {
        for (int i = 0; i < params.length; i += 2) {
            args.put(params[i], params[i + 1]);
        }
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("callHostPlugin executing for command " + cmd + " with " + getArgsString(args));
        }
        final Host host = Host.getByUuid(conn, _host.getUuid());
        final String result = host.callPlugin(conn, plugin, cmd, args);
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("callHostPlugin Result: " + result);
        }
        return result.replace("\n", "");
    } catch (final XenAPIException e) {
        msg = "callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.toString();
        s_logger.warn(msg);
    } catch (final XmlRpcException e) {
        msg = "callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.getMessage();
        s_logger.debug(msg);
    }
    throw new CloudRuntimeException(msg);
}
Also used : HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

Host (com.xensource.xenapi.Host)47 XenAPIException (com.xensource.xenapi.Types.XenAPIException)35 XmlRpcException (org.apache.xmlrpc.XmlRpcException)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)32 HashMap (java.util.HashMap)24 Connection (com.xensource.xenapi.Connection)17 SR (com.xensource.xenapi.SR)17 IOException (java.io.IOException)15 ConfigurationException (javax.naming.ConfigurationException)14 InternalErrorException (com.cloud.exception.InternalErrorException)13 MalformedURLException (java.net.MalformedURLException)13 URISyntaxException (java.net.URISyntaxException)12 TimeoutException (java.util.concurrent.TimeoutException)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 SAXException (org.xml.sax.SAXException)12 VM (com.xensource.xenapi.VM)11 PBD (com.xensource.xenapi.PBD)10 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)7 Map (java.util.Map)7 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)6