-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
127 lines (109 loc) · 5.3 KB
/
build.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
import argparse
import json
import logging
import os
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
sm_client = boto3.client("sagemaker")
def get_approved_model_binary(model_package_group_name):
"""Gets the latest approved model package for a model package group.
Args:
model_package_group_name: The model package group name.
Returns:
The SageMaker Model Package ARN.
"""
try:
# Get the latest approved model package
response = sm_client.list_model_packages(
ModelPackageGroupName=model_package_group_name,
ModelApprovalStatus="Approved",
SortBy="CreationTime",
MaxResults=100,
)
approved_packages = response["ModelPackageSummaryList"]
# Fetch more packages if none returned with continuation token
while len(approved_packages) == 0 and "NextToken" in response:
logger.debug("Getting more packages for token: {}".format(response["NextToken"]))
response = sm_client.list_model_packages(
ModelPackageGroupName=model_package_group_name,
ModelApprovalStatus="Approved",
SortBy="CreationTime",
MaxResults=100,
NextToken=response["NextToken"],
)
approved_packages.extend(response["ModelPackageSummaryList"])
# Return error if no packages found
if len(approved_packages) == 0:
error_message = (
f"No approved ModelPackage found for ModelPackageGroup: {model_package_group_name}"
)
logger.error(error_message)
raise Exception(error_message)
# get model package arn
model_package_arn = approved_packages[0]["ModelPackageArn"]
logger.info(f"Identified the latest approved model package: {model_package_arn}")
# return model binary location for latest model package
response = sm_client.describe_model_package(
ModelPackageName=model_package_arn
)
model_binary_location = response['InferenceSpecification']['Containers'][0]['ModelDataUrl']
logger.info(f"Identified the model binary location: {model_binary_location}")
return model_binary_location
except ClientError as e:
error_message = e.response["Error"]["Message"]
logger.error(error_message)
raise Exception(error_message)
def extend_config(args, model_binary_location, stage_config):
"""
Extend the stage configuration with additional parameters and tags based.
"""
# Verify that config has parameters and tags sections
if not "Parameters" in stage_config or not "StageName" in stage_config["Parameters"]:
raise Exception("Configuration file must include SageName parameter")
if not "Tags" in stage_config:
stage_config["Tags"] = {}
# Create new params and tags
new_params = {
"SageMakerProjectName": args.sagemaker_project_name,
"ModelBinaryLocation": model_binary_location,
"ModelExecutionRoleArn": args.model_execution_role,
}
new_tags = {
"sagemaker:deployment-stage": stage_config["Parameters"]["StageName"],
"sagemaker:project-id": args.sagemaker_project_id,
"sagemaker:project-name": args.sagemaker_project_name,
}
return {
"Parameters": {**stage_config["Parameters"], **new_params},
"Tags": {**stage_config.get("Tags", {}), **new_tags},
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--log-level", type=str, default=os.environ.get("LOGLEVEL", "INFO").upper())
parser.add_argument("--model-execution-role", type=str, required=True)
parser.add_argument("--model-package-group-name", type=str, required=True)
parser.add_argument("--sagemaker-project-id", type=str, required=True)
parser.add_argument("--sagemaker-project-name", type=str, required=True)
parser.add_argument("--import-staging-config", type=str, default="staging-config.json")
parser.add_argument("--import-prod-config", type=str, default="prod-config.json")
parser.add_argument("--export-staging-config", type=str, default="staging-config-export.json")
parser.add_argument("--export-prod-config", type=str, default="prod-config-export.json")
args, _ = parser.parse_known_args()
# Configure logging to output the line number and message
log_format = "%(levelname)s: [%(filename)s:%(lineno)s] %(message)s"
logging.basicConfig(format=log_format, level=args.log_level)
# Get the latest approved package
model_binary_location = get_approved_model_binary(args.model_package_group_name)
# Write the staging config
with open(args.import_staging_config, "r") as f:
staging_config = extend_config(args, model_binary_location, json.load(f))
logger.debug("Staging config: {}".format(json.dumps(staging_config, indent=4)))
with open(args.export_staging_config, "w") as f:
json.dump(staging_config, f, indent=4)
# Write the prod config
with open(args.import_prod_config, "r") as f:
prod_config = extend_config(args, model_binary_location, json.load(f))
logger.debug("Prod config: {}".format(json.dumps(prod_config, indent=4)))
with open(args.export_prod_config, "w") as f:
json.dump(prod_config, f, indent=4)