-
Notifications
You must be signed in to change notification settings - Fork 26
/
AciService.java
127 lines (109 loc) · 6.13 KB
/
AciService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.microsoft.jenkins.containeragents.aci;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.containerinstance.models.ContainerGroup;
import com.azure.resourcemanager.resources.models.Deployment;
import com.azure.resourcemanager.resources.models.DeploymentMode;
import com.azure.resourcemanager.resources.models.Deployments;
import com.microsoft.jenkins.containeragents.builders.AciDeploymentTemplateBuilder;
import com.microsoft.jenkins.containeragents.builders.AciDeploymentTemplateBuilder.AciDeploymentTemplate;
import com.microsoft.jenkins.containeragents.util.AzureContainerUtils;
import com.microsoft.jenkins.containeragents.util.Constants;
import org.apache.commons.lang3.time.StopWatch;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class AciService {
private static final Logger LOGGER = Logger.getLogger(AciService.class.getName());
private AciService() {
//
}
public static void createDeployment(final AciCloud cloud,
final AciContainerTemplate template,
final AciAgent agent,
final StopWatch stopWatch) throws Exception {
String deployName = getDeploymentName(template);
AciDeploymentTemplate deploymentTemplate =
new AciDeploymentTemplateBuilder().buildDeploymentTemplate(cloud, template, agent);
// register the deployment for cleanup
AciCleanTask.DeploymentRegistrar deploymentRegistrar = AciCleanTask.DeploymentRegistrar.getInstance();
deploymentRegistrar.registerDeployment(cloud.getName(), cloud.getResourceGroup(), deployName);
final AzureResourceManager azureClient = cloud.getAzureClient();
azureClient.deployments()
.define(deployName)
.withExistingResourceGroup(cloud.getResourceGroup())
.withTemplate(deploymentTemplate.deploymentTemplateAsString())
.withParameters(deploymentTemplate.templateParameterAsString())
.withMode(DeploymentMode.INCREMENTAL)
.beginCreate();
//register deployName
agent.setDeployName(deployName);
//Wait deployment to success
final int retryInterval = 10 * 1000;
LOGGER.log(Level.INFO, "Waiting for deployment {0}", deployName);
while (true) {
if (AzureContainerUtils.isTimeout(template.getTimeout(), stopWatch.getTime())) {
throw new TimeoutException("Deployment timeout");
}
Deployment deployment
= azureClient.deployments().getByResourceGroup(cloud.getResourceGroup(), deployName);
if (deployment.provisioningState().equalsIgnoreCase("succeeded")) {
LOGGER.log(Level.INFO, "Deployment {0} succeed", deployName);
break;
} else if (deployment.provisioningState().equalsIgnoreCase("Failed")) {
throw new Exception(String.format("Deployment %s status: Failed", deployName));
} else {
// If half of time passed, we need to inspect what happened from logs
if (AzureContainerUtils.isHalfTimePassed(template.getTimeout(), stopWatch.getTime())) {
ContainerGroup containerGroup
= azureClient.containerGroups()
.getByResourceGroup(cloud.getResourceGroup(), agent.getNodeName());
if (containerGroup != null) {
LOGGER.log(Level.INFO, "Logs from container {0}: {1}",
new Object[]{agent.getNodeName(),
containerGroup.getLogContent(agent.getNodeName())});
}
}
Thread.sleep(retryInterval);
}
}
}
private static String getDeploymentName(AciContainerTemplate template) {
return AzureContainerUtils.generateName(template.getName(), Constants.ACI_DEPLOYMENT_RANDOM_NAME_LENGTH);
}
public static void deleteAciContainerGroup(String credentialsId,
String resourceGroup,
String containerGroupName,
String deployName) {
AzureResourceManager azureClient;
try {
azureClient = AzureContainerUtils.getAzureClient(credentialsId);
azureClient.containerGroups().deleteByResourceGroup(resourceGroup, containerGroupName);
LOGGER.log(Level.INFO, "Delete ACI Container Group: {0} successfully", containerGroupName);
} catch (Exception e) {
LOGGER.log(Level.WARNING, String.format("Delete ACI Container Group: %s failed", containerGroupName), e);
return;
}
try {
//To avoid to many deployments. May over deployment limits.
if (deployName != null) {
// Only to delete succeeded deployments for future debugging.
Deployments deployments = azureClient.deployments();
Deployment deployment = deployments.getByResourceGroup(resourceGroup, deployName);
if (deployment != null) {
String provisioningState = deployment.provisioningState();
LOGGER.fine(() -> String.format("Checking deployment: %s, provisioning state: %s",
deployName, provisioningState));
if (provisioningState
.equalsIgnoreCase("succeeded")) {
deployments.deleteByResourceGroup(resourceGroup, deployName);
LOGGER.log(Level.INFO, "Delete ACI deployment: {0} successfully", deployName);
}
} else {
LOGGER.fine(() -> String.format("Skipped deployment: %s as we couldn't find it", deployName));
}
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, String.format("Delete ACI deployment: %s failed", deployName), e);
}
}
}