use of com.azure.resourcemanager.resources.models.Deployment 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.resources.models.Deployment 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();
}
use of com.azure.resourcemanager.resources.models.Deployment 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));
}
Aggregations