Search in sources :

Example 31 with Host

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

the class RollingMaintenanceManagerImpl method startRollingMaintenance.

@Override
public Ternary<Boolean, String, Pair<List<HostUpdated>, List<HostSkipped>>> startRollingMaintenance(StartRollingMaintenanceCmd cmd) {
    Pair<ResourceType, List<Long>> pair = getResourceTypeAndIdPair(cmd.getPodIds(), cmd.getClusterIds(), cmd.getZoneIds(), cmd.getHostIds());
    ResourceType type = pair.first();
    List<Long> ids = pair.second();
    int timeout = cmd.getTimeout() == null ? KvmRollingMaintenanceStageTimeout.value() : cmd.getTimeout();
    String payload = cmd.getPayload();
    Boolean forced = cmd.getForced();
    Set<Long> disabledClusters = new HashSet<>();
    Map<Long, String> hostsToAvoidMaintenance = new HashMap<>();
    boolean success = false;
    String details = null;
    List<HostUpdated> hostsUpdated = new ArrayList<>();
    List<HostSkipped> hostsSkipped = new ArrayList<>();
    if (timeout <= KvmRollingMaintenancePingInterval.value()) {
        return new Ternary<>(success, "The timeout value provided must be greater or equal than the ping interval " + "defined in '" + KvmRollingMaintenancePingInterval.key() + "'", new Pair<>(hostsUpdated, hostsSkipped));
    }
    try {
        Map<Long, List<Host>> hostsByCluster = getHostsByClusterForRollingMaintenance(type, ids);
        for (Long clusterId : hostsByCluster.keySet()) {
            Cluster cluster = resourceManager.getCluster(clusterId);
            List<Host> hosts = hostsByCluster.get(clusterId);
            if (!isMaintenanceAllowedByVMStates(cluster, hosts, hostsSkipped)) {
                if (forced) {
                    continue;
                }
                success = false;
                details = "VMs in invalid states in cluster: " + cluster.getUuid();
                return new Ternary<>(success, details, new Pair<>(hostsUpdated, hostsSkipped));
            }
            disableClusterIfEnabled(cluster, disabledClusters);
            s_logger.debug("State checks on the hosts in the cluster");
            performStateChecks(cluster, hosts, forced, hostsSkipped);
            s_logger.debug("Checking hosts capacity before attempting rolling maintenance");
            performCapacityChecks(cluster, hosts, forced);
            s_logger.debug("Attempting pre-flight stages on each host before starting rolling maintenance");
            performPreFlightChecks(hosts, timeout, payload, forced, hostsToAvoidMaintenance);
            for (Host host : hosts) {
                Ternary<Boolean, Boolean, String> hostResult = startRollingMaintenanceHostInCluster(cluster, host, timeout, payload, forced, hostsToAvoidMaintenance, hostsUpdated, hostsSkipped);
                if (hostResult.second()) {
                    continue;
                }
                if (hostResult.first()) {
                    success = false;
                    details = hostResult.third();
                    return new Ternary<>(success, details, new Pair<>(hostsUpdated, hostsSkipped));
                }
            }
            enableClusterIfDisabled(cluster, disabledClusters);
        }
    } catch (AgentUnavailableException | InterruptedException | CloudRuntimeException e) {
        String err = "Error starting rolling maintenance: " + e.getMessage();
        s_logger.error(err, e);
        success = false;
        details = err;
        return new Ternary<>(success, details, new Pair<>(hostsUpdated, hostsSkipped));
    } finally {
        // Enable back disabled clusters
        for (Long clusterId : disabledClusters) {
            Cluster cluster = resourceManager.getCluster(clusterId);
            if (cluster.getAllocationState() == Grouping.AllocationState.Disabled) {
                updateCluster(clusterId, "Enabled");
            }
        }
        generateReportAndFinishingEvent(cmd, success, details, hostsUpdated, hostsSkipped);
    }
    success = true;
    details = "OK";
    return new Ternary<>(success, details, new Pair<>(hostsUpdated, hostsSkipped));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Pair(com.cloud.utils.Pair) Ternary(com.cloud.utils.Ternary) Cluster(com.cloud.org.Cluster) Host(com.cloud.host.Host)

Example 32 with Host

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

the class RollingMaintenanceManagerImpl method performStateChecks.

/**
 * Perform state checks on the hosts in a cluster
 */
protected void performStateChecks(Cluster cluster, List<Host> hosts, Boolean forced, List<HostSkipped> hostsSkipped) {
    List<Host> hostsToDrop = new ArrayList<>();
    for (Host host : hosts) {
        if (host.getStatus() != Status.Up) {
            String msg = "Host " + host.getUuid() + " is not connected, state = " + host.getStatus().toString();
            if (forced) {
                hostsSkipped.add(new HostSkipped(host, msg));
                hostsToDrop.add(host);
                continue;
            }
            throw new CloudRuntimeException(msg);
        }
        if (host.getResourceState() != ResourceState.Enabled) {
            String msg = "Host " + host.getUuid() + " is not enabled, state = " + host.getResourceState().toString();
            if (forced) {
                hostsSkipped.add(new HostSkipped(host, msg));
                hostsToDrop.add(host);
                continue;
            }
            throw new CloudRuntimeException(msg);
        }
    }
    if (CollectionUtils.isNotEmpty(hostsToDrop)) {
        hosts.removeAll(hostsToDrop);
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host)

Example 33 with Host

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

the class BaremetalDhcpElement method prepare.

@Override
@DB
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    Host host = dest.getHost();
    if (vm.getType() != Type.User || vm.getHypervisorType() != HypervisorType.BareMetal) {
        return false;
    }
    nic.setMacAddress(host.getPrivateMacAddress());
    NicVO vo = _nicDao.findById(nic.getId());
    assert vo != null : "Where ths nic " + nic.getId() + " going???";
    vo.setMacAddress(nic.getMacAddress());
    _nicDao.update(vo.getId(), vo);
    return true;
}
Also used : Host(com.cloud.host.Host) NicVO(com.cloud.vm.NicVO) DB(com.cloud.utils.db.DB)

Example 34 with Host

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

the class GloboDnsElement method addGloboDnsHost.

@Override
@DB
public Host addGloboDnsHost(Long physicalNetworkId, final String username, final String password, String url) {
    if (username == null || username.trim().isEmpty()) {
        throw new InvalidParameterValueException("Invalid username: " + username);
    }
    if (password == null || password.trim().isEmpty()) {
        throw new InvalidParameterValueException("Invalid password: " + password);
    }
    if (url == null || url.trim().isEmpty()) {
        throw new InvalidParameterValueException("Invalid url: " + url);
    }
    // validate physical network and zone
    // Check if physical network exists
    PhysicalNetwork pNtwk = null;
    if (physicalNetworkId != null) {
        pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
        if (pNtwk == null) {
            throw new InvalidParameterValueException("Unable to find a physical network having the specified physical network id");
        }
    } else {
        throw new InvalidParameterValueException("Invalid physicalNetworkId: " + physicalNetworkId);
    }
    final Long zoneId = pNtwk.getDataCenterId();
    final Map<String, String> params = new HashMap<String, String>();
    params.put("guid", "globodns-" + String.valueOf(zoneId));
    params.put("zoneId", String.valueOf(zoneId));
    params.put("name", Provider.GloboDns.getName());
    params.put("url", url);
    params.put("username", username);
    params.put("password", password);
    final Map<String, Object> hostDetails = new HashMap<String, Object>();
    hostDetails.putAll(params);
    Host host = Transaction.execute(new TransactionCallbackWithException<Host, CloudRuntimeException>() {

        @Override
        public Host doInTransaction(TransactionStatus status) throws CloudRuntimeException {
            try {
                GloboDnsResource resource = new GloboDnsResource();
                resource.configure(Provider.GloboDns.getName(), hostDetails);
                Host host = _resourceMgr.addHost(zoneId, resource, resource.getType(), params);
                if (host == null) {
                    throw new CloudRuntimeException("Failed to add GloboDNS host");
                }
                // Validate username and password by logging in
                SignInCommand cmd = new SignInCommand(username, password);
                Answer answer = callCommand(cmd, zoneId);
                if (answer == null || !answer.getResult()) {
                    // Could not sign in on GloboDNS
                    throw new ConfigurationException("Could not sign in on GloboDNS. Please verify URL, username and password.");
                }
                return host;
            } catch (ConfigurationException e) {
                throw new CloudRuntimeException(e);
            }
        }
    });
    return host;
}
Also used : SignInCommand(com.globo.globodns.cloudstack.commands.SignInCommand) HashMap(java.util.HashMap) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) GloboDnsResource(com.globo.globodns.cloudstack.resource.GloboDnsResource) Answer(com.cloud.agent.api.Answer) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PhysicalNetwork(com.cloud.network.PhysicalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Example 35 with Host

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

the class NetworkServiceImpl method replugNicsForUpdatedNetwork.

private void replugNicsForUpdatedNetwork(NetworkVO network) throws ResourceUnavailableException, InsufficientCapacityException {
    List<NicVO> nics = _nicDao.listByNetworkId(network.getId());
    Network updatedNetwork = getNetwork(network.getId());
    for (NicVO nic : nics) {
        long vmId = nic.getInstanceId();
        VMInstanceVO vm = _vmDao.findById(vmId);
        if (vm == null) {
            s_logger.error(String.format("Cannot replug NIC: %s as VM for it is not found with ID: %d", nic, vmId));
            continue;
        }
        if (!Hypervisor.HypervisorType.VMware.equals(vm.getHypervisorType())) {
            s_logger.debug(String.format("Cannot replug NIC: %s for VM: %s as it is not on VMware", nic, vm));
            continue;
        }
        if (!VirtualMachine.Type.User.equals(vm.getType())) {
            s_logger.debug(String.format("Cannot replug NIC: %s for VM: %s as it is not a user VM", nic, vm));
            continue;
        }
        if (!VirtualMachine.State.Running.equals(vm.getState())) {
            s_logger.debug(String.format("Cannot replug NIC: %s for VM: %s as it is not in running state", nic, vm));
            continue;
        }
        Host host = _hostDao.findById(vm.getHostId());
        VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm, null, null, null, null);
        NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.isSecurityGroupSupportedInNetwork(updatedNetwork), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
        vmManager.replugNic(updatedNetwork, vmManager.toNicTO(nicProfile, vm.getHypervisorType()), vmManager.toVmTO(vmProfile), host);
    }
}
Also used : VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) VMInstanceVO(com.cloud.vm.VMInstanceVO) Host(com.cloud.host.Host) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicProfile(com.cloud.vm.NicProfile) NicVO(com.cloud.vm.NicVO)

Aggregations

Host (com.cloud.host.Host)226 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)77 ArrayList (java.util.ArrayList)67 HashMap (java.util.HashMap)50 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)42 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)31 ServerApiException (org.apache.cloudstack.api.ServerApiException)30 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)28 Answer (com.cloud.agent.api.Answer)25 ConfigurationException (javax.naming.ConfigurationException)25 Pair (com.cloud.utils.Pair)24 Map (java.util.Map)24 StoragePool (com.cloud.storage.StoragePool)23 DeployDestination (com.cloud.deploy.DeployDestination)22 HostVO (com.cloud.host.HostVO)22 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)21 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)21 VolumeVO (com.cloud.storage.VolumeVO)21 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)20 List (java.util.List)20