Search in sources :

Example 1 with GenericResource

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

the class AzureVMAgentCleanUpTask method cleanLeakedResources.

public void cleanLeakedResources(AzureVMCloud cloud, String resourceGroup, DeploymentRegistrar deploymentRegistrar) {
    try {
        final List<String> validVMs = getValidVMs();
        final AzureResourceManager azureClient = cloud.getAzureClient();
        final AzureVMManagementServiceDelegate serviceDelegate = cloud.getServiceDelegate();
        // can't use listByTag because for some reason that method strips all the tags from the outputted resources
        // (https://github.com/Azure/azure-sdk-for-java/issues/1436)
        final PagedIterable<GenericResource> resources = azureClient.genericResources().listByResourceGroup(resourceGroup);
        if (resources == null || !resources.iterator().hasNext()) {
            LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: No resources found in rg: " + resourceGroup);
            return;
        }
        final PriorityQueue<GenericResource> resourcesMarkedForDeletion = new PriorityQueue<>(10, new Comparator<GenericResource>() {

            @Override
            public int compare(GenericResource o1, GenericResource o2) {
                int o1Priority = getPriority(o1);
                int o2Priority = getPriority(o2);
                if (o1Priority == o2Priority) {
                    return 0;
                }
                return (o1Priority < o2Priority) ? -1 : 1;
            }

            private int getPriority(GenericResource resource) {
                // suppress magic number check
                // CHECKSTYLE:OFF
                final String type = resource.type();
                if (StringUtils.containsIgnoreCase(type, "virtualMachine")) {
                    return 1;
                }
                if (StringUtils.containsIgnoreCase(type, "networkInterface")) {
                    return 2;
                }
                if (StringUtils.containsIgnoreCase(type, "IPAddress")) {
                    return 3;
                }
                return 4;
            // CHECKSTYLE:ON
            }
        });
        LOGGER.log(getNormalLoggingLevel(), String.format("cleanLeakedResources: beginning to look at leaked " + "resources in rg: %s", resourceGroup));
        for (GenericResource resource : resources) {
            final Map<String, String> tags = resource.tags();
            if (!tags.containsKey(Constants.AZURE_RESOURCES_TAG_NAME) || !deploymentRegistrar.getDeploymentTag().matches(new AzureUtil.DeploymentTag(tags.get(Constants.AZURE_RESOURCES_TAG_NAME)))) {
                continue;
            }
            boolean shouldSkipDeletion = false;
            for (String validVM : validVMs) {
                if (resource.name().contains(validVM)) {
                    shouldSkipDeletion = true;
                    break;
                }
            }
            // we're not removing storage accounts of networks - someone else might be using them
            if (shouldSkipDeletion || StringUtils.containsIgnoreCase(resource.type(), "StorageAccounts") || StringUtils.containsIgnoreCase(resource.type(), "virtualNetworks")) {
                continue;
            }
            resourcesMarkedForDeletion.add(resource);
        }
        LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: %d resources marked for deletion" + resourcesMarkedForDeletion.size());
        while (!resourcesMarkedForDeletion.isEmpty()) {
            try {
                final GenericResource resource = resourcesMarkedForDeletion.poll();
                if (resource == null) {
                    LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: resource was null continuing");
                    continue;
                }
                LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: looking at {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
                URI osDiskURI = null;
                String managedOsDiskId = null;
                if (StringUtils.containsIgnoreCase(resource.type(), "virtualMachine")) {
                    LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: retrieving VM {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
                    VirtualMachine virtualMachine = azureClient.virtualMachines().getById(resource.id());
                    if (!virtualMachine.isManagedDiskEnabled()) {
                        osDiskURI = new URI(virtualMachine.osUnmanagedDiskVhdUri());
                    } else {
                        managedOsDiskId = virtualMachine.osDiskId();
                    }
                    LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: completed retrieving VM {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
                }
                LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: deleting {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
                azureClient.genericResources().deleteById(resource.id());
                if (osDiskURI != null) {
                    serviceDelegate.removeStorageBlob(osDiskURI, resourceGroup);
                }
                if (managedOsDiskId != null) {
                    azureClient.disks().deleteById(managedOsDiskId);
                    serviceDelegate.removeImage(azureClient, resource.name(), resourceGroup);
                }
                LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: deleted {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Failed to clean resource ", e);
            }
        }
    } catch (Exception e) {
        // No need to throw exception back, just log and move on.
        LOGGER.log(Level.WARNING, "Failed to clean leaked resources ", e);
    }
}
Also used : AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) PriorityQueue(java.util.PriorityQueue) URI(java.net.URI) TimeoutException(java.util.concurrent.TimeoutException) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) GenericResource(com.azure.resourcemanager.resources.models.GenericResource) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine)

Example 2 with GenericResource

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

the class ITAzureVMAgentCleanUpTask method cleanLeakedResourcesRemovesDeployedResources.

@Test
public void cleanLeakedResourcesRemovesDeployedResources() throws Exception {
    final AzureUtil.DeploymentTag tagValue = new AzureUtil.DeploymentTag("some_value/123");
    final AzureUtil.DeploymentTag matchingTagValue = new AzureUtil.DeploymentTag("some_value/9999123");
    final String cloudName = "some_cloud_name";
    AzureVMCloud cloud = mock(AzureVMCloud.class);
    when(cloud.getCloudName()).thenReturn(cloudName);
    when(cloud.getAzureClient()).thenReturn(azureClient);
    when(cloud.getServiceDelegate()).thenReturn(delegate);
    final DeploymentRegistrar deploymentRegistrarMock = mock(DeploymentRegistrar.class);
    when(deploymentRegistrarMock.getDeploymentTag()).thenReturn(tagValue);
    final DeploymentRegistrar deploymentRegistrarMock_matching = mock(DeploymentRegistrar.class);
    when(deploymentRegistrarMock_matching.getDeploymentTag()).thenReturn(matchingTagValue);
    final AzureVMDeploymentInfo deployment = createDefaultDeployment(2, deploymentRegistrarMock);
    final List<String> validVMs = Collections.singletonList(deployment.getVmBaseName() + "0");
    AzureVMAgentCleanUpTask cleanUpTask = spy(AzureVMAgentCleanUpTask.class);
    when(cleanUpTask.getValidVMs()).thenReturn(validVMs);
    // should remove second deployment
    cleanUpTask.cleanLeakedResources(cloud, testEnv.azureResourceGroup, deploymentRegistrarMock_matching);
    // give time for azure to realize that some resources are missing
    Thread.sleep(20 * 1000);
    PagedIterable<GenericResource> resources = azureClient.genericResources().listByResourceGroup(testEnv.azureResourceGroup);
    for (GenericResource resource : resources) {
        if (StringUtils.containsIgnoreCase(resource.type(), "storageAccounts") || StringUtils.containsIgnoreCase(resource.type(), "virtualNetworks")) {
            continue;
        }
        if (resource.tags().get(Constants.AZURE_RESOURCES_TAG_NAME) != null && matchingTagValue.matches(new AzureUtil.DeploymentTag(resource.tags().get(Constants.AZURE_RESOURCES_TAG_NAME)))) {
            String resourceName = resource.name();
            String depl = deployment.getVmBaseName() + "0";
            Assert.assertTrue("Resource shouldn't exist: " + resourceName + " (vmbase: " + depl + " )", resourceName.contains(depl));
        }
    }
}
Also used : DeploymentRegistrar(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) GenericResource(com.azure.resourcemanager.resources.models.GenericResource) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) Test(org.junit.Test)

Aggregations

GenericResource (com.azure.resourcemanager.resources.models.GenericResource)2 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)2 AzureResourceManager (com.azure.resourcemanager.AzureResourceManager)1 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)1 AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)1 DeploymentRegistrar (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar)1 AzureVMCloud (com.microsoft.azure.vmagent.AzureVMCloud)1 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)1 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 PriorityQueue (java.util.PriorityQueue)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1