Mercurial > public > mercurial-scm > hg
comparison contrib/automation/hgautomation/aws.py @ 42277:dd6a9723ae2b
automation: don't create resources when deleting things
Otherwise running these commands can result in resources being
created. In the case of `purge-ec2-resources`, we will create
resources only to delete them immediately afterwards!
With this change, `purge-ec2-resources` now no-ops if no
resources exist.
# no-check-commit because foo_bar function name
Differential Revision: https://phab.mercurial-scm.org/D6285
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 19 Apr 2019 05:20:33 -0700 |
parents | fcb97cb91ff8 |
children | 8dc22a209420 |
comparison
equal
deleted
inserted
replaced
42276:fcb97cb91ff8 | 42277:dd6a9723ae2b |
---|---|
178 | 178 |
179 | 179 |
180 class AWSConnection: | 180 class AWSConnection: |
181 """Manages the state of a connection with AWS.""" | 181 """Manages the state of a connection with AWS.""" |
182 | 182 |
183 def __init__(self, automation, region: str): | 183 def __init__(self, automation, region: str, ensure_ec2_state: bool=True): |
184 self.automation = automation | 184 self.automation = automation |
185 self.local_state_path = automation.state_path | 185 self.local_state_path = automation.state_path |
186 | 186 |
187 self.prefix = 'hg-' | 187 self.prefix = 'hg-' |
188 | 188 |
189 self.session = boto3.session.Session(region_name=region) | 189 self.session = boto3.session.Session(region_name=region) |
190 self.ec2client = self.session.client('ec2') | 190 self.ec2client = self.session.client('ec2') |
191 self.ec2resource = self.session.resource('ec2') | 191 self.ec2resource = self.session.resource('ec2') |
192 self.iamclient = self.session.client('iam') | 192 self.iamclient = self.session.client('iam') |
193 self.iamresource = self.session.resource('iam') | 193 self.iamresource = self.session.resource('iam') |
194 | 194 self.security_groups = {} |
195 ensure_key_pairs(automation.state_path, self.ec2resource) | 195 |
196 | 196 if ensure_ec2_state: |
197 self.security_groups = ensure_security_groups(self.ec2resource) | 197 ensure_key_pairs(automation.state_path, self.ec2resource) |
198 ensure_iam_state(self.iamresource) | 198 self.security_groups = ensure_security_groups(self.ec2resource) |
199 ensure_iam_state(self.iamresource) | |
199 | 200 |
200 def key_pair_path_private(self, name): | 201 def key_pair_path_private(self, name): |
201 """Path to a key pair private key file.""" | 202 """Path to a key pair private key file.""" |
202 return self.local_state_path / 'keys' / ('keypair-%s' % name) | 203 return self.local_state_path / 'keys' / ('keypair-%s' % name) |
203 | 204 |