# HG changeset patch # User Gregory Szorc # Date 2019-10-15 04:21:58 # Node ID c09e8ac3f61f980cf138ee91a086d6ff5edffe6c # Parent ea62d7b06c129be54aaf0cf389b6e14dfedf638b 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 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 @@ def temporary_ec2_instances(ec2resource, @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 @@ def create_temp_windows_ec2_instances(c: '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 @@ def ensure_windows_dev_ami( 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 @@ def ensure_windows_dev_ami( 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]