def list_clusters(): for namespace in PCWConfig.get_namespaces_for('clusters'): try: clusters = EC2(namespace).all_clusters() logger.info("%d clusters found", len(clusters)) send_cluster_notification(namespace, clusters) except Exception as e: logger.exception("[{}] List clusters failed!".format(namespace)) send_mail('{} on List clusters in [{}]'.format(type(e).__name__, namespace), traceback.format_exc())
def list_clusters(): cfg = ConfigFile() if cfg.has('clusters'): for vault_namespace in cfg.getList(['clusters', 'namespaces'], ['']): try: clusters = EC2(vault_namespace).all_clusters() logger.info("%d clusters found", len(clusters)) send_cluster_notification(vault_namespace, clusters) except Exception as e: logger.exception("[{}] List clusters failed!".format(vault_namespace)) send_mail('{} on List clusters in [{}]'.format( type(e).__name__, vault_namespace), traceback.format_exc())
def cleanup_run(): for namespace in PCWConfig.get_namespaces_for('cleanup'): try: providers = PCWConfig.get_providers_for('cleanup', namespace) logger.debug("[{}] Run cleanup for {}".format(namespace, ','.join(providers))) if 'azure' in providers: Azure(namespace).cleanup_all() if 'ec2' in providers: EC2(namespace).cleanup_all() if 'gce' in providers: GCE(namespace).cleanup_all() except Exception as e: logger.exception("[{}] Cleanup failed!".format(namespace)) send_mail('{} on Cleanup in [{}]'.format(type(e).__name__, namespace), traceback.format_exc())
def cleanup_run(): cfg = ConfigFile() if cfg.has('cleanup'): for vault_namespace in cfg.getList(['cleanup', 'namespaces'], cfg.getList(['vault', 'namespaces'], [''])): try: providers = cfg.getList(['vault.namespace.{}'.format(vault_namespace), 'providers'], ['ec2', 'azure', 'gce']) logger.debug("[{}] Run cleanup for {}".format(vault_namespace, ','.join(providers))) if 'azure' in providers: Azure(vault_namespace).cleanup_all() if 'ec2' in providers: EC2(vault_namespace).cleanup_all() if 'gce' in providers: GCE(vault_namespace).cleanup_all() except Exception as e: logger.exception("[{}] Cleanup failed!".format(vault_namespace)) send_mail('{} on Cleanup in [{}]'.format(type(e).__name__, vault_namespace), traceback.format_exc())
def cleanup_uploader_vpcs(self): for region in self.all_regions: response = self.ec2_client(region).describe_vpcs( Filters=[{ 'Name': 'isDefault', 'Values': ['false'] }, { 'Name': 'tag:Name', 'Values': ['uploader-*'] }]) for response_vpc in response['Vpcs']: self.log_info( '{} in {} looks like uploader leftover. (OwnerId={}).', response_vpc['VpcId'], region, response_vpc['OwnerId']) if PCWConfig.getBoolean('cleanup/vpc-notify-only', self._namespace): send_mail( 'VPC {} should be deleted, skipping due vpc-notify-only=True' .format(response_vpc['VpcId']), '') else: resource_vpc = self.ec2_resource(region).Vpc( response_vpc['VpcId']) can_be_deleted = True for subnet in resource_vpc.subnets.all(): if len(list(subnet.instances.all())): self.log_warn( '{} has associated instance(s) so can not be deleted', response_vpc['VpcId']) can_be_deleted = False break if can_be_deleted: self.delete_vpc(region, resource_vpc, response_vpc['VpcId']) elif not self.dry_run: body = 'Uploader leftover {} (OwnerId={}) in {} is locked'.format( response_vpc['VpcId'], response_vpc['OwnerId'], region) send_mail('VPC deletion locked by running VMs', body)
def delete_vpc(self, region, vpc, vpcId): try: self.log_info( '{} has no associated instances. Initializing cleanup of it', vpc) self.delete_internet_gw(vpc) self.delete_routing_tables(vpc) self.delete_vpc_endpoints(region, vpcId) self.delete_security_groups(vpc) self.delete_vpc_peering_connections(region, vpcId) self.delete_network_acls(vpc) self.delete_vpc_subnets(vpc) if self.dry_run: self.log_info('Deletion of VPC skipped due to dry_run mode') else: # finally, delete the vpc self.ec2_resource(region).meta.client.delete_vpc(VpcId=vpcId) except Exception as e: self.log_err("{} on VPC deletion. {}", type(e).__name__, traceback.format_exc()) send_mail( '{} on VPC deletion in [{}]'.format( type(e).__name__, self._namespace), traceback.format_exc())