Search in sources :

Example 6 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cosmic by MissionCriticalCloud.

the class MigrateVirtualMachineWithVolumeCmd method execute.

@Override
public void execute() {
    final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
    if (userVm == null) {
        throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
    }
    final Host destinationHost = _resourceService.getHost(getHostId());
    if (destinationHost == null) {
        throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id =" + getHostId());
    }
    try {
        final VirtualMachine migratedVm = _userVmService.migrateVirtualMachineWithVolume(getVirtualMachineId(), destinationHost, getVolumeToPool());
        if (migratedVm != null) {
            final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm) migratedVm).get(0);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm");
        }
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException | ManagementServerException | VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Host(com.cloud.host.Host) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) UserVmResponse(com.cloud.api.response.UserVmResponse) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 7 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class KubernetesClusterResourceModifierActionWorker method provisionKubernetesClusterNodeVms.

protected List<UserVm> provisionKubernetesClusterNodeVms(final long nodeCount, final int offset, final String publicIpAddress) throws ManagementServerException, ResourceUnavailableException, InsufficientCapacityException {
    List<UserVm> nodes = new ArrayList<>();
    for (int i = offset + 1; i <= nodeCount; i++) {
        UserVm vm = createKubernetesNode(publicIpAddress);
        addKubernetesClusterVm(kubernetesCluster.getId(), vm.getId(), false);
        if (kubernetesCluster.getNodeRootDiskSize() > 0) {
            resizeNodeVolume(vm);
        }
        startKubernetesVM(vm);
        vm = userVmDao.findById(vm.getId());
        if (vm == null) {
            throw new ManagementServerException(String.format("Failed to provision worker VM for Kubernetes cluster : %s", kubernetesCluster.getName()));
        }
        nodes.add(vm);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Provisioned node VM : %s in to the Kubernetes cluster : %s", vm.getDisplayName(), kubernetesCluster.getName()));
        }
    }
    return nodes;
}
Also used : UserVm(com.cloud.uservm.UserVm) ManagementServerException(com.cloud.exception.ManagementServerException) ArrayList(java.util.ArrayList)

Example 8 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class KubernetesClusterResourceModifierActionWorker method resizeNodeVolume.

protected void resizeNodeVolume(final UserVm vm) throws ManagementServerException {
    try {
        if (vm.getHypervisorType() == Hypervisor.HypervisorType.VMware && templateDao.findById(vm.getTemplateId()).isDeployAsIs()) {
            List<VolumeVO> vmVols = volumeDao.findByInstance(vm.getId());
            for (VolumeVO volumeVO : vmVols) {
                if (volumeVO.getVolumeType() == Volume.Type.ROOT) {
                    ResizeVolumeCmd resizeVolumeCmd = new ResizeVolumeCmd();
                    resizeVolumeCmd = ComponentContext.inject(resizeVolumeCmd);
                    Field f = resizeVolumeCmd.getClass().getDeclaredField("size");
                    Field f1 = resizeVolumeCmd.getClass().getDeclaredField("id");
                    f.setAccessible(true);
                    f1.setAccessible(true);
                    f1.set(resizeVolumeCmd, volumeVO.getId());
                    f.set(resizeVolumeCmd, kubernetesCluster.getNodeRootDiskSize());
                    volumeService.resizeVolume(resizeVolumeCmd);
                }
            }
        }
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new ManagementServerException(String.format("Failed to resize volume of  VM in the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
}
Also used : Field(java.lang.reflect.Field) ResizeVolumeCmd(org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd) VolumeVO(com.cloud.storage.VolumeVO) ManagementServerException(com.cloud.exception.ManagementServerException)

Example 9 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class KubernetesClusterStartWorker method provisionKubernetesClusterAdditionalControlVms.

private List<UserVm> provisionKubernetesClusterAdditionalControlVms(final String publicIpAddress) throws InsufficientCapacityException, ManagementServerException, ResourceUnavailableException {
    List<UserVm> additionalControlVms = new ArrayList<>();
    if (kubernetesCluster.getControlNodeCount() > 1) {
        for (int i = 1; i < kubernetesCluster.getControlNodeCount(); i++) {
            UserVm vm = null;
            vm = createKubernetesAdditionalControlNode(publicIpAddress, i);
            addKubernetesClusterVm(kubernetesCluster.getId(), vm.getId(), true);
            if (kubernetesCluster.getNodeRootDiskSize() > 0) {
                resizeNodeVolume(vm);
            }
            startKubernetesVM(vm);
            vm = userVmDao.findById(vm.getId());
            if (vm == null) {
                throw new ManagementServerException(String.format("Failed to provision additional control VM for Kubernetes cluster : %s", kubernetesCluster.getName()));
            }
            additionalControlVms.add(vm);
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info(String.format("Provisioned additional control VM : %s in to the Kubernetes cluster : %s", vm.getDisplayName(), kubernetesCluster.getName()));
            }
        }
    }
    return additionalControlVms;
}
Also used : UserVm(com.cloud.uservm.UserVm) ManagementServerException(com.cloud.exception.ManagementServerException) ArrayList(java.util.ArrayList)

Example 10 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class KubernetesClusterStartWorker method startKubernetesClusterOnCreate.

public boolean startKubernetesClusterOnCreate() {
    init();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info(String.format("Starting Kubernetes cluster : %s", kubernetesCluster.getName()));
    }
    final long startTimeoutTime = System.currentTimeMillis() + KubernetesClusterService.KubernetesClusterStartTimeout.value() * 1000;
    stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.StartRequested);
    DeployDestination dest = null;
    try {
        dest = plan();
    } catch (InsufficientCapacityException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Provisioning the cluster failed due to insufficient capacity in the Kubernetes cluster: %s", kubernetesCluster.getUuid()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
    }
    Network network = null;
    try {
        network = startKubernetesClusterNetwork(dest);
    } catch (ManagementServerException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Failed to start Kubernetes cluster : %s as its network cannot be started", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
    }
    Pair<String, Integer> publicIpSshPort = getKubernetesClusterServerIpSshPort(null);
    publicIpAddress = publicIpSshPort.first();
    if (StringUtils.isEmpty(publicIpAddress) && (Network.GuestType.Isolated.equals(network.getGuestType()) || kubernetesCluster.getControlNodeCount() > 1)) {
        // Shared network, single-control node cluster won't have an IP yet
        logTransitStateAndThrow(Level.ERROR, String.format("Failed to start Kubernetes cluster : %s as no public IP found for the cluster", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed);
    }
    // Allow account creating the kubernetes cluster to access systemVM template
    LaunchPermissionVO launchPermission = new LaunchPermissionVO(clusterTemplate.getId(), owner.getId());
    launchPermissionDao.persist(launchPermission);
    List<UserVm> clusterVMs = new ArrayList<>();
    UserVm k8sControlVM = null;
    try {
        k8sControlVM = provisionKubernetesClusterControlVm(network, publicIpAddress);
    } catch (CloudRuntimeException | ManagementServerException | ResourceUnavailableException | InsufficientCapacityException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Provisioning the control VM failed in the Kubernetes cluster : %s", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
    }
    clusterVMs.add(k8sControlVM);
    if (StringUtils.isEmpty(publicIpAddress)) {
        publicIpSshPort = getKubernetesClusterServerIpSshPort(k8sControlVM);
        publicIpAddress = publicIpSshPort.first();
        if (StringUtils.isEmpty(publicIpAddress)) {
            logTransitStateAndThrow(Level.WARN, String.format("Failed to start Kubernetes cluster : %s as no public IP found for the cluster", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed);
        }
    }
    try {
        List<UserVm> additionalControlVMs = provisionKubernetesClusterAdditionalControlVms(publicIpAddress);
        clusterVMs.addAll(additionalControlVMs);
    } catch (CloudRuntimeException | ManagementServerException | ResourceUnavailableException | InsufficientCapacityException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Provisioning additional control VM failed in the Kubernetes cluster : %s", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
    }
    try {
        List<UserVm> nodeVMs = provisionKubernetesClusterNodeVms(kubernetesCluster.getNodeCount(), publicIpAddress);
        clusterVMs.addAll(nodeVMs);
    } catch (CloudRuntimeException | ManagementServerException | ResourceUnavailableException | InsufficientCapacityException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Provisioning node VM failed in the Kubernetes cluster : %s", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info(String.format("Kubernetes cluster : %s VMs successfully provisioned", kubernetesCluster.getName()));
    }
    try {
        setupKubernetesClusterNetworkRules(network, clusterVMs);
    } catch (ManagementServerException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s, unable to setup network rules", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
    }
    attachIsoKubernetesVMs(clusterVMs);
    if (!KubernetesClusterUtil.isKubernetesClusterControlVmRunning(kubernetesCluster, publicIpAddress, publicIpSshPort.second(), startTimeoutTime)) {
        String msg = String.format("Failed to setup Kubernetes cluster : %s in usable state as unable to access control node VMs of the cluster", kubernetesCluster.getName());
        if (kubernetesCluster.getControlNodeCount() > 1 && Network.GuestType.Shared.equals(network.getGuestType())) {
            msg = String.format("%s. Make sure external load-balancer has port forwarding rules for SSH access on ports %d-%d and API access on port %d", msg, CLUSTER_NODES_DEFAULT_START_SSH_PORT, CLUSTER_NODES_DEFAULT_START_SSH_PORT + kubernetesCluster.getTotalNodeCount() - 1, CLUSTER_API_PORT);
        }
        logTransitStateDetachIsoAndThrow(Level.ERROR, msg, kubernetesCluster, clusterVMs, KubernetesCluster.Event.CreateFailed, null);
    }
    boolean k8sApiServerSetup = KubernetesClusterUtil.isKubernetesClusterServerRunning(kubernetesCluster, publicIpAddress, CLUSTER_API_PORT, startTimeoutTime, 15000);
    if (!k8sApiServerSetup) {
        logTransitStateDetachIsoAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s in usable state as unable to provision API endpoint for the cluster", kubernetesCluster.getName()), kubernetesCluster, clusterVMs, KubernetesCluster.Event.CreateFailed, null);
    }
    sshPort = publicIpSshPort.second();
    updateKubernetesClusterEntryEndpoint();
    boolean readyNodesCountValid = KubernetesClusterUtil.validateKubernetesClusterReadyNodesCount(kubernetesCluster, publicIpAddress, sshPort, getControlNodeLoginUser(), sshKeyFile, startTimeoutTime, 15000);
    detachIsoKubernetesVMs(clusterVMs);
    if (!readyNodesCountValid) {
        logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s as it does not have desired number of nodes in ready state", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed);
    }
    if (!isKubernetesClusterKubeConfigAvailable(startTimeoutTime)) {
        logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s in usable state as unable to retrieve kube-config for the cluster", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
    }
    if (!isKubernetesClusterDashboardServiceRunning(true, startTimeoutTime)) {
        logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s in usable state as unable to get Dashboard service running for the cluster", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
    }
    taintControlNodes();
    deployProvider();
    updateLoginUserDetails(clusterVMs.stream().map(InternalIdentity::getId).collect(Collectors.toList()));
    stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.OperationSucceeded);
    return true;
}
Also used : ArrayList(java.util.ArrayList) LaunchPermissionVO(com.cloud.storage.LaunchPermissionVO) UserVm(com.cloud.uservm.UserVm) ManagementServerException(com.cloud.exception.ManagementServerException) DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InternalIdentity(org.apache.cloudstack.api.InternalIdentity)

Aggregations

ManagementServerException (com.cloud.exception.ManagementServerException)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)25 UserVm (com.cloud.uservm.UserVm)18 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)17 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)16 VirtualMachine (com.cloud.vm.VirtualMachine)10 ServerApiException (com.cloud.api.ServerApiException)8 Host (com.cloud.host.Host)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 ServerApiException (org.apache.cloudstack.api.ServerApiException)6 UserVmResponse (com.cloud.api.response.UserVmResponse)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)5 ArrayList (java.util.ArrayList)5 StoragePool (com.cloud.storage.StoragePool)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)4 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)4 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)3 KubernetesClusterVmMapVO (com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO)3 IpAddress (com.cloud.network.IpAddress)3