Search in sources :

Example 1 with DeploymentOperation

use of com.azure.resourcemanager.resources.models.DeploymentOperation in project azure-vm-agents-plugin by jenkinsci.

the class IntegrationTest method areAllVMsDeployed.

protected boolean areAllVMsDeployed(final List<String> vmNames) throws AzureCloudException {
    int deployedVMs = 0;
    PagedIterable<Deployment> deployments = azureClient.deployments().listByResourceGroup(testEnv.azureResourceGroup);
    for (Deployment dep : deployments) {
        PagedIterable<DeploymentOperation> ops = dep.deploymentOperations().list();
        for (DeploymentOperation op : ops) {
            if (op.targetResource() == null) {
                continue;
            }
            final String resource = op.targetResource().resourceName();
            final String state = op.provisioningState();
            if (op.targetResource().resourceType().contains("virtualMachine")) {
                switch(state.toLowerCase()) {
                    case "creating":
                    case "running":
                        return false;
                    case "succeeded":
                        for (String vmName : vmNames) {
                            if (resource.equalsIgnoreCase(vmName)) {
                                deployedVMs++;
                                break;
                            }
                        }
                        break;
                    default:
                        throw new IllegalStateException(String.format("the state of VM %s is '%s', message: %s", resource, state, op.statusMessage()));
                }
            }
        }
    }
    return deployedVMs == vmNames.size();
}
Also used : Deployment(com.azure.resourcemanager.resources.models.Deployment) DeploymentOperation(com.azure.resourcemanager.resources.models.DeploymentOperation)

Example 2 with DeploymentOperation

use of com.azure.resourcemanager.resources.models.DeploymentOperation in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMCloud method createProvisionedAgent.

/**
 * Once a new deployment is created, construct a new AzureVMAgent object
 * given information about the template.
 *
 * @param template       Template used to create the new agent
 * @param vmName         Name of the created VM
 * @param deploymentName Name of the deployment containing the VM
 * @return New agent
 * @throws AzureCloudException If the agent cannot be created
 */
public AzureVMAgent createProvisionedAgent(ProvisioningActivity.Id provisioningId, AzureVMAgentTemplate template, String vmName, String deploymentName) throws AzureCloudException {
    LOGGER.log(Level.INFO, "Waiting for deployment {0} with VM {1} to be completed", new Object[] { deploymentName, vmName });
    final int sleepTimeInSeconds = 30;
    final int timeoutInSeconds = getDeploymentTimeout();
    final int maxTries = timeoutInSeconds / sleepTimeInSeconds;
    int triesLeft = maxTries;
    do {
        triesLeft--;
        try {
            Thread.sleep(sleepTimeInSeconds * MILLIS_IN_SECOND);
        } catch (InterruptedException ex) {
        // ignore
        }
        try {
            // Create a new RM client each time because the config may expire while
            // in this long running operation
            final AzureResourceManager newAzureClient = template.retrieveAzureCloudReference().getAzureClient();
            final Deployment dep = newAzureClient.deployments().getByResourceGroup(template.getResourceGroupName(), deploymentName);
            // Might find no deployment.
            if (dep == null) {
                throw AzureCloudException.create(String.format("Could not find deployment %s", deploymentName));
            }
            PagedIterable<DeploymentOperation> ops = dep.deploymentOperations().list();
            for (DeploymentOperation op : ops) {
                if (op.targetResource() == null) {
                    continue;
                }
                final String resource = op.targetResource().resourceName();
                final String type = op.targetResource().resourceType();
                final String state = op.provisioningState();
                if (op.targetResource().resourceType().contains("virtualMachine")) {
                    if (resource.equalsIgnoreCase(vmName)) {
                        if (!state.equalsIgnoreCase("creating") && !state.equalsIgnoreCase("succeeded") && !state.equalsIgnoreCase("running")) {
                            final String statusCode = op.statusCode();
                            final Object statusMessage = op.statusMessage();
                            String finalStatusMessage = statusCode;
                            if (statusMessage != null) {
                                finalStatusMessage += " - " + statusMessage;
                            }
                            throw AzureCloudException.create(String.format("Deployment %s: %s:%s - %s", state, type, resource, finalStatusMessage));
                        } else if (state.equalsIgnoreCase("succeeded")) {
                            LOGGER.log(Level.FINE, "VM available: {0}", resource);
                            final VirtualMachine vm = newAzureClient.virtualMachines().getByResourceGroup(resourceGroupName, resource);
                            final OperatingSystemTypes osType = vm.storageProfile().osDisk().osType();
                            AzureVMAgent newAgent = getServiceDelegate().parseResponse(provisioningId, vmName, deploymentName, template, osType);
                            getServiceDelegate().setVirtualMachineDetails(newAgent, template);
                            return newAgent;
                        } else {
                            LOGGER.log(Level.INFO, "Deployment {0} not yet finished ({1}): {2}:{3} - waited {4} seconds", new Object[] { deploymentName, state, type, resource, (maxTries - triesLeft) * sleepTimeInSeconds });
                        }
                    }
                }
            }
        } catch (AzureCloudException e) {
            throw e;
        } catch (Exception e) {
            throw AzureCloudException.create(e);
        }
    } while (triesLeft > 0);
    throw AzureCloudException.create(String.format("Deployment %s failed, max timeout reached (%d seconds)", deploymentName, timeoutInSeconds));
}
Also used : OperatingSystemTypes(com.azure.resourcemanager.compute.models.OperatingSystemTypes) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) Deployment(com.azure.resourcemanager.resources.models.Deployment) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) ServletException(javax.servlet.ServletException) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DeploymentOperation(com.azure.resourcemanager.resources.models.DeploymentOperation) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine)

Aggregations

Deployment (com.azure.resourcemanager.resources.models.Deployment)2 DeploymentOperation (com.azure.resourcemanager.resources.models.DeploymentOperation)2 AzureResourceManager (com.azure.resourcemanager.AzureResourceManager)1 OperatingSystemTypes (com.azure.resourcemanager.compute.models.OperatingSystemTypes)1 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)1 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1