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.
93 lines
2.7 KiB
Python
93 lines
2.7 KiB
Python
11 years ago
|
'''
|
||
|
Find and delete GCE resources matching the provided --match string. Unless
|
||
|
--yes|-y is provided, the prompt for confirmation prior to deleting resources.
|
||
|
Please use caution, you can easily delete your *ENTIRE* GCE infrastructure.
|
||
|
'''
|
||
|
|
||
8 years ago
|
import optparse
|
||
11 years ago
|
import os
|
||
|
import re
|
||
|
import sys
|
||
|
import yaml
|
||
|
|
||
|
try:
|
||
8 years ago
|
from libcloud.common.google import (
|
||
|
GoogleBaseError,
|
||
|
QuotaExceededError,
|
||
|
ResourceExistsError,
|
||
|
ResourceInUseError,
|
||
|
ResourceNotFoundError,
|
||
|
)
|
||
11 years ago
|
from libcloud.compute.providers import get_driver
|
||
8 years ago
|
from libcloud.compute.types import Provider
|
||
11 years ago
|
_ = Provider.GCE
|
||
|
except ImportError:
|
||
8 years ago
|
print("failed=True msg='libcloud with GCE support (0.13.3+) required for this module'")
|
||
11 years ago
|
sys.exit(1)
|
||
|
|
||
11 years ago
|
import gce_credentials
|
||
|
|
||
7 years ago
|
from ansible.module_utils.six.moves import input
|
||
|
|
||
11 years ago
|
|
||
|
def delete_gce_resources(get_func, attr, opts):
|
||
|
for item in get_func():
|
||
|
val = getattr(item, attr)
|
||
|
if re.search(opts.match_re, val, re.IGNORECASE):
|
||
|
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)
|
||
|
|
||
8 years ago
|
|
||
11 years ago
|
def prompt_and_delete(item, prompt, assumeyes):
|
||
|
if not assumeyes:
|
||
7 years ago
|
assumeyes = input(prompt).lower() == 'y'
|
||
11 years ago
|
assert hasattr(item, 'destroy'), "Class <%s> has no delete attribute" % item.__class__
|
||
|
if assumeyes:
|
||
|
item.destroy()
|
||
8 years ago
|
print("Deleted %s" % item)
|
||
|
|
||
11 years ago
|
|
||
|
def parse_args():
|
||
8 years ago
|
parser = optparse.OptionParser(
|
||
|
usage="%s [options]" % sys.argv[0],
|
||
|
description=__doc__
|
||
|
)
|
||
11 years ago
|
gce_credentials.add_credentials_options(parser)
|
||
8 years ago
|
parser.add_option(
|
||
|
"--yes", "-y",
|
||
11 years ago
|
action="store_true", dest="assumeyes",
|
||
|
default=False,
|
||
8 years ago
|
help="Don't prompt for confirmation"
|
||
|
)
|
||
|
parser.add_option(
|
||
|
"--match",
|
||
11 years ago
|
action="store", dest="match_re",
|
||
|
default="^ansible-testing-",
|
||
8 years ago
|
help="Regular expression used to find GCE resources (default: %default)"
|
||
|
)
|
||
11 years ago
|
|
||
|
(opts, args) = parser.parse_args()
|
||
11 years ago
|
gce_credentials.check_required(opts, parser)
|
||
11 years ago
|
return (opts, args)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
|
||
|
(opts, args) = parse_args()
|
||
|
|
||
|
# Connect to GCE
|
||
11 years ago
|
gce = gce_credentials.get_gce_driver(opts)
|
||
11 years ago
|
|
||
|
try:
|
||
8 years ago
|
# Delete matching instances
|
||
|
delete_gce_resources(gce.list_nodes, 'name', opts)
|
||
8 years ago
|
|
||
8 years ago
|
# Delete matching snapshots
|
||
|
def get_snapshots():
|
||
|
for volume in gce.list_volumes():
|
||
|
for snapshot in gce.list_volume_snapshots(volume):
|
||
|
yield snapshot
|
||
|
delete_gce_resources(get_snapshots, 'name', opts)
|
||
|
# Delete matching disks
|
||
|
delete_gce_resources(gce.list_volumes, 'name', opts)
|
||
9 years ago
|
except KeyboardInterrupt as e:
|
||
9 years ago
|
print("\nExiting on user command.")
|