Skip to content

Commit d7e0fa9

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 d7e0fa9

File tree

1 file changed

+73
-72
lines changed

1 file changed

+73
-72
lines changed

src/cosalib/aws.py

Lines changed: 73 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,81 @@ 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+
meta_key = "amis"
36+
# only replicate WinLI AMIs if `--winli` is used
37+
if args.winli:
38+
meta_key = "aws-winli"
39+
if len(buildmeta.get(meta_key, [])) < 1:
3640
raise SystemExit(("buildmeta doesn't contain source AMIs."
3741
" Run buildextend-aws --upload first"))
3842

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
43+
# Determine which region to copy from
44+
if not args.source_region:
45+
args.source_region = buildmeta[meta_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])
7752

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()
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[meta_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+
sys.exit(0)
72+
73+
source_image = None
74+
for a in buildmeta[meta_key]:
75+
if a['name'] == args.source_region:
76+
source_image = a['hvm']
77+
break
78+
79+
if source_image is None:
80+
raise Exception(("Unable to find AMI ID for "
81+
f"{args.source_region} region"))
82+
83+
ore_args.extend(['copy-image', '--image', source_image])
84+
ore_args.extend(region_list)
85+
print("+ {}".format(subprocess.list2cmdline(ore_args)))
86+
87+
ore_data = ""
88+
try:
89+
ore_data = subprocess.check_output(ore_args, encoding='utf-8')
90+
except subprocess.CalledProcessError as e:
91+
ore_data = e.output or ""
92+
raise e
93+
finally:
94+
ore_data = ore_data.strip()
95+
if len(ore_data) > 0:
96+
for line in ore_data.split('\n'):
97+
j = json.loads(line)
98+
# This matches the Container Linux schema:
99+
# https://stable.release.core-os.net/amd64-usr/current/coreos_production_ami_all.json
100+
ami_data = [{'name': region,
101+
'hvm': vals['ami'],
102+
'snapshot': vals['snapshot']}
103+
for region, vals in j.items()]
104+
buildmeta[meta_key].extend(ami_data)
105+
106+
# Record the AMI's that have been replicated as they happen.
107+
# When re-running the replication, we don't want to be lose
108+
# what has been done.
109+
build.meta_write()
109110

110111

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

0 commit comments

Comments
 (0)