generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool.py
218 lines (198 loc) · 7.84 KB
/
pool.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import datetime
import os
import sys
import toml
from azure.identity import (
ChainedTokenCredential,
EnvironmentCredential,
AzureCliCredential,
ClientSecretCredential,
)
from azure.keyvault.secrets import SecretClient
from azure.storage.blob import BlobServiceClient
from azure.mgmt.batch import BatchManagementClient
from azure.core.exceptions import HttpResponseError
def create_container(blob_service_client: BlobServiceClient, container_name: str):
container_client = blob_service_client.get_container_client(
container=container_name
)
if not container_client.exists():
container_client.create_container()
print("Container [{}] created.".format(container_name))
else:
print("Container [{}] already exists.".format(container_name))
def get_autoscale_formula(fn):
with open(fn, "r") as autoscale_text:
return autoscale_text.read()
if __name__ == "__main__":
start_time = datetime.datetime.now()
# Reading a configuration file from the command line
if len(sys.argv) == 3:
config_file = sys.argv[1]
config = toml.load(config_file)
autoscale_fn = sys.argv[2]
else:
raise Exception(
"The function needs two arguments, a path to the config.toml "\
"and a path to the autoscale formula file."
)
# # Load configuration
# config = toml.load("run_azure_batch/configuration.toml")
# Get credential
# First use user credential to access the key vault
credential_order = (EnvironmentCredential(), AzureCliCredential())
user_credential = ChainedTokenCredential(*credential_order)
secret_client = SecretClient(
vault_url=config["Authentication"]["vault_url"],
credential=user_credential,
)
sp_secret = secret_client.get_secret(
config["Authentication"]["vault_sp_secret_id"]
).value
# Get Service Principal credential from key vault
sp_credential = ClientSecretCredential(
tenant_id=config["Authentication"]["tenant_id"],
client_id=config["Authentication"]["application_id"],
client_secret=sp_secret,
)
# Create the Azure Storage Blob Service Client
blob_service_client = BlobServiceClient(
account_url=config["Storage"]["storage_account_url"],
credential=sp_credential,
)
# Create the blob storage container for this batch job
# [2024-11-20 George] We are not using this now, so it should be
# removed before merging the PR.
input_container_name = "nnh-rt-input"
create_container(blob_service_client, input_container_name)
output_container_name = "nnh-rt-output"
create_container(blob_service_client, output_container_name)
# Create the Azure Batch Management client
batch_mgmt_client = BatchManagementClient(
credential=sp_credential,
subscription_id=config["Authentication"]["subscription_id"],
)
# Define the JSON for the batch pool creation request
# User-assigned identity for the pool
user_identity = {
"type": "UserAssigned",
"userAssignedIdentities": {
config["Authentication"]["user_assigned_identity"]: {
"clientId": config["Authentication"]["client_id"],
"principalId": config["Authentication"]["principal_id"],
}
},
}
# Network configuration with no public IP and virtual network
network_config = {
"subnetId": config["Authentication"]["subnet_id"],
"publicIPAddressConfiguration": {"provision": "NoPublicIPAddresses"},
"dynamicVnetAssignmentScope": "None",
}
# Virtual machine configuration
deployment_config = {
"virtualMachineConfiguration": {
"imageReference": {
"publisher": "microsoft-azure-batch",
"offer": "ubuntu-server-container",
"sku": "20-04-lts",
"version": "latest",
},
"nodeAgentSkuId": "batch.node.ubuntu 20.04",
"containerConfiguration": {
"type": "dockercompatible",
"containerImageNames": [config["Container"]["container_image_name"]],
"containerRegistries": [
{
"registryServer": config["Container"]["container_registry_url"],
"userName": config["Container"]["container_registry_username"],
"password": config["Container"]["container_registry_password"],
"registryServer": config["Container"][
"container_registry_server"
],
# "registryServer": config["Container"]["container_registry_url"],
# "identityReference": {
# "resourceId": config["Authentication"][
# "user_assigned_identity"
# ]
# },
}
],
},
}
}
# Mount configuration
mount_config = [
{
"azureBlobFileSystemConfiguration": {
"accountName": config["Storage"]["storage_account_name"],
"identityReference": {
"resourceId": config["Authentication"]["user_assigned_identity"]
},
"containerName": "nnh-rt-input",
"blobfuseOptions": "-o direct_io",
"relativeMountPath": "input",
}
},
{
"azureBlobFileSystemConfiguration": {
"accountName": config["Storage"]["storage_account_name"],
"identityReference": {
"resourceId": config["Authentication"]["user_assigned_identity"]
},
"containerName": "nnh-rt-output",
"blobfuseOptions": "-o direct_io",
"relativeMountPath": "output",
}
},
]
# Assemble the pool parameters JSON
pool_parameters = {
"identity": user_identity,
"properties": {
"vmSize": config["Batch"]["pool_vm_size"],
"interNodeCommunication": "Disabled",
"taskSlotsPerNode": 1,
"taskSchedulingPolicy": {"nodeFillType": "Spread"},
"deploymentConfiguration": deployment_config,
"networkConfiguration": network_config,
"scaleSettings": {
# "fixedScale": {
# "targetDedicatedNodes": 1,
# "targetLowPriorityNodes": 0,
# "resizeTimeout": "PT15M"
# }
"autoScale": {
"evaluationInterval": "PT5M",
"formula": get_autoscale_formula(autoscale_fn),
}
},
"resizeOperationStatus": {
"targetDedicatedNodes": 1,
"nodeDeallocationOption": "Requeue",
"resizeTimeout": "PT15M",
"startTime": "2023-07-05T13:18:25.7572321Z",
},
"currentDedicatedNodes": 1,
"currentLowPriorityNodes": 0,
"targetNodeCommunicationMode": "Simplified",
"currentNodeCommunicationMode": "Simplified",
"mountConfiguration": mount_config,
},
}
pool_id = config["Batch"]["pool_id"]
account_name = config["Batch"]["batch_account_name"]
resource_group_name = config["Authentication"]["resource_group"]
try:
batch_mgmt_client.pool.create(
resource_group_name=resource_group_name,
account_name=account_name,
pool_name=pool_id,
parameters=pool_parameters,
)
print(f"Pool {pool_id!r} created")
except HttpResponseError as error:
if "PropertyCannotBeUpdated" in error.message:
print(f"Pool {pool_id!r} already exists")
else:
raise error