Search in sources :

Example 41 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class VmwareStorageManagerImpl method createTemplateFromSnapshot.

private Ternary<String, Long, Long> createTemplateFromSnapshot(long accountId, long templateId, String templateUniqueName, String secStorageUrl, long volumeId, String backedUpSnapshotUuid, Integer nfsVersion) throws Exception {
    String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
    String installPath = getTemplateRelativeDirInSecStorage(accountId, templateId);
    String installFullPath = secondaryMountPoint + "/" + installPath;
    //Note: volss for tmpl
    String installFullOVAName = installFullPath + "/" + templateUniqueName + ".ova";
    String snapshotRoot = secondaryMountPoint + "/" + getSnapshotRelativeDirInSecStorage(accountId, volumeId);
    String snapshotFullOVAName = snapshotRoot + "/" + backedUpSnapshotUuid + ".ova";
    String snapshotFullOvfName = snapshotRoot + "/" + backedUpSnapshotUuid + ".ovf";
    String result;
    Script command;
    String templateVMDKName = "";
    //String snapshotFullVMDKName = snapshotRoot + "/";
    // the backedUpSnapshotUuid field currently has the format: uuid/uuid. so we need to extract the uuid out
    String backupSSUuid = backedUpSnapshotUuid.substring(0, backedUpSnapshotUuid.indexOf('/'));
    String snapshotFullVMDKName = snapshotRoot + "/" + backupSSUuid + "/";
    synchronized (installPath.intern()) {
        command = new Script(false, "mkdir", _timeout, s_logger);
        command.add("-p");
        command.add(installFullPath);
        result = command.execute();
        if (result != null) {
            String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    try {
        if (new File(snapshotFullOVAName).exists()) {
            command = new Script(false, "cp", _timeout, s_logger);
            command.add(snapshotFullOVAName);
            command.add(installFullOVAName);
            result = command.execute();
            if (result != null) {
                String msg = "unable to copy snapshot " + snapshotFullOVAName + " to " + installFullPath;
                s_logger.error(msg);
                throw new Exception(msg);
            }
            // untar OVA file at template directory
            command = new Script("tar", 0, s_logger);
            command.add("--no-same-owner");
            command.add("-xf", installFullOVAName);
            command.setWorkDir(installFullPath);
            s_logger.info("Executing command: " + command.toString());
            result = command.execute();
            if (result != null) {
                String msg = "unable to untar snapshot " + snapshotFullOVAName + " to " + installFullPath;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            // there is no ova file, only ovf originally;
            if (new File(snapshotFullOvfName).exists()) {
                command = new Script(false, "cp", _timeout, s_logger);
                command.add(snapshotFullOvfName);
                //command.add(installFullOvfName);
                command.add(installFullPath);
                result = command.execute();
                if (result != null) {
                    String msg = "unable to copy snapshot " + snapshotFullOvfName + " to " + installFullPath;
                    s_logger.error(msg);
                    throw new Exception(msg);
                }
                s_logger.info("vmdkfile parent dir: " + snapshotFullVMDKName);
                File snapshotdir = new File(snapshotFullVMDKName);
                // File snapshotdir = new File(snapshotRoot);
                File[] ssfiles = snapshotdir.listFiles();
                // List<String> filenames = new ArrayList<String>();
                for (int i = 0; i < ssfiles.length; i++) {
                    String vmdkfile = ssfiles[i].getName();
                    s_logger.info("vmdk file name: " + vmdkfile);
                    if (vmdkfile.toLowerCase().startsWith(backupSSUuid) && vmdkfile.toLowerCase().endsWith(".vmdk")) {
                        snapshotFullVMDKName += vmdkfile;
                        templateVMDKName += vmdkfile;
                        break;
                    }
                }
                if (snapshotFullVMDKName != null) {
                    command = new Script(false, "cp", _timeout, s_logger);
                    command.add(snapshotFullVMDKName);
                    command.add(installFullPath);
                    result = command.execute();
                    s_logger.info("Copy VMDK file: " + snapshotFullVMDKName);
                    if (result != null) {
                        String msg = "unable to copy snapshot vmdk file " + snapshotFullVMDKName + " to " + installFullPath;
                        s_logger.error(msg);
                        throw new Exception(msg);
                    }
                }
            } else {
                String msg = "unable to find any snapshot ova/ovf files" + snapshotFullOVAName + " to " + installFullPath;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
        OVAProcessor processor = new OVAProcessor();
        // long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        processor.configure("OVA Processor", params);
        long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
        postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
        writeMetaOvaForTemplate(installFullPath, backedUpSnapshotUuid + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } catch (Exception e) {
        // TODO, clean up left over files
        throw e;
    }
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) Ternary(com.cloud.utils.Ternary) File(java.io.File) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 42 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class Site2SiteVpnManagerImpl method searchForVpnGateways.

@Override
public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(ListVpnGatewaysCmd cmd) {
    Long id = cmd.getId();
    Long vpcId = cmd.getVpcId();
    Boolean display = cmd.getDisplay();
    Long domainId = cmd.getDomainId();
    boolean isRecursive = cmd.isRecursive();
    String accountName = cmd.getAccountName();
    boolean listAll = cmd.listAll();
    long startIndex = cmd.getStartIndex();
    long pageSizeVal = cmd.getPageSizeVal();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
    _accountMgr.buildACLSearchParameters(caller, id, accountName, cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    domainId = domainIdRecursiveListProject.first();
    isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(Site2SiteVpnGatewayVO.class, "id", false, startIndex, pageSizeVal);
    SearchBuilder<Site2SiteVpnGatewayVO> sb = _vpnGatewayDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
    SearchCriteria<Site2SiteVpnGatewayVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (vpcId != null) {
        sc.addAnd("vpcId", SearchCriteria.Op.EQ, vpcId);
    }
    Pair<List<Site2SiteVpnGatewayVO>, Integer> result = _vpnGatewayDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends Site2SiteVpnGateway>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) Site2SiteVpnGatewayVO(com.cloud.network.dao.Site2SiteVpnGatewayVO) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 43 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class Site2SiteVpnManagerImpl method searchForVpnConnections.

@Override
public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnections(ListVpnConnectionsCmd cmd) {
    Long id = cmd.getId();
    Long vpcId = cmd.getVpcId();
    Boolean display = cmd.getDisplay();
    Long domainId = cmd.getDomainId();
    boolean isRecursive = cmd.isRecursive();
    String accountName = cmd.getAccountName();
    boolean listAll = cmd.listAll();
    long startIndex = cmd.getStartIndex();
    long pageSizeVal = cmd.getPageSizeVal();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
    _accountMgr.buildACLSearchParameters(caller, id, accountName, cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    domainId = domainIdRecursiveListProject.first();
    isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(Site2SiteVpnConnectionVO.class, "id", false, startIndex, pageSizeVal);
    SearchBuilder<Site2SiteVpnConnectionVO> sb = _vpnConnectionDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
    if (vpcId != null) {
        SearchBuilder<Site2SiteVpnGatewayVO> gwSearch = _vpnGatewayDao.createSearchBuilder();
        gwSearch.and("vpcId", gwSearch.entity().getVpcId(), SearchCriteria.Op.EQ);
        sb.join("gwSearch", gwSearch, sb.entity().getVpnGatewayId(), gwSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    SearchCriteria<Site2SiteVpnConnectionVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (vpcId != null) {
        sc.setJoinParameters("gwSearch", "vpcId", vpcId);
    }
    Pair<List<Site2SiteVpnConnectionVO>, Integer> result = _vpnConnectionDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends Site2SiteVpnConnection>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) Site2SiteVpnGatewayVO(com.cloud.network.dao.Site2SiteVpnGatewayVO) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 44 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class LibvirtComputingResource method cleanVMSnapshotMetadata.

public List<Ternary<String, Boolean, String>> cleanVMSnapshotMetadata(Domain dm) throws LibvirtException {
    s_logger.debug("Cleaning the metadata of vm snapshots of vm " + dm.getName());
    List<Ternary<String, Boolean, String>> vmsnapshots = new ArrayList<Ternary<String, Boolean, String>>();
    if (dm.snapshotNum() == 0) {
        return vmsnapshots;
    }
    String currentSnapshotName = null;
    try {
        DomainSnapshot snapshotCurrent = dm.snapshotCurrent();
        String snapshotXML = snapshotCurrent.getXMLDesc();
        snapshotCurrent.free();
        DocumentBuilder builder;
        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(snapshotXML));
            Document doc = builder.parse(is);
            Element rootElement = doc.getDocumentElement();
            currentSnapshotName = getTagValue("name", rootElement);
        } catch (ParserConfigurationException e) {
            s_logger.debug(e.toString());
        } catch (SAXException e) {
            s_logger.debug(e.toString());
        } catch (IOException e) {
            s_logger.debug(e.toString());
        }
    } catch (LibvirtException e) {
        s_logger.debug("Fail to get the current vm snapshot for vm: " + dm.getName() + ", continue");
    }
    // VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY = 2
    int flags = 2;
    String[] snapshotNames = dm.snapshotListNames();
    Arrays.sort(snapshotNames);
    for (String snapshotName : snapshotNames) {
        DomainSnapshot snapshot = dm.snapshotLookupByName(snapshotName);
        Boolean isCurrent = (currentSnapshotName != null && currentSnapshotName.equals(snapshotName)) ? true : false;
        vmsnapshots.add(new Ternary<String, Boolean, String>(snapshotName, isCurrent, snapshot.getXMLDesc()));
    }
    for (String snapshotName : snapshotNames) {
        DomainSnapshot snapshot = dm.snapshotLookupByName(snapshotName);
        // clean metadata of vm snapshot
        snapshot.delete(flags);
    }
    return vmsnapshots;
}
Also used : InputSource(org.xml.sax.InputSource) LibvirtException(org.libvirt.LibvirtException) Ternary(com.cloud.utils.Ternary) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DomainSnapshot(org.libvirt.DomainSnapshot) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 45 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class LibvirtMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = command.getVmName();
    String result = null;
    List<InterfaceDef> ifaces = null;
    List<DiskDef> disks = null;
    Domain dm = null;
    Connect dconn = null;
    Domain destDomain = null;
    Connect conn = null;
    String xmlDesc = null;
    List<Ternary<String, Boolean, String>> vmsnapshots = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        ifaces = libvirtComputingResource.getInterfaces(conn, vmName);
        disks = libvirtComputingResource.getDisks(conn, vmName);
        dm = conn.domainLookupByName(vmName);
        /*
                We replace the private IP address with the address of the destination host.
                This is because the VNC listens on the private IP address of the hypervisor,
                but that address is ofcourse different on the target host.

                MigrateCommand.getDestinationIp() returns the private IP address of the target
                hypervisor. So it's safe to use.

                The Domain.migrate method from libvirt supports passing a different XML
                description for the instance to be used on the target host.

                This is supported by libvirt-java from version 0.50.0

                CVE-2015-3252: Get XML with sensitive information suitable for migration by using
                               VIR_DOMAIN_XML_MIGRATABLE flag (value = 8)
                               https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainXMLFlags

                               Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
             */
        // 1000000 equals v1.0.0
        final int xmlFlag = conn.getLibVirVersion() >= 1000000 ? 8 : 1;
        final String target = command.getDestinationIp();
        xmlDesc = dm.getXMLDesc(xmlFlag);
        xmlDesc = replaceIpForVNCInDescFile(xmlDesc, target);
        // delete the metadata of vm snapshots before migration
        vmsnapshots = libvirtComputingResource.cleanVMSnapshotMetadata(dm);
        dconn = libvirtUtilitiesHelper.retrieveQemuConnection("qemu+tcp://" + command.getDestinationIp() + "/system");
        //run migration in thread so we can monitor it
        s_logger.info("Live migration of instance " + vmName + " initiated");
        final ExecutorService executor = Executors.newFixedThreadPool(1);
        final Callable<Domain> worker = new MigrateKVMAsync(libvirtComputingResource, dm, dconn, xmlDesc, vmName, command.getDestinationIp());
        final Future<Domain> migrateThread = executor.submit(worker);
        executor.shutdown();
        long sleeptime = 0;
        while (!executor.isTerminated()) {
            Thread.sleep(100);
            sleeptime += 100;
            if (sleeptime == 1000) {
                // wait 1s before attempting to set downtime on migration, since I don't know of a VIR_DOMAIN_MIGRATING state
                final int migrateDowntime = libvirtComputingResource.getMigrateDowntime();
                if (migrateDowntime > 0) {
                    try {
                        final int setDowntime = dm.migrateSetMaxDowntime(migrateDowntime);
                        if (setDowntime == 0) {
                            s_logger.debug("Set max downtime for migration of " + vmName + " to " + String.valueOf(migrateDowntime) + "ms");
                        }
                    } catch (final LibvirtException e) {
                        s_logger.debug("Failed to set max downtime for migration, perhaps migration completed? Error: " + e.getMessage());
                    }
                }
            }
            if (sleeptime % 1000 == 0) {
                s_logger.info("Waiting for migration of " + vmName + " to complete, waited " + sleeptime + "ms");
            }
            // pause vm if we meet the vm.migrate.pauseafter threshold and not already paused
            final int migratePauseAfter = libvirtComputingResource.getMigratePauseAfter();
            if (migratePauseAfter > 0 && sleeptime > migratePauseAfter && dm.getInfo().state == DomainState.VIR_DOMAIN_RUNNING) {
                s_logger.info("Pausing VM " + vmName + " due to property vm.migrate.pauseafter setting to " + migratePauseAfter + "ms to complete migration");
                try {
                    dm.suspend();
                } catch (final LibvirtException e) {
                    // pause could be racy if it attempts to pause right when vm is finished, simply warn
                    s_logger.info("Failed to pause vm " + vmName + " : " + e.getMessage());
                }
            }
        }
        s_logger.info("Migration thread for " + vmName + " is done");
        destDomain = migrateThread.get(10, TimeUnit.SECONDS);
        if (destDomain != null) {
            for (final DiskDef disk : disks) {
                libvirtComputingResource.cleanupDisk(disk);
            }
        }
    } catch (final LibvirtException e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } catch (final InterruptedException e) {
        s_logger.debug("Interrupted while migrating domain: " + e.getMessage());
        result = e.getMessage();
    } catch (final ExecutionException e) {
        s_logger.debug("Failed to execute while migrating domain: " + e.getMessage());
        result = e.getMessage();
    } catch (final TimeoutException e) {
        s_logger.debug("Timed out while migrating domain: " + e.getMessage());
        result = e.getMessage();
    } finally {
        try {
            if (dm != null && result != null) {
                // restore vm snapshots in case of failed migration
                if (vmsnapshots != null) {
                    libvirtComputingResource.restoreVMSnapshotMetadata(dm, vmName, vmsnapshots);
                }
            }
            if (dm != null) {
                if (dm.isPersistent() == 1) {
                    dm.undefine();
                }
                dm.free();
            }
            if (dconn != null) {
                dconn.close();
            }
            if (destDomain != null) {
                destDomain.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
    if (result != null) {
    } else {
        libvirtComputingResource.destroyNetworkRulesForVM(conn, vmName);
        for (final InterfaceDef iface : ifaces) {
            // We don't know which "traffic type" is associated with
            // each interface at this point, so inform all vif drivers
            final List<VifDriver> allVifDrivers = libvirtComputingResource.getAllVifDrivers();
            for (final VifDriver vifDriver : allVifDrivers) {
                vifDriver.unplug(iface);
            }
        }
    }
    return new MigrateAnswer(command, result == null, result, null);
}
Also used : LibvirtException(org.libvirt.LibvirtException) Ternary(com.cloud.utils.Ternary) Connect(org.libvirt.Connect) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) MigrateKVMAsync(com.cloud.hypervisor.kvm.resource.MigrateKVMAsync) ExecutorService(java.util.concurrent.ExecutorService) Domain(org.libvirt.Domain) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Ternary (com.cloud.utils.Ternary)53 ArrayList (java.util.ArrayList)40 Account (com.cloud.user.Account)34 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)32 Filter (com.cloud.utils.db.Filter)30 List (java.util.List)29 Pair (com.cloud.utils.Pair)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)15 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)13 ResourceTagVO (com.cloud.tags.ResourceTagVO)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 IPAddressVO (com.cloud.network.dao.IPAddressVO)6 HashMap (java.util.HashMap)6 OVAProcessor (com.cloud.storage.template.OVAProcessor)4 Script (com.cloud.utils.script.Script)4 File (java.io.File)4 RemoteException (java.rmi.RemoteException)4 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)3 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)3 SSHKeyPair (com.cloud.user.SSHKeyPair)3