use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method postProcessing.
protected CopyCmdAnswer postProcessing(final File destFile, final String downloadPath, final String destPath, final DataTO srcData, final DataTO destData) throws ConfigurationException {
if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
final SnapshotObjectTO snapshot = new SnapshotObjectTO();
snapshot.setPath(destPath + File.separator + destFile.getName());
final CopyCmdAnswer answer = new CopyCmdAnswer(snapshot);
return answer;
}
// do post processing to unzip the file if it is compressed
final String scriptsDir = "scripts/storage/secondary";
final String createTmpltScr = Script.findScript(scriptsDir, "createtmplt.sh");
if (createTmpltScr == null) {
throw new ConfigurationException("Unable to find createtmplt.sh");
}
s_logger.info("createtmplt.sh found in " + createTmpltScr);
final String createVolScr = Script.findScript(scriptsDir, "createvolume.sh");
if (createVolScr == null) {
throw new ConfigurationException("Unable to find createvolume.sh");
}
s_logger.info("createvolume.sh found in " + createVolScr);
final String script = srcData.getObjectType() == DataObjectType.TEMPLATE ? createTmpltScr : createVolScr;
final int installTimeoutPerGig = 180 * 60 * 1000;
long imgSizeGigs = (long) Math.ceil(destFile.length() * 1.0d / (1024 * 1024 * 1024));
// add one just in case
imgSizeGigs++;
final long timeout = imgSizeGigs * installTimeoutPerGig;
final String origPath = destFile.getAbsolutePath();
String extension = null;
if (srcData.getObjectType() == DataObjectType.TEMPLATE) {
extension = ((TemplateObjectTO) srcData).getFormat().toString().toLowerCase();
} else if (srcData.getObjectType() == DataObjectType.VOLUME) {
extension = ((VolumeObjectTO) srcData).getFormat().toString().toLowerCase();
}
final String templateName = UUID.randomUUID().toString();
final String templateFilename = templateName + "." + extension;
final Script scr = new Script(script, timeout, s_logger);
// not used for now
scr.add("-s", Long.toString(imgSizeGigs));
scr.add("-n", templateFilename);
scr.add("-t", downloadPath);
// this is the temporary
scr.add("-f", origPath);
// template file downloaded
final String result;
result = scr.execute();
if (result != null) {
// script execution failure
throw new CloudRuntimeException("Failed to run script " + script);
}
final String finalFileName = templateFilename;
final String finalDownloadPath = destPath + File.separator + templateFilename;
// compute the size of
final long size = this._storage.getSize(downloadPath + File.separator + templateFilename);
DataTO newDestTO = null;
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
final TemplateObjectTO newTemplTO = new TemplateObjectTO();
newTemplTO.setPath(finalDownloadPath);
newTemplTO.setName(finalFileName);
newTemplTO.setSize(size);
newTemplTO.setPhysicalSize(size);
newDestTO = newTemplTO;
} else {
final VolumeObjectTO newVolTO = new VolumeObjectTO();
newVolTO.setPath(finalDownloadPath);
newVolTO.setName(finalFileName);
newVolTO.setSize(size);
newDestTO = newVolTO;
}
return new CopyCmdAnswer(newDestTO);
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method attemptMount.
protected void attemptMount(final String localRootPath, final String remoteDevice, final URI uri) {
final String result;
s_logger.debug("Make cmdline call to mount " + remoteDevice + " at " + localRootPath + " based on uri " + uri);
final Script command = new Script(!this._inSystemVM, "mount", this._timeout, s_logger);
final String scheme = uri.getScheme().toLowerCase();
command.add("-t", scheme);
if (scheme.equals("nfs")) {
if ("Mac OS X".equalsIgnoreCase(System.getProperty("os.name"))) {
// See http://wiki.qnap.com/wiki/Mounting_an_NFS_share_from_OS_X
command.add("-o", "resvport");
}
if (this._inSystemVM) {
command.add("-o", "soft,timeo=133,retrans=2147483647,tcp,acdirmax=0,acdirmin=0");
}
} else if (scheme.equals("cifs")) {
final String extraOpts = parseCifsMountOptions(uri);
// nfs acdirmax / acdirmin correspoonds to CIFS actimeo (see
// http://linux.die.net/man/8/mount.cifs)
// no equivalent to nfs timeo, retrans or tcp in CIFS
// todo: allow security mode to be set.
command.add("-o", extraOpts + "soft,actimeo=0");
} else {
final String errMsg = "Unsupported storage device scheme " + scheme + " in uri " + uri.toString();
s_logger.error(errMsg);
throw new CloudRuntimeException(errMsg);
}
command.add(remoteDevice);
command.add(localRootPath);
result = command.execute();
if (result != null) {
// Fedora Core 12 errors out with any -o option executed from java
final String errMsg = "Unable to mount " + remoteDevice + " at " + localRootPath + " due to " + result;
s_logger.error(errMsg);
final File file = new File(localRootPath);
if (file.exists()) {
file.delete();
}
throw new CloudRuntimeException(errMsg);
}
s_logger.debug("Successfully mounted " + remoteDevice + " at " + localRootPath);
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method getRootDir.
@Override
public synchronized String getRootDir(final String secUrl) {
if (!this._inSystemVM) {
return this._parent;
}
try {
final URI uri = new URI(secUrl);
final String dir = mountUri(uri);
return this._parent + "/" + dir;
} catch (final Exception e) {
final String msg = "GetRootDir for " + secUrl + " failed due to " + e.toString();
s_logger.error(msg, e);
throw new CloudRuntimeException(msg);
}
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method ensureLocalRootPathExists.
protected void ensureLocalRootPathExists(final String localRootPath, final URI uri) {
s_logger.debug("making available " + localRootPath + " on " + uri.toString());
final File file = new File(localRootPath);
s_logger.debug("local folder for mount will be " + file.getPath());
if (!file.exists()) {
s_logger.debug("create mount point: " + file.getPath());
this._storage.mkdir(file.getPath());
// Need to check after mkdir to allow O/S to complete operation
if (!file.exists()) {
final String errMsg = "Unable to create local folder for: " + localRootPath + " in order to mount " + uri.toString();
s_logger.error(errMsg);
throw new CloudRuntimeException(errMsg);
}
}
}
use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.
the class ScpTemplateDownloader method download.
@Override
public long download(final boolean resume, final DownloadCompleteCallback callback) {
if (this._status == TemplateDownloadStatus.ABORTED || this._status == TemplateDownloadStatus.UNRECOVERABLE_ERROR || this._status == TemplateDownloadStatus.DOWNLOAD_FINISHED) {
return 0;
}
this._resume = resume;
this._start = System.currentTimeMillis();
final URI uri;
try {
uri = new URI(this._downloadUrl);
} catch (final URISyntaxException e1) {
this._status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
return 0;
}
final String username = uri.getUserInfo();
final String queries = uri.getQuery();
String password = null;
if (queries != null) {
final String[] qs = queries.split("&");
for (final String q : qs) {
final String[] tokens = q.split("=");
if (tokens[0].equalsIgnoreCase("password")) {
password = tokens[1];
break;
}
}
}
int port = uri.getPort();
if (port == -1) {
port = 22;
}
final File file = new File(this._toFile);
final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(uri.getHost(), port);
try {
if (this._storage != null) {
file.createNewFile();
this._storage.setWorldReadableAndWriteable(file);
}
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(username, password)) {
throw new CloudRuntimeException("Unable to authenticate");
}
final SCPClient scp = new SCPClient(sshConnection);
final String src = uri.getPath();
this._status = TemplateDownloadStatus.IN_PROGRESS;
scp.get(src, this._toDir);
if (!file.exists()) {
this._status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
s_logger.debug("unable to scp the file " + this._downloadUrl);
return 0;
}
this._status = TemplateDownloadStatus.DOWNLOAD_FINISHED;
this._totalBytes = file.length();
final String downloaded = "(download complete)";
this._errorString = "Downloaded " + this._remoteSize + " bytes " + downloaded;
this._downloadTime += System.currentTimeMillis() - this._start;
return this._totalBytes;
} catch (final Exception e) {
s_logger.warn("Unable to download " + this._downloadUrl, e);
this._status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
this._errorString = e.getMessage();
return 0;
} finally {
sshConnection.close();
if (this._status == TemplateDownloadStatus.UNRECOVERABLE_ERROR && file.exists()) {
file.delete();
}
if (callback != null) {
callback.downloadComplete(this._status);
}
}
}
Aggregations