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());
}
}
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;
}
}
}
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");
}
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);
}
}
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;
}
Aggregations