Search in sources :

Example 6 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cloudstack by apache.

the class LibvirtRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    String vmName = cmd.getVmName();
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
    Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    VirtualMachine.PowerState vmState = null;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
        if (snapshot == null)
            return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
        dm.revertToSnapshot(snapshot);
        snapshot.free();
        if (!snapshotMemory) {
            dm.destroy();
            if (dm.isPersistent() == 1)
                dm.undefine();
            vmState = VirtualMachine.PowerState.PowerOff;
        } else {
            vmState = VirtualMachine.PowerState.PowerOn;
        }
        return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (LibvirtException e) {
        String msg = " Revert to VM snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RevertToVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 7 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class CreateSnapshotFromVMSnapshotCmd method getHostId.

private Long getHostId() {
    final VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
    if (vmsnapshot == null) {
        throw new InvalidParameterValueException("Unable to find vm snapshot by id=" + getVMSnapshotId());
    }
    final UserVm vm = _entityMgr.findById(UserVm.class, vmsnapshot.getVmId());
    if (vm != null) {
        if (vm.getHostId() != null) {
            return vm.getHostId();
        } else if (vm.getLastHostId() != null) {
            return vm.getLastHostId();
        }
    }
    return null;
}
Also used : UserVm(com.cloud.uservm.UserVm) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Example 8 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class CreateSnapshotFromVMSnapshotCmd method getVmId.

private Long getVmId() {
    final VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
    if (vmsnapshot == null) {
        throw new InvalidParameterValueException("Unable to find vm snapshot by id=" + getVMSnapshotId());
    }
    final UserVm vm = _entityMgr.findById(UserVm.class, vmsnapshot.getVmId());
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find vm by vm snapshot id=" + getVMSnapshotId());
    }
    return vm.getId();
}
Also used : UserVm(com.cloud.uservm.UserVm) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Example 9 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class CreateSnapshotFromVMSnapshotCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    final VMSnapshot vmsnapshot = _entityMgr.findById(VMSnapshot.class, getVMSnapshotId());
    if (vmsnapshot == null) {
        throw new InvalidParameterValueException("Unable to find vmsnapshot by id=" + getVMSnapshotId());
    }
    final Account account = _accountService.getAccount(vmsnapshot.getAccountId());
    // Can create templates for enabled projects/accounts only
    if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        final Project project = _projectService.findByProjectAccountId(vmsnapshot.getAccountId());
        if (project == null) {
            throw new InvalidParameterValueException("Unable to find project by account id=" + account.getUuid());
        }
        if (project.getState() != Project.State.Active) {
            throw new PermissionDeniedException("Can't add resources to the project id=" + project.getUuid() + " in state=" + project.getState() + " as it's no longer active");
        }
    } else if (account.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of template is disabled: " + account);
    }
    return vmsnapshot.getAccountId();
}
Also used : Account(com.cloud.user.Account) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Example 10 with VMSnapshot

use of com.cloud.vm.snapshot.VMSnapshot in project cosmic by MissionCriticalCloud.

the class ListVMSnapshotCmd method execute.

@Override
public void execute() {
    final List<? extends VMSnapshot> result = _vmSnapshotService.listVMSnapshots(this);
    final ListResponse<VMSnapshotResponse> response = new ListResponse<>();
    final List<VMSnapshotResponse> snapshotResponses = new ArrayList<>();
    for (final VMSnapshot r : result) {
        final VMSnapshotResponse vmSnapshotResponse = _responseGenerator.createVMSnapshotResponse(r);
        vmSnapshotResponse.setObjectName("vmSnapshot");
        snapshotResponses.add(vmSnapshotResponse);
    }
    response.setResponses(snapshotResponses);
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : ListResponse(com.cloud.api.response.ListResponse) VMSnapshotResponse(com.cloud.api.response.VMSnapshotResponse) ArrayList(java.util.ArrayList) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot)

Aggregations

VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)7 UserVm (com.cloud.uservm.UserVm)6 ArrayList (java.util.ArrayList)6 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)5 UserVmVO (com.cloud.vm.UserVmVO)5 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)4 Project (com.cloud.projects.Project)4 VolumeVO (com.cloud.storage.VolumeVO)4 HashMap (java.util.HashMap)4 VMSnapshotResponse (com.cloud.api.response.VMSnapshotResponse)3 HostVO (com.cloud.host.HostVO)3 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)3 Account (com.cloud.user.Account)3 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)3 PowerState (com.cloud.vm.VirtualMachine.PowerState)3