Patchwork D7113: automation: schedule an EC2Launch run on next boot

login
register
mail settings
Submitter phabricator
Date Oct. 16, 2019, 4:17 a.m.
Message ID <differential-rev-PHID-DREV-qoxhszatrjban52bqqf5-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42377/
State Superseded
Headers show

Comments

phabricator - Oct. 16, 2019, 4:17 a.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7113

AFFECTED FILES
  contrib/automation/hgautomation/aws.py

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel
phabricator - Oct. 16, 2019, 4:11 p.m.
martinvonz added inline comments.

INLINE COMMENTS

> aws.py:1128
> +            r'''"extendBootVolumeSize": true, "handleUserData": true, '''
> +            r'''"adminPasswordType": "Specify", "adminPassword": "%s"}' ''' % c.automation.default_password(),
> +            r'C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 '

black wants this line to be wrapped. I'll fix that in flight.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7113/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7113

To: indygreg, #hg-reviewers, pulkit
Cc: martinvonz, mercurial-devel
phabricator - Oct. 16, 2019, 4:32 p.m.
pulkit added inline comments.

INLINE COMMENTS

> martinvonz wrote in aws.py:1128
> black wants this line to be wrapped. I'll fix that in flight.

I should install black, I was waiting for Augie's fix to land to prevent doing some extra work.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7113/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7113

To: indygreg, #hg-reviewers, pulkit
Cc: martinvonz, mercurial-devel

Patch

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
@@ -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,22 @@ 
     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 +1155,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]