mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 20:31:27 -07:00
test/: PEP8 compliancy (#24803)
* test/: PEP8 compliancy - Make PEP8 compliant * Python3 chokes on casting int to bytes (#24952) But if we tell the formatter that the var is a number, it works
This commit is contained in:
parent
31c59ad5f9
commit
4efec414e7
110 changed files with 1702 additions and 1547 deletions
|
@ -4,15 +4,16 @@ Find and delete AWS resources matching the provided --match string. Unless
|
|||
Please use caution, you can easily delete you're *ENTIRE* EC2 infrastructure.
|
||||
'''
|
||||
|
||||
import boto
|
||||
import boto.ec2.elb
|
||||
import optparse
|
||||
import os
|
||||
import os.path
|
||||
import re
|
||||
import sys
|
||||
import boto
|
||||
import optparse
|
||||
import yaml
|
||||
import os.path
|
||||
import boto.ec2.elb
|
||||
import time
|
||||
import yaml
|
||||
|
||||
|
||||
def delete_aws_resources(get_func, attr, opts):
|
||||
for item in get_func():
|
||||
|
@ -20,6 +21,7 @@ def delete_aws_resources(get_func, attr, opts):
|
|||
if re.search(opts.match_re, val):
|
||||
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)
|
||||
|
||||
|
||||
def delete_autoscaling_group(get_func, attr, opts):
|
||||
assumeyes = opts.assumeyes
|
||||
group_name = None
|
||||
|
@ -49,7 +51,8 @@ def delete_autoscaling_group(get_func, attr, opts):
|
|||
group.delete()
|
||||
while len(asg.get_all_groups(names=[group_name])):
|
||||
time.sleep(5)
|
||||
print ("Terminated ASG: %s" % group_name)
|
||||
print("Terminated ASG: %s" % group_name)
|
||||
|
||||
|
||||
def delete_aws_eips(get_func, attr, opts):
|
||||
|
||||
|
@ -65,22 +68,25 @@ def delete_aws_eips(get_func, attr, opts):
|
|||
if val in eip_log:
|
||||
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)
|
||||
|
||||
|
||||
def delete_aws_instances(reservation, opts):
|
||||
for list in reservation:
|
||||
for item in list.instances:
|
||||
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)
|
||||
|
||||
|
||||
def prompt_and_delete(item, prompt, assumeyes):
|
||||
if not assumeyes:
|
||||
assumeyes = raw_input(prompt).lower() == 'y'
|
||||
assert hasattr(item, 'delete') or hasattr(item, 'terminate') , "Class <%s> has no delete or terminate attribute" % item.__class__
|
||||
assert hasattr(item, 'delete') or hasattr(item, 'terminate'), "Class <%s> has no delete or terminate attribute" % item.__class__
|
||||
if assumeyes:
|
||||
if hasattr(item, 'delete'):
|
||||
item.delete()
|
||||
print ("Deleted %s" % item)
|
||||
print("Deleted %s" % item)
|
||||
if hasattr(item, 'terminate'):
|
||||
item.terminate()
|
||||
print ("Terminated %s" % item)
|
||||
print("Terminated %s" % item)
|
||||
|
||||
|
||||
def parse_args():
|
||||
# Load details from credentials.yml
|
||||
|
@ -94,45 +100,61 @@ def parse_args():
|
|||
if default_aws_secret_key is None:
|
||||
default_aws_secret_key = credentials['ec2_secret_key']
|
||||
|
||||
parser = optparse.OptionParser(usage="%s [options]" % (sys.argv[0],),
|
||||
description=__doc__)
|
||||
parser.add_option("--access",
|
||||
parser = optparse.OptionParser(
|
||||
usage="%s [options]" % (sys.argv[0], ),
|
||||
description=__doc__
|
||||
)
|
||||
parser.add_option(
|
||||
"--access",
|
||||
action="store", dest="ec2_access_key",
|
||||
default=default_aws_access_key,
|
||||
help="Amazon ec2 access id. Can use EC2_ACCESS_KEY environment variable, or a values from credentials.yml.")
|
||||
parser.add_option("--secret",
|
||||
help="Amazon ec2 access id. Can use EC2_ACCESS_KEY environment variable, or a values from credentials.yml."
|
||||
)
|
||||
parser.add_option(
|
||||
"--secret",
|
||||
action="store", dest="ec2_secret_key",
|
||||
default=default_aws_secret_key,
|
||||
help="Amazon ec2 secret key. Can use EC2_SECRET_KEY environment variable, or a values from credentials.yml.")
|
||||
parser.add_option("--eip-log",
|
||||
help="Amazon ec2 secret key. Can use EC2_SECRET_KEY environment variable, or a values from credentials.yml."
|
||||
)
|
||||
parser.add_option(
|
||||
"--eip-log",
|
||||
action="store", dest="eip_log",
|
||||
default = None,
|
||||
help = "Path to log of EIPs created during test.")
|
||||
parser.add_option("--integration-config",
|
||||
default=None,
|
||||
help="Path to log of EIPs created during test."
|
||||
)
|
||||
parser.add_option(
|
||||
"--integration-config",
|
||||
action="store", dest="int_config",
|
||||
default = "integration_config.yml",
|
||||
help = "path to integration config")
|
||||
parser.add_option("--credentials", "-c",
|
||||
default="integration_config.yml",
|
||||
help="path to integration config"
|
||||
)
|
||||
parser.add_option(
|
||||
"--credentials", "-c",
|
||||
action="store", dest="credential_file",
|
||||
default="credentials.yml",
|
||||
help="YAML file to read cloud credentials (default: %default)")
|
||||
parser.add_option("--yes", "-y",
|
||||
help="YAML file to read cloud credentials (default: %default)"
|
||||
)
|
||||
parser.add_option(
|
||||
"--yes", "-y",
|
||||
action="store_true", dest="assumeyes",
|
||||
default=False,
|
||||
help="Don't prompt for confirmation")
|
||||
parser.add_option("--match",
|
||||
help="Don't prompt for confirmation"
|
||||
)
|
||||
parser.add_option(
|
||||
"--match",
|
||||
action="store", dest="match_re",
|
||||
default="^ansible-testing-",
|
||||
help="Regular expression used to find AWS resources (default: %default)")
|
||||
help="Regular expression used to find AWS resources (default: %default)"
|
||||
)
|
||||
|
||||
(opts, args) = parser.parse_args()
|
||||
for required in ['ec2_access_key', 'ec2_secret_key']:
|
||||
if getattr(opts, required) is None:
|
||||
parser.error("Missing required parameter: --%s" % required)
|
||||
|
||||
|
||||
return (opts, args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
(opts, args) = parse_args()
|
||||
|
@ -140,17 +162,17 @@ if __name__ == '__main__':
|
|||
int_config = yaml.load(open(opts.int_config).read())
|
||||
if not opts.eip_log:
|
||||
output_dir = os.path.expanduser(int_config["output_dir"])
|
||||
opts.eip_log = output_dir + '/' + opts.match_re.replace('^','') + '-eip_integration_tests.log'
|
||||
opts.eip_log = output_dir + '/' + opts.match_re.replace('^', '') + '-eip_integration_tests.log'
|
||||
|
||||
# Connect to AWS
|
||||
aws = boto.connect_ec2(aws_access_key_id=opts.ec2_access_key,
|
||||
aws_secret_access_key=opts.ec2_secret_key)
|
||||
aws_secret_access_key=opts.ec2_secret_key)
|
||||
|
||||
elb = boto.connect_elb(aws_access_key_id=opts.ec2_access_key,
|
||||
aws_secret_access_key=opts.ec2_secret_key)
|
||||
aws_secret_access_key=opts.ec2_secret_key)
|
||||
|
||||
asg = boto.connect_autoscale(aws_access_key_id=opts.ec2_access_key,
|
||||
aws_secret_access_key=opts.ec2_secret_key)
|
||||
aws_secret_access_key=opts.ec2_secret_key)
|
||||
|
||||
try:
|
||||
# Delete matching keys
|
||||
|
@ -172,7 +194,7 @@ if __name__ == '__main__':
|
|||
delete_aws_eips(aws.get_all_addresses, 'public_ip', opts)
|
||||
|
||||
# Delete temporary instances
|
||||
filters = {"tag:Name":opts.match_re.replace('^',''), "instance-state-name": ['running', 'pending', 'stopped' ]}
|
||||
filters = {"tag:Name": opts.match_re.replace('^', ''), "instance-state-name": ['running', 'pending', 'stopped']}
|
||||
delete_aws_instances(aws.get_all_instances(filters=filters), opts)
|
||||
|
||||
except KeyboardInterrupt as e:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue