Mercurial > public > mercurial-scm > hg
comparison contrib/automation/hgautomation/aws.py @ 44739:828d3277618a stable
automation: always use latest Windows AMI
The old AMI isn't available any more.
We seem to run into this problem every few months. Amazon (or
Microsoft) appears to be removing the old AMIs when they are
superseded or something. Let's give up on tracking known images
and switch the image selection logic to use the latest published
image.
Differential Revision: https://phab.mercurial-scm.org/D8465
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 19 Apr 2020 13:29:50 -0700 |
parents | 3d53f9cc73ab |
children | 89a2afe31e82 |
comparison
equal
deleted
inserted
replaced
44735:46856c2cc4f2 | 44739:828d3277618a |
---|---|
57 DEBIAN_ACCOUNT_ID = '379101102735' | 57 DEBIAN_ACCOUNT_ID = '379101102735' |
58 DEBIAN_ACCOUNT_ID_2 = '136693071363' | 58 DEBIAN_ACCOUNT_ID_2 = '136693071363' |
59 UBUNTU_ACCOUNT_ID = '099720109477' | 59 UBUNTU_ACCOUNT_ID = '099720109477' |
60 | 60 |
61 | 61 |
62 WINDOWS_BASE_IMAGE_NAME = 'Windows_Server-2019-English-Full-Base-2019.11.13' | 62 WINDOWS_BASE_IMAGE_NAME = 'Windows_Server-2019-English-Full-Base-*' |
63 | 63 |
64 | 64 |
65 KEY_PAIRS = { | 65 KEY_PAIRS = { |
66 'automation', | 66 'automation', |
67 } | 67 } |
462 for role in sorted(wanted - have): | 462 for role in sorted(wanted - have): |
463 print('adding role %s to %s' % (role, profile.name)) | 463 print('adding role %s to %s' % (role, profile.name)) |
464 profile.add_role(RoleName=role) | 464 profile.add_role(RoleName=role) |
465 | 465 |
466 | 466 |
467 def find_image(ec2resource, owner_id, name): | 467 def find_image(ec2resource, owner_id, name, reverse_sort_field=None): |
468 """Find an AMI by its owner ID and name.""" | 468 """Find an AMI by its owner ID and name.""" |
469 | 469 |
470 images = ec2resource.images.filter( | 470 images = ec2resource.images.filter( |
471 Filters=[ | 471 Filters=[ |
472 {'Name': 'owner-id', 'Values': [owner_id],}, | 472 {'Name': 'owner-id', 'Values': [owner_id],}, |
473 {'Name': 'state', 'Values': ['available'],}, | 473 {'Name': 'state', 'Values': ['available'],}, |
474 {'Name': 'image-type', 'Values': ['machine'],}, | 474 {'Name': 'image-type', 'Values': ['machine'],}, |
475 {'Name': 'name', 'Values': [name],}, | 475 {'Name': 'name', 'Values': [name],}, |
476 ] | 476 ] |
477 ) | 477 ) |
478 | |
479 if reverse_sort_field: | |
480 images = sorted( | |
481 images, | |
482 key=lambda image: getattr(image, reverse_sort_field), | |
483 reverse=True, | |
484 ) | |
478 | 485 |
479 for image in images: | 486 for image in images: |
480 return image | 487 return image |
481 | 488 |
482 raise Exception('unable to find image for %s' % name) | 489 raise Exception('unable to find image for %s' % name) |
1057 for instance in instances: | 1064 for instance in instances: |
1058 instance.ssh_client.close() | 1065 instance.ssh_client.close() |
1059 | 1066 |
1060 | 1067 |
1061 def ensure_windows_dev_ami( | 1068 def ensure_windows_dev_ami( |
1062 c: AWSConnection, prefix='hg-', base_image_name=WINDOWS_BASE_IMAGE_NAME | 1069 c: AWSConnection, prefix='hg-', base_image_name=WINDOWS_BASE_IMAGE_NAME, |
1063 ): | 1070 ): |
1064 """Ensure Windows Development AMI is available and up-to-date. | 1071 """Ensure Windows Development AMI is available and up-to-date. |
1065 | 1072 |
1066 If necessary, a modern AMI will be built by starting a temporary EC2 | 1073 If necessary, a modern AMI will be built by starting a temporary EC2 |
1067 instance and bootstrapping it. | 1074 instance and bootstrapping it. |
1076 ec2resource = c.ec2resource | 1083 ec2resource = c.ec2resource |
1077 ssmclient = c.session.client('ssm') | 1084 ssmclient = c.session.client('ssm') |
1078 | 1085 |
1079 name = '%s%s' % (prefix, 'windows-dev') | 1086 name = '%s%s' % (prefix, 'windows-dev') |
1080 | 1087 |
1081 image = find_image(ec2resource, AMAZON_ACCOUNT_ID, base_image_name) | 1088 image = find_image( |
1089 ec2resource, | |
1090 AMAZON_ACCOUNT_ID, | |
1091 base_image_name, | |
1092 reverse_sort_field="name", | |
1093 ) | |
1082 | 1094 |
1083 config = { | 1095 config = { |
1084 'BlockDeviceMappings': [ | 1096 'BlockDeviceMappings': [ |
1085 { | 1097 { |
1086 'DeviceName': '/dev/sda1', | 1098 'DeviceName': '/dev/sda1', |