Search in sources :

Example 1 with Ternary

use of com.cloud.utils.Ternary in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method backupCurrentSnapshot.

// destName does not contain extension name
public void backupCurrentSnapshot(String deviceName, ManagedObjectReference morDestDs, String destDsDirectory, String destName, boolean includeBase) throws Exception {
    SnapshotDescriptor descriptor = getSnapshotDescriptor();
    SnapshotInfo[] snapshotInfo = descriptor.getCurrentDiskChain();
    if (snapshotInfo.length == 0) {
        String msg = "No snapshot found in this VM";
        throw new Exception(msg);
    }
    HostMO hostMo = getRunningHost();
    DatacenterMO dcMo = getOwnerDatacenter().first();
    List<Pair<ManagedObjectReference, String>> mounts = hostMo.getDatastoreMountsOnHost();
    VirtualMachineFileInfo vmFileInfo = getFileInfo();
    List<Ternary<String, String, String>> backupInfo = new ArrayList<Ternary<String, String, String>>();
    for (int i = 0; i < snapshotInfo.length; i++) {
        if (!includeBase && i == snapshotInfo.length - 1) {
            break;
        }
        SnapshotDescriptor.DiskInfo[] disks = snapshotInfo[i].getDisks();
        if (disks != null) {
            String destBaseFileName;
            String destFileName;
            String destParentFileName;
            for (SnapshotDescriptor.DiskInfo disk : disks) {
                if (deviceName == null || deviceName.equals(disk.getDeviceName())) {
                    String srcVmdkFullDsPath = getSnapshotDiskFileDatastorePath(vmFileInfo, mounts, disk.getDiskFileName());
                    Pair<DatastoreMO, String> srcDsInfo = getOwnerDatastore(srcVmdkFullDsPath);
                    Pair<VmdkFileDescriptor, byte[]> vmdkInfo = getVmdkFileInfo(srcVmdkFullDsPath);
                    String srcVmdkBaseFilePath = DatastoreFile.getCompanionDatastorePath(srcVmdkFullDsPath, vmdkInfo.first().getBaseFileName());
                    destFileName = destName + (snapshotInfo.length - i - 1) + ".vmdk";
                    if (vmdkInfo.first().getParentFileName() != null) {
                        destBaseFileName = destName + (snapshotInfo.length - i - 1) + "-delta.vmdk";
                        destParentFileName = destName + (snapshotInfo.length - i - 2) + ".vmdk";
                    } else {
                        destBaseFileName = destName + (snapshotInfo.length - i - 1) + "-flat.vmdk";
                        destParentFileName = null;
                    }
                    s_logger.info("Copy VMDK base file " + srcVmdkBaseFilePath + " to " + destDsDirectory + "/" + destBaseFileName);
                    srcDsInfo.first().copyDatastoreFile(srcVmdkBaseFilePath, dcMo.getMor(), morDestDs, destDsDirectory + "/" + destBaseFileName, dcMo.getMor(), true);
                    byte[] newVmdkContent = VmdkFileDescriptor.changeVmdkContentBaseInfo(vmdkInfo.second(), destBaseFileName, destParentFileName);
                    String vmdkUploadUrl = getContext().composeDatastoreBrowseUrl(dcMo.getName(), destDsDirectory + "/" + destFileName);
                    s_logger.info("Upload VMDK content file to " + destDsDirectory + "/" + destFileName);
                    getContext().uploadResourceContent(vmdkUploadUrl, newVmdkContent);
                    backupInfo.add(new Ternary<String, String, String>(destFileName, destBaseFileName, destParentFileName));
                }
            }
        }
    }
    byte[] vdiskInfo = VmwareHelper.composeDiskInfo(backupInfo, snapshotInfo.length, includeBase);
    String vdiskUploadUrl = getContext().composeDatastoreBrowseUrl(dcMo.getName(), destDsDirectory + "/" + destName + ".vdisk");
    getContext().uploadResourceContent(vdiskUploadUrl, vdiskInfo);
}
Also used : Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) VirtualMachineSnapshotInfo(com.vmware.vim25.VirtualMachineSnapshotInfo) SnapshotInfo(com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Example 2 with Ternary

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

the class OvmResourceBase method execute.

protected Answer execute(PrepareOCFS2NodesCommand cmd) {
    List<Ternary<Integer, String, String>> nodes = cmd.getNodes();
    StringBuffer params = new StringBuffer();
    for (Ternary<Integer, String, String> node : nodes) {
        String param = String.format("%1$s:%2$s:%3$s", node.first(), node.second(), node.third());
        params.append(param);
        params.append(";");
    }
    try {
        OvmStoragePool.prepareOCFS2Nodes(_conn, cmd.getClusterName(), params.toString());
        return new Answer(cmd, true, "Success");
    } catch (XmlRpcException e) {
        s_logger.debug("OCFS2 prepare nodes failed", e);
        return new Answer(cmd, false, e.getMessage());
    }
}
Also used : FenceAnswer(com.cloud.agent.api.FenceAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CheckNetworkAnswer(com.cloud.agent.api.CheckNetworkAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) Ternary(com.cloud.utils.Ternary) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 3 with Ternary

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

the class VmwareStorageProcessor method createTemplateFromSnapshot.

private Ternary<String, Long, Long> createTemplateFromSnapshot(String installPath, String templateUniqueName, String secStorageUrl, String snapshotPath, Long templateId, long wait, Integer nfsVersion) throws Exception {
    //Snapshot path is decoded in this form: /snapshots/account/volumeId/uuid/uuid
    String backupSSUuid;
    String snapshotFolder;
    if (snapshotPath.endsWith(".ova")) {
        int index = snapshotPath.lastIndexOf(File.separator);
        backupSSUuid = snapshotPath.substring(index + 1).replace(".ova", "");
        snapshotFolder = snapshotPath.substring(0, index);
    } else {
        String[] tokens = snapshotPath.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
        backupSSUuid = tokens[tokens.length - 1];
        snapshotFolder = StringUtils.join(tokens, File.separator, 0, tokens.length - 1);
    }
    String secondaryMountPoint = mountService.getMountPoint(secStorageUrl, nfsVersion);
    String installFullPath = secondaryMountPoint + "/" + installPath;
    //Note: volss for tmpl
    String installFullOVAName = installFullPath + "/" + templateUniqueName + ".ova";
    String snapshotRoot = secondaryMountPoint + "/" + snapshotFolder;
    String snapshotFullOVAName = snapshotRoot + "/" + backupSSUuid + ".ova";
    String snapshotFullOvfName = snapshotRoot + "/" + backupSSUuid + ".ovf";
    String result;
    Script command;
    String templateVMDKName = "";
    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", wait, 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", wait, 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", wait, 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: " + snapshotRoot);
                File snapshotdir = new File(snapshotRoot);
                File[] ssfiles = snapshotdir.listFiles();
                if (ssfiles == null) {
                    String msg = "unable to find snapshot vmdk files in " + snapshotRoot;
                    s_logger.error(msg);
                    throw new Exception(msg);
                }
                // 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 = snapshotRoot + File.separator + vmdkfile;
                        templateVMDKName += vmdkfile;
                        break;
                    }
                }
                if (snapshotFullVMDKName != null) {
                    command = new Script(false, "cp", wait, 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, backupSSUuid + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } finally {
    // TODO, clean up left over files
    }
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) Ternary(com.cloud.utils.Ternary) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File)

Example 4 with Ternary

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

the class FirewallManagerImpl method listFirewallRules.

@Override
public Pair<List<? extends FirewallRule>, Integer> listFirewallRules(IListFirewallRulesCmd cmd) {
    Long ipId = cmd.getIpAddressId();
    Long id = cmd.getId();
    Long networkId = cmd.getNetworkId();
    Map<String, String> tags = cmd.getTags();
    FirewallRule.TrafficType trafficType = cmd.getTrafficType();
    Boolean display = cmd.getDisplay();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    if (ipId != null) {
        IPAddressVO ipAddressVO = _ipAddressDao.findById(ipId);
        if (ipAddressVO == null || !ipAddressVO.readyToUse()) {
            throw new InvalidParameterValueException("Ip address id=" + ipId + " not ready for firewall rules yet");
        }
        _accountMgr.checkAccess(caller, null, true, ipAddressVO);
    }
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter filter = new Filter(FirewallRuleVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<FirewallRuleVO> sb = _firewallDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), Op.EQ);
    sb.and("trafficType", sb.entity().getTrafficType(), Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), Op.EQ);
    sb.and("ip", sb.entity().getSourceIpAddressId(), Op.EQ);
    sb.and("purpose", sb.entity().getPurpose(), Op.EQ);
    sb.and("display", sb.entity().isDisplay(), Op.EQ);
    if (tags != null && !tags.isEmpty()) {
        SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
        for (int count = 0; count < tags.size(); count++) {
            tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    SearchCriteria<FirewallRuleVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (tags != null && !tags.isEmpty()) {
        int count = 0;
        sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.FirewallRule.toString());
        for (String key : tags.keySet()) {
            sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
            sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
            count++;
        }
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (ipId != null) {
        sc.setParameters("ip", ipId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    sc.setParameters("purpose", Purpose.Firewall);
    sc.setParameters("trafficType", trafficType);
    Pair<List<FirewallRuleVO>, Integer> result = _firewallDao.searchAndCount(sc, filter);
    return new Pair<List<? extends FirewallRule>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceTagVO(com.cloud.tags.ResourceTagVO) List(java.util.List) ArrayList(java.util.ArrayList) FirewallRule(com.cloud.network.rules.FirewallRule) Pair(com.cloud.utils.Pair) Ternary(com.cloud.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 5 with Ternary

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

the class VmwareStorageManagerImpl method createTemplateFromVolume.

private Ternary<String, Long, Long> createTemplateFromVolume(VirtualMachineMO vmMo, long accountId, long templateId, String templateUniqueName, String secStorageUrl, String volumePath, String workerVmName, Integer nfsVersion) throws Exception {
    String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
    String installPath = getTemplateRelativeDirInSecStorage(accountId, templateId);
    String installFullPath = secondaryMountPoint + "/" + installPath;
    synchronized (installPath.intern()) {
        Script command = new Script(false, "mkdir", _timeout, s_logger);
        command.add("-p");
        command.add(installFullPath);
        String 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);
        }
    }
    VirtualMachineMO clonedVm = null;
    try {
        Pair<VirtualDisk, String> volumeDeviceInfo = vmMo.getDiskDevice(volumePath);
        if (volumeDeviceInfo == null) {
            String msg = "Unable to find related disk device for volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        if (!vmMo.createSnapshot(templateUniqueName, "Temporary snapshot for template creation", false, false)) {
            String msg = "Unable to take snapshot for creating template from volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        // 4 MB is the minimum requirement for VM memory in VMware
        vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()));
        clonedVm = vmMo.getRunningHost().findVmOnHyperHost(workerVmName);
        if (clonedVm == null) {
            String msg = "Unable to create dummy VM to export volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        clonedVm.exportVm(secondaryMountPoint + "/" + installPath, templateUniqueName, true, false);
        long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
        OVAProcessor processor = new OVAProcessor();
        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);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } finally {
        if (clonedVm != null) {
            clonedVm.detachAllDisks();
            clonedVm.destroy();
        }
        vmMo.removeSnapshot(templateUniqueName, false);
    }
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) Ternary(com.cloud.utils.Ternary) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualDisk(com.vmware.vim25.VirtualDisk) File(java.io.File)

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