Search in sources :

Example 1 with NoRetryStrategy

use of com.microsoft.azure.vmagent.retry.NoRetryStrategy in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMComputer method doDoDelete.

protected HttpResponse doDoDelete(ExecutionEngine executionEngine) {
    checkPermission(DELETE);
    this.setAcceptingTasks(false);
    final AzureVMAgent agent = getNode();
    if (agent != null) {
        Callable<Void> task = () -> {
            LOGGER.log(Level.INFO, "AzureVMComputer: doDoDelete called for agent {0}", agent.getNodeName());
            try {
                // Deprovision
                agent.deprovision(Messages._User_Delete());
            } catch (Exception e) {
                LOGGER.log(Level.INFO, "AzureVMComputer: doDoDelete: Exception occurred while deleting agent", e);
                throw AzureCloudException.create("AzureVMComputer: doDoDelete: Exception occurred while deleting agent", e);
            }
            return null;
        };
        try {
            executionEngine.executeAsync(task, new NoRetryStrategy());
        } catch (AzureCloudException exception) {
            // No need to throw exception back, just log and move on.
            LOGGER.log(Level.INFO, "AzureVMComputer: execute: failed to shutdown/delete " + agent.getDisplayName(), exception);
        }
    }
    return new HttpRedirect("..");
}
Also used : NoRetryStrategy(com.microsoft.azure.vmagent.retry.NoRetryStrategy) HttpRedirect(org.kohsuke.stapler.HttpRedirect) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException)

Example 2 with NoRetryStrategy

use of com.microsoft.azure.vmagent.retry.NoRetryStrategy in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMManagementServiceDelegate method terminateVirtualMachine.

/**
 * Terminates a virtual machine.
 *
 * @param vmName            VM name
 * @param resourceGroupName Resource group containing the VM
 */
public void terminateVirtualMachine(final String vmName, final String resourceGroupName, ExecutionEngine executionEngine) throws AzureCloudException {
    try {
        if (virtualMachineExists(vmName, resourceGroupName)) {
            List<URI> diskUrisToRemove = new ArrayList<>();
            List<String> diskIdToRemove = new ArrayList<>();
            if (!azureClient.virtualMachines().getByResourceGroup(resourceGroupName, vmName).isManagedDiskEnabled()) {
                // Mark OS disk for removal
                diskUrisToRemove.add(new URI(azureClient.virtualMachines().getByResourceGroup(resourceGroupName, vmName).osUnmanagedDiskVhdUri()));
            } else {
                diskIdToRemove.add(azureClient.virtualMachines().getByResourceGroup(resourceGroupName, vmName).osDiskId());
            }
            // TODO: Remove data disks or add option to do so?
            // Remove the VM
            LOGGER.log(Level.INFO, "Removing virtual machine {0}", vmName);
            azureClient.virtualMachines().deleteByResourceGroup(resourceGroupName, vmName);
            // Now remove the disks
            for (URI diskUri : diskUrisToRemove) {
                this.removeStorageBlob(diskUri, resourceGroupName);
            }
            for (String id : diskIdToRemove) {
                LOGGER.log(Level.INFO, "Removing managed disk with id: {0}", id);
                azureClient.disks().deleteById(id);
            }
            // If used managed Disk with custom vhd, we need to delete the temporary image.
            if (!diskIdToRemove.isEmpty()) {
                removeImage(azureClient, vmName, resourceGroupName);
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Exception while deleting VM", e);
        // Check if VM is already deleted: if VM is already deleted then just ignore exception.
        if (!Constants.ERROR_CODE_RESOURCE_NF.equalsIgnoreCase(e.getMessage())) {
            throw AzureCloudException.create(e);
        }
    } finally {
        LOGGER.log(Level.INFO, "Clean operation starting for {0} NIC and IP", vmName);
        executionEngine.executeAsync((Callable<Void>) () -> {
            removeIPName(resourceGroupName, vmName);
            return null;
        }, new NoRetryStrategy());
    }
}
Also used : NoRetryStrategy(com.microsoft.azure.vmagent.retry.NoRetryStrategy) URI(java.net.URI) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) FormException(hudson.model.Descriptor.FormException) ManagementException(com.azure.core.management.exception.ManagementException) IOException(java.io.IOException)

Aggregations

AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)2 NoRetryStrategy (com.microsoft.azure.vmagent.retry.NoRetryStrategy)2 ManagementException (com.azure.core.management.exception.ManagementException)1 FormException (hudson.model.Descriptor.FormException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 HttpRedirect (org.kohsuke.stapler.HttpRedirect)1