Search in sources :

Example 46 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project CloudStack-archive by CloudStack-extras.

the class SimulatorDiscoverer method associateTemplatesToZone.

private void associateTemplatesToZone(long hostId, long dcId) {
    VMTemplateZoneVO tmpltZone;
    List<VMTemplateVO> allTemplates = _vmTemplateDao.listAll();
    for (VMTemplateVO vt : allTemplates) {
        if (vt.isCrossZones()) {
            tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
            if (tmpltZone == null) {
                VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
                _vmTemplateZoneDao.persist(vmTemplateZone);
            }
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.util.Date)

Example 47 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class UserVmManagerImpl method resetVMSSHKeyInternal.

private boolean resetVMSSHKeyInternal(Long vmId, String sshPublicKey, String password) throws ResourceUnavailableException, InsufficientCapacityException {
    Long userId = CallContext.current().getCallingUserId();
    VMInstanceVO vmInstance = _vmDao.findById(vmId);
    VMTemplateVO template = _templateDao.findByIdIncludingRemoved(vmInstance.getTemplateId());
    Nic defaultNic = _networkModel.getDefaultNic(vmId);
    if (defaultNic == null) {
        s_logger.error("Unable to reset SSH Key for vm " + vmInstance + " as the instance doesn't have default nic");
        return false;
    }
    Network defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
    NicProfile defaultNicProfile = new NicProfile(defaultNic, defaultNetwork, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork), _networkModel.getNetworkTag(template.getHypervisorType(), defaultNetwork));
    VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vmInstance);
    if (template.getEnablePassword()) {
        vmProfile.setParameter(VirtualMachineProfile.Param.VmPassword, password);
    }
    UserDataServiceProvider element = _networkMgr.getSSHKeyResetProvider(defaultNetwork);
    if (element == null) {
        throw new CloudRuntimeException("Can't find network element for " + Service.UserData.getName() + " provider needed for SSH Key reset");
    }
    boolean result = element.saveSSHKey(defaultNetwork, defaultNicProfile, vmProfile, sshPublicKey);
    // Need to reboot the virtual machine so that the password gets redownloaded from the DomR, and reset on the VM
    if (!result) {
        s_logger.debug("Failed to reset SSH Key for the virutal machine; no need to reboot the vm");
        return false;
    } else {
        if (vmInstance.getState() == State.Stopped) {
            s_logger.debug("Vm " + vmInstance + " is stopped, not rebooting it as a part of SSH Key reset");
            return true;
        }
        if (rebootVirtualMachine(userId, vmId) == null) {
            s_logger.warn("Failed to reboot the vm " + vmInstance);
            return false;
        } else {
            s_logger.debug("Vm " + vmInstance + " is rebooted successfully as a part of SSH Key reset");
            return true;
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) VMTemplateVO(com.cloud.storage.VMTemplateVO) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider)

Example 48 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class RoleBasedAPIAccessChecker method start.

@Override
public boolean start() {
    for (RoleType role : RoleType.values()) {
        Long policyId = getDefaultPolicyId(role);
        if (policyId != null) {
            _iamSrv.resetIAMPolicy(policyId);
        }
    }
    // add the system-domain capability
    _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_ADMIN + 1), null, null, null, "SystemCapability", null, Permission.Allow, false);
    _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_DOMAIN_ADMIN + 1), null, null, null, "DomainCapability", null, Permission.Allow, false);
    _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN + 1), null, null, null, "DomainResourceCapability", null, Permission.Allow, false);
    // add permissions for public templates
    List<VMTemplateVO> pTmplts = _templateDao.listByPublic();
    for (VMTemplateVO tmpl : pTmplts) {
        _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_ADMIN + 1), VirtualMachineTemplate.class.getSimpleName(), PermissionScope.RESOURCE.toString(), tmpl.getId(), "listTemplates", AccessType.UseEntry.toString(), Permission.Allow, false);
        _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_DOMAIN_ADMIN + 1), VirtualMachineTemplate.class.getSimpleName(), PermissionScope.RESOURCE.toString(), tmpl.getId(), "listTemplates", AccessType.UseEntry.toString(), Permission.Allow, false);
        _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_NORMAL + 1), VirtualMachineTemplate.class.getSimpleName(), PermissionScope.RESOURCE.toString(), tmpl.getId(), "listTemplates", AccessType.UseEntry.toString(), Permission.Allow, false);
    }
    for (PluggableService service : _services) {
        for (Class<?> cmdClass : service.getCommands()) {
            APICommand command = cmdClass.getAnnotation(APICommand.class);
            if (!commandsPropertiesOverrides.contains(command.name())) {
                for (RoleType role : command.authorized()) {
                    addDefaultAclPolicyPermission(command.name(), cmdClass, role);
                }
            }
        }
    }
    for (String apiName : commandsPropertiesOverrides) {
        Class<?> cmdClass = _apiServer.getCmdClass(apiName);
        for (RoleType role : RoleType.values()) {
            if (commandsPropertiesRoleBasedApisMap.get(role).contains(apiName)) {
                // insert permission for this role for this api
                addDefaultAclPolicyPermission(apiName, cmdClass, role);
            }
        }
    }
    return super.start();
}
Also used : VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) RoleType(org.apache.cloudstack.acl.RoleType) VMTemplateVO(com.cloud.storage.VMTemplateVO) PluggableService(com.cloud.utils.component.PluggableService) APICommand(org.apache.cloudstack.api.APICommand)

Example 49 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class BareMetalTemplateAdapter method create.

@Override
public VMTemplateVO create(TemplateProfile profile) {
    VMTemplateVO template = persistTemplate(profile, State.Active);
    Long zoneId = profile.getZoneId();
    // create an entry at template_store_ref with store_id = null to represent that this template is ready for use.
    TemplateDataStoreVO vmTemplateHost = new TemplateDataStoreVO(null, template.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, null, template.getUrl());
    this._tmpltStoreDao.persist(vmTemplateHost);
    if (zoneId == null || zoneId == -1) {
        List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
        if (dcs != null && dcs.size() > 0) {
            templateCreateUsage(template, dcs.get(0).getId());
        }
    } else {
        templateCreateUsage(template, zoneId);
    }
    _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
    return template;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) Date(java.util.Date)

Example 50 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class ManagementServerMock method createVM.

public UserVm createVM(String name, Network network) {
    VMTemplateVO tmpl = getVMTemplate();
    assertNotNull(tmpl);
    ServiceOffering small = getServiceByName("Small Instance");
    assertNotNull(small);
    Answer<?> callback = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            Commands cmds = (Commands) args[1];
            if (cmds == null) {
                return null;
            }
            PlugNicAnswer reply = new PlugNicAnswer(null, true, "PlugNic");
            com.cloud.agent.api.Answer[] answers = { reply };
            cmds.setAnswers(answers);
            return null;
        }
    };
    try {
        Mockito.when(_agentMgr.send(Matchers.anyLong(), Matchers.any(Commands.class))).thenAnswer(callback);
    } catch (AgentUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OperationTimedoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    long id = _userVmDao.getNextInSequence(Long.class, "id");
    UserVmVO vm = new UserVmVO(id, name, name, tmpl.getId(), HypervisorType.XenServer, tmpl.getGuestOSId(), false, false, _zone.getDomainId(), Account.ACCOUNT_ID_SYSTEM, 1, small.getId(), null, name, null);
    vm.setState(com.cloud.vm.VirtualMachine.State.Running);
    vm.setHostId(_hostId);
    vm.setDataCenterId(network.getDataCenterId());
    _userVmDao.persist(vm);
    NicProfile profile = new NicProfile();
    try {
        _vmMgr.addVmToNetwork(vm, network, profile);
    } catch (Exception ex) {
    // TODO Auto-generated catch block
    //ex.printStackTrace();
    }
    return vm;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) UserVmVO(com.cloud.vm.UserVmVO) ServiceOffering(com.cloud.offering.ServiceOffering) VMTemplateVO(com.cloud.storage.VMTemplateVO) NicProfile(com.cloud.vm.NicProfile) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) Answer(org.mockito.stubbing.Answer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer)

Aggregations

VMTemplateVO (com.cloud.storage.VMTemplateVO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)27 Account (com.cloud.user.Account)27 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)24 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)22 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)17 Date (java.util.Date)16 ArrayList (java.util.ArrayList)15 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)13 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 DataCenterVO (com.cloud.dc.DataCenterVO)12 ActionEvent (com.cloud.event.ActionEvent)11 TemplateProfile (com.cloud.storage.TemplateProfile)11 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)11 VolumeVO (com.cloud.storage.VolumeVO)11 Test (org.junit.Test)11 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)10 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)9 DB (com.cloud.utils.db.DB)9