2014-03-30 00:27:48 +00:00
|
|
|
'''
|
|
|
|
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.
|
|
|
|
'''
|
|
|
|
|
2017-05-30 17:05:19 +00:00
|
|
|
import optparse
|
2014-03-30 00:27:48 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
try:
|
2017-05-30 17:05:19 +00:00
|
|
|
from libcloud.common.google import (
|
|
|
|
GoogleBaseError,
|
|
|
|
QuotaExceededError,
|
|
|
|
ResourceExistsError,
|
|
|
|
ResourceInUseError,
|
|
|
|
ResourceNotFoundError,
|
|
|
|
)
|
2014-03-30 00:27:48 +00:00
|
|
|
from libcloud.compute.providers import get_driver
|
2017-05-30 17:05:19 +00:00
|
|
|
from libcloud.compute.types import Provider
|
2014-03-30 00:27:48 +00:00
|
|
|
_ = Provider.GCE
|
|
|
|
except ImportError:
|
2017-05-30 17:05:19 +00:00
|
|
|
print("failed=True msg='libcloud with GCE support (0.13.3+) required for this module'")
|
2014-03-30 00:27:48 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2014-04-13 23:16:21 +00:00
|
|
|
import gce_credentials
|
|
|
|
|
2017-07-23 01:15:46 +00:00
|
|
|
from ansible.module_utils.six.moves import input
|
|
|
|
|
2014-03-30 00:27:48 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-05-30 17:05:19 +00:00
|
|
|
|
2014-03-30 00:27:48 +00:00
|
|
|
def prompt_and_delete(item, prompt, assumeyes):
|
|
|
|
if not assumeyes:
|
2017-07-23 01:15:46 +00:00
|
|
|
assumeyes = input(prompt).lower() == 'y'
|
2014-03-30 00:27:48 +00:00
|
|
|
assert hasattr(item, 'destroy'), "Class <%s> has no delete attribute" % item.__class__
|
|
|
|
if assumeyes:
|
|
|
|
item.destroy()
|
2017-05-30 17:05:19 +00:00
|
|
|
print("Deleted %s" % item)
|
|
|
|
|
2014-03-30 00:27:48 +00:00
|
|
|
|
|
|
|
def parse_args():
|
2017-05-30 17:05:19 +00:00
|
|
|
parser = optparse.OptionParser(
|
|
|
|
usage="%s [options]" % sys.argv[0],
|
|
|
|
description=__doc__
|
|
|
|
)
|
2014-04-13 23:16:21 +00:00
|
|
|
gce_credentials.add_credentials_options(parser)
|
2017-05-30 17:05:19 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--yes", "-y",
|
2014-03-30 00:27:48 +00:00
|
|
|
action="store_true", dest="assumeyes",
|
|
|
|
default=False,
|
2017-05-30 17:05:19 +00:00
|
|
|
help="Don't prompt for confirmation"
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"--match",
|
2014-03-30 00:27:48 +00:00
|
|
|
action="store", dest="match_re",
|
|
|
|
default="^ansible-testing-",
|
2017-05-30 17:05:19 +00:00
|
|
|
help="Regular expression used to find GCE resources (default: %default)"
|
|
|
|
)
|
2014-03-30 00:27:48 +00:00
|
|
|
|
|
|
|
(opts, args) = parser.parse_args()
|
2014-04-13 23:16:21 +00:00
|
|
|
gce_credentials.check_required(opts, parser)
|
2014-03-30 00:27:48 +00:00
|
|
|
return (opts, args)
|
|
|
|
|
2018-07-29 11:46:06 +00:00
|
|
|
|
2014-03-30 00:27:48 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
(opts, args) = parse_args()
|
|
|
|
|
|
|
|
# Connect to GCE
|
2014-04-13 23:16:21 +00:00
|
|
|
gce = gce_credentials.get_gce_driver(opts)
|
2014-03-30 00:27:48 +00:00
|
|
|
|
|
|
|
try:
|
2017-01-30 23:01:47 +00:00
|
|
|
# Delete matching instances
|
|
|
|
delete_gce_resources(gce.list_nodes, 'name', opts)
|
2017-05-30 17:05:19 +00:00
|
|
|
|
2017-01-30 23:01:47 +00:00
|
|
|
# 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)
|
2015-08-27 18:58:51 +00:00
|
|
|
except KeyboardInterrupt as e:
|
2015-08-27 19:02:35 +00:00
|
|
|
print("\nExiting on user command.")
|