Mercurial > public > mercurial-scm > hg
comparison contrib/automation/hgautomation/aws.py @ 43234:c09e8ac3f61f
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
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 14 Oct 2019 21:21:58 -0700 |
parents | 2372284d9457 |
children | a77338d2bdab |
comparison
equal
deleted
inserted
replaced
43233:ea62d7b06c12 | 43234:c09e8ac3f61f |
---|---|
685 instance.terminate() | 685 instance.terminate() |
686 print('terminated %d instances' % len(ids)) | 686 print('terminated %d instances' % len(ids)) |
687 | 687 |
688 | 688 |
689 @contextlib.contextmanager | 689 @contextlib.contextmanager |
690 def create_temp_windows_ec2_instances(c: AWSConnection, config): | 690 def create_temp_windows_ec2_instances( |
691 c: AWSConnection, config, bootstrap: bool = False | |
692 ): | |
691 """Create temporary Windows EC2 instances. | 693 """Create temporary Windows EC2 instances. |
692 | 694 |
693 This is a higher-level wrapper around ``create_temp_ec2_instances()`` that | 695 This is a higher-level wrapper around ``create_temp_ec2_instances()`` that |
694 configures the Windows instance for Windows Remote Management. The emitted | 696 configures the Windows instance for Windows Remote Management. The emitted |
695 instances will have a ``winrm_client`` attribute containing a | 697 instances will have a ``winrm_client`` attribute containing a |
710 { | 712 { |
711 'ResourceType': 'instance', | 713 'ResourceType': 'instance', |
712 'Tags': [{'Key': 'Name', 'Value': 'hg-temp-windows'}], | 714 'Tags': [{'Key': 'Name', 'Value': 'hg-temp-windows'}], |
713 } | 715 } |
714 ) | 716 ) |
715 config['UserData'] = WINDOWS_USER_DATA % password | 717 |
718 if bootstrap: | |
719 config['UserData'] = WINDOWS_USER_DATA % password | |
716 | 720 |
717 with temporary_ec2_instances(c.ec2resource, config) as instances: | 721 with temporary_ec2_instances(c.ec2resource, config) as instances: |
718 wait_for_ip_addresses(instances) | 722 wait_for_ip_addresses(instances) |
719 | 723 |
720 print('waiting for Windows Remote Management service...') | 724 print('waiting for Windows Remote Management service...') |
1109 ] | 1113 ] |
1110 | 1114 |
1111 with INSTALL_WINDOWS_DEPENDENCIES.open('r', encoding='utf-8') as fh: | 1115 with INSTALL_WINDOWS_DEPENDENCIES.open('r', encoding='utf-8') as fh: |
1112 commands.extend(l.rstrip() for l in fh) | 1116 commands.extend(l.rstrip() for l in fh) |
1113 | 1117 |
1118 # Schedule run of EC2Launch on next boot. This ensures that UserData | |
1119 # is executed. | |
1120 # We disable setComputerName because it forces a reboot. | |
1121 # We set an explicit admin password because this causes UserData to run | |
1122 # as Administrator instead of System. | |
1123 commands.extend( | |
1124 [ | |
1125 r'''Set-Content -Path C:\ProgramData\Amazon\EC2-Windows\Launch\Config\LaunchConfig.json ''' | |
1126 r'''-Value '{"setComputerName": false, "setWallpaper": true, "addDnsSuffixList": true, ''' | |
1127 r'''"extendBootVolumeSize": true, "handleUserData": true, ''' | |
1128 r'''"adminPasswordType": "Specify", "adminPassword": "%s"}' ''' | |
1129 % c.automation.default_password(), | |
1130 r'C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 ' | |
1131 r'–Schedule', | |
1132 ] | |
1133 ) | |
1134 | |
1114 # Disable Windows Defender when bootstrapping because it just slows | 1135 # Disable Windows Defender when bootstrapping because it just slows |
1115 # things down. | 1136 # things down. |
1116 commands.insert(0, 'Set-MpPreference -DisableRealtimeMonitoring $true') | 1137 commands.insert(0, 'Set-MpPreference -DisableRealtimeMonitoring $true') |
1117 commands.append('Set-MpPreference -DisableRealtimeMonitoring $false') | 1138 commands.append('Set-MpPreference -DisableRealtimeMonitoring $false') |
1118 | 1139 |
1133 if existing_image: | 1154 if existing_image: |
1134 return existing_image | 1155 return existing_image |
1135 | 1156 |
1136 print('no suitable Windows development image found; creating one...') | 1157 print('no suitable Windows development image found; creating one...') |
1137 | 1158 |
1138 with create_temp_windows_ec2_instances(c, config) as instances: | 1159 with create_temp_windows_ec2_instances( |
1160 c, config, bootstrap=True | |
1161 ) as instances: | |
1139 assert len(instances) == 1 | 1162 assert len(instances) == 1 |
1140 instance = instances[0] | 1163 instance = instances[0] |
1141 | 1164 |
1142 wait_for_ssm(ssmclient, [instance]) | 1165 wait_for_ssm(ssmclient, [instance]) |
1143 | 1166 |