Search in sources :

Example 46 with Volume

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

the class UserVmManagerImpl method destroyVm.

@Override
public UserVm destroyVm(long vmId, boolean expunge) throws ResourceUnavailableException, ConcurrentOperationException {
    // Account caller = CallContext.current().getCallingAccount();
    // Long userId = CallContext.current().getCallingUserId();
    Long userId = 2L;
    // Verify input parameters
    UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null || vm.getRemoved() != null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a virtual machine with specified vmId");
        throw ex;
    }
    if (vm.getState() == State.Destroyed || vm.getState() == State.Expunging) {
        s_logger.trace("Vm id=" + vmId + " is already destroyed");
        return vm;
    }
    boolean status;
    State vmState = vm.getState();
    try {
        VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
        status = vmEntity.destroy(Long.toString(userId), expunge);
    } catch (CloudException e) {
        CloudRuntimeException ex = new CloudRuntimeException("Unable to destroy with specified vmId", e);
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    if (status) {
        // Mark the account's volumes as destroyed
        List<VolumeVO> volumes = _volsDao.findByInstance(vmId);
        for (VolumeVO volume : volumes) {
            if (volume.getVolumeType().equals(Volume.Type.ROOT)) {
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_DELETE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), Volume.class.getName(), volume.getUuid(), volume.isDisplayVolume());
            }
        }
        if (vmState != State.Error) {
            // Get serviceOffering for Virtual Machine
            ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
            //Update Resource Count for the given account
            resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
        }
        return _vmDao.findById(vmId);
    } else {
        CloudRuntimeException ex = new CloudRuntimeException("Failed to destroy vm with specified vmId");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Volume(com.cloud.storage.Volume) ResourceState(com.cloud.resource.ResourceState) State(com.cloud.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineEntity(org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity) CloudException(com.cloud.exception.CloudException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO)

Example 47 with Volume

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

the class VolumeStateListener method pubishOnEventBus.

private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
    String configKey = Config.PublishResourceStateEvent.key();
    String value = _configDao.getValue(configKey);
    boolean configValue = Boolean.parseBoolean(value);
    if (!configValue)
        return;
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    String resourceName = getEntityFromClassName(Volume.class.getName());
    org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
    Map<String, String> eventDescription = new HashMap<String, String>();
    eventDescription.put("resource", resourceName);
    eventDescription.put("id", vo.getUuid());
    eventDescription.put("old-state", oldState.name());
    eventDescription.put("new-state", newState.name());
    String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
    eventDescription.put("eventDateTime", eventDate);
    eventMsg.setDescription(eventDescription);
    try {
        s_eventBus.publish(eventMsg);
    } catch (EventBusException e) {
        s_logger.warn("Failed to state change event on the the event bus.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) Date(java.util.Date) Volume(com.cloud.storage.Volume) Event(com.cloud.storage.Volume.Event) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 48 with Volume

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

the class CitrixResourceBase method mount.

protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
    final DataTO data = volume.getData();
    final Volume.Type type = volume.getType();
    if (type == Volume.Type.ISO) {
        final TemplateObjectTO iso = (TemplateObjectTO) data;
        final DataStoreTO store = iso.getDataStore();
        if (store == null) {
            // It's a fake iso
            return null;
        }
        // corer case, xenserver pv driver iso
        final String templateName = iso.getName();
        if (templateName.startsWith("xs-tools")) {
            try {
                final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
                if (vdis.isEmpty()) {
                    throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
                }
                return vdis.iterator().next();
            } catch (final XenAPIException e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            } catch (final Exception e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            }
        }
        if (!(store instanceof NfsTO)) {
            throw new CloudRuntimeException("only support mount iso on nfs");
        }
        final NfsTO nfsStore = (NfsTO) store;
        final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String mountpoint = isoPath.substring(0, index);
        URI uri;
        try {
            uri = new URI(mountpoint);
        } catch (final URISyntaxException e) {
            throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
        }
        final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
        final String isoname = isoPath.substring(index + 1);
        final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
        if (isoVdi == null) {
            throw new CloudRuntimeException("Unable to find ISO " + isoPath);
        }
        return isoVdi;
    } else {
        final VolumeObjectTO vol = (VolumeObjectTO) data;
        return VDI.getByUuid(conn, vol.getPath());
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) URISyntaxException(java.net.URISyntaxException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DataTO(com.cloud.agent.api.to.DataTO) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) SR(com.xensource.xenapi.SR)

Example 49 with Volume

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

the class CitrixResourceBase method createVbd.

public VBD createVbd(final Connection conn, final DiskTO volume, final String vmName, final VM vm, final BootloaderType bootLoaderType, VDI vdi) throws XmlRpcException, XenAPIException {
    final Volume.Type type = volume.getType();
    if (vdi == null) {
        vdi = mount(conn, vmName, volume);
    }
    if (vdi != null) {
        if ("detached".equals(vdi.getNameLabel(conn))) {
            vdi.setNameLabel(conn, vmName + "-DATA");
        }
        final Map<String, String> smConfig = vdi.getSmConfig(conn);
        for (final String key : smConfig.keySet()) {
            if (key.startsWith("host_")) {
                vdi.removeFromSmConfig(conn, key);
                break;
            }
        }
    }
    final VBD.Record vbdr = new VBD.Record();
    vbdr.VM = vm;
    if (vdi != null) {
        vbdr.VDI = vdi;
    } else {
        vbdr.empty = true;
    }
    if (type == Volume.Type.ROOT && bootLoaderType == BootloaderType.PyGrub) {
        vbdr.bootable = true;
    } else if (type == Volume.Type.ISO && bootLoaderType == BootloaderType.CD) {
        vbdr.bootable = true;
    }
    if (volume.getType() == Volume.Type.ISO) {
        vbdr.mode = Types.VbdMode.RO;
        vbdr.type = Types.VbdType.CD;
        vbdr.userdevice = "3";
    } else {
        vbdr.mode = Types.VbdMode.RW;
        vbdr.type = Types.VbdType.DISK;
        vbdr.unpluggable = (volume.getType() == Volume.Type.ROOT) ? false : true;
        vbdr.userdevice = "autodetect";
        final Long deviceId = volume.getDiskSeq();
        if (deviceId != null && (!isDeviceUsed(conn, vm, deviceId) || deviceId > 3)) {
            vbdr.userdevice = deviceId.toString();
        }
    }
    final VBD vbd = VBD.create(conn, vbdr);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("VBD " + vbd.getUuid(conn) + " created for " + volume);
    }
    return vbd;
}
Also used : Volume(com.cloud.storage.Volume) VBD(com.xensource.xenapi.VBD)

Example 50 with Volume

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

the class DeployDestination method toString.

@Override
public String toString() {
    Long dcId = null;
    Long podId = null;
    Long clusterId = null;
    Long hostId = null;
    if (_dc != null) {
        dcId = _dc.getId();
    }
    if (_pod != null) {
        podId = _pod.getId();
    }
    if (_cluster != null) {
        clusterId = _cluster.getId();
    }
    if (_host != null) {
        hostId = _host.getId();
    }
    StringBuilder destination = new StringBuilder("Dest[Zone(Id)-Pod(Id)-Cluster(Id)-Host(Id)-Storage(Volume(Id|Type-->Pool(Id))] : Dest[");
    destination.append("Zone(").append(dcId).append(")").append("-");
    destination.append("Pod(").append(podId).append(")").append("-");
    destination.append("Cluster(").append(clusterId).append(")").append("-");
    destination.append("Host(").append(hostId).append(")").append("-");
    destination.append("Storage(");
    if (_storage != null) {
        StringBuffer storageBuf = new StringBuffer();
        //String storageStr = "";
        for (Volume vol : _storage.keySet()) {
            if (!storageBuf.toString().equals("")) {
                storageBuf.append(storageBuf.toString());
                storageBuf.append(", ");
            }
            storageBuf.append(storageBuf);
            storageBuf.append("Volume(");
            storageBuf.append(vol.getId());
            storageBuf.append("|");
            storageBuf.append(vol.getVolumeType().name());
            storageBuf.append("-->Pool(");
            storageBuf.append(_storage.get(vol).getId());
            storageBuf.append(")");
        }
        destination.append(storageBuf.toString());
    }
    return destination.append(")]").toString();
}
Also used : Volume(com.cloud.storage.Volume)

Aggregations

Volume (com.cloud.storage.Volume)70 StoragePool (com.cloud.storage.StoragePool)21 ServerApiException (org.apache.cloudstack.api.ServerApiException)16 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)15 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)14 VolumeResponse (org.apache.cloudstack.api.response.VolumeResponse)14 ArrayList (java.util.ArrayList)12 Account (com.cloud.user.Account)11 VolumeVO (com.cloud.storage.VolumeVO)10 DiskProfile (com.cloud.vm.DiskProfile)10 StoragePoolAllocator (org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator)10 Test (org.junit.Test)10 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)9 HashMap (java.util.HashMap)9 DeploymentPlan (com.cloud.deploy.DeploymentPlan)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)8 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)8 Project (com.cloud.projects.Project)7 VmWorkAttachVolume (com.cloud.vm.VmWorkAttachVolume)7