|
21 | 21 | import random |
22 | 22 |
|
23 | 23 | from acktest import resources |
| 24 | +from acktest.bootstrapping import Resources, BootstrapFailureException |
| 25 | +from acktest.bootstrapping.iam import Role |
| 26 | +from acktest.bootstrapping.s3 import Bucket |
24 | 27 | from acktest.aws.identity import get_region, get_account_id |
25 | 28 | from acktest.aws.s3 import duplicate_bucket_contents |
26 | 29 | from e2e import bootstrap_directory |
27 | 30 | from e2e.bootstrap_resources import TestBootstrapResources, SAGEMAKER_SOURCE_DATA_BUCKET |
28 | 31 |
|
29 | 32 |
|
30 | | -def create_execution_role() -> str: |
| 33 | +def sync_data_bucket(bucket) -> str: |
| 34 | + bucket_name = bucket.name |
31 | 35 | region = get_region() |
32 | | - role_name = resources.random_suffix_name(f"ack-sagemaker-execution-role", 63) |
33 | | - iam = boto3.client("iam", region_name=region) |
34 | | - |
35 | | - iam.create_role( |
36 | | - RoleName=role_name, |
37 | | - AssumeRolePolicyDocument=json.dumps( |
38 | | - { |
39 | | - "Version": "2012-10-17", |
40 | | - "Statement": [ |
41 | | - { |
42 | | - "Effect": "Allow", |
43 | | - "Principal": {"Service": "sagemaker.amazonaws.com"}, |
44 | | - "Action": "sts:AssumeRole", |
45 | | - } |
46 | | - ], |
47 | | - } |
48 | | - ), |
49 | | - Description="SageMaker execution role for ACK integration and canary tests", |
50 | | - ) |
51 | | - |
52 | | - # random sleep to prevent throttling |
53 | | - time.sleep(random.randrange(1, 3)) |
54 | | - iam.attach_role_policy( |
55 | | - RoleName=role_name, |
56 | | - PolicyArn="arn:aws:iam::aws:policy/AmazonSageMakerFullAccess", |
57 | | - ) |
58 | | - |
59 | | - # random sleep to prevent throttling |
60 | | - time.sleep(random.randrange(1, 3)) |
61 | | - iam.attach_role_policy( |
62 | | - RoleName=role_name, PolicyArn="arn:aws:iam::aws:policy/AmazonS3FullAccess" |
63 | | - ) |
64 | | - |
65 | | - iam_resource = iam.get_role(RoleName=role_name) |
66 | | - resource_arn = iam_resource["Role"]["Arn"] |
67 | | - |
68 | | - # There appears to be a delay in role availability after role creation |
69 | | - # resulting in failure that role is not present. So adding a delay |
70 | | - # to allow for the role to become available |
71 | | - time.sleep(10) |
72 | | - logging.info(f"Created SageMaker execution role {resource_arn}") |
73 | | - |
74 | | - return resource_arn |
75 | | - |
76 | | - |
77 | | -def create_data_bucket() -> str: |
78 | | - region = get_region() |
79 | | - account_id = get_account_id() |
80 | | - bucket_name = resources.random_suffix_name( |
81 | | - f"ack-data-bucket-{region}-{account_id}", 63 |
82 | | - ) |
83 | | - |
84 | | - s3 = boto3.client("s3", region_name=region) |
85 | | - if region == "us-east-1": |
86 | | - s3.create_bucket(Bucket=bucket_name) |
87 | | - else: |
88 | | - s3.create_bucket( |
89 | | - Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": region} |
90 | | - ) |
91 | | - |
92 | | - logging.info(f"Created SageMaker data bucket {bucket_name}") |
93 | | - |
94 | 36 | s3_resource = boto3.resource("s3", region_name=region) |
95 | 37 |
|
96 | 38 | source_bucket = s3_resource.Bucket(SAGEMAKER_SOURCE_DATA_BUCKET) |
@@ -122,18 +64,35 @@ def create_data_bucket() -> str: |
122 | 64 |
|
123 | 65 | logging.info(f"Synced data bucket") |
124 | 66 |
|
125 | | - return bucket_name |
| 67 | + return bucket |
126 | 68 |
|
127 | 69 |
|
128 | | -def service_bootstrap() -> dict: |
| 70 | +def service_bootstrap() -> Resources: |
129 | 71 | logging.getLogger().setLevel(logging.INFO) |
130 | | - |
131 | | - return TestBootstrapResources( |
132 | | - create_data_bucket(), create_execution_role(), |
133 | | - ).__dict__ |
| 72 | + region = get_region() |
| 73 | + account_id = get_account_id() |
| 74 | + bucket_name = f"ack-data-bucket-{region}-{account_id}" |
| 75 | + |
| 76 | + resources = TestBootstrapResources( |
| 77 | + DataBucket=Bucket(bucket_name), |
| 78 | + ExecutionRole=Role( |
| 79 | + "ack-sagemaker-execution-role", |
| 80 | + "sagemaker.amazonaws.com", |
| 81 | + managed_policies=[ |
| 82 | + "arn:aws:iam::aws:policy/AmazonSageMakerFullAccess", |
| 83 | + "arn:aws:iam::aws:policy/AmazonS3FullAccess", |
| 84 | + ], |
| 85 | + ), |
| 86 | + ) |
| 87 | + try: |
| 88 | + resources.bootstrap() |
| 89 | + sync_data_bucket(resources.DataBucket) |
| 90 | + except BootstrapFailureException as ex: |
| 91 | + exit(254) |
| 92 | + return resources |
134 | 93 |
|
135 | 94 |
|
136 | 95 | if __name__ == "__main__": |
137 | 96 | config = service_bootstrap() |
138 | 97 | # Write config to current directory by default |
139 | | - resources.write_bootstrap_config(config, bootstrap_directory) |
| 98 | + config.serialize(bootstrap_directory) |
0 commit comments