Package com.cloud.vm

Examples of com.cloud.vm.UserVmVO


            throws ResourceAllocationException {

        Account caller = getCaller();

        // check if VM exists
        UserVmVO userVmVo = _userVMDao.findById(vmId);
        if (userVmVo == null) {
            throw new InvalidParameterValueException("Creating VM snapshot failed due to VM:" + vmId + " is a system VM or does not exist");
        }

        // check hypervisor capabilities
        if(!_hypervisorCapabilitiesDao.isVmSnapshotEnabled(userVmVo.getHypervisorType(), "default"))
            throw new InvalidParameterValueException("VM snapshot is not enabled for hypervisor type: " + userVmVo.getHypervisorType());

        // parameter length check
        if(vsDisplayName != null && vsDisplayName.length()>255)
            throw new InvalidParameterValueException("Creating VM snapshot failed due to length of VM snapshot vsDisplayName should not exceed 255");
        if(vsDescription != null && vsDescription.length()>255)
            throw new InvalidParameterValueException("Creating VM snapshot failed due to length of VM snapshot vsDescription should not exceed 255");
       
        // VM snapshot display name must be unique for a VM
        String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(), DateUtil.YYYYMMDD_FORMAT);
        String vmSnapshotName = userVmVo.getInstanceName() + "_VS_" + timeString;
        if (vsDisplayName == null) {
            vsDisplayName = vmSnapshotName;
        }
        if(_vmSnapshotDao.findByName(vmId,vsDisplayName) != null){
            throw new InvalidParameterValueException("Creating VM snapshot failed due to VM snapshot with name" + vsDisplayName + "  already exists");
        }
       
        // check VM state
        if (userVmVo.getState() != VirtualMachine.State.Running && userVmVo.getState() != VirtualMachine.State.Stopped) {
            throw new InvalidParameterValueException("Creating vm snapshot failed due to VM:" + vmId + " is not in the running or Stopped state");
        }
       
        if(snapshotMemory && userVmVo.getState() == VirtualMachine.State.Stopped){
            throw new InvalidParameterValueException("Can not snapshot memory when VM is in stopped state");
        }
       
        // for KVM, only allow snapshot with memory when VM is in running state
        if(userVmVo.getHypervisorType() == HypervisorType.KVM && userVmVo.getState() == State.Running && !snapshotMemory){
            throw new InvalidParameterValueException("KVM VM does not allow to take a disk-only snapshot when VM is in running state");
        }
       
        // check access
        _accountMgr.checkAccess(caller, null, true, userVmVo);

        // check max snapshot limit for per VM
        if (_vmSnapshotDao.findByVm(vmId).size() >= _vmSnapshotMax) {
            throw new CloudRuntimeException("Creating vm snapshot failed due to a VM can just have : " + _vmSnapshotMax
                    + " VM snapshots. Please delete old ones");
        }

        // check if there are active volume snapshots tasks
        List<VolumeVO> listVolumes = _volumeDao.findByInstance(vmId);
        for (VolumeVO volume : listVolumes) {
            List<SnapshotVO> activeSnapshots = _snapshotDao.listByInstanceId(volume.getInstanceId(), Snapshot.State.Creating,
                    Snapshot.State.CreatedOnPrimary, Snapshot.State.BackingUp);
            if (activeSnapshots.size() > 0) {
                throw new CloudRuntimeException(
                        "There is other active volume snapshot tasks on the instance to which the volume is attached, please try again later.");
            }
        }
       
        // check if there are other active VM snapshot tasks
        if (hasActiveVMSnapshotTasks(vmId)) {
            throw new CloudRuntimeException("There is other active vm snapshot tasks on the instance, please try again later");
        }
       
        VMSnapshot.Type vmSnapshotType = VMSnapshot.Type.Disk;
        if(snapshotMemory && userVmVo.getState() == VirtualMachine.State.Running)
            vmSnapshotType = VMSnapshot.Type.DiskAndMemory;
       
        try {
            VMSnapshotVO vmSnapshotVo = new VMSnapshotVO(userVmVo.getAccountId(), userVmVo.getDomainId(), vmId, vsDescription, vmSnapshotName,
                    vsDisplayName, userVmVo.getServiceOfferingId(), vmSnapshotType, null);
            VMSnapshot vmSnapshot = _vmSnapshotDao.persist(vmSnapshotVo);
            if (vmSnapshot == null) {
                throw new CloudRuntimeException("Failed to create snapshot for vm: " + vmId);
            }
            return vmSnapshot;
View Full Code Here


    }

    @Override
    @ActionEvent(eventType = EventTypes.EVENT_VM_SNAPSHOT_CREATE, eventDescription = "creating VM snapshot", async = true)
    public VMSnapshot creatVMSnapshot(Long vmId, Long vmSnapshotId) {
        UserVmVO userVm = _userVMDao.findById(vmId);
        if (userVm == null) {
            throw new InvalidParameterValueException("Create vm to snapshot failed due to vm: " + vmId + " is not found");
        }
        VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
        if(vmSnapshot == null){
View Full Code Here

        }
    }

    @DB
    protected boolean deleteSnapshotInternal(VMSnapshotVO vmSnapshot) {
        UserVmVO userVm = _userVMDao.findById(vmSnapshot.getVmId());
        DeleteVMSnapshotAnswer answer = null;
        try {
            vmSnapshotStateTransitTo(vmSnapshot,VMSnapshot.Event.ExpungeRequested);
            Long hostId = pickRunningHost(vmSnapshot.getVmId());

            // prepare snapshotVolumeTos
            List<VolumeTO> volumeTOs = getVolumeTOList(vmSnapshot.getVmId());
           
            // prepare DeleteVMSnapshotCommand
            String vmInstanceName = userVm.getInstanceName();
            VMSnapshotTO parent = getSnapshotWithParents(vmSnapshot).getParent();
            VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(vmSnapshot.getId(), vmSnapshot.getName(), vmSnapshot.getType(),
                    vmSnapshot.getCreated().getTime(), vmSnapshot.getDescription(), vmSnapshot.getCurrent(), parent);
            GuestOSVO guestOS = _guestOSDao.findById(userVm.getGuestOSId());
            DeleteVMSnapshotCommand deleteSnapshotCommand = new DeleteVMSnapshotCommand(vmInstanceName, vmSnapshotTO, volumeTOs,guestOS.getDisplayName());
           
            answer = (DeleteVMSnapshotAnswer) sendToPool(hostId, deleteSnapshotCommand);
          
            if (answer != null && answer.getResult()) {
                processAnswer(vmSnapshot, userVm, answer, hostId);
                s_logger.debug("Delete VM snapshot " + vmSnapshot.getName() + " succeeded for vm: " + userVm.getInstanceName());
                return true;
            } else {
                s_logger.error("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + answer.getDetails());
                return false;
            }
        } catch (Exception e) {
            String msg = "Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + e.getMessage();
            s_logger.error(msg , e);
            throw new CloudRuntimeException(e.getMessage());
        } finally{
            if(answer != null && answer.getResult()){
                for (VolumeTO volumeTo : answer.getVolumeTOs()){
View Full Code Here

        if (vmSnapshotVo == null) {
            throw new InvalidParameterValueException(
                    "unable to find the vm snapshot with id " + vmSnapshotId);
        }
        Long vmId = vmSnapshotVo.getVmId();
        UserVmVO userVm = _userVMDao.findById(vmId);
        // check if VM exists
        if (userVm == null) {
            throw new InvalidParameterValueException("Revert vm to snapshot: "
                    + vmSnapshotId + " failed due to vm: " + vmId
                    + " is not found");
        }
       
        // check if there are other active VM snapshot tasks
        if (hasActiveVMSnapshotTasks(vmId)) {
            throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
        }
       
        Account caller = getCaller();
        _accountMgr.checkAccess(caller, null, true, vmSnapshotVo);

        // VM should be in running or stopped states
        if (userVm.getState() != VirtualMachine.State.Running
                && userVm.getState() != VirtualMachine.State.Stopped) {
            throw new InvalidParameterValueException(
                    "VM Snapshot reverting failed due to vm is not in the state of Running or Stopped.");
        }
       
        // if snapshot is not created, error out
        if (vmSnapshotVo.getState() != VMSnapshot.State.Ready) {
            throw new InvalidParameterValueException(
                    "VM Snapshot reverting failed due to vm snapshot is not in the state of Created.");
        }

        UserVO callerUser = _userDao.findById(UserContext.current().getCallerUserId());
       
        UserVmVO vm = null;
        Long hostId = null;
        Account owner = _accountDao.findById(vmSnapshotVo.getAccountId());
       
        // start or stop VM first, if revert from stopped state to running state, or from running to stopped
        if(userVm.getState() == VirtualMachine.State.Stopped && vmSnapshotVo.getType() == VMSnapshot.Type.DiskAndMemory){
            try {
                vm = _itMgr.advanceStart(userVm, new HashMap<VirtualMachineProfile.Param, Object>(), callerUser, owner);
                hostId = vm.getHostId();
            } catch (Exception e) {
                s_logger.error("Start VM " + userVm.getInstanceName() + " before reverting failed due to " + e.getMessage());
                throw new CloudRuntimeException(e.getMessage());
            }
        }else {
View Full Code Here

        VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(id);
        return vmSnapshot;
    }

    protected Long pickRunningHost(Long vmId) {
        UserVmVO vm = _userVMDao.findById(vmId);
        // use VM's host if VM is running
        if(vm.getState() == State.Running)
            return vm.getHostId();
       
        // check if lastHostId is available
        if(vm.getLastHostId() != null){
           HostVO lastHost =  _hostDao.findById(vm.getLastHostId());
           if(lastHost.getStatus() == com.cloud.host.Status.Up && !lastHost.isInMaintenanceStates())
               return lastHost.getId();
        }
       
        List<VolumeVO> listVolumes = _volumeDao.findByInstance(vmId);
View Full Code Here

        VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(id);
        if(vmSnapshot == null){
            throw new InvalidParameterValueException("unable to find the vm snapshot with id " + id);
        }
        Long vmId = vmSnapshot.getVmId();
        UserVmVO vm = _userVMDao.findById(vmId);
        return vm;
    }
View Full Code Here

    @Override
    public boolean syncVMSnapshot(VMInstanceVO vm, Long hostId) {
        try{
           
            UserVmVO userVm = _userVMDao.findById(vm.getId());
            if(userVm == null)
                return false;
           
            List<VMSnapshotVO> vmSnapshotsInExpungingStates = _vmSnapshotDao.listByInstanceId(vm.getId(), VMSnapshot.State.Expunging, VMSnapshot.State.Reverting, VMSnapshot.State.Creating);
            for (VMSnapshotVO vmSnapshotVO : vmSnapshotsInExpungingStates) {
View Full Code Here

        return getServiceByType(BaremetalPxeType.KICK_START.toString()).listPxeServers(cmd);
    }

    @Override
    public boolean addUserData(NicProfile nic, VirtualMachineProfile<UserVm> profile) {
        UserVmVO vm = (UserVmVO) profile.getVirtualMachine();
        _vmDao.loadDetails(vm);
       
        String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getServiceOfferingId()).getDisplayText();
        String zoneName = _dcDao.findById(vm.getDataCenterId()).getName();
        NicVO nvo = _nicDao.findById(nic.getId());
        VmDataCommand cmd = new VmDataCommand(nvo.getIp4Address(), vm.getInstanceName(), _ntwkModel.getExecuteInSeqNtwkElmtCmd());
        cmd.addVmData("userdata", "user-data", vm.getUserData());
        cmd.addVmData("metadata", "service-offering", StringUtils.unicodeEscape(serviceOffering));
        cmd.addVmData("metadata", "availability-zone", StringUtils.unicodeEscape(zoneName));
        cmd.addVmData("metadata", "local-ipv4", nic.getIp4Address());
        cmd.addVmData("metadata", "local-hostname", StringUtils.unicodeEscape(vm.getInstanceName()));
        cmd.addVmData("metadata", "public-ipv4", nic.getIp4Address());
        cmd.addVmData("metadata", "public-hostname",  StringUtils.unicodeEscape(vm.getInstanceName()));
        cmd.addVmData("metadata", "instance-id", String.valueOf(vm.getId()));
        cmd.addVmData("metadata", "vm-id", String.valueOf(vm.getInstanceName()));
        cmd.addVmData("metadata", "public-keys", null);
        String cloudIdentifier = _configDao.getValue("cloud.identifier");
        if (cloudIdentifier == null) {
            cloudIdentifier = "";
        } else {
            cloudIdentifier = "CloudStack-{" + cloudIdentifier + "}";
        }
        cmd.addVmData("metadata", "cloud-identifier", cloudIdentifier);
       
        List<PhysicalNetworkVO> phys = _phynwDao.listByZone(vm.getDataCenterId());
        if (phys.isEmpty()) {
            throw new CloudRuntimeException(String.format("Cannot find physical network in zone %s", vm.getDataCenterId()));
        }
        if (phys.size() > 1) {
            throw new CloudRuntimeException(String.format("Baremetal only supports one physical network in zone, but zone %s has %s physical networks", vm.getDataCenterId(), phys.size()));
        }
        PhysicalNetworkVO phy = phys.get(0);
       
        SearchCriteriaService<BaremetalPxeVO, BaremetalPxeVO> sc = SearchCriteria2.create(BaremetalPxeVO.class);
        //TODO: handle both kickstart and PING
        //sc.addAnd(sc.getEntity().getPodId(), Op.EQ, vm.getPodIdToDeployIn());
        sc.addAnd(sc.getEntity().getPhysicalNetworkId(), Op.EQ, phy.getId());
        BaremetalPxeVO pxeVo = sc.find();
        if (pxeVo == null) {
            throw new CloudRuntimeException("No PXE server found in pod: " + vm.getPodIdToDeployIn() + ", you need to add it before starting VM");
        }
       
        try {
            Answer ans = _agentMgr.send(pxeVo.getHostId(), cmd);
            if (!ans.getResult()) {
                s_logger.debug(String.format("Add userdata to vm:%s failed because %s", vm.getInstanceName(), ans.getDetails()));
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            s_logger.debug(String.format("Add userdata to vm:%s failed", vm.getInstanceName()), e);
            return false;
        }
    }
View Full Code Here

    }

    private void initializeForTest(VirtualMachineProfileImpl<VMInstanceVO> vmProfile, DataCenterDeployment plan) {
        DataCenterVO mockDc = mock(DataCenterVO.class);
        VMInstanceVO vm = mock(VMInstanceVO.class);
        UserVmVO userVm = mock(UserVmVO.class);
        ServiceOfferingVO offering = mock(ServiceOfferingVO.class);

        AccountVO account = mock(AccountVO.class);
        when(account.getId()).thenReturn(accountId);
        when(account.getAccountId()).thenReturn(accountId);
        when(vmProfile.getOwner()).thenReturn(account);
        when(vmProfile.getVirtualMachine()).thenReturn(vm);
        when(vmProfile.getId()).thenReturn(12L);
        when(vmDao.findById(12L)).thenReturn(userVm);
        when(userVm.getAccountId()).thenReturn(accountId);

        when(vm.getDataCenterId()).thenReturn(dataCenterId);
        when(dcDao.findById(1L)).thenReturn(mockDc);
        when(plan.getDataCenterId()).thenReturn(dataCenterId);
        when(plan.getClusterId()).thenReturn(null);
View Full Code Here

        when(resourceMgr.listAllHostsInCluster(3)).thenReturn(hostsInCluster3);

        // Mock vms on each host.
        long offeringIdForVmsOfThisAccount = 15L;
        long offeringIdForVmsOfOtherAccount = 16L;
        UserVmVO vm1 = mock(UserVmVO.class);
        when(vm1.getAccountId()).thenReturn(accountId);
        when(vm1.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
        UserVmVO vm2 = mock(UserVmVO.class);
        when(vm2.getAccountId()).thenReturn(accountId);
        when(vm2.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
        // Vm from different account
        UserVmVO vm3 = mock(UserVmVO.class);
        when(vm3.getAccountId()).thenReturn(201L);
        when(vm3.getServiceOfferingId()).thenReturn(offeringIdForVmsOfOtherAccount);
        List<VMInstanceVO> vmsForHost1 = new ArrayList<VMInstanceVO>();
        List<VMInstanceVO> vmsForHost2 = new ArrayList<VMInstanceVO>();
        List<VMInstanceVO> vmsForHost3 = new ArrayList<VMInstanceVO>();
        List<VMInstanceVO> stoppedVmsForHost = new ArrayList<VMInstanceVO>();
        // Host 2 is empty.
View Full Code Here

TOP

Related Classes of com.cloud.vm.UserVmVO

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.