Search in sources :

Example 86 with UserVmVO

use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method attachIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_ATTACH, eventDescription = "attaching ISO", async = true)
public boolean attachIso(final long isoId, final long vmId) {
    final Account caller = CallContext.current().getCallingAccount();
    final Long userId = CallContext.current().getCallingUserId();
    // Verify input parameters
    final UserVmVO vm = _userVmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
    }
    final VMTemplateVO iso = _tmpltDao.findById(isoId);
    if (iso == null || iso.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find an ISO with id " + isoId);
    }
    // check permissions
    // check if caller has access to VM and ISO
    // and also check if the VM's owner has access to the ISO.
    _accountMgr.checkAccess(caller, null, false, iso, vm);
    final Account vmOwner = _accountDao.findById(vm.getAccountId());
    _accountMgr.checkAccess(vmOwner, null, false, iso, vm);
    final State vmState = vm.getState();
    if (vmState != State.Running && vmState != State.Stopped) {
        throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.");
    }
    if ("xen-pv-drv-iso".equals(iso.getDisplayText()) && vm.getHypervisorType() != Hypervisor.HypervisorType.XenServer) {
        throw new InvalidParameterValueException("Cannot attach Xenserver PV drivers to incompatible hypervisor " + vm.getHypervisorType());
    }
    final boolean result = attachISOToVM(vmId, userId, isoId, true);
    if (result) {
        return result;
    } else {
        throw new CloudRuntimeException("Failed to attach iso");
    }
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) State(com.cloud.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) ActionEvent(com.cloud.event.ActionEvent)

Example 87 with UserVmVO

use of com.cloud.vm.UserVmVO in project CloudStack-archive by CloudStack-extras.

the class UserVmDaoImplTest method testPersist.

public void testPersist() {
    UserVmDao dao = ComponentLocator.inject(UserVmDaoImpl.class);
    dao.expunge(1000l);
    UserVmVO vo = new UserVmVO(1000l, "instancename", "displayname", 1, HypervisorType.XenServer, 1, true, true, 1, 1, 1, "userdata", "name");
    dao.persist(vo);
    vo = dao.findById(1000l);
    assert (vo.getType() == VirtualMachine.Type.User) : "Incorrect type " + vo.getType();
}
Also used : UserVmVO(com.cloud.vm.UserVmVO)

Example 88 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class TemplateManagerImpl method templateIsDeleteable.

@Override
public boolean templateIsDeleteable(VMTemplateHostVO templateHostRef) {
    VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templateHostRef.getTemplateId());
    long templateId = template.getId();
    HostVO secondaryStorageHost = _hostDao.findById(templateHostRef.getHostId());
    long zoneId = secondaryStorageHost.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    // Check if there are VMs running in the template host ref's zone that
    // use the template
    List<VMInstanceVO> nonExpungedVms = _vmInstanceDao.listNonExpungedByZoneAndTemplate(zoneId, templateId);
    if (!nonExpungedVms.isEmpty()) {
        s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are non-expunged VMs deployed from this template.");
        return false;
    }
    List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(templateId);
    // check if there is any VM using this ISO.
    if (!userVmUsingIso.isEmpty()) {
        s_logger.debug("ISO " + template.getName() + " in zone " + zone.getName() + " is not deleteable because it is attached to " + userVmUsingIso.size() + " VMs");
        return false;
    }
    // Check if there are any snapshots for the template in the template
    // host ref's zone
    List<VolumeVO> volumes = _volumeDao.findByTemplateAndZone(templateId, zoneId);
    for (VolumeVO volume : volumes) {
        List<SnapshotVO> snapshots = _snapshotDao.listByVolumeIdVersion(volume.getId(), "2.1");
        if (!snapshots.isEmpty()) {
            s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are 2.1 snapshots using this template.");
            return false;
        }
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) UserVmVO(com.cloud.vm.UserVmVO) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) VMTemplateHostVO(com.cloud.storage.VMTemplateHostVO) HostVO(com.cloud.host.HostVO)

Example 89 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class TemplateManagerImpl method attachIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_ATTACH, eventDescription = "attaching ISO", async = true)
public boolean attachIso(long isoId, long vmId) {
    Account caller = CallContext.current().getCallingAccount();
    Long userId = CallContext.current().getCallingUserId();
    // Verify input parameters
    UserVmVO vm = _userVmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
    }
    VMTemplateVO iso = _tmpltDao.findById(isoId);
    if (iso == null || iso.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find an ISO with id " + isoId);
    }
    // check permissions
    // check if caller has access to VM and ISO
    // and also check if the VM's owner has access to the ISO.
    _accountMgr.checkAccess(caller, null, false, iso, vm);
    Account vmOwner = _accountDao.findById(vm.getAccountId());
    _accountMgr.checkAccess(vmOwner, null, false, iso, vm);
    State vmState = vm.getState();
    if (vmState != State.Running && vmState != State.Stopped) {
        throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.");
    }
    if ("xen-pv-drv-iso".equals(iso.getDisplayText()) && vm.getHypervisorType() != Hypervisor.HypervisorType.XenServer) {
        throw new InvalidParameterValueException("Cannot attach Xenserver PV drivers to incompatible hypervisor " + vm.getHypervisorType());
    }
    if ("vmware-tools.iso".equals(iso.getName()) && vm.getHypervisorType() != Hypervisor.HypervisorType.VMware) {
        throw new InvalidParameterValueException("Cannot attach VMware tools drivers to incompatible hypervisor " + vm.getHypervisorType());
    }
    boolean result = attachISOToVM(vmId, userId, isoId, true);
    if (result) {
        return result;
    } else {
        throw new CloudRuntimeException("Failed to attach iso");
    }
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) State(com.cloud.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) ActionEvent(com.cloud.event.ActionEvent)

Example 90 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class TemplateManagerImpl method prepareIsoForVmProfile.

@Override
public void prepareIsoForVmProfile(VirtualMachineProfile profile) {
    UserVmVO vm = _userVmDao.findById(profile.getId());
    if (vm.getIsoId() != null) {
        TemplateInfo template = prepareIso(vm.getIsoId(), vm.getDataCenterId());
        if (template == null) {
            s_logger.error("Failed to prepare ISO on secondary or cache storage");
            throw new CloudRuntimeException("Failed to prepare ISO on secondary or cache storage");
        }
        if (template.isBootable()) {
            profile.setBootLoaderType(BootloaderType.CD);
        }
        GuestOSVO guestOS = _guestOSDao.findById(template.getGuestOSId());
        String displayName = null;
        if (guestOS != null) {
            displayName = guestOS.getDisplayName();
        }
        TemplateObjectTO iso = (TemplateObjectTO) template.getTO();
        iso.setGuestOsType(displayName);
        DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
        profile.addDisk(disk);
    } else {
        TemplateObjectTO iso = new TemplateObjectTO();
        iso.setFormat(ImageFormat.ISO);
        DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
        profile.addDisk(disk);
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) GuestOSVO(com.cloud.storage.GuestOSVO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

UserVmVO (com.cloud.vm.UserVmVO)190 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)79 Account (com.cloud.user.Account)50 ArrayList (java.util.ArrayList)45 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)39 HostVO (com.cloud.host.HostVO)34 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)33 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)31 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)30 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)28 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)25 VolumeVO (com.cloud.storage.VolumeVO)25 VMInstanceVO (com.cloud.vm.VMInstanceVO)24 ActionEvent (com.cloud.event.ActionEvent)23 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)22 GuestOSVO (com.cloud.storage.GuestOSVO)20 HashMap (java.util.HashMap)19 ConfigurationException (javax.naming.ConfigurationException)19 Test (org.junit.Test)19 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)18