Search in sources :

Example 1 with VMSnapshot

use of com.cloud.legacymodel.storage.VMSnapshot in project cosmic by MissionCriticalCloud.

the class LibvirtRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    final PowerState vmState;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final 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);
        }
        final 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, VIR_DOMAIN_SNAPSHOT_REVERT_FORCE | VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING);
        snapshot.free();
        if (!snapshotMemory) {
            dm.destroy();
            if (dm.isPersistent() == 1) {
                dm.undefine();
            }
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (final LibvirtException e) {
        final 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 (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 2 with VMSnapshot

use of com.cloud.legacymodel.storage.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.legacymodel.storage.VMSnapshot)

Example 3 with VMSnapshot

use of com.cloud.legacymodel.storage.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.legacymodel.exceptions.InvalidParameterValueException) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot)

Example 4 with VMSnapshot

use of com.cloud.legacymodel.storage.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.legacymodel.user.Account) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot)

Example 5 with VMSnapshot

use of com.cloud.legacymodel.storage.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.legacymodel.exceptions.InvalidParameterValueException) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot)

Aggregations

VMSnapshot (com.cloud.legacymodel.storage.VMSnapshot)14 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)8 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)6 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)5 UserVmVO (com.cloud.vm.UserVmVO)5 Account (com.cloud.legacymodel.user.Account)4 ExecutionException (java.util.concurrent.ExecutionException)4 VMSnapshotResponse (com.cloud.api.response.VMSnapshotResponse)3 ActionEvent (com.cloud.event.ActionEvent)3 AsyncJobExecutionContext (com.cloud.framework.jobs.AsyncJobExecutionContext)3 VmWorkJobVO (com.cloud.framework.jobs.impl.VmWorkJobVO)3 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)3 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)3 UserVm (com.cloud.uservm.UserVm)3 RevertToVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer)2 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)2 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)2 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)2 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)2 PowerState (com.cloud.legacymodel.vm.VirtualMachine.PowerState)2