Search in sources :

Example 26 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ResourceManagerImpl method updateHost.

@Override
public Host updateHost(final UpdateHostCmd cmd) throws NoTransitionException {
    Long hostId = cmd.getId();
    String name = cmd.getName();
    Long guestOSCategoryId = cmd.getOsCategoryId();
    // Verify that the host exists
    final HostVO host = _hostDao.findById(hostId);
    if (host == null) {
        throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist");
    }
    if (cmd.getAllocationState() != null) {
        final ResourceState.Event resourceEvent = ResourceState.Event.toEvent(cmd.getAllocationState());
        if (resourceEvent != ResourceState.Event.Enable && resourceEvent != ResourceState.Event.Disable) {
            throw new CloudRuntimeException("Invalid allocation state:" + cmd.getAllocationState() + ", only Enable/Disable are allowed");
        }
        resourceStateTransitTo(host, resourceEvent, _nodeId);
    }
    if (StringUtils.isNotBlank(name)) {
        s_logger.debug("Updating Host name to: " + name);
        host.setName(name);
        _hostDao.update(host.getId(), host);
    }
    if (guestOSCategoryId != null) {
        // Verify that the guest OS Category exists
        if (!(guestOSCategoryId > 0) || _guestOSCategoryDao.findById(guestOSCategoryId) == null) {
            throw new InvalidParameterValueException("Please specify a valid guest OS category.");
        }
        final GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId);
        final DetailVO guestOSDetail = _hostDetailsDao.findDetail(hostId, "guest.os.category.id");
        if (guestOSCategory != null && !GuestOSCategoryVO.CATEGORY_NONE.equalsIgnoreCase(guestOSCategory.getName())) {
            // Create/Update an entry for guest.os.category.id
            if (guestOSDetail != null) {
                guestOSDetail.setValue(String.valueOf(guestOSCategory.getId()));
                _hostDetailsDao.update(guestOSDetail.getId(), guestOSDetail);
            } else {
                final Map<String, String> detail = new HashMap<String, String>();
                detail.put("guest.os.category.id", String.valueOf(guestOSCategory.getId()));
                _hostDetailsDao.persist(hostId, detail);
            }
        } else {
            // Delete any existing entry for guest.os.category.id
            if (guestOSDetail != null) {
                _hostDetailsDao.remove(guestOSDetail.getId());
            }
        }
    }
    final List<String> hostTags = cmd.getHostTags();
    if (hostTags != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Updating Host Tags to :" + hostTags);
        }
        _hostTagsDao.persist(hostId, new ArrayList(new HashSet<String>(hostTags)));
    }
    final String url = cmd.getUrl();
    if (url != null) {
        _storageMgr.updateSecondaryStorage(cmd.getId(), cmd.getUrl());
    }
    final HostVO updatedHost = _hostDao.findById(hostId);
    return updatedHost;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DetailVO(com.cloud.host.DetailVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) GuestOSCategoryVO(com.cloud.storage.GuestOSCategoryVO) HashSet(java.util.HashSet)

Example 27 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method deleteExternalFirewall.

@Override
public boolean deleteExternalFirewall(Long hostId) {
    HostVO externalFirewall = _hostDao.findById(hostId);
    if (externalFirewall == null) {
        throw new InvalidParameterValueException("Could not find an external firewall with ID: " + hostId);
    }
    DetailVO fwHostDetails = _hostDetailDao.findDetail(hostId, ApiConstants.FIREWALL_DEVICE_ID);
    long fwDeviceId = Long.parseLong(fwHostDetails.getValue());
    // check if any networks are using this balancer device
    List<NetworkExternalFirewallVO> networks = _networkExternalFirewallDao.listByFirewallDeviceId(fwDeviceId);
    if ((networks != null) && !networks.isEmpty()) {
        throw new CloudRuntimeException("Delete can not be done as there are networks using the firewall device ");
    }
    try {
        // put the host in maintenance state in order for it to be deleted
        externalFirewall.setResourceState(ResourceState.Maintenance);
        _hostDao.update(hostId, externalFirewall);
        _resourceMgr.deleteHost(hostId, false, false);
        // delete the external load balancer entry
        _externalFirewallDeviceDao.remove(fwDeviceId);
        return true;
    } catch (Exception e) {
        s_logger.debug("Failed to delete external firewall device due to " + e.getMessage());
        return false;
    }
}
Also used : NetworkExternalFirewallVO(com.cloud.network.dao.NetworkExternalFirewallVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DetailVO(com.cloud.host.DetailVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HostVO(com.cloud.host.HostVO) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException)

Example 28 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method addExternalFirewall.

@Override
@DB
public ExternalFirewallDeviceVO addExternalFirewall(long physicalNetworkId, String url, String username, String password, final String deviceName, ServerResource resource) {
    String guid;
    PhysicalNetworkVO pNetwork = null;
    NetworkDevice ntwkDevice = NetworkDevice.getNetworkDevice(deviceName);
    long zoneId;
    if ((ntwkDevice == null) || (url == null) || (username == null) || (resource == null) || (password == null)) {
        throw new InvalidParameterValueException("Atleast one of the required parameters (url, username, password," + " server resource, zone id/physical network id) is not specified or a valid parameter.");
    }
    pNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    zoneId = pNetwork.getDataCenterId();
    final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), ntwkDevice.getNetworkServiceProvder());
    if (ntwkSvcProvider == null) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkDevice.getNetworkServiceProvder() + " is not enabled in the physical network: " + physicalNetworkId + "to add this device");
    } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + " is not added or in shutdown state in the physical network: " + physicalNetworkId + "to add this device");
    }
    URI uri;
    try {
        uri = new URI(url);
    } catch (Exception e) {
        s_logger.debug(e);
        throw new InvalidParameterValueException(e.getMessage());
    }
    String ipAddress = uri.getHost();
    Map hostDetails = new HashMap<String, String>();
    guid = getExternalNetworkResourceGuid(pNetwork.getId(), deviceName, ipAddress);
    hostDetails.put("name", guid);
    hostDetails.put("guid", guid);
    hostDetails.put("zoneId", String.valueOf(pNetwork.getDataCenterId()));
    hostDetails.put("ip", ipAddress);
    hostDetails.put("physicalNetworkId", String.valueOf(pNetwork.getId()));
    hostDetails.put("username", username);
    hostDetails.put("password", password);
    hostDetails.put("deviceName", deviceName);
    final Map<String, String> configParams = new HashMap<String, String>();
    UrlUtil.parseQueryParameters(uri.getQuery(), false, configParams);
    hostDetails.putAll(configParams);
    // let the server resource to do parameters validation
    try {
        resource.configure(guid, hostDetails);
    } catch (ConfigurationException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    final Host externalFirewall = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalFirewall, hostDetails);
    if (externalFirewall != null) {
        final PhysicalNetworkVO pNetworkFinal = pNetwork;
        return Transaction.execute(new TransactionCallback<ExternalFirewallDeviceVO>() {

            @Override
            public ExternalFirewallDeviceVO doInTransaction(TransactionStatus status) {
                boolean dedicatedUse = (configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED)) : false;
                long capacity = NumbersUtil.parseLong(configParams.get(ApiConstants.FIREWALL_DEVICE_CAPACITY), 0);
                if (capacity == 0) {
                    capacity = _defaultFwCapacity;
                }
                ExternalFirewallDeviceVO fwDevice = new ExternalFirewallDeviceVO(externalFirewall.getId(), pNetworkFinal.getId(), ntwkSvcProvider.getProviderName(), deviceName, capacity, dedicatedUse);
                _externalFirewallDeviceDao.persist(fwDevice);
                DetailVO hostDetail = new DetailVO(externalFirewall.getId(), ApiConstants.FIREWALL_DEVICE_ID, String.valueOf(fwDevice.getId()));
                _hostDetailDao.persist(hostDetail);
                return fwDevice;
            }
        });
    } else {
        return null;
    }
}
Also used : ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) HashMap(java.util.HashMap) NetworkDevice(org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) URI(java.net.URI) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) DetailVO(com.cloud.host.DetailVO) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) Map(java.util.Map) HashMap(java.util.HashMap) DB(com.cloud.utils.db.DB)

Example 29 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ExternalLoadBalancerDeviceManagerImpl method deleteExternalLoadBalancer.

@Override
public boolean deleteExternalLoadBalancer(long hostId) {
    HostVO externalLoadBalancer = _hostDao.findById(hostId);
    if (externalLoadBalancer == null) {
        throw new InvalidParameterValueException("Could not find an external load balancer with ID: " + hostId);
    }
    DetailVO lbHostDetails = _hostDetailDao.findDetail(hostId, ApiConstants.LOAD_BALANCER_DEVICE_ID);
    long lbDeviceId = Long.parseLong(lbHostDetails.getValue());
    ExternalLoadBalancerDeviceVO lbDeviceVo = _externalLoadBalancerDeviceDao.findById(lbDeviceId);
    if (lbDeviceVo.getAllocationState() == LBDeviceAllocationState.Provider) {
        // check if cloudstack has provisioned any load balancer appliance on the device before deleting
        List<ExternalLoadBalancerDeviceVO> lbDevices = _externalLoadBalancerDeviceDao.listAll();
        if (lbDevices != null) {
            for (ExternalLoadBalancerDeviceVO lbDevice : lbDevices) {
                if (lbDevice.getParentHostId() == hostId) {
                    throw new CloudRuntimeException("This load balancer device can not be deleted as there are one or more load balancers applainces provisioned by cloudstack on the device.");
                }
            }
        }
    } else {
        // check if any networks are using this load balancer device
        List<NetworkExternalLoadBalancerVO> networks = _networkLBDao.listByLoadBalancerDeviceId(lbDeviceId);
        if ((networks != null) && !networks.isEmpty()) {
            throw new CloudRuntimeException("Delete can not be done as there are networks using this load balancer device ");
        }
    }
    try {
        // put the host in maintenance state in order for it to be deleted
        externalLoadBalancer.setResourceState(ResourceState.Maintenance);
        _hostDao.update(hostId, externalLoadBalancer);
        _resourceMgr.deleteHost(hostId, false, false);
        // delete the external load balancer entry
        _externalLoadBalancerDeviceDao.remove(lbDeviceId);
        return true;
    } catch (Exception e) {
        s_logger.debug(e);
        return false;
    }
}
Also used : ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DetailVO(com.cloud.host.DetailVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkExternalLoadBalancerVO(com.cloud.network.dao.NetworkExternalLoadBalancerVO) HostVO(com.cloud.host.HostVO) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException)

Example 30 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ExternalLoadBalancerDeviceManagerImpl method freeLoadBalancerForNetwork.

@DB
protected boolean freeLoadBalancerForNetwork(final Network guestConfig) {
    GlobalLock deviceMapLock = GlobalLock.getInternLock("LoadBalancerAllocLock");
    try {
        if (deviceMapLock.lock(120)) {
            ExternalLoadBalancerDeviceVO lbDevice = Transaction.execute(new TransactionCallback<ExternalLoadBalancerDeviceVO>() {

                @Override
                public ExternalLoadBalancerDeviceVO doInTransaction(TransactionStatus status) {
                    // since network is shutdown remove the network mapping to the load balancer device
                    NetworkExternalLoadBalancerVO networkLBDevice = _networkExternalLBDao.findByNetworkId(guestConfig.getId());
                    long lbDeviceId = networkLBDevice.getExternalLBDeviceId();
                    _networkExternalLBDao.remove(networkLBDevice.getId());
                    List<NetworkExternalLoadBalancerVO> ntwksMapped = _networkExternalLBDao.listByLoadBalancerDeviceId(networkLBDevice.getExternalLBDeviceId());
                    ExternalLoadBalancerDeviceVO lbDevice = _externalLoadBalancerDeviceDao.findById(lbDeviceId);
                    boolean lbInUse = !(ntwksMapped == null || ntwksMapped.isEmpty());
                    boolean lbCloudManaged = lbDevice.getIsManagedDevice();
                    if (!lbInUse && !lbCloudManaged) {
                        // this is the last network mapped to the load balancer device so set device allocation state to be free
                        lbDevice.setAllocationState(LBDeviceAllocationState.Free);
                        _externalLoadBalancerDeviceDao.update(lbDevice.getId(), lbDevice);
                    }
                    // commit the changes before sending agent command to destroy cloudstack managed LB
                    if (!lbInUse && lbCloudManaged) {
                        return lbDevice;
                    } else {
                        return null;
                    }
                }
            });
            if (lbDevice != null) {
                // send DestroyLoadBalancerApplianceCommand to the host where load balancer appliance is provisioned
                Host lbHost = _hostDao.findById(lbDevice.getHostId());
                String lbIP = lbHost.getPrivateIpAddress();
                DestroyLoadBalancerApplianceCommand lbDeleteCmd = new DestroyLoadBalancerApplianceCommand(lbIP);
                DestroyLoadBalancerApplianceAnswer answer = null;
                try {
                    answer = (DestroyLoadBalancerApplianceAnswer) _agentMgr.easySend(lbDevice.getParentHostId(), lbDeleteCmd);
                    if (answer == null || !answer.getResult()) {
                        s_logger.warn("Failed to destoy load balancer appliance used by the network" + guestConfig.getId() + " due to " + answer == null ? "communication error with agent" : answer.getDetails());
                    }
                } catch (Exception e) {
                    s_logger.warn("Failed to destroy load balancer appliance used by the network" + guestConfig.getId() + " due to " + e.getMessage());
                }
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Successfully destroyed load balancer appliance used for the network" + guestConfig.getId());
                }
                deviceMapLock.unlock();
                // remove the provisioned load balancer appliance from cloudstack
                deleteExternalLoadBalancer(lbHost.getId());
                // release the private IP back to dc pool, as the load balancer appliance is now destroyed
                _dcDao.releasePrivateIpAddress(lbHost.getPrivateIpAddress(), guestConfig.getDataCenterId(), null);
                // release the public IP allocated for this LB appliance
                DetailVO publicIpDetail = _hostDetailDao.findDetail(lbHost.getId(), "publicip");
                IPAddressVO ipVo = _ipAddressDao.findByIpAndDcId(guestConfig.getDataCenterId(), publicIpDetail.toString());
                _ipAddrMgr.disassociatePublicIpAddress(ipVo.getId(), _accountMgr.getSystemUser().getId(), _accountMgr.getSystemAccount());
            } else {
                deviceMapLock.unlock();
            }
            return true;
        } else {
            s_logger.error("Failed to release load balancer device for the network" + guestConfig.getId() + "as failed to acquire lock ");
            return false;
        }
    } catch (Exception exception) {
        s_logger.error("Failed to release load balancer device for the network" + guestConfig.getId() + " due to " + exception.getMessage());
    } finally {
        deviceMapLock.releaseRef();
    }
    return false;
}
Also used : TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) NetworkExternalLoadBalancerVO(com.cloud.network.dao.NetworkExternalLoadBalancerVO) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) GlobalLock(com.cloud.utils.db.GlobalLock) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) DestroyLoadBalancerApplianceCommand(com.cloud.agent.api.routing.DestroyLoadBalancerApplianceCommand) DetailVO(com.cloud.host.DetailVO) DestroyLoadBalancerApplianceAnswer(com.cloud.network.resource.DestroyLoadBalancerApplianceAnswer) ArrayList(java.util.ArrayList) List(java.util.List) IPAddressVO(com.cloud.network.dao.IPAddressVO) DB(com.cloud.utils.db.DB)

Aggregations

DetailVO (com.cloud.host.DetailVO)38 HostVO (com.cloud.host.HostVO)15 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)13 TransactionStatus (com.cloud.utils.db.TransactionStatus)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 ConfigurationException (javax.naming.ConfigurationException)13 HashMap (java.util.HashMap)12 Host (com.cloud.host.Host)9 DB (com.cloud.utils.db.DB)9 PhysicalNetworkServiceProviderVO (com.cloud.network.dao.PhysicalNetworkServiceProviderVO)8 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)8 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)7 NetworkDevice (org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice)6 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)5 InsufficientNetworkCapacityException (com.cloud.exception.InsufficientNetworkCapacityException)5 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 ServerResource (com.cloud.resource.ServerResource)5 UnableDeleteHostException (com.cloud.resource.UnableDeleteHostException)5 GuestOSCategoryVO (com.cloud.storage.GuestOSCategoryVO)4 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)4