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);
}
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations