Skip to content

Commit

Permalink
WIP: Copy tested AMI to more regions
Browse files Browse the repository at this point in the history
Closes: #328
  • Loading branch information
cgwalters committed Sep 28, 2018
1 parent 74c90c8 commit e3ef6e6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Jenkinsfile.aws-test
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
"""
}
}
Expand All @@ -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")
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions scripts/ami-copy-regions
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e3ef6e6

Please sign in to comment.