mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
6.4 KiB
Python
183 lines
6.4 KiB
Python
10 years ago
|
#!/usr/bin/env python
|
||
|
|
||
|
import os
|
||
10 years ago
|
import re
|
||
10 years ago
|
import yaml
|
||
|
import argparse
|
||
|
|
||
|
try:
|
||
|
import pyrax
|
||
|
HAS_PYRAX = True
|
||
|
except ImportError:
|
||
|
HAS_PYRAX = False
|
||
|
|
||
7 years ago
|
from ansible.module_utils.six.moves import input
|
||
|
|
||
10 years ago
|
|
||
10 years ago
|
def rax_list_iterator(svc, *args, **kwargs):
|
||
|
method = kwargs.pop('method', 'list')
|
||
|
items = getattr(svc, method)(*args, **kwargs)
|
||
|
while items:
|
||
|
retrieved = getattr(svc, method)(*args, marker=items[-1].id, **kwargs)
|
||
|
if items and retrieved and items[-1].id == retrieved[0].id:
|
||
|
del items[-1]
|
||
|
items.extend(retrieved)
|
||
|
if len(retrieved) < 2:
|
||
|
break
|
||
|
return items
|
||
|
|
||
|
|
||
10 years ago
|
def parse_args():
|
||
|
parser = argparse.ArgumentParser()
|
||
|
parser.add_argument('-y', '--yes', action='store_true', dest='assumeyes',
|
||
|
default=False, help="Don't prompt for confirmation")
|
||
|
parser.add_argument('--match', dest='match_re',
|
||
|
default='^ansible-testing',
|
||
|
help='Regular expression used to find resources '
|
||
|
'(default: %(default)s)')
|
||
|
|
||
|
return parser.parse_args()
|
||
|
|
||
|
|
||
|
def authenticate():
|
||
|
try:
|
||
|
with open(os.path.realpath('./credentials.yml')) as f:
|
||
|
credentials = yaml.load(f)
|
||
|
except Exception as e:
|
||
|
raise SystemExit(e)
|
||
|
|
||
|
try:
|
||
|
pyrax.set_credentials(credentials.get('rackspace_username'),
|
||
|
credentials.get('rackspace_api_key'))
|
||
|
except Exception as e:
|
||
|
raise SystemExit(e)
|
||
|
|
||
|
|
||
|
def prompt_and_delete(item, prompt, assumeyes):
|
||
|
if not assumeyes:
|
||
7 years ago
|
assumeyes = input(prompt).lower() == 'y'
|
||
9 years ago
|
assert hasattr(item, 'delete') or hasattr(item, 'terminate'), \
|
||
8 years ago
|
"Class <%s> has no delete or terminate attribute" % item.__class__
|
||
10 years ago
|
if assumeyes:
|
||
|
if hasattr(item, 'delete'):
|
||
|
item.delete()
|
||
8 years ago
|
print("Deleted %s" % item)
|
||
10 years ago
|
if hasattr(item, 'terminate'):
|
||
|
item.terminate()
|
||
8 years ago
|
print("Terminated %s" % item)
|
||
10 years ago
|
|
||
|
|
||
|
def delete_rax(args):
|
||
|
"""Function for deleting CloudServers"""
|
||
8 years ago
|
print("--- Cleaning CloudServers matching '%s'" % args.match_re)
|
||
10 years ago
|
search_opts = dict(name='^%s' % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.compute.regions:
|
||
|
cs = pyrax.connect_to_cloudservers(region=region)
|
||
10 years ago
|
servers = rax_list_iterator(cs.servers, search_opts=search_opts)
|
||
10 years ago
|
for server in servers:
|
||
|
prompt_and_delete(server,
|
||
|
'Delete matching %s? [y/n]: ' % server,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def delete_rax_clb(args):
|
||
|
"""Function for deleting Cloud Load Balancers"""
|
||
8 years ago
|
print("--- Cleaning Cloud Load Balancers matching '%s'" % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.load_balancer.regions:
|
||
|
clb = pyrax.connect_to_cloud_loadbalancers(region=region)
|
||
|
for lb in rax_list_iterator(clb):
|
||
|
if re.search(args.match_re, lb.name):
|
||
|
prompt_and_delete(lb,
|
||
|
'Delete matching %s? [y/n]: ' % lb,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def delete_rax_keypair(args):
|
||
|
"""Function for deleting Rackspace Key pairs"""
|
||
8 years ago
|
print("--- Cleaning Key Pairs matching '%s'" % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.compute.regions:
|
||
|
cs = pyrax.connect_to_cloudservers(region=region)
|
||
|
for keypair in cs.keypairs.list():
|
||
|
if re.search(args.match_re, keypair.name):
|
||
|
prompt_and_delete(keypair,
|
||
|
'Delete matching %s? [y/n]: ' % keypair,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def delete_rax_network(args):
|
||
|
"""Function for deleting Cloud Networks"""
|
||
8 years ago
|
print("--- Cleaning Cloud Networks matching '%s'" % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.network.regions:
|
||
|
cnw = pyrax.connect_to_cloud_networks(region=region)
|
||
|
for network in cnw.list():
|
||
|
if re.search(args.match_re, network.name):
|
||
|
prompt_and_delete(network,
|
||
|
'Delete matching %s? [y/n]: ' % network,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def delete_rax_cbs(args):
|
||
|
"""Function for deleting Cloud Networks"""
|
||
8 years ago
|
print("--- Cleaning Cloud Block Storage matching '%s'" % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.network.regions:
|
||
|
cbs = pyrax.connect_to_cloud_blockstorage(region=region)
|
||
|
for volume in cbs.list():
|
||
|
if re.search(args.match_re, volume.name):
|
||
|
prompt_and_delete(volume,
|
||
|
'Delete matching %s? [y/n]: ' % volume,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def delete_rax_cdb(args):
|
||
|
"""Function for deleting Cloud Databases"""
|
||
8 years ago
|
print("--- Cleaning Cloud Databases matching '%s'" % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.database.regions:
|
||
|
cdb = pyrax.connect_to_cloud_databases(region=region)
|
||
|
for db in rax_list_iterator(cdb):
|
||
|
if re.search(args.match_re, db.name):
|
||
|
prompt_and_delete(db,
|
||
|
'Delete matching %s? [y/n]: ' % db,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def _force_delete_rax_scaling_group(manager):
|
||
|
def wrapped(uri):
|
||
|
manager.api.method_delete('%s?force=true' % uri)
|
||
|
return wrapped
|
||
|
|
||
|
|
||
|
def delete_rax_scaling_group(args):
|
||
|
"""Function for deleting Autoscale Groups"""
|
||
8 years ago
|
print("--- Cleaning Autoscale Groups matching '%s'" % args.match_re)
|
||
10 years ago
|
for region in pyrax.identity.services.autoscale.regions:
|
||
|
asg = pyrax.connect_to_autoscale(region=region)
|
||
|
for group in rax_list_iterator(asg):
|
||
|
if re.search(args.match_re, group.name):
|
||
|
group.manager._delete = \
|
||
|
_force_delete_rax_scaling_group(group.manager)
|
||
|
prompt_and_delete(group,
|
||
|
'Delete matching %s? [y/n]: ' % group,
|
||
|
args.assumeyes)
|
||
|
|
||
|
|
||
10 years ago
|
def main():
|
||
|
if not HAS_PYRAX:
|
||
|
raise SystemExit('The pyrax python module is required for this script')
|
||
|
|
||
|
args = parse_args()
|
||
|
authenticate()
|
||
10 years ago
|
|
||
10 years ago
|
funcs = [f for n, f in globals().items() if n.startswith('delete_rax')]
|
||
|
for func in sorted(funcs, key=lambda f: f.__name__):
|
||
10 years ago
|
try:
|
||
|
func(args)
|
||
|
except Exception as e:
|
||
8 years ago
|
print("---- %s failed (%s)" % (func.__name__, e.message))
|
||
10 years ago
|
|
||
10 years ago
|
|
||
|
if __name__ == '__main__':
|
||
10 years ago
|
try:
|
||
|
main()
|
||
|
except KeyboardInterrupt:
|
||
8 years ago
|
print('\nExiting...')
|