Search in sources :

Example 51 with CloudRuntimeException

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

the class Agent method setupStartupCommand.

protected void setupStartupCommand(final StartupCommand startup) {
    final InetAddress addr;
    try {
        addr = InetAddress.getLocalHost();
    } catch (final UnknownHostException e) {
        logger.warn("Unknown host", e);
        throw new CloudRuntimeException("Cannot get local IP address", e);
    }
    final Script command = new Script("hostname", 500, logger);
    final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
    final String result = command.execute(parser);
    final String hostname = result == null ? parser.getLine() : addr.toString();
    startup.setId(getId());
    if (startup.getName() == null) {
        startup.setName(hostname);
    }
    startup.setDataCenter(this.agentConfiguration.getZone());
    startup.setPod(this.agentConfiguration.getPod());
    startup.setGuid(getResourceGuid());
    startup.setResourceName(getResourceName());
}
Also used : Script(com.cloud.utils.script.Script) UnknownHostException(java.net.UnknownHostException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) OutputInterpreter(com.cloud.utils.script.OutputInterpreter) InetAddress(java.net.InetAddress)

Example 52 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 53 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 54 with CloudRuntimeException

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

the class ArrayTypeAdaptor method deserialize.

@Override
public T[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
    final JsonArray array = json.getAsJsonArray();
    final Iterator<JsonElement> it = array.iterator();
    final ArrayList<T> cmds = new ArrayList<>();
    while (it.hasNext()) {
        final JsonObject element = (JsonObject) it.next();
        final Map.Entry<String, JsonElement> entry = element.entrySet().iterator().next();
        final String name = entry.getKey();
        final Class<?> clazz;
        try {
            clazz = Class.forName(name);
        } catch (final ClassNotFoundException e) {
            throw new CloudRuntimeException("can't find " + name);
        }
        final T cmd = (T) _gson.fromJson(entry.getValue(), clazz);
        cmds.add(cmd);
    }
    final Class<?> type;
    final String typeOfTClass = typeOfT.getTypeName().replace("[]", "");
    try {
        type = Class.forName(typeOfTClass);
    } catch (ClassNotFoundException e) {
        throw new CloudRuntimeException("can't find " + typeOfTClass);
    }
    final T[] ts = (T[]) Array.newInstance(type, cmds.size());
    return cmds.toArray(ts);
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Map(java.util.Map)

Example 55 with CloudRuntimeException

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

the class InterfaceTypeAdaptor method deserialize.

@Override
public T deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject element = (JsonObject) json;
    final Map.Entry<String, JsonElement> entry = element.entrySet().iterator().next();
    final String name = entry.getKey();
    final Class<?> clazz;
    try {
        clazz = Class.forName(name);
    } catch (final ClassNotFoundException e) {
        throw new CloudRuntimeException("can't find " + name);
    }
    return (T) _gson.fromJson(entry.getValue(), clazz);
}
Also used : JsonElement(com.google.gson.JsonElement) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

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