Search in sources :

Example 71 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
    final String templateInstallFolder = "template/tmpl/" + templateFolder;
    final String tmplName = libvirtUtilitiesHelper.generateUUIDName();
    final String tmplFileName = tmplName + ".qcow2";
    KVMStoragePool secondaryPool = null;
    KVMStoragePool snapshotPool = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        String snapshotPath = command.getSnapshotUuid();
        final int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        snapshotPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
        secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl());
        final KVMPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName());
        final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(templatePath);
        final String tmplPath = templateInstallFolder + File.separator + tmplFileName;
        final String createTmplPath = libvirtComputingResource.createTmplPath();
        final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
        final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
        scriptCommand.add("-t", templatePath);
        scriptCommand.add("-n", tmplFileName);
        scriptCommand.add("-f", snapshot.getPath());
        scriptCommand.execute();
        final Processor qcow2Processor = libvirtUtilitiesHelper.buildQCOW2Processor(storage);
        final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
        final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath);
        loc.create(1, true, tmplName);
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } finally {
        if (secondaryPool != null) {
            storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
        if (snapshotPool != null) {
            storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid());
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 72 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class LibvirtComputingResource method prepareNetworkElementCommand.

public ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    Connect conn;
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final List<InterfaceDef> nics = getInterfaces(conn, routerName);
        final Map<String, Integer> broadcastUriAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (final InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                broadcastUriAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                if (nic.getBrName().equalsIgnoreCase(_publicBridgeName) || nic.getBrName().equalsIgnoreCase(_privBridgeName) || nic.getBrName().equalsIgnoreCase(_guestBridgeName)) {
                    broadcastUriAllocatedToVM.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), nicPos);
                } else {
                    final String broadcastUri = getBroadcastUriFromBridge(nic.getBrName());
                    broadcastUriAllocatedToVM.put(broadcastUri, nicPos);
                }
            }
            nicPos++;
        }
        final IpAddressTO[] ips = cmd.getIpAddresses();
        int nicNum = 0;
        for (final IpAddressTO ip : ips) {
            boolean newNic = false;
            if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
                broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
                newNic = true;
            }
            nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
            networkUsage(routerIp, "addVif", "eth" + nicNum);
            ip.setNicDevId(nicNum);
            ip.setNewNic(newNic);
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final InternalErrorException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)

Example 73 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class LibvirtAttachIsoCommandWrapper method execute.

@Override
public Answer execute(final AttachIsoCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        libvirtComputingResource.attachOrDetachISO(conn, command.getVmName(), command.getIsoPath(), command.isAttach());
    } catch (final LibvirtException e) {
        return new Answer(command, false, e.toString());
    } catch (final URISyntaxException e) {
        return new Answer(command, false, e.toString());
    } catch (final InternalErrorException e) {
        return new Answer(command, false, e.toString());
    }
    return new Answer(command);
}
Also used : Answer(com.cloud.agent.api.Answer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException)

Example 74 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class LibvirtComputingResource method rebootVM.

public String rebootVM(final Connect conn, final String vmName) {
    Domain dm = null;
    String msg = null;
    try {
        dm = conn.domainLookupByName(vmName);
        // Get XML Dump including the secure information such as VNC password
        // By passing 1, or VIR_DOMAIN_XML_SECURE flag
        // https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainXMLFlags
        String vmDef = dm.getXMLDesc(1);
        final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        parser.parseDomainXML(vmDef);
        for (final InterfaceDef nic : parser.getInterfaces()) {
            if (nic.getNetType() == GuestNetType.BRIDGE && nic.getBrName().startsWith("cloudVirBr")) {
                try {
                    final int vnetId = Integer.parseInt(nic.getBrName().replaceFirst("cloudVirBr", ""));
                    final String pifName = getPif(_guestBridgeName);
                    final String newBrName = "br" + pifName + "-" + vnetId;
                    vmDef = vmDef.replaceAll("'" + nic.getBrName() + "'", "'" + newBrName + "'");
                    s_logger.debug("VM bridge name is changed from " + nic.getBrName() + " to " + newBrName);
                } catch (final NumberFormatException e) {
                    continue;
                }
            }
        }
        s_logger.debug(vmDef);
        msg = stopVM(conn, vmName);
        msg = startVM(conn, vmName, vmDef);
        return null;
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to create vm", e);
        msg = e.getMessage();
    } catch (final InternalErrorException e) {
        s_logger.warn("Failed to create vm", e);
        msg = e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
    return msg;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain)

Example 75 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class ServerDBSyncImpl method createVirtualMachine.

public void createVirtualMachine(VMInstanceVO dbVm, StringBuffer syncLogMesg) throws IOException {
    syncLogMesg.append("VM# DB: " + dbVm.getInstanceName() + "/" + dbVm.getUuid() + "; VNC: none; action: create\n");
    VirtualMachineModel vmModel = new VirtualMachineModel(dbVm, dbVm.getUuid());
    vmModel.build(_manager.getModelController(), dbVm);
    buildNicResources(vmModel, dbVm, syncLogMesg);
    if (_rwMode) {
        try {
            vmModel.update(_manager.getModelController());
        } catch (InternalErrorException ex) {
            s_logger.warn("create virtual-machine", ex);
            return;
        }
        _manager.getDatabase().getVirtualMachines().add(vmModel);
        syncLogMesg.append("VM# VNC: " + dbVm.getUuid() + " created\n");
    }
}
Also used : VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel) InternalErrorException(com.cloud.exception.InternalErrorException)

Aggregations

InternalErrorException (com.cloud.exception.InternalErrorException)86 IOException (java.io.IOException)28 LibvirtException (org.libvirt.LibvirtException)27 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 Connect (org.libvirt.Connect)23 URISyntaxException (java.net.URISyntaxException)21 ConfigurationException (javax.naming.ConfigurationException)21 Answer (com.cloud.agent.api.Answer)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)16 NicTO (com.cloud.agent.api.to.NicTO)15 URI (java.net.URI)15 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 Script (com.cloud.utils.script.Script)14 Test (org.junit.Test)14 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)12 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)12 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 Processor (com.cloud.storage.template.Processor)11 TemplateLocation (com.cloud.storage.template.TemplateLocation)11