Search in sources :

Example 96 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class EncryptionSecretKeyChanger method migrateVNCPassword.

private void migrateVNCPassword(Connection conn) {
    System.out.println("Begin migrate VNC password");
    try (PreparedStatement select_pstmt = conn.prepareStatement("select id, vnc_password from vm_instance");
        ResultSet rs = select_pstmt.executeQuery();
        PreparedStatement pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?")) {
        while (rs.next()) {
            long id = rs.getLong(1);
            String value = rs.getString(2);
            if (value == null || value.isEmpty()) {
                continue;
            }
            String encryptedValue = migrateValue(value);
            pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
            pstmt.setLong(2, id);
            pstmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
    } catch (UnsupportedEncodingException e) {
        throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
    }
    System.out.println("End migrate VNC password");
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Example 97 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class ClusterManagerImpl method getHeartbeatTask.

private Runnable getHeartbeatTask() {
    return new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            final TransactionLegacy txn = TransactionLegacy.open("ClusterHeartbeat");
            try {
                final Profiler profiler = new Profiler();
                final Profiler profilerHeartbeatUpdate = new Profiler();
                final Profiler profilerPeerScan = new Profiler();
                try {
                    profiler.start();
                    profilerHeartbeatUpdate.start();
                    txn.transitToUserManagedConnection(getHeartbeatConnection());
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Cluster manager heartbeat update, id:" + _mshostId);
                    }
                    _mshostDao.update(_mshostId, _runId, DateUtil.currentGMTTime());
                    profilerHeartbeatUpdate.stop();
                    profilerPeerScan.start();
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Cluster manager peer-scan, id:" + _mshostId);
                    }
                    if (!_peerScanInited) {
                        _peerScanInited = true;
                        initPeerScan();
                    }
                    peerScan();
                    profilerPeerScan.stop();
                } finally {
                    profiler.stop();
                    if (profiler.getDurationInMillis() >= HeartbeatInterval.value()) {
                        if (s_logger.isDebugEnabled()) {
                            s_logger.debug("Management server heartbeat takes too long to finish. profiler: " + profiler.toString() + ", profilerHeartbeatUpdate: " + profilerHeartbeatUpdate.toString() + ", profilerPeerScan: " + profilerPeerScan.toString());
                        }
                    }
                }
            } catch (final CloudRuntimeException e) {
                s_logger.error("Runtime DB exception ", e.getCause());
                if (e.getCause() instanceof ClusterInvalidSessionException) {
                    s_logger.error("Invalid cluster session found, fence it");
                    queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated));
                }
                if (isRootCauseConnectionRelated(e.getCause())) {
                    invalidHeartbeatConnection();
                }
            } catch (final ActiveFencingException e) {
                queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated));
            } catch (final Throwable e) {
                s_logger.error("Unexpected exception in cluster heartbeat", e);
                if (isRootCauseConnectionRelated(e.getCause())) {
                    invalidHeartbeatConnection();
                }
            } finally {
                txn.transitToAutoManagedConnection(TransactionLegacy.CLOUD_DB);
                txn.close("ClusterHeartbeat");
            }
        }
    };
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) Profiler(com.cloud.utils.Profiler) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 98 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class ManagementServerHostDaoImpl method listOrphanMsids.

@Override
public List<Long> listOrphanMsids() {
    List<Long> orphanList = new ArrayList<Long>();
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    try {
        pstmt = txn.prepareAutoCloseStatement("select t.mgmt_server_id from (select mgmt_server_id, count(*) as count from host group by mgmt_server_id) as t WHERE t.count > 0 AND t.mgmt_server_id NOT IN (select msid from mshost)");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            orphanList.add(rs.getLong(1));
        }
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB exception: ", e);
    }
    return orphanList;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 99 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class LibvirtCreatePrivateTemplateFromVolumeCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String secondaryStorageURL = command.getSecondaryStorageUrl();
    KVMStoragePool secondaryStorage = null;
    KVMStoragePool primary = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final String templateFolder = command.getAccountId() + File.separator + command.getTemplateId() + File.separator;
        final String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = storagePoolMgr.getStoragePoolByURI(secondaryStorageURL);
        try {
            primary = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primary = storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), command.getPool().getUserInfo(), command.getPool().getType());
            } else {
                return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
            }
        }
        final KVMPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(tmpltPath);
        if (primary.getType() != StoragePoolType.RBD) {
            final String createTmplPath = libvirtComputingResource.createTmplPath();
            final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
            final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
            scriptCommand.add("-f", disk.getPath());
            scriptCommand.add("-t", tmpltPath);
            scriptCommand.add("-n", command.getUniqueName() + ".qcow2");
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("failed to create template: " + result);
                return new CreatePrivateTemplateAnswer(command, false, result);
            }
        } else {
            s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + command.getUniqueName());
            final QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
            srcFile.setFormat(PhysicalDiskFormat.RAW);
            final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + command.getUniqueName() + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(0);
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException e) {
                s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + command.getUniqueName() + ".qcow2" + System.getProperty("line.separator");
            final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
            final Date date = new Date();
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
            try (FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes("UTF-8"));
                templFo.flush();
            } catch (final IOException ex) {
                s_logger.error("CreatePrivateTemplateAnswer:Exception:" + ex.getMessage());
            }
        }
        final Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, storage);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final FormatInfo info = qcow2Processor.process(tmpltPath, null, command.getUniqueName());
        final TemplateLocation loc = new TemplateLocation(storage, tmpltPath);
        loc.create(1, true, command.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, null, templateInstallFolder + command.getUniqueName() + ".qcow2", info.virtualSize, info.size, command.getUniqueName(), ImageFormat.QCOW2);
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid());
        }
    }
}
Also used : StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) HashMap(java.util.HashMap) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateLocation(com.cloud.storage.template.TemplateLocation) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) IOException(java.io.IOException) InternalErrorException(com.cloud.exception.InternalErrorException) Date(java.util.Date) QemuImg(org.apache.cloudstack.utils.qemu.QemuImg) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 100 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class LibvirtCreateCommandWrapper method execute.

@Override
public Answer execute(final CreateCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final StorageFilerTO pool = command.getPool();
    final DiskProfile dskch = command.getDiskCharacteristics();
    KVMPhysicalDisk baseVol = null;
    KVMStoragePool primaryPool = null;
    KVMPhysicalDisk vol = null;
    long disksize;
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
        disksize = dskch.getSize();
        if (command.getTemplateUrl() != null) {
            if (primaryPool.getType() == StoragePoolType.CLVM) {
                vol = libvirtComputingResource.templateToPrimaryDownload(command.getTemplateUrl(), primaryPool, dskch.getPath());
            } else {
                baseVol = primaryPool.getPhysicalDisk(command.getTemplateUrl());
                vol = storagePoolMgr.createDiskFromTemplate(baseVol, dskch.getPath(), dskch.getProvisioningType(), primaryPool, 0);
            }
            if (vol == null) {
                return new Answer(command, false, " Can't create storage volume on storage pool");
            }
        } else {
            vol = primaryPool.createPhysicalDisk(dskch.getPath(), dskch.getProvisioningType(), dskch.getSize());
            if (vol == null) {
                return new Answer(command, false, " Can't create Physical Disk");
            }
        }
        final VolumeTO volume = new VolumeTO(command.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), pool.getPath(), vol.getName(), vol.getName(), disksize, null);
        volume.setBytesReadRate(dskch.getBytesReadRate());
        volume.setBytesWriteRate(dskch.getBytesWriteRate());
        volume.setIopsReadRate(dskch.getIopsReadRate());
        volume.setIopsWriteRate(dskch.getIopsWriteRate());
        volume.setCacheMode(dskch.getCacheMode());
        return new CreateAnswer(command, volume);
    } catch (final CloudRuntimeException e) {
        s_logger.debug("Failed to create volume: " + e.toString());
        return new CreateAnswer(command, e);
    }
}
Also used : CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) Answer(com.cloud.agent.api.Answer) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) VolumeTO(com.cloud.agent.api.to.VolumeTO) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) DiskProfile(com.cloud.vm.DiskProfile)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1279 PreparedStatement (java.sql.PreparedStatement)320 SQLException (java.sql.SQLException)320 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)236 ResultSet (java.sql.ResultSet)217 ArrayList (java.util.ArrayList)217 ConfigurationException (javax.naming.ConfigurationException)171 HashMap (java.util.HashMap)129 DB (com.cloud.utils.db.DB)118 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)115 IOException (java.io.IOException)95 HostVO (com.cloud.host.HostVO)94 Answer (com.cloud.agent.api.Answer)84 Account (com.cloud.user.Account)84 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)82 URISyntaxException (java.net.URISyntaxException)82 ActionEvent (com.cloud.event.ActionEvent)70 TransactionStatus (com.cloud.utils.db.TransactionStatus)65 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)63 InternalErrorException (com.cloud.exception.InternalErrorException)57