Search in sources :

Example 1 with AzureResourceManager

use of com.azure.resourcemanager.AzureResourceManager in project azure-credentials-plugin by jenkinsci.

the class KeyVaultIntegrationTestBase method setUp.

@Before
public void setUp() throws InterruptedException {
    // Create Azure KeyVault
    final AzureResourceManager azureClient = IntegrationTestBase.getAzureClient();
    vaultName = "tst-vault-" + TestEnvironment.GenerateRandomString(5);
    final Vault vault = azureClient.vaults().define(vaultName).withRegion(testEnv.region).withNewResourceGroup(testEnv.resourceGroup).defineAccessPolicy().forServicePrincipal(testEnv.clientId).allowSecretAllPermissions().attach().create();
    vaultUri = vault.vaultUri();
    waitForKeyVaultAvailable();
    // Create Jenkins Azure Credentials
    final AzureCredentials credentials = new AzureCredentials(CredentialsScope.SYSTEM, jenkinsAzureCredentialsId, "", testEnv.subscriptionId, testEnv.clientId, Secret.fromString(testEnv.clientSecret));
    credentials.setTenant(testEnv.tenantId);
    final CredentialsStore store = CredentialsProvider.lookupStores(j.jenkins).iterator().next();
    try {
        store.addCredentials(Domain.global(), credentials);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : AzureCredentials(com.microsoft.azure.util.AzureCredentials) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Vault(com.azure.resourcemanager.keyvault.models.Vault) IOException(java.io.IOException) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) Before(org.junit.Before)

Example 2 with AzureResourceManager

use of com.azure.resourcemanager.AzureResourceManager in project azure-credentials-plugin by jenkinsci.

the class IntegrationTestBase method tearDownClass.

@AfterClass
public static void tearDownClass() {
    try {
        final AzureResourceManager azureClient = getAzureClient();
        azureClient.resourceGroups().deleteByNameAsync(testEnv.resourceGroup);
    } catch (ManagementException e) {
        if (e.getResponse().getStatusCode() != 404) {
            throw e;
        }
    }
}
Also used : ManagementException(com.azure.core.management.exception.ManagementException) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) AfterClass(org.junit.AfterClass)

Example 3 with AzureResourceManager

use of com.azure.resourcemanager.AzureResourceManager in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMAgentCleanUpTask method cleanDeployments.

public void cleanDeployments(long successTimeoutInMinutes, long failTimeoutInMinutes) {
    LOGGER.log(getNormalLoggingLevel(), "Cleaning deployments");
    // Walk the queue, popping and pushing until we reach an item that we've already
    // dealt with or the queue is empty.
    DeploymentInfo firstBackInQueue = null;
    ConcurrentLinkedQueue<DeploymentInfo> deploymentsToClean = DeploymentRegistrar.getInstance().getDeploymentsToClean();
    while (!deploymentsToClean.isEmpty() && firstBackInQueue != deploymentsToClean.peek()) {
        DeploymentInfo info = deploymentsToClean.remove();
        LOGGER.log(getNormalLoggingLevel(), "Checking deployment {0}", info.getDeploymentName());
        AzureVMCloud cloud = getCloud(info.getCloudName());
        if (cloud == null) {
            // Cloud could have been deleted, skip
            continue;
        }
        try {
            final AzureResourceManager azureClient = cloud.getAzureClient();
            final AzureVMManagementServiceDelegate delegate = cloud.getServiceDelegate();
            // This will throw if the deployment can't be found.  This could happen in a couple instances
            // 1) The deployment has already been deleted
            // 2) The deployment doesn't exist yet (race between creating the deployment and it
            // being accepted by Azure.
            // To avoid this, we implement a retry.  If we hit an exception, we will decrement the number
            // of retries.  If we hit 0, we remove the deployment from our list.
            Deployment deployment;
            try {
                deployment = azureClient.deployments().getByResourceGroup(info.getResourceGroupName(), info.getDeploymentName());
            } catch (NullPointerException e) {
                LOGGER.log(getNormalLoggingLevel(), "Deployment " + info.getDeploymentName() + " not found, skipping");
                continue;
            }
            if (deployment == null) {
                LOGGER.log(getNormalLoggingLevel(), "Deployment " + info.getDeploymentName() + " not found, skipping");
                continue;
            }
            OffsetDateTime deploymentTime = deployment.timestamp();
            LOGGER.log(getNormalLoggingLevel(), "Deployment created on {0}", deploymentTime.toString());
            long diffTimeInMinutes = ChronoUnit.MINUTES.between(deploymentTime, OffsetDateTime.now());
            String state = deployment.provisioningState();
            if (!state.equalsIgnoreCase("succeeded") && diffTimeInMinutes > failTimeoutInMinutes) {
                LOGGER.log(getNormalLoggingLevel(), "Failed deployment older than {0} minutes, deleting", failTimeoutInMinutes);
                // Delete the deployment
                azureClient.deployments().deleteByResourceGroup(info.getResourceGroupName(), info.getDeploymentName());
                if (StringUtils.isNotBlank(info.scriptUri)) {
                    delegate.removeStorageBlob(new URI(info.scriptUri), info.getResourceGroupName());
                }
            } else if (state.equalsIgnoreCase("succeeded") && diffTimeInMinutes > successTimeoutInMinutes) {
                LOGGER.log(getNormalLoggingLevel(), "Successful deployment older than {0} minutes, deleting", successTimeoutInMinutes);
                // Delete the deployment
                azureClient.deployments().deleteByResourceGroup(info.getResourceGroupName(), info.getDeploymentName());
                if (StringUtils.isNotBlank(info.scriptUri)) {
                    delegate.removeStorageBlob(new URI(info.scriptUri), info.getResourceGroupName());
                }
            } else {
                LOGGER.log(getNormalLoggingLevel(), "Deployment newer than timeout, keeping");
                if (firstBackInQueue == null) {
                    firstBackInQueue = info;
                }
                // Put it back
                deploymentsToClean.add(info);
            }
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Failed to get/delete deployment: {0}", e);
            // and add back into the queue.
            if (info.hasAttemptsRemaining()) {
                info.decrementAttemptsRemaining();
                if (firstBackInQueue == null) {
                    firstBackInQueue = info;
                }
                // Put it back in the queue for another attempt
                deploymentsToClean.add(info);
            }
        }
    }
    DeploymentRegistrar.getInstance().syncDeploymentsToClean();
    LOGGER.log(getNormalLoggingLevel(), "Done cleaning deployments");
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Deployment(com.azure.resourcemanager.resources.models.Deployment) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) 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)

Example 4 with AzureResourceManager

use of com.azure.resourcemanager.AzureResourceManager 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 5 with AzureResourceManager

use of com.azure.resourcemanager.AzureResourceManager in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMComputer method getAzurePortalLink.

// UI only
@Restricted(NoExternalUse.class)
public String getAzurePortalLink() {
    AzureVMAgent agent = getNode();
    if (agent != null) {
        AzureVMCloud cloud = agent.getCloud();
        if (cloud != null) {
            AzureResourceManager azureClient = cloud.getAzureClient();
            String subscriptionId = azureClient.getCurrentSubscription().subscriptionId();
            String resourceGroup = agent.getResourceGroupName();
            // can't see a way to guarantee getting the tenant ID, this should be enough for now anyway
            return String.format("https://portal.azure.com/#resource/subscriptions/%s/resourceGroups/%s/" + "providers/Microsoft.Compute/virtualMachines/%s", subscriptionId, resourceGroup, nodeName);
        }
    }
    return null;
}
Also used : AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) Restricted(org.kohsuke.accmod.Restricted)

Aggregations

AzureResourceManager (com.azure.resourcemanager.AzureResourceManager)11 IOException (java.io.IOException)5 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)4 AzureProfile (com.azure.core.management.profile.AzureProfile)3 URI (java.net.URI)3 ExecutionException (java.util.concurrent.ExecutionException)3 TokenCredential (com.azure.core.credential.TokenCredential)2 ManagementException (com.azure.core.management.exception.ManagementException)2 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)2 Deployment (com.azure.resourcemanager.resources.models.Deployment)2 Subscription (com.azure.resourcemanager.resources.models.Subscription)2 FileNotFoundException (java.io.FileNotFoundException)2 TimeoutException (java.util.concurrent.TimeoutException)2 ManagedIdentityCredential (com.azure.identity.ManagedIdentityCredential)1 ManagedIdentityCredentialBuilder (com.azure.identity.ManagedIdentityCredentialBuilder)1 GalleryImageVersion (com.azure.resourcemanager.compute.models.GalleryImageVersion)1 OperatingSystemTypes (com.azure.resourcemanager.compute.models.OperatingSystemTypes)1 PurchasePlan (com.azure.resourcemanager.compute.models.PurchasePlan)1 VirtualMachineCustomImage (com.azure.resourcemanager.compute.models.VirtualMachineCustomImage)1 VirtualMachineImage (com.azure.resourcemanager.compute.models.VirtualMachineImage)1