def delete_hosts(ec2_conn, ipa_client, hostnames): """ Unenrolls hosts from IPA and AWS """ for hostname in hostnames: try: ipa_client.unenroll_host(hostname=hostname) except (KeyError, ipaclient.NotFoundError): pass ec2client.delete_instances(ec2_conn, hostnames=hostnames)
def delete_hosts(ec2_conn, ipa_client, hostnames, ipa_delete=True): """ Unenrolls hosts from IPA and AWS Removes any A or PTR records left by the host post-deletion EC2 imposes a maximum limit on the number of instances that can be selected using filters (200); delete instances in batches of _EC2_DELETE_BATCH """ _LOGGER.debug('Delete instances: %r', hostnames) hostnames_left = hostnames[:] # Terminate EC2 instances while hostnames_left: batch = hostnames_left[:_EC2_DELETE_BATCH] hostnames_left = hostnames_left[_EC2_DELETE_BATCH:] ec2client.delete_instances(ec2_conn=ec2_conn, hostnames=batch) if not ipa_delete: return # Remove host from IPA domain and DNS for hostname in hostnames: _LOGGER.debug('Unenroll host from IPA: %s', hostname) shortname = hostname.split('.')[0] # Fetch A and PTR records if they exist try: arecord = ipa_client.get_dns_record( idnsname=shortname )['arecord'].pop() record_zone = '{2}.{1}.{0}.in-addr.arpa.'.format( *arecord.split('.')) record = '{3}'.format(*arecord.split('.')) except (ipaclient.NotFoundError, KeyError): arecord = record_zone = record = None # Remove host from IPA try: ipa_client.unenroll_host(hostname) except (KeyError, ipaclient.NotFoundError): _LOGGER.debug('Host not found: %s', hostname) # Delete A and PTR records. try: if arecord: ipa_client.delete_dns_record('arecord', shortname, arecord) except ipaclient.NotFoundError: pass try: if record: ipa_client.delete_ptr_record(record, hostname, record_zone) except ipaclient.NotFoundError: pass except ipaclient.ExecutionError: # Invalid PTR record, delete bad data ipa_client.force_delete_dns_record(record, dns_zone=record_zone)
def test_delete_single_host(self): """ Test delete_instance call to AWS with single hostname """ ec2_conn = mock.MagicMock() treadmill_aws.ec2client.list_instances.return_value = [{ 'InstanceId': 'i-0123456789' }] ec2client.delete_instances(ec2_conn, hostnames=['host1.foo.com']) self.assertEqual(ec2_conn.terminate_instances.call_count, 1) ec2_conn.terminate_instances.assert_called_with( InstanceIds=['i-0123456789'], DryRun=False)
def delete_hosts(ec2_conn, ipa_client, hostnames): """ Unenrolls hosts from IPA and AWS EC2 imposes a maximum limit on the number of instances that can be selected using filters (200); delete instances in batches of _EC2_DELETE_BATCH """ for hostname in hostnames: _LOGGER.debug('Unenroll host from IPA: %s', hostname) try: ipa_client.unenroll_host(hostname=hostname) except (KeyError, ipaclient.NotFoundError): _LOGGER.debug('Host not found: %s', hostname) _LOGGER.debug('Delete instances: %r', hostnames) while hostnames: batch = hostnames[:_EC2_DELETE_BATCH] hostnames = hostnames[_EC2_DELETE_BATCH:] ec2client.delete_instances(ec2_conn=ec2_conn, hostnames=batch)
def test_delete_nonexistant_host(self): """ Test delete_instance call to AWS with nonexistant hostname """ ec2_conn = mock.MagicMock() ec2client.delete_instances(ec2_conn, hostnames=['host1.foo.com']) self.assertEqual(ec2_conn.terminate_instances.call_count, 0)