automation: schedule an EC2Launch run on next boot
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 14 Oct 2019 21:21:58 -0700
changeset 43234 c09e8ac3f61f
parent 43233 ea62d7b06c12
child 43235 252ec4dbc1b4
automation: schedule an EC2Launch run on next boot Without this, launching EC2 instances constructed from the AMI won't go through the normal EC2 instance launch machinery. This missing machinery does important things like set up network routes to use the instance metadata service and process any UserData. Since EC2Launch now runs on subsequent boots and UserData is processed, we needed to make setting of UserData conditional on bootstrapping mode. Differential Revision: https://phab.mercurial-scm.org/D7113
contrib/automation/hgautomation/aws.py
--- a/contrib/automation/hgautomation/aws.py	Tue Oct 15 09:54:05 2019 -0400
+++ b/contrib/automation/hgautomation/aws.py	Mon Oct 14 21:21:58 2019 -0700
@@ -687,7 +687,9 @@
 
 
 @contextlib.contextmanager
-def create_temp_windows_ec2_instances(c: AWSConnection, config):
+def create_temp_windows_ec2_instances(
+    c: AWSConnection, config, bootstrap: bool = False
+):
     """Create temporary Windows EC2 instances.
 
     This is a higher-level wrapper around ``create_temp_ec2_instances()`` that
@@ -712,7 +714,9 @@
             'Tags': [{'Key': 'Name', 'Value': 'hg-temp-windows'}],
         }
     )
-    config['UserData'] = WINDOWS_USER_DATA % password
+
+    if bootstrap:
+        config['UserData'] = WINDOWS_USER_DATA % password
 
     with temporary_ec2_instances(c.ec2resource, config) as instances:
         wait_for_ip_addresses(instances)
@@ -1111,6 +1115,23 @@
     with INSTALL_WINDOWS_DEPENDENCIES.open('r', encoding='utf-8') as fh:
         commands.extend(l.rstrip() for l in fh)
 
+    # Schedule run of EC2Launch on next boot. This ensures that UserData
+    # is executed.
+    # We disable setComputerName because it forces a reboot.
+    # We set an explicit admin password because this causes UserData to run
+    # as Administrator instead of System.
+    commands.extend(
+        [
+            r'''Set-Content -Path C:\ProgramData\Amazon\EC2-Windows\Launch\Config\LaunchConfig.json '''
+            r'''-Value '{"setComputerName": false, "setWallpaper": true, "addDnsSuffixList": true, '''
+            r'''"extendBootVolumeSize": true, "handleUserData": true, '''
+            r'''"adminPasswordType": "Specify", "adminPassword": "%s"}' '''
+            % c.automation.default_password(),
+            r'C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 '
+            r'–Schedule',
+        ]
+    )
+
     # Disable Windows Defender when bootstrapping because it just slows
     # things down.
     commands.insert(0, 'Set-MpPreference -DisableRealtimeMonitoring $true')
@@ -1135,7 +1156,9 @@
 
     print('no suitable Windows development image found; creating one...')
 
-    with create_temp_windows_ec2_instances(c, config) as instances:
+    with create_temp_windows_ec2_instances(
+        c, config, bootstrap=True
+    ) as instances:
         assert len(instances) == 1
         instance = instances[0]