From e3ef6e6c7be92c53c01d023220176b4f66b7c969 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 27 Sep 2018 20:05:21 -0400 Subject: [PATCH] WIP: Copy tested AMI to more regions Closes: https://github.com/openshift/os/issues/328 --- Jenkinsfile.aws-test | 19 +++++++++++---- scripts/ami-copy-regions | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100755 scripts/ami-copy-regions diff --git a/Jenkinsfile.aws-test b/Jenkinsfile.aws-test index 9e03678f8..d82c4ca22 100644 --- a/Jenkinsfile.aws-test +++ b/Jenkinsfile.aws-test @@ -1,5 +1,8 @@ def NODE = "rhcos-jenkins" +// Our primary def AWS_REGION = "us-east-1" +// We copy tested AMIs to other regions +def OTHER_AWS_REGIONS = ["us-east-2"] // location on the server we'll rsync to/from our $WORKSPACE def images = "/srv/rhcos/output/images" @@ -66,11 +69,18 @@ node(NODE) { aws ec2 modify-image-attribute \ --image-id ${ami_intermediate} \ --launch-permission '{"Add":[{"Group":"all"}]}' + # And upload to more regions + ./scripts/ami-copy-regions --source-region ${AWS_REGION} \ + --name rhcos_dev_${version} \ + --source-image-id ${ami_intermediate} \ + --regions ${OTHER_AWS_REGIONS.join(',')} \ + --out ${WORKSPACE}/aws-tested.json - # Upload the json file to a public location - aws s3 cp --acl public-read \ - ${WORKSPACE}/aws-${AWS_REGION}.json \ - s3://${S3_PUBLIC_BUCKET}/aws-${AWS_REGION}-tested.json + # Upload the json files to a public location + for k in aws-${AWS_REGION}-tested.json aws-tested.json; do + aws s3 cp --acl public-read ${WORKSPACE}/\${k} \ + s3://${S3_PUBLIC_BUCKET}/\${k} + done """ } } @@ -86,6 +96,7 @@ node(NODE) { sshUserPrivateKey(credentialsId: params.ARTIFACT_SSH_CREDS_ID, keyFileVariable: 'KEY_FILE'), ]) { utils.rsync_file_out_dest(ARTIFACT_SERVER, KEY_FILE, "${WORKSPACE}/aws-${AWS_REGION}.json", "${images}/aws-${AWS_REGION}-tested.json") + utils.rsync_file_out_dest(ARTIFACT_SERVER, KEY_FILE, "${WORKSPACE}/aws-tested.json", "${images}/aws-tested.json") } } } diff --git a/scripts/ami-copy-regions b/scripts/ami-copy-regions new file mode 100755 index 000000000..dde3960f3 --- /dev/null +++ b/scripts/ami-copy-regions @@ -0,0 +1,50 @@ +#!/usr/bin/python3 +# Copy an AMI to multiple regions, generating an "AMI JSON" +# file matching the Container Linux schema: +# https://alpha.release.core-os.net/amd64-usr/current/coreos_production_ami_all.json +# Note this assumes the images are HVM. + +import os,sys,argparse,subprocess,io,time,re,multiprocessing +import tempfile, json + +def fatal(msg): + print('error: {}'.format(msg), file=sys.stderr) + raise SystemExit(1) + +def csv_list(string): + return string.split(',') + +parser = argparse.ArgumentParser() +parser.add_argument("--source-region", help="Source region", + action='store', required=True) +parser.add_argument("--source-image-id", help="Source AMI", + action='store', required=True) +parser.add_argument("--name", help="AMI name", + action='store', required=True) +parser.add_argument("--regions", help="Name", + action='store', required=True, + type=csv_list) +parser.add_argument("--out", help="Store output in FILE", + action='store', metavar='FILE', + required=True) + +args = parser.parse_args() + +amis = [] + +for region in args.regions: + print("Uploading to: {}".format(region)) + res = json.loads(subprocess.check_output(['aws', '--output', 'json', 'ec2', 'copy-image', + '--source-region', args.source_region, + '--source-image-id', args.source_image_id, + '--name', args.name, + '--region', region])) + iid = res['ImageId'] + print("Complete, ImageId={}".format(iid)) + amis.append({'name': region, + 'hvm': iid}) + +amis.sort(key=lambda x: x['name']) + +with open(args.out, 'w') as f: + json.dump({'amis': amis}, f)