# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 2019-07-23 02:06:20
# Node ID d80edcb0b30cedfc8f3706f133a34d7da53a03e2
# Parent  8804aa6c07a0fa114c7208b33d891837ed7e655a

automation: make Windows base image name configurable

Since automation broke in the middle of the 5.0 release cycle,
there's a good chance it will break again in the future. While
a robust solution might be to search for all available images and
choose the newest one, it does seem useful to be able to explicitly
choose the name of the image to find and use so users can opt in
to using a different image.

This commit implements that functionality.

Differential Revision: https://phab.mercurial-scm.org/D6673

diff --git a/contrib/automation/hgautomation/aws.py b/contrib/automation/hgautomation/aws.py
--- a/contrib/automation/hgautomation/aws.py
+++ b/contrib/automation/hgautomation/aws.py
@@ -1032,7 +1032,8 @@ def temporary_linux_dev_instances(c: AWS
                 instance.ssh_client.close()
 
 
-def ensure_windows_dev_ami(c: AWSConnection, prefix='hg-'):
+def ensure_windows_dev_ami(c: AWSConnection, prefix='hg-',
+                           base_image_name=WINDOWS_BASE_IMAGE_NAME):
     """Ensure Windows Development AMI is available and up-to-date.
 
     If necessary, a modern AMI will be built by starting a temporary EC2
@@ -1050,7 +1051,7 @@ def ensure_windows_dev_ami(c: AWSConnect
 
     name = '%s%s' % (prefix, 'windows-dev')
 
-    image = find_image(ec2resource, AMAZON_ACCOUNT_ID, WINDOWS_BASE_IMAGE_NAME)
+    image = find_image(ec2resource, AMAZON_ACCOUNT_ID, base_image_name)
 
     config = {
         'BlockDeviceMappings': [
@@ -1103,6 +1104,7 @@ def ensure_windows_dev_ami(c: AWSConnect
         'user_data': WINDOWS_USER_DATA,
         'initial_bootstrap': WINDOWS_BOOTSTRAP_POWERSHELL,
         'bootstrap_commands': commands,
+        'base_image_name': base_image_name,
     })
 
     existing_image = find_and_reconcile_image(ec2resource, name, fingerprint)
diff --git a/contrib/automation/hgautomation/cli.py b/contrib/automation/hgautomation/cli.py
--- a/contrib/automation/hgautomation/cli.py
+++ b/contrib/automation/hgautomation/cli.py
@@ -52,15 +52,16 @@ def bootstrap_linux_dev(hga: HGAutomatio
             aws.ensure_linux_dev_ami(c, distro=distro)
 
 
-def bootstrap_windows_dev(hga: HGAutomation, aws_region):
+def bootstrap_windows_dev(hga: HGAutomation, aws_region, base_image_name):
     c = hga.aws_connection(aws_region)
-    image = aws.ensure_windows_dev_ami(c)
+    image = aws.ensure_windows_dev_ami(c, base_image_name=base_image_name)
     print('Windows development AMI available as %s' % image.id)
 
 
-def build_inno(hga: HGAutomation, aws_region, arch, revision, version):
+def build_inno(hga: HGAutomation, aws_region, arch, revision, version,
+               base_image_name):
     c = hga.aws_connection(aws_region)
-    image = aws.ensure_windows_dev_ami(c)
+    image = aws.ensure_windows_dev_ami(c, base_image_name=base_image_name)
     DIST_PATH.mkdir(exist_ok=True)
 
     with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts:
@@ -74,9 +75,10 @@ def build_inno(hga: HGAutomation, aws_re
                                          version=version)
 
 
-def build_wix(hga: HGAutomation, aws_region, arch, revision, version):
+def build_wix(hga: HGAutomation, aws_region, arch, revision, version,
+              base_image_name):
     c = hga.aws_connection(aws_region)
-    image = aws.ensure_windows_dev_ami(c)
+    image = aws.ensure_windows_dev_ami(c, base_image_name=base_image_name)
     DIST_PATH.mkdir(exist_ok=True)
 
     with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts:
@@ -89,9 +91,10 @@ def build_wix(hga: HGAutomation, aws_reg
                                         DIST_PATH, version=version)
 
 
-def build_windows_wheel(hga: HGAutomation, aws_region, arch, revision):
+def build_windows_wheel(hga: HGAutomation, aws_region, arch, revision,
+                        base_image_name):
     c = hga.aws_connection(aws_region)
-    image = aws.ensure_windows_dev_ami(c)
+    image = aws.ensure_windows_dev_ami(c, base_image_name=base_image_name)
     DIST_PATH.mkdir(exist_ok=True)
 
     with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts:
@@ -104,9 +107,9 @@ def build_windows_wheel(hga: HGAutomatio
 
 
 def build_all_windows_packages(hga: HGAutomation, aws_region, revision,
-                               version):
+                               version, base_image_name):
     c = hga.aws_connection(aws_region)
-    image = aws.ensure_windows_dev_ami(c)
+    image = aws.ensure_windows_dev_ami(c, base_image_name=base_image_name)
     DIST_PATH.mkdir(exist_ok=True)
 
     with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts:
@@ -169,9 +172,9 @@ def run_tests_linux(hga: HGAutomation, a
 
 
 def run_tests_windows(hga: HGAutomation, aws_region, instance_type,
-                      python_version, arch, test_flags):
+                      python_version, arch, test_flags, base_image_name):
     c = hga.aws_connection(aws_region)
-    image = aws.ensure_windows_dev_ami(c)
+    image = aws.ensure_windows_dev_ami(c, base_image_name=base_image_name)
 
     with aws.temporary_windows_dev_instances(c, image, instance_type,
                                              disable_antivirus=True) as insts:
@@ -217,6 +220,11 @@ def get_parser():
         'bootstrap-windows-dev',
         help='Bootstrap the Windows development environment',
     )
+    sp.add_argument(
+        '--base-image-name',
+        help='AMI name of base image',
+        default=aws.WINDOWS_BASE_IMAGE_NAME,
+    )
     sp.set_defaults(func=bootstrap_windows_dev)
 
     sp = subparsers.add_parser(
@@ -232,6 +240,11 @@ def get_parser():
         '--version',
         help='Mercurial version string to use',
     )
+    sp.add_argument(
+        '--base-image-name',
+        help='AMI name of base image',
+        default=aws.WINDOWS_BASE_IMAGE_NAME,
+    )
     sp.set_defaults(func=build_all_windows_packages)
 
     sp = subparsers.add_parser(
@@ -254,6 +267,11 @@ def get_parser():
         '--version',
         help='Mercurial version string to use in installer',
     )
+    sp.add_argument(
+        '--base-image-name',
+        help='AMI name of base image',
+        default=aws.WINDOWS_BASE_IMAGE_NAME,
+    )
     sp.set_defaults(func=build_inno)
 
     sp = subparsers.add_parser(
@@ -272,6 +290,11 @@ def get_parser():
         help='Mercurial revision to build',
         default='.',
     )
+    sp.add_argument(
+        '--base-image-name',
+        help='AMI name of base image',
+        default=aws.WINDOWS_BASE_IMAGE_NAME,
+    )
     sp.set_defaults(func=build_windows_wheel)
 
     sp = subparsers.add_parser(
@@ -294,6 +317,11 @@ def get_parser():
         '--version',
         help='Mercurial version string to use in installer',
     )
+    sp.add_argument(
+        '--base-image-name',
+        help='AMI name of base image',
+        default=aws.WINDOWS_BASE_IMAGE_NAME,
+    )
     sp.set_defaults(func=build_wix)
 
     sp = subparsers.add_parser(
@@ -368,6 +396,11 @@ def get_parser():
         '--test-flags',
         help='Extra command line flags to pass to run-tests.py',
     )
+    sp.add_argument(
+        '--base-image-name',
+        help='AMI name of base image',
+        default=aws.WINDOWS_BASE_IMAGE_NAME,
+    )
     sp.set_defaults(func=run_tests_windows)
 
     return parser