Search in sources :

Example 16 with ImageStoreVO

use of com.cloud.storage.datastore.db.ImageStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateAdapterBase method prepare.

@Override
public TemplateProfile prepare(final RegisterIsoCmd cmd) throws ResourceAllocationException {
    // check if the caller can operate with the template owner
    final Account caller = CallContext.current().getCallingAccount();
    final Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId());
    _accountMgr.checkAccess(caller, null, true, owner);
    Long zoneId = cmd.getZoneId();
    // ignore passed zoneId if we are using region wide image store
    final List<ImageStoreVO> stores = _imgStoreDao.findRegionImageStores();
    if (stores != null && stores.size() > 0) {
        zoneId = -1L;
    }
    return prepare(true, CallContext.current().getCallingUserId(), cmd.getIsoName(), cmd.getDisplayText(), 64, false, true, cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(), cmd.isExtractable(), ImageFormat.ISO.toString(), cmd.getOsTypeId(), zoneId, HypervisorType.None, cmd.getChecksum(), cmd.isBootable(), null, owner, null, false, cmd.getImageStoreUuid(), cmd.isDynamicallyScalable(), TemplateType.USER);
}
Also used : Account(com.cloud.user.Account) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO)

Example 17 with ImageStoreVO

use of com.cloud.storage.datastore.db.ImageStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateAdapterBase method prepare.

@Override
public TemplateProfile prepare(final RegisterTemplateCmd cmd) throws ResourceAllocationException {
    // check if the caller can operate with the template owner
    final Account caller = CallContext.current().getCallingAccount();
    final Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId());
    _accountMgr.checkAccess(caller, null, true, owner);
    final boolean isRouting = (cmd.isRoutingType() == null) ? false : cmd.isRoutingType();
    Long zoneId = cmd.getZoneId();
    // ignore passed zoneId if we are using region wide image store
    final List<ImageStoreVO> stores = _imgStoreDao.findRegionImageStores();
    if (stores != null && stores.size() > 0) {
        zoneId = -1L;
    }
    return prepare(false, CallContext.current().getCallingUserId(), cmd.getTemplateName(), cmd.getDisplayText(), cmd.getBits(), cmd.isPasswordEnabled(), cmd.getRequiresHvm(), cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(), cmd.isExtractable(), cmd.getFormat(), cmd.getOsTypeId(), zoneId, HypervisorType.getType(cmd.getHypervisor()), cmd.getChecksum(), true, cmd.getTemplateTag(), owner, cmd.getDetails(), cmd.isSshKeyEnabled(), null, cmd.isDynamicallyScalable(), isRouting ? TemplateType.ROUTING : TemplateType.USER);
}
Also used : Account(com.cloud.user.Account) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO)

Example 18 with ImageStoreVO

use of com.cloud.storage.datastore.db.ImageStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method createPrivateTemplateRecord.

@Override
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", create = true)
public VMTemplateVO createPrivateTemplateRecord(final CreateTemplateCmd cmd, final Account templateOwner) throws ResourceAllocationException {
    final Account caller = CallContext.current().getCallingAccount();
    final boolean isAdmin = _accountMgr.isAdmin(caller.getId());
    _accountMgr.checkAccess(caller, null, true, templateOwner);
    final String name = cmd.getTemplateName();
    if (name == null || name.length() > 32) {
        throw new InvalidParameterValueException("Template name cannot be null and should be less than 32 characters");
    }
    if (cmd.getTemplateTag() != null) {
        if (!_accountService.isRootAdmin(caller.getId())) {
            throw new PermissionDeniedException("Parameter templatetag can only be specified by a Root Admin, permission denied");
        }
    }
    // do some parameter defaulting
    final Integer bits = cmd.getBits();
    final Boolean requiresHvm = cmd.getRequiresHvm();
    final Boolean passwordEnabled = cmd.isPasswordEnabled();
    Boolean isPublic = cmd.isPublic();
    Boolean featured = cmd.isFeatured();
    final int bitsValue = bits == null ? 64 : bits;
    final boolean requiresHvmValue = requiresHvm == null || requiresHvm;
    final boolean passwordEnabledValue = passwordEnabled != null && passwordEnabled;
    if (isPublic == null) {
        isPublic = Boolean.FALSE;
    }
    final boolean isDynamicScalingEnabled = cmd.isDynamicallyScalable();
    // check whether template owner can create public templates
    final boolean allowPublicUserTemplates = AllowPublicUserTemplates.valueIn(templateOwner.getId());
    if (!isAdmin && !allowPublicUserTemplates && isPublic) {
        throw new PermissionDeniedException("Failed to create template " + name + ", only private templates can be created.");
    }
    final Long volumeId = cmd.getVolumeId();
    final Long snapshotId = cmd.getSnapshotId();
    if (volumeId == null && snapshotId == null) {
        throw new InvalidParameterValueException("Failed to create private template record, neither volume ID nor snapshot ID were specified.");
    }
    if (volumeId != null && snapshotId != null) {
        throw new InvalidParameterValueException("Failed to create private template record, please specify only one of volume ID (" + volumeId + ") and snapshot ID (" + snapshotId + ")");
    }
    HypervisorType hyperType;
    final VolumeVO volume;
    SnapshotVO snapshot = null;
    final VMTemplateVO privateTemplate;
    if (volumeId != null) {
        // create template from volume
        volume = _volumeDao.findById(volumeId);
        if (volume == null) {
            throw new InvalidParameterValueException("Failed to create private template record, unable to find volume " + volumeId);
        }
        // check permissions
        _accountMgr.checkAccess(caller, null, true, volume);
        // created
        if (!_volumeMgr.volumeInactive(volume)) {
            final String msg = "Unable to create private template for volume: " + volume.getName() + "; volume is attached to a non-stopped VM, please stop the VM first";
            if (s_logger.isInfoEnabled()) {
                s_logger.info(msg);
            }
            throw new CloudRuntimeException(msg);
        }
        hyperType = _volumeDao.getHypervisorType(volumeId);
    } else {
        // create template from snapshot
        snapshot = _snapshotDao.findById(snapshotId);
        if (snapshot == null) {
            throw new InvalidParameterValueException("Failed to create private template record, unable to find snapshot " + snapshotId);
        }
        // Volume could be removed so find including removed to record source template id.
        volume = _volumeDao.findByIdIncludingRemoved(snapshot.getVolumeId());
        // check permissions
        _accountMgr.checkAccess(caller, null, true, snapshot);
        if (snapshot.getState() != Snapshot.State.BackedUp) {
            throw new InvalidParameterValueException("Snapshot id=" + snapshotId + " is not in " + Snapshot.State.BackedUp + " state yet and can't be used for template creation");
        }
        /*
             * // bug #11428. Operation not supported if vmware and snapshots
             * parent volume = ROOT if(snapshot.getHypervisorType() ==
             * HypervisorType.VMware && snapshotVolume.getVolumeType() ==
             * Type.DATADISK){ throw new UnsupportedServiceException(
             * "operation not supported, snapshot with id " + snapshotId +
             * " is created from Data Disk"); }
             */
        hyperType = snapshot.getHypervisorType();
    }
    _resourceLimitMgr.checkResourceLimit(templateOwner, ResourceType.template);
    _resourceLimitMgr.checkResourceLimit(templateOwner, ResourceType.secondary_storage, volume != null ? volume.getSize() : snapshot.getSize());
    if (!isAdmin || featured == null) {
        featured = Boolean.FALSE;
    }
    final Long guestOSId = cmd.getOsTypeId();
    final GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
    if (guestOS == null) {
        throw new InvalidParameterValueException("GuestOS with ID: " + guestOSId + " does not exist.");
    }
    final Long nextTemplateId = _tmpltDao.getNextInSequence(Long.class, "id");
    final String description = cmd.getDisplayText();
    boolean isExtractable = false;
    Long sourceTemplateId = null;
    if (volume != null) {
        final VMTemplateVO template = ApiDBUtils.findTemplateById(volume.getTemplateId());
        isExtractable = template != null && template.isExtractable() && template.getTemplateType() != Storage.TemplateType.SYSTEM;
        if (volume.getIsoId() != null && volume.getIsoId() != 0) {
            sourceTemplateId = volume.getIsoId();
        } else if (volume.getTemplateId() != null) {
            sourceTemplateId = volume.getTemplateId();
        }
    }
    final String templateTag = cmd.getTemplateTag();
    if (templateTag != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Adding template tag: " + templateTag);
        }
    }
    // if specified from the API, use that one
    if (cmd.getHypervisor() != null) {
        hyperType = HypervisorType.getType(cmd.getHypervisor());
    }
    privateTemplate = new VMTemplateVO(nextTemplateId, name, ImageFormat.RAW, isPublic, featured, isExtractable, TemplateType.USER, null, requiresHvmValue, bitsValue, templateOwner.getId(), null, description, passwordEnabledValue, guestOS.getId(), true, hyperType, templateTag, cmd.getDetails(), false, isDynamicScalingEnabled);
    if (sourceTemplateId != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("This template is getting created from other template, setting source template Id to: " + sourceTemplateId);
        }
    }
    // for region wide storage, set cross zones flag
    final List<ImageStoreVO> stores = _imgStoreDao.findRegionImageStores();
    if (!CollectionUtils.isEmpty(stores)) {
        privateTemplate.setCrossZones(true);
    }
    privateTemplate.setSourceTemplateId(sourceTemplateId);
    final VMTemplateVO template = _tmpltDao.persist(privateTemplate);
    // Increment the number of templates
    if (template != null) {
        final Map<String, String> details = new HashMap<>();
        if (volume != null) {
            final Long vmId = volume.getInstanceId();
            if (vmId != null) {
                final UserVmVO userVm = _userVmDao.findById(vmId);
                if (userVm != null) {
                    _userVmDao.loadDetails(userVm);
                    details.putAll(userVm.getDetails());
                }
            }
        }
        if (cmd.getDetails() != null) {
            // new password will be generated during vm deployment from password enabled template
            details.remove("Encrypted.Password");
            details.putAll(cmd.getDetails());
        }
        if (!details.isEmpty()) {
            privateTemplate.setDetails(details);
            _tmpltDao.saveDetails(privateTemplate);
        }
        _resourceLimitMgr.incrementResourceCount(templateOwner.getId(), ResourceType.template);
        _resourceLimitMgr.incrementResourceCount(templateOwner.getId(), ResourceType.secondary_storage, new Long(volume != null ? volume.getSize() : snapshot.getSize()));
    }
    if (template != null) {
        return template;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) VMTemplateVO(com.cloud.storage.VMTemplateVO) GuestOSVO(com.cloud.storage.GuestOSVO) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO) ActionEvent(com.cloud.event.ActionEvent)

Example 19 with ImageStoreVO

use of com.cloud.storage.datastore.db.ImageStoreVO in project cosmic by MissionCriticalCloud.

the class SecondaryStorageManagerImpl method generateSetupCommand.

@Override
public boolean generateSetupCommand(final Long ssHostId) {
    final HostVO cssHost = _hostDao.findById(ssHostId);
    final Long zoneId = cssHost.getDataCenterId();
    if (cssHost.getType() == Host.Type.SecondaryStorageVM) {
        final SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(cssHost.getName());
        if (secStorageVm == null) {
            logger.warn("secondary storage VM " + cssHost.getName() + " doesn't exist");
            return false;
        }
        final List<DataStore> ssStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(zoneId));
        for (final DataStore ssStore : ssStores) {
            if (!(ssStore.getTO() instanceof NfsTO)) {
                // only do this for Nfs
                continue;
            }
            final String secUrl = ssStore.getUri();
            final SecStorageSetupCommand setupCmd;
            if (!_useSSlCopy) {
                setupCmd = new SecStorageSetupCommand(ssStore.getTO(), secUrl, null);
            } else {
                final KeystoreManager.Certificates certs = _keystoreMgr.getCertificates(ConsoleProxyManager.CERTIFICATE_NAME);
                setupCmd = new SecStorageSetupCommand(ssStore.getTO(), secUrl, certs);
            }
            // template/volume file upload key
            final String postUploadKey = _configDao.getValue(Config.SSVMPSK.key());
            setupCmd.setPostUploadKey(postUploadKey);
            final Answer answer = _agentMgr.easySend(ssHostId, setupCmd);
            if (answer != null && answer.getResult()) {
                final SecStorageSetupAnswer an = (SecStorageSetupAnswer) answer;
                if (an.get_dir() != null) {
                    // update the parent path in image_store table for this image store
                    final ImageStoreVO svo = _imageStoreDao.findById(ssStore.getId());
                    svo.setParent(an.get_dir());
                    _imageStoreDao.update(ssStore.getId(), svo);
                }
                logger.debug("Successfully programmed secondary storage " + ssStore.getName() + " in secondary storage VM " + secStorageVm.getInstanceName());
            } else {
                logger.debug("Successfully programmed secondary storage " + ssStore.getName() + " in secondary storage VM " + secStorageVm.getInstanceName());
                return false;
            }
        }
    }
    return true;
}
Also used : SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) KeystoreManager(com.cloud.framework.security.keystore.KeystoreManager) SecStorageSetupCommand(com.cloud.agent.api.SecStorageSetupCommand) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) HostVO(com.cloud.host.HostVO) ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) Answer(com.cloud.agent.api.Answer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO)

Aggregations

ImageStoreVO (com.cloud.storage.datastore.db.ImageStoreVO)19 Account (com.cloud.user.Account)8 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)7 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 DataCenterVO (com.cloud.dc.DataCenterVO)2 ImageStoreProvider (com.cloud.engine.subsystem.api.storage.ImageStoreProvider)2 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)2 GuestOSVO (com.cloud.storage.GuestOSVO)2 SnapshotVO (com.cloud.storage.SnapshotVO)2 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)2 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)2 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)2 TransactionStatus (com.cloud.utils.db.TransactionStatus)2 Answer (com.cloud.agent.api.Answer)1 SecStorageSetupAnswer (com.cloud.agent.api.SecStorageSetupAnswer)1