use of org.apache.xmlrpc.XmlRpcException 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 org.apache.xmlrpc.XmlRpcException 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 org.apache.xmlrpc.XmlRpcException in project cloudstack by apache.
the class CitrixResourceBase method destroyPatchVbd.
public void destroyPatchVbd(final Connection conn, final String vmName) throws XmlRpcException, XenAPIException {
try {
if (!vmName.startsWith("r-") && !vmName.startsWith("s-") && !vmName.startsWith("v-")) {
return;
}
final Set<VM> vms = VM.getByNameLabel(conn, vmName);
for (final VM vm : vms) {
final Set<VBD> vbds = vm.getVBDs(conn);
for (final VBD vbd : vbds) {
if (vbd.getType(conn) == Types.VbdType.CD) {
vbd.eject(conn);
vbd.destroy(conn);
break;
}
}
}
} catch (final Exception e) {
s_logger.debug("Cannot destory CD-ROM device for VM " + vmName + " due to " + e.toString(), e);
}
}
use of org.apache.xmlrpc.XmlRpcException 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);
}
use of org.apache.xmlrpc.XmlRpcException in project cloudstack by apache.
the class CitrixResourceBase method getLocalEXTSR.
protected SR getLocalEXTSR(final Connection conn) {
try {
final Map<SR, SR.Record> map = SR.getAllRecords(conn);
if (map != null && !map.isEmpty()) {
for (final Map.Entry<SR, SR.Record> entry : map.entrySet()) {
final SR.Record srRec = entry.getValue();
if (SRType.FILE.equals(srRec.type) || SRType.EXT.equals(srRec.type)) {
final Set<PBD> pbds = srRec.PBDs;
if (pbds == null) {
continue;
}
for (final PBD pbd : pbds) {
final Host host = pbd.getHost(conn);
if (!isRefNull(host) && host.getUuid(conn).equals(_host.getUuid())) {
if (!pbd.getCurrentlyAttached(conn)) {
pbd.plug(conn);
}
final SR sr = entry.getKey();
sr.scan(conn);
return sr;
}
}
}
}
}
} catch (final XenAPIException e) {
final String msg = "Unable to get local EXTSR in host:" + _host.getUuid() + e.toString();
s_logger.warn(msg);
} catch (final XmlRpcException e) {
final String msg = "Unable to get local EXTSR in host:" + _host.getUuid() + e.getCause();
s_logger.warn(msg);
}
return null;
}
Aggregations