Search in sources :

Example 6 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class DownloadManagerImpl method downloadPublicTemplate.

@Override
public String downloadPublicTemplate(final long id, final String url, final String name, final ImageFormat format, final Long accountId, final String descr, final String cksum, final String installPathPrefix, final String templatePath, final String user, final String password, final long maxTemplateSizeInBytes, final Proxy proxy, final ResourceType resourceType) {
    final UUID uuid = UUID.randomUUID();
    final String jobId = uuid.toString();
    final String tmpDir = installPathPrefix;
    try {
        if (!this._storage.mkdirs(tmpDir)) {
            s_logger.warn("Unable to create " + tmpDir);
            return "Unable to create " + tmpDir;
        }
        // TO DO - define constant for volume properties.
        final File file = ResourceType.TEMPLATE == resourceType ? this._storage.getFile(tmpDir + File.separator + TemplateLocation.Filename) : this._storage.getFile(tmpDir + File.separator + "volume.properties");
        if (file.exists()) {
            if (!file.delete()) {
                s_logger.warn("Deletion of file '" + file.getAbsolutePath() + "' failed.");
            }
        }
        if (!file.createNewFile()) {
            s_logger.warn("Unable to create new file: " + file.getAbsolutePath());
            return "Unable to create new file: " + file.getAbsolutePath();
        }
        final URI uri;
        try {
            uri = new URI(url);
        } catch (final URISyntaxException e) {
            throw new CloudRuntimeException("URI is incorrect: " + url);
        }
        final TemplateDownloader td;
        if (uri != null && uri.getScheme() != null) {
            if (uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https")) {
                td = new HttpTemplateDownloader(this._storage, url, tmpDir, new Completion(jobId), maxTemplateSizeInBytes, user, password, proxy, resourceType);
            } else if (uri.getScheme().equalsIgnoreCase("file")) {
                td = new LocalTemplateDownloader(this._storage, url, tmpDir, maxTemplateSizeInBytes, new Completion(jobId));
            } else if (uri.getScheme().equalsIgnoreCase("scp")) {
                td = new ScpTemplateDownloader(this._storage, url, tmpDir, maxTemplateSizeInBytes, new Completion(jobId));
            } else if (uri.getScheme().equalsIgnoreCase("nfs") || uri.getScheme().equalsIgnoreCase("cifs")) {
                td = null;
                // TODO: implement this.
                throw new CloudRuntimeException("Scheme is not supported " + url);
            } else {
                throw new CloudRuntimeException("Scheme is not supported " + url);
            }
        } else {
            throw new CloudRuntimeException("Unable to download from URL: " + url);
        }
        // NOTE the difference between installPathPrefix and templatePath
        // here. instalPathPrefix is the absolute path for template
        // including mount directory
        // on ssvm, while templatePath is the final relative path on
        // secondary storage.
        final DownloadJob dj = new DownloadJob(td, jobId, id, name, format, accountId, descr, cksum, installPathPrefix, resourceType);
        dj.setTmpltPath(templatePath);
        this.jobs.put(jobId, dj);
        this.threadPool.execute(td);
        return jobId;
    } catch (final IOException e) {
        s_logger.warn("Unable to download to " + tmpDir, e);
        return null;
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) UUID(java.util.UUID) File(java.io.File)

Example 7 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class LocalTemplateDownloader 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) {
        throw new CloudRuntimeException("Invalid status for downloading: " + this._status);
    }
    this._start = System.currentTimeMillis();
    this._resume = resume;
    final File src;
    try {
        src = new File(new URI(this._downloadUrl));
    } catch (final URISyntaxException e) {
        final String message = "Invalid URI " + this._downloadUrl;
        s_logger.warn(message);
        this._status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
        throw new CloudRuntimeException(message, e);
    }
    final File dst = new File(this._toFile);
    FileChannel fic = null;
    FileChannel foc = null;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        if (this._storage != null) {
            dst.createNewFile();
            this._storage.setWorldReadableAndWriteable(dst);
        }
        final ByteBuffer buffer = ByteBuffer.allocate(1024 * 512);
        try {
            fis = new FileInputStream(src);
        } catch (final FileNotFoundException e) {
            this._errorString = "Unable to find " + this._downloadUrl;
            s_logger.warn(this._errorString);
            throw new CloudRuntimeException(this._errorString, e);
        }
        fic = fis.getChannel();
        try {
            if (!dst.exists()) {
                dst.delete();
            }
            fos = new FileOutputStream(dst);
        } catch (final FileNotFoundException e) {
            final String message = "Unable to find " + this._toFile;
            s_logger.warn(message);
            throw new CloudRuntimeException(message, e);
        }
        foc = fos.getChannel();
        this._remoteSize = src.length();
        this._totalBytes = 0;
        this._status = TemplateDownloadStatus.IN_PROGRESS;
        try {
            while (this._status != TemplateDownloadStatus.ABORTED && fic.read(buffer) != -1) {
                buffer.flip();
                final int count = foc.write(buffer);
                this._totalBytes += count;
                buffer.clear();
            }
        } catch (final IOException e) {
            s_logger.warn("Unable to download");
        }
        String downloaded = "(incomplete download)";
        if (this._totalBytes == this._remoteSize) {
            this._status = TemplateDownloadStatus.DOWNLOAD_FINISHED;
            downloaded = "(download complete)";
        }
        this._errorString = "Downloaded " + this._remoteSize + " bytes " + downloaded;
        this._downloadTime += System.currentTimeMillis() - this._start;
        return this._totalBytes;
    } catch (final Exception e) {
        this._status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
        this._errorString = e.getMessage();
        throw new CloudRuntimeException(this._errorString, e);
    } finally {
        if (fic != null) {
            try {
                fic.close();
            } catch (final IOException e) {
                s_logger.info("[ignore] error while closing file input channel.");
            }
        }
        if (foc != null) {
            try {
                foc.close();
            } catch (final IOException e) {
                s_logger.info("[ignore] error while closing file output channel.");
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (final IOException e) {
                s_logger.info("[ignore] error while closing file input stream.");
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (final IOException e) {
                s_logger.info("[ignore] error while closing file output stream.");
            }
        }
        if (this._status == TemplateDownloadStatus.UNRECOVERABLE_ERROR && dst.exists()) {
            dst.delete();
        }
        if (callback != null) {
            callback.downloadComplete(this._status);
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) FileNotFoundException(java.io.FileNotFoundException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 8 with CloudRuntimeException

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);
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) File(java.io.File)

Example 9 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method umount.

protected void umount(final String localRootPath, final URI uri) {
    ensureLocalRootPathExists(localRootPath, uri);
    if (!mountExists(localRootPath, uri)) {
        return;
    }
    final Script command = new Script(!this._inSystemVM, "mount", this._timeout, s_logger);
    command.add(localRootPath);
    final String result = command.execute();
    if (result != null) {
        // Fedora Core 12 errors out with any -o option executed from java
        final String errMsg = "Unable to umount " + 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 umounted " + localRootPath);
}
Also used : Script(com.cloud.utils.script.Script) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) File(java.io.File)

Example 10 with CloudRuntimeException

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);
        }
    }
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) File(java.io.File)

Aggregations

CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)587 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)159 ArrayList (java.util.ArrayList)110 DB (com.cloud.utils.db.DB)90 Account (com.cloud.legacymodel.user.Account)84 SQLException (java.sql.SQLException)84 ActionEvent (com.cloud.event.ActionEvent)73 ConfigurationException (javax.naming.ConfigurationException)73 PreparedStatement (java.sql.PreparedStatement)68 HashMap (java.util.HashMap)68 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)62 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)52 HostVO (com.cloud.host.HostVO)50 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)50 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)47 Answer (com.cloud.legacymodel.communication.answer.Answer)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)45 TransactionStatus (com.cloud.utils.db.TransactionStatus)44 IOException (java.io.IOException)44