4
4
import ipify
5
5
import webbrowser
6
6
import os .path
7
+ import axon .progress_reporter
7
8
8
9
all_perm = {
9
10
"FromPort" : - 1 ,
22
23
}
23
24
24
25
26
+ def make_client (name , region ):
27
+ if region is None :
28
+ return boto3 .client (name )
29
+ else :
30
+ return boto3 .client (name , region_name = region )
31
+
32
+
25
33
def ensure_log_group (group_name , region ):
26
34
"""
27
35
Ensures that a log group is present. If there is a matching log group, nothing is created.
28
36
If there is no matching log group, one is created.
29
37
30
38
:param group_name: The name of the log group.
31
- :param region: The region.
39
+ :param region: The region, or `None` to pull the region from the environment .
32
40
:return: Nothing.
33
41
"""
34
- client = boto3 . client ("logs" , region_name = region )
42
+ client = make_client ("logs" , region )
35
43
matching_log_groups = client .describe_log_groups (
36
44
logGroupNamePrefix = group_name
37
45
)
@@ -66,7 +74,7 @@ def ensure_ecs_gress(sg_id, region):
66
74
egress permissions are revoked. The permissions that Axon needs are authorized.
67
75
68
76
:param sg_id: The SecurityGroup's GroupId.
69
- :param region: The region.
77
+ :param region: The region, or `None` to pull the region from the environment .
70
78
:return: Nothing.
71
79
"""
72
80
ec2 = boto3 .resource ('ec2' , region_name = region )
@@ -92,7 +100,7 @@ def ensure_ec2_gress(sg_id, region):
92
100
egress permissions are revoked. The permissions that Axon needs are authorized.
93
101
94
102
:param sg_id: The SecurityGroup's GroupId.
95
- :param region: The region.
103
+ :param region: The region, or `None` to pull the region from the environment .
96
104
:return: Nothing.
97
105
"""
98
106
ec2 = boto3 .resource ('ec2' , region_name = region )
@@ -144,12 +152,11 @@ def get_single_security_group(client, sg_name, desc):
144
152
def ensure_ecs_security_group (region ):
145
153
"""
146
154
Ensures that the ECS SecurityGroup exists.
147
-
148
- :param region: The region.
155
+ :param region: The region, or `None` to pull the region from the environment.
149
156
:return: The GroupId of the SecurityGroup.
150
157
"""
151
158
sg_name = "axon-ecs-autogenerated"
152
- client = boto3 . client ("ec2" , region_name = region )
159
+ client = make_client ("ec2" , region )
153
160
sg_id = get_single_security_group (client , sg_name , "Axon autogenerated for ECS." )
154
161
ensure_ecs_gress (sg_id , region )
155
162
return sg_id
@@ -158,12 +165,11 @@ def ensure_ecs_security_group(region):
158
165
def ensure_ec2_security_group (region ):
159
166
"""
160
167
Ensures that the EC2 SecurityGroup exists.
161
-
162
- :param region: The region.
168
+ :param region: The region, or `None` to pull the region from the environment.
163
169
:return: The GroupId of the SecurityGroup.
164
170
"""
165
171
sg_name = "axon-ec2-autogenerated"
166
- client = boto3 . client ("ec2" , region_name = region )
172
+ client = make_client ("ec2" , region )
167
173
sg_id = get_single_security_group (client , sg_name , "Axon autogenerated for EC2." )
168
174
ensure_ec2_gress (sg_id , region )
169
175
return sg_id
@@ -172,12 +178,11 @@ def ensure_ec2_security_group(region):
172
178
def select_subnet (region ):
173
179
"""
174
180
Picks the first available subnet.
175
-
176
- :param region: The region.
181
+ :param region: The region, or `None` to pull the region from the environment.
177
182
:return: The SubnetId.
178
183
"""
179
- ec2 = boto3 . client ("ec2" , region_name = region )
180
- return ec2 .describe_subnets (Filters = [])["Subnets" ][0 ]["SubnetId" ]
184
+ client = make_client ("ec2" , region )
185
+ return client .describe_subnets (Filters = [])["Subnets" ][0 ]["SubnetId" ]
181
186
182
187
183
188
def ensure_role (client , role_name ):
@@ -207,12 +212,11 @@ def ensure_task_role(region):
207
212
208
213
TODO: Fix this.
209
214
This method does not check that a matching role has the correct policies.
210
-
211
- :param region: The region.
215
+ :param region: The region, or `None` to pull the region from the environment.
212
216
:return: The role Arn.
213
217
"""
214
218
role_name = "axon-ecs-autogenerated-task-role"
215
- client = boto3 . client ("iam" , region_name = region )
219
+ client = make_client ("iam" , region )
216
220
role_arn = ensure_role (client , role_name )
217
221
if role_arn is None :
218
222
# Need to create the role
@@ -250,7 +254,7 @@ def ensure_task_role(region):
250
254
251
255
def ensure_ec2_role (region ):
252
256
role_name = "axon-ec2-role-manual"
253
- client = boto3 . client ("iam" , region_name = region )
257
+ client = make_client ("iam" , region )
254
258
role_arn = ensure_role (client , role_name )
255
259
if role_arn is None :
256
260
# Need to create the role
@@ -301,7 +305,7 @@ def ensure_task(ecs_client, task_family, region, vcpu, memory):
301
305
302
306
:param ecs_client: The ECS client to use.
303
307
:param task_family: The task family name.
304
- :param region: The region.
308
+ :param region: The region, or `None` to pull the region from the environment .
305
309
:param vcpu: The amount of cpu in vcpu units.
306
310
:param memory: The amount of memory in MB.
307
311
:return: The task definition's Arn.
@@ -357,10 +361,10 @@ def wait_for_task_to_start(task_arn, cluster, region):
357
361
358
362
:param task_arn: The Arn of the task to wait for.
359
363
:param cluster: The simple name of the cluster the task is in.
360
- :param region: The region.
364
+ :param region: The region, or `None` to pull the region from the environment .
361
365
:return: Nothing.
362
366
"""
363
- client = boto3 . client ("ecs" , region_name = region )
367
+ client = make_client ("ecs" , region )
364
368
waiter = client .get_waiter ("tasks_running" )
365
369
waiter .wait (cluster = cluster , tasks = [task_arn ])
366
370
@@ -371,9 +375,9 @@ def impl_ensure_configuration(cluster_name, task_family, region):
371
375
372
376
:param cluster_name: The simple name of the cluster to start the task in.
373
377
:param task_family: The family of the task to start.
374
- :param region: The region.
378
+ :param region: The region, or `None` to pull the region from the environment .
375
379
"""
376
- client = boto3 . client ("ecs" , region_name = region )
380
+ client = make_client ("ecs" , region )
377
381
ensure_cluster (client , cluster_name )
378
382
ensure_task (client , task_family , region , 2048 , 4096 )
379
383
ensure_ec2_security_group (region )
@@ -392,10 +396,10 @@ def impl_start_task(cluster_name, task_family, revision, region):
392
396
:param cluster_name: The simple name of the cluster to start the task in.
393
397
:param task_family: The family of the task to start.
394
398
:param revision: A task definition revision number, or None to use the latest revision.
395
- :param region: The region.
399
+ :param region: The region, or `None` to pull the region from the environment .
396
400
:return: The started task's Arn.
397
401
"""
398
- client = boto3 . client ("ecs" , region_name = region )
402
+ client = make_client ("ecs" , region )
399
403
400
404
impl_ensure_configuration (cluster_name , task_family , region )
401
405
@@ -431,10 +435,10 @@ def impl_stop_task(cluster_name, task_arn, region):
431
435
432
436
:param cluster_name: The simple name of the cluster.
433
437
:param task_arn: The Arn of the task.
434
- :param region: The region.
438
+ :param region: The region, or `None` to pull the region from the environment .
435
439
:return: Nothing.
436
440
"""
437
- client = boto3 . client ("ecs" , region_name = region )
441
+ client = make_client ("ecs" , region )
438
442
client .stop_task (cluster = cluster_name , task = task_arn )
439
443
440
444
@@ -444,10 +448,10 @@ def impl_get_task_ip(cluster_name, task_arn, region):
444
448
445
449
:param cluster_name: The simple name of the cluster.
446
450
:param task_arn: The task's Arn.
447
- :param region: The region.
451
+ :param region: The region, or `None` to pull the region from the environment .
448
452
:return: The public IP of the task.
449
453
"""
450
- client = boto3 . client ("ecs" , region_name = region )
454
+ client = make_client ("ecs" , region )
451
455
task_arn = client .describe_tasks (
452
456
cluster = cluster_name ,
453
457
tasks = [task_arn ]
@@ -458,7 +462,7 @@ def impl_get_task_ip(cluster_name, task_arn, region):
458
462
eni = next (
459
463
x ["value" ] for x in interface_attachment ["details" ] if x ["name" ] == "networkInterfaceId" )
460
464
461
- ec2 = boto3 . client ("ec2" , region_name = region )
465
+ ec2 = make_client ("ec2" , region )
462
466
nics = ec2 .describe_network_interfaces (
463
467
Filters = [
464
468
{
@@ -476,9 +480,9 @@ def impl_upload_model_file(local_file_path, bucket_name, region):
476
480
477
481
:param local_file_path: The path to the model file on disk.
478
482
:param bucket_name: The S3 bucket name.
479
- :param region: The region.
483
+ :param region: The region, or `None` to pull the region from the environment .
480
484
"""
481
- client = boto3 . client ("s3" , region_name = region )
485
+ client = make_client ("s3" , region )
482
486
remote_path = "axon-uploaded-trained-models/" + os .path .basename (local_file_path )
483
487
client .upload_file (local_file_path , bucket_name , remote_path )
484
488
print ("Uploaded to: {}\n " .format (remote_path ))
@@ -490,9 +494,9 @@ def impl_download_model_file(local_file_path, bucket_name, region):
490
494
491
495
:param local_file_path: The path to the model file on disk.
492
496
:param bucket_name: The S3 bucket name.
493
- :param region: The region.
497
+ :param region: The region, or `None` to pull the region from the environment .
494
498
"""
495
- client = boto3 . client ("s3" , region_name = region )
499
+ client = make_client ("s3" , region )
496
500
remote_path = "axon-uploaded-trained-models/" + os .path .basename (local_file_path )
497
501
client .download_file (bucket_name , remote_path , local_file_path )
498
502
print ("Downloaded from: {}\n " .format (remote_path ))
@@ -504,9 +508,9 @@ def impl_download_training_script(local_script_path, bucket_name, region):
504
508
505
509
:param local_script_path: The path to the training script on disk.
506
510
:param bucket_name: The S3 bucket name.
507
- :param region: The region.
511
+ :param region: The region, or `None` to pull the region from the environment .
508
512
"""
509
- client = boto3 . client ("s3" , region_name = region )
513
+ client = make_client ("s3" , region )
510
514
remote_path = "axon-uploaded-training-scripts/" + os .path .basename (local_script_path )
511
515
client .download_file (bucket_name , remote_path , local_script_path )
512
516
print ("Downloaded from: {}\n " .format (remote_path ))
@@ -518,9 +522,9 @@ def impl_download_dataset(local_dataset_path, bucket_name, region):
518
522
519
523
:param local_dataset_path: The path to the dataset on disk.
520
524
:param bucket_name: The S3 bucket name.
521
- :param region: The region.
525
+ :param region: The region, or `None` to pull the region from the environment .
522
526
"""
523
- client = boto3 . client ("s3" , region_name = region )
527
+ client = make_client ("s3" , region )
524
528
remote_path = "axon-uploaded-datasets/" + os .path .basename (local_dataset_path )
525
529
client .download_file (bucket_name , remote_path , local_dataset_path )
526
530
print ("Downloaded from: {}\n " .format (remote_path ))
0 commit comments