Skip to content

Commit eca4877

Browse files
committed
cosalib/aws: only replicate WinLI AMIs when --winli is specified
The current logic always considers the `aws-winli` buildmeta key when present, which causes WinLI AMIs to be replicated in both the standard AWS and GovCloud cases. Restore `aws_run_ore_replicate`[1] to only work on the 'amis' buildmeta key and switch to aws-winli when `--winli` is used. This ensures WinLI replication happens only when explicitly requested. Also update the CLI help text for `--winli` to clarify that it applies to replication as well as creation. [1]: a5bfbde
1 parent 0c3493c commit eca4877

File tree

1 file changed

+72
-72
lines changed

1 file changed

+72
-72
lines changed

src/cosalib/aws.py

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import subprocess
4+
import sys
45

56
from cosalib.builds import Builds
67
from cosalib.cmdlib import runcmd
@@ -31,81 +32,80 @@ def deregister_aws_resource(ami, snapshot, region, credentials_file):
3132
def aws_run_ore_replicate(build, args):
3233
build.refresh_meta()
3334
buildmeta = build.meta
34-
buildmeta_keys = ["amis"]
35-
if len(buildmeta.get(buildmeta_keys[0], [])) < 1:
35+
# only replicate winli AMIs if `--winli` is used
36+
# otherwise, replicate only the AMIs in the 'amis' section of build meta
37+
meta_key = "aws-winli" if args.winli else "amis"
38+
if len(buildmeta.get(meta_key, [])) < 1:
3639
raise SystemExit(("buildmeta doesn't contain source AMIs."
3740
" Run buildextend-aws --upload first"))
3841

39-
if len(buildmeta.get('aws-winli', [])) > 0:
40-
buildmeta_keys.append("aws-winli")
41-
42-
for key in buildmeta_keys:
43-
# Determine which region to copy from
44-
if not args.source_region:
45-
args.source_region = buildmeta[key][0]['name']
46-
47-
ore_args = ['ore', 'aws', '--region', args.source_region]
48-
if args.log_level:
49-
ore_args.extend(['--log-level', args.log_level])
50-
if args.credentials_file:
51-
ore_args.extend(['--credentials-file', args.credentials_file])
52-
53-
# If no region specified then autodetect the regions to replicate to.
54-
# Specify --region=args.source_region here so ore knows to talk to
55-
# a region that exists (i.e. it will talk to govcloud if copying
56-
# from a govcloud region).
57-
if not args.region:
58-
args.region = subprocess.check_output(
59-
ore_args + ['list-regions']).decode().strip().split()
60-
61-
# only replicate to regions that don't already exist
62-
existing_regions = [item['name'] for item in buildmeta[key]]
63-
duplicates = list(set(args.region).intersection(existing_regions))
64-
if len(duplicates) > 0:
65-
print((f"AMIs already exist in {duplicates} region(s), "
66-
"skipping listed region(s)..."))
67-
68-
region_list = list(set(args.region) - set(duplicates))
69-
if len(region_list) == 0:
70-
print("no new regions detected")
71-
continue
72-
source_image = None
73-
for a in buildmeta[key]:
74-
if a['name'] == args.source_region:
75-
source_image = a['hvm']
76-
break
42+
# Determine which region to copy from
43+
if not args.source_region:
44+
args.source_region = buildmeta[meta_key][0]['name']
7745

78-
if source_image is None:
79-
raise Exception(("Unable to find AMI ID for "
80-
f"{args.source_region} region"))
81-
82-
ore_args.extend(['copy-image', '--image', source_image])
83-
ore_args.extend(region_list)
84-
print("+ {}".format(subprocess.list2cmdline(ore_args)))
85-
86-
ore_data = ""
87-
try:
88-
ore_data = subprocess.check_output(ore_args, encoding='utf-8')
89-
except subprocess.CalledProcessError as e:
90-
ore_data = e.output or ""
91-
raise e
92-
finally:
93-
ore_data = ore_data.strip()
94-
if len(ore_data) > 0:
95-
for line in ore_data.split('\n'):
96-
j = json.loads(line)
97-
# This matches the Container Linux schema:
98-
# https://stable.release.core-os.net/amd64-usr/current/coreos_production_ami_all.json
99-
ami_data = [{'name': region,
100-
'hvm': vals['ami'],
101-
'snapshot': vals['snapshot']}
102-
for region, vals in j.items()]
103-
buildmeta[key].extend(ami_data)
104-
105-
# Record the AMI's that have been replicated as they happen.
106-
# When re-running the replication, we don't want to be lose
107-
# what has been done.
108-
build.meta_write()
46+
ore_args = ['ore', 'aws', '--region', args.source_region]
47+
if args.log_level:
48+
ore_args.extend(['--log-level', args.log_level])
49+
if args.credentials_file:
50+
ore_args.extend(['--credentials-file', args.credentials_file])
51+
52+
# If no region specified then autodetect the regions to replicate to.
53+
# Specify --region=args.source_region here so ore knows to talk to
54+
# a region that exists (i.e. it will talk to govcloud if copying
55+
# from a govcloud region).
56+
if not args.region:
57+
args.region = subprocess.check_output(
58+
ore_args + ['list-regions']).decode().strip().split()
59+
60+
# only replicate to regions that don't already exist
61+
existing_regions = [item['name'] for item in buildmeta[meta_key]]
62+
duplicates = list(set(args.region).intersection(existing_regions))
63+
if len(duplicates) > 0:
64+
print((f"AMIs already exist in {duplicates} region(s), "
65+
"skipping listed region(s)..."))
66+
67+
region_list = list(set(args.region) - set(duplicates))
68+
if len(region_list) == 0:
69+
print("no new regions detected")
70+
sys.exit(0)
71+
72+
source_image = None
73+
for a in buildmeta[meta_key]:
74+
if a['name'] == args.source_region:
75+
source_image = a['hvm']
76+
break
77+
78+
if source_image is None:
79+
raise Exception(("Unable to find AMI ID for "
80+
f"{args.source_region} region"))
81+
82+
ore_args.extend(['copy-image', '--image', source_image])
83+
ore_args.extend(region_list)
84+
print("+ {}".format(subprocess.list2cmdline(ore_args)))
85+
86+
ore_data = ""
87+
try:
88+
ore_data = subprocess.check_output(ore_args, encoding='utf-8')
89+
except subprocess.CalledProcessError as e:
90+
ore_data = e.output or ""
91+
raise e
92+
finally:
93+
ore_data = ore_data.strip()
94+
if len(ore_data) > 0:
95+
for line in ore_data.split('\n'):
96+
j = json.loads(line)
97+
# This matches the Container Linux schema:
98+
# https://stable.release.core-os.net/amd64-usr/current/coreos_production_ami_all.json
99+
ami_data = [{'name': region,
100+
'hvm': vals['ami'],
101+
'snapshot': vals['snapshot']}
102+
for region, vals in j.items()]
103+
buildmeta[meta_key].extend(ami_data)
104+
105+
# Record the AMI's that have been replicated as they happen.
106+
# When re-running the replication, we don't want to be lose
107+
# what has been done.
108+
build.meta_write()
109109

110110

111111
@retry(reraise=True, stop=stop_after_attempt(3))
@@ -226,6 +226,6 @@ def aws_cli(parser):
226226
parser.add_argument("--public", action="store_true", help="Mark images as publicly available")
227227
parser.add_argument("--tags", help="list of key=value tags to attach to the AMI",
228228
action='append', default=[])
229-
parser.add_argument("--winli", action="store_true", help="create an AWS Windows LI Ami")
229+
parser.add_argument("--winli", action="store_true", help="create or replicate an AWS Windows LI Ami")
230230
parser.add_argument("--winli-billing-product", help="Windows billing product code used to create a Windows LI AMI")
231231
return parser

0 commit comments

Comments
 (0)