Search in sources :

Example 11 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class VolumeOrchestrator method createVolumeFromSnapshot.

@DB
@Override
public VolumeInfo createVolumeFromSnapshot(final Volume volume, final Snapshot snapshot, final UserVm vm) throws StorageUnavailableException {
    final Account account = this._entityMgr.findById(Account.class, volume.getAccountId());
    final HashSet<StoragePool> poolsToAvoid = new HashSet<>();
    StoragePool pool = null;
    final Set<Long> podsToAvoid = new HashSet<>();
    Pair<Pod, Long> pod = null;
    final DiskOffering diskOffering = this._entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());
    final DataCenter dc = this._entityMgr.findById(DataCenter.class, volume.getDataCenterId());
    final DiskProfile dskCh = new DiskProfile(volume, diskOffering, snapshot.getHypervisorType());
    String msg = "There are no available storage pools to store the volume in";
    if (vm != null) {
        final Pod podofVM = this._entityMgr.findById(Pod.class, vm.getPodIdToDeployIn());
        if (podofVM != null) {
            pod = new Pair<>(podofVM, podofVM.getId());
        }
    }
    if (vm != null && pod != null) {
        // if VM is running use the hostId to find the clusterID. If it is stopped, refer the cluster where the ROOT volume of the VM exists.
        Long hostId = null;
        Long clusterId = null;
        if (vm.getState() == State.Running) {
            hostId = vm.getHostId();
            if (hostId != null) {
                final Host vmHost = this._entityMgr.findById(Host.class, hostId);
                clusterId = vmHost.getClusterId();
            }
        } else {
            final List<VolumeVO> rootVolumesOfVm = this._volsDao.findByInstanceAndType(vm.getId(), VolumeType.ROOT);
            if (rootVolumesOfVm.size() != 1) {
                throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state. Please contact Cloud Support.");
            } else {
                final VolumeVO rootVolumeOfVm = rootVolumesOfVm.get(0);
                final StoragePoolVO rootDiskPool = this._storagePoolDao.findById(rootVolumeOfVm.getPoolId());
                clusterId = (rootDiskPool == null ? null : rootDiskPool.getClusterId());
            }
        }
        // Determine what storage pool to store the volume in
        while ((pool = findStoragePool(dskCh, dc, pod.first(), clusterId, hostId, vm, poolsToAvoid)) != null) {
            break;
        }
        if (pool == null) {
            // pool could not be found in the VM's pod/cluster.
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Could not find any storage pool to create Volume in the pod/cluster of the provided VM " + vm.getUuid());
            }
            final StringBuilder addDetails = new StringBuilder(msg);
            addDetails.append(", Could not find any storage pool to create Volume in the pod/cluster of the VM ");
            addDetails.append(vm.getUuid());
            msg = addDetails.toString();
        }
    } else {
        // Determine what pod to store the volume in
        while ((pod = findPod(null, null, dc, account.getId(), podsToAvoid)) != null) {
            podsToAvoid.add(pod.first().getId());
            // Determine what storage pool to store the volume in
            while ((pool = findStoragePool(dskCh, dc, pod.first(), null, null, null, poolsToAvoid)) != null) {
                break;
            }
            if (pool != null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Found a suitable pool for create volume: " + pool.getId());
                }
                break;
            }
        }
    }
    if (pool == null) {
        s_logger.info(msg);
        throw new StorageUnavailableException(msg, -1);
    }
    final VolumeInfo vol = this.volFactory.getVolume(volume.getId());
    final DataStore store = this.dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
    final DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
    SnapshotInfo snapInfo = this.snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
    if (snapInfo == null && dataStoreRole == DataStoreRole.Image) {
        // snapshot is not backed up to secondary, let's do that now.
        snapInfo = this.snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Primary);
        if (snapInfo == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId());
        }
        // We need to copy the snapshot onto secondary.
        final SnapshotStrategy snapshotStrategy = this._storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
        snapshotStrategy.backupSnapshot(snapInfo);
        // Attempt to grab it again.
        snapInfo = this.snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
        if (snapInfo == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId() + " on secondary and could not create backup");
        }
    }
    // don't try to perform a sync if the DataStoreRole of the snapshot is equal to DataStoreRole.Primary
    if (!DataStoreRole.Primary.equals(dataStoreRole)) {
        try {
            // sync snapshot to region store if necessary
            final DataStore snapStore = snapInfo.getDataStore();
            final long snapVolId = snapInfo.getVolumeId();
            this._snapshotSrv.syncVolumeSnapshotsToRegionStore(snapVolId, snapStore);
        } catch (final Exception ex) {
            // log but ignore the sync error to avoid any potential S3 down issue, it should be sync next time
            s_logger.warn(ex.getMessage(), ex);
        }
    }
    // create volume on primary from snapshot
    final AsyncCallFuture<VolumeService.VolumeApiResult> future = this.volService.createVolumeFromSnapshot(vol, store, snapInfo);
    try {
        final VolumeService.VolumeApiResult result = future.get();
        if (result.isFailed()) {
            s_logger.debug("Failed to create volume from snapshot:" + result.getResult());
            throw new CloudRuntimeException("Failed to create volume from snapshot:" + result.getResult());
        }
        return result.getVolume();
    } catch (final InterruptedException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    } catch (final ExecutionException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) StoragePool(com.cloud.legacymodel.storage.StoragePool) DiskOffering(com.cloud.legacymodel.storage.DiskOffering) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) DataStoreRole(com.cloud.model.enumeration.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeService(com.cloud.engine.subsystem.api.storage.VolumeService) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) PrimaryDataStore(com.cloud.engine.subsystem.api.storage.PrimaryDataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) HashSet(java.util.HashSet) Pod(com.cloud.legacymodel.dc.Pod) Host(com.cloud.legacymodel.dc.Host) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.legacymodel.dc.DataCenter) DB(com.cloud.utils.db.DB)

Example 12 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method stopVmThroughJobQueue.

public Outcome<VirtualMachine> stopVmThroughJobQueue(final String vmUuid, final boolean cleanup) {
    final CallContext context = CallContext.current();
    final Account account = context.getCallingAccount();
    final User user = context.getCallingUser();
    final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    final List<VmWorkJobVO> pendingWorkJobs = _workJobDao.listPendingWorkJobs(vm.getType(), vm.getId(), VmWorkStop.class.getName());
    VmWorkJobVO workJob = null;
    if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) {
        assert pendingWorkJobs.size() == 1;
        workJob = pendingWorkJobs.get(0);
    } else {
        workJob = new VmWorkJobVO(context.getContextId());
        workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
        workJob.setCmd(VmWorkStop.class.getName());
        workJob.setAccountId(account.getId());
        workJob.setUserId(user.getId());
        workJob.setStep(VmWorkJobVO.Step.Prepare);
        workJob.setVmType(VirtualMachineType.Instance);
        workJob.setVmInstanceId(vm.getId());
        workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
        // save work context info (there are some duplications)
        final VmWorkStop workInfo = new VmWorkStop(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, cleanup);
        workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
        _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    }
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmStateSyncOutcome(workJob, VirtualMachine.PowerState.PowerOff, vm.getId(), null);
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) CallContext(com.cloud.context.CallContext) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO)

Example 13 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImplTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    _vmMgr._templateDao = _templateDao;
    _vmMgr._volsDao = _volsDao;
    _vmMgr.volumeMgr = _storageMgr;
    _vmMgr._capacityMgr = _capacityMgr;
    _vmMgr._hostDao = _hostDao;
    _vmMgr._nodeId = 1L;
    _vmMgr._workDao = _workDao;
    _vmMgr._agentMgr = _agentMgr;
    _vmMgr._podDao = _podDao;
    _vmMgr._clusterDao = _clusterDao;
    _vmMgr._clusterDetailsDao = _clusterDetailsDao;
    _vmMgr._dcDao = _dcDao;
    _vmMgr._diskOfferingDao = _diskOfferingDao;
    _vmMgr._storagePoolDao = _storagePoolDao;
    _vmMgr._poolHostDao = _poolHostDao;
    _vmMgr._offeringDao = _offeringDao;
    _vmMgr._networkMgr = _networkMgr;
    _vmMgr._hvGuruMgr = _hvGuruMgr;
    _vmMgr._vmSnapshotMgr = _vmSnapshotMgr;
    _vmMgr._vmDao = _vmInstanceDao;
    _vmMgr._uservmDetailsDao = _vmDetailsDao;
    _vmMgr._entityMgr = _entityMgr;
    _vmMgr._configDepot = _configDepot;
    _vmMgr._dpMgr = _dpMgr;
    final AccountVO account = new AccountVO("admin", 5L, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
    when(_vmMock.getId()).thenReturn(314l);
    when(_vmMock.getServiceOfferingId()).thenReturn(2L);
    when(_vmMock.getTemplateId()).thenReturn(1L);
    when(_vmMock.getType()).thenReturn(VirtualMachineType.User);
    when(_vmMock.getHypervisorType()).thenReturn(HypervisorType.KVM);
    when(_vmInstance.getId()).thenReturn(1L);
    when(_vmInstance.getServiceOfferingId()).thenReturn(2L);
    when(_vmInstance.getInstanceName()).thenReturn("myVm");
    when(_vmInstance.getHostId()).thenReturn(2L);
    when(_vmInstance.getType()).thenReturn(VirtualMachineType.User);
    when(_host.getId()).thenReturn(1L);
    when(_hostDao.findById(anyLong())).thenReturn(null);
    when(_pod.getId()).thenReturn(1L);
    when(_cluster.getId()).thenReturn(1L);
    when(_entityMgr.findById(eq(ServiceOffering.class), anyLong())).thenReturn(getSvcoffering(512));
    when(_entityMgr.findById(eq(Account.class), anyLong())).thenReturn(account);
    when(_entityMgr.findByIdIncludingRemoved(eq(VirtualMachineTemplate.class), anyLong())).thenReturn(_templateMock);
    when(_workDao.persist(any(ItWorkVO.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    when(_workDao.update("1", _work)).thenReturn(true);
    when(_workDao.findById(any(String.class))).thenReturn(_work);
    when(_work.getId()).thenReturn("1");
    doNothing().when(_work).setStep(ItWorkVO.Step.Done);
    when(_vmInstanceDao.findById(anyLong())).thenReturn(_vmMock);
    when(_vmInstanceDao.findByUuid(any(String.class))).thenReturn(_vmMock);
    when(_vmInstanceDao.findVMByInstanceName(any(String.class))).thenReturn(_vmMock);
    when(_vmInstanceDao.persist(any(VMInstanceVO.class))).thenReturn(_vmMock);
    when(_offeringDao.findById(anyLong(), anyLong())).thenReturn(getSvcoffering(512));
    when(_clusterDetailsDao.findDetail(anyLong(), any(String.class))).thenAnswer(invocation -> new ClusterDetailsVO((long) invocation.getArguments()[0], (String) invocation.getArguments()[1], "1"));
// doNothing().when(_volsDao).detachVolume(anyLong());
// when(_work.setStep(ItWorkVO.HaWorkStep.Done)).thenReturn("1");
}
Also used : Account(com.cloud.legacymodel.user.Account) UserVO(com.cloud.user.UserVO) VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) ServiceOffering(com.cloud.offering.ServiceOffering) AccountVO(com.cloud.user.AccountVO) ClusterDetailsVO(com.cloud.dc.ClusterDetailsVO) Before(org.junit.Before)

Example 14 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method implementNetworkElementsAndResources.

@Override
public void implementNetworkElementsAndResources(final DeployDestination dest, final ReservationContext context, final Network network, final NetworkOffering offering) throws ConcurrentOperationException, InsufficientAddressCapacityException, ResourceUnavailableException, InsufficientCapacityException {
    // Associate a source NAT IP (if one isn't already associated with the network) if this is a
    // 1) 'Isolated' or 'Shared' guest virtual network in the advance zone
    // 2) network has sourceNat service
    // 3) network offering does not support a shared source NAT rule
    final boolean sharedSourceNat = offering.getSharedSourceNat();
    final Zone zone = _zoneRepository.findById(network.getDataCenterId()).orElse(null);
    if (!sharedSourceNat && _networkModel.areServicesSupportedInNetwork(network.getId(), Service.SourceNat) && (network.getGuestType() == GuestType.Isolated || network.getGuestType() == GuestType.Shared && zone.getNetworkType() == com.cloud.model.enumeration.NetworkType.Advanced)) {
        List<IPAddressVO> ips = null;
        final Account owner = _entityMgr.findById(Account.class, network.getAccountId());
        if (network.getVpcId() != null) {
            ips = _ipAddressDao.listByVpc(network.getVpcId(), true);
            if (ips.isEmpty()) {
                final Vpc vpc = _vpcMgr.getActiveVpc(network.getVpcId());
                s_logger.debug("Creating a source nat ip for vpc " + vpc);
                _vpcMgr.assignSourceNatIpAddressToVpc(owner, vpc);
            }
        } else {
            ips = _ipAddressDao.listByAssociatedNetwork(network.getId(), true);
            if (ips.isEmpty()) {
                s_logger.debug("Creating a source nat ip for network " + network);
                _ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(owner, network);
            }
        }
    }
    // get providers to implement
    final List<Provider> providersToImplement = getNetworkProviders(network.getId());
    implementNetworkElements(dest, context, network, offering, providersToImplement);
    for (final NetworkElement element : networkElements) {
        if (element instanceof AggregatedCommandExecutor && providersToImplement.contains(element.getProvider())) {
            ((AggregatedCommandExecutor) element).prepareAggregatedExecution(network, dest);
        }
    }
    try {
        // reapply all the firewall/staticNat/lb rules
        s_logger.debug("Reprogramming network " + network + " as a part of network implement");
        if (!reprogramNetworkRules(network.getId(), CallContext.current().getCallingAccount(), network)) {
            s_logger.warn("Failed to re-program the network as a part of network " + network + " implement");
            // see DataCenterVO.java
            final ResourceUnavailableException ex = new ResourceUnavailableException("Unable to apply network rules as a part of network " + network + " implement", DataCenter.class, network.getDataCenterId());
            ex.addProxyObject(_entityMgr.findById(DataCenter.class, network.getDataCenterId()).getUuid());
            throw ex;
        }
        for (final NetworkElement element : networkElements) {
            if (element instanceof AggregatedCommandExecutor && providersToImplement.contains(element.getProvider())) {
                if (!((AggregatedCommandExecutor) element).completeAggregatedExecution(network, dest)) {
                    s_logger.warn("Failed to re-program the network as a part of network " + network + " implement due to aggregated commands execution failure!");
                    // see DataCenterVO.java
                    final ResourceUnavailableException ex = new ResourceUnavailableException("Unable to apply network rules as a part of network " + network + " implement", DataCenter.class, network.getDataCenterId());
                    ex.addProxyObject(_entityMgr.findById(DataCenter.class, network.getDataCenterId()).getUuid());
                    throw ex;
                }
            }
        }
    } finally {
        for (final NetworkElement element : networkElements) {
            if (element instanceof AggregatedCommandExecutor && providersToImplement.contains(element.getProvider())) {
                ((AggregatedCommandExecutor) element).cleanupAggregatedExecution(network, dest);
            }
        }
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) AggregatedCommandExecutor(com.cloud.network.element.AggregatedCommandExecutor) NetworkElement(com.cloud.network.element.NetworkElement) Zone(com.cloud.db.model.Zone) Vpc(com.cloud.legacymodel.network.vpc.Vpc) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) Provider(com.cloud.legacymodel.network.Network.Provider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider)

Example 15 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method removeVmFromNetworkThroughJobQueue.

public Outcome<VirtualMachine> removeVmFromNetworkThroughJobQueue(final VirtualMachine vm, final Network network, final URI broadcastUri) {
    final CallContext context = CallContext.current();
    final User user = context.getCallingUser();
    final Account account = context.getCallingAccount();
    final List<VmWorkJobVO> pendingWorkJobs = _workJobDao.listPendingWorkJobs(VirtualMachineType.Instance, vm.getId(), VmWorkRemoveVmFromNetwork.class.getName());
    VmWorkJobVO workJob = null;
    if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) {
        assert pendingWorkJobs.size() == 1;
        workJob = pendingWorkJobs.get(0);
    } else {
        workJob = new VmWorkJobVO(context.getContextId());
        workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
        workJob.setCmd(VmWorkRemoveVmFromNetwork.class.getName());
        workJob.setAccountId(account.getId());
        workJob.setUserId(user.getId());
        workJob.setVmType(VirtualMachineType.Instance);
        workJob.setVmInstanceId(vm.getId());
        workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
        // save work context info (there are some duplications)
        final VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network, broadcastUri);
        workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
        _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    }
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVirtualMachineOutcome(workJob, vm.getId());
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) CallContext(com.cloud.context.CallContext) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO)

Aggregations

Account (com.cloud.legacymodel.user.Account)435 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)229 ActionEvent (com.cloud.event.ActionEvent)120 ArrayList (java.util.ArrayList)103 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)98 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)78 User (com.cloud.legacymodel.user.User)73 DB (com.cloud.utils.db.DB)59 List (java.util.List)58 Pair (com.cloud.legacymodel.utils.Pair)53 Network (com.cloud.legacymodel.network.Network)48 CallContext (com.cloud.context.CallContext)47 DomainVO (com.cloud.domain.DomainVO)47 UserAccount (com.cloud.legacymodel.user.UserAccount)47 Filter (com.cloud.utils.db.Filter)47 TransactionStatus (com.cloud.utils.db.TransactionStatus)40 Domain (com.cloud.legacymodel.domain.Domain)39 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)37 Test (org.junit.Test)36 Ternary (com.cloud.legacymodel.utils.Ternary)34