Changes cleanup_gce.py to use gce_credentials module.

pull/6976/head
Chris Conway 11 years ago
parent 2257a69723
commit 1e0eed3fce

@ -21,6 +21,8 @@ except ImportError:
"msg='libcloud with GCE support (0.13.3+) required for this module'") "msg='libcloud with GCE support (0.13.3+) required for this module'")
sys.exit(1) sys.exit(1)
import gce_credentials
def delete_gce_resources(get_func, attr, opts): def delete_gce_resources(get_func, attr, opts):
for item in get_func(): for item in get_func():
@ -37,39 +39,9 @@ def prompt_and_delete(item, prompt, assumeyes):
print ("Deleted %s" % item) print ("Deleted %s" % item)
def parse_args(): def parse_args():
default_service_account_email = None
default_pem_file = None
default_project_id = None
# Load details from credentials.yml
if os.path.isfile('credentials.yml'):
credentials = yaml.load(open('credentials.yml', 'r'))
if default_service_account_email is None:
default_service_account_email = credentials['gce_service_account_email']
if default_pem_file is None:
default_pem_file = credentials['gce_pem_file']
if default_project_id is None:
default_project_id = credentials['gce_project_id']
parser = optparse.OptionParser(usage="%s [options]" % (sys.argv[0],), parser = optparse.OptionParser(usage="%s [options]" % (sys.argv[0],),
description=__doc__) description=__doc__)
parser.add_option("--service_account_email", gce_credentials.add_credentials_options(parser)
action="store", dest="service_account_email",
default=default_service_account_email,
help="GCE service account email. Default is loaded from credentials.yml.")
parser.add_option("--pem_file",
action="store", dest="pem_file",
default=default_pem_file,
help="GCE client key. Default is loaded from credentials.yml.")
parser.add_option("--project_id",
action="store", dest="project_id",
default=default_project_id,
help="Google Cloud project ID. Default is loaded from credentials.yml.")
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", parser.add_option("--yes", "-y",
action="store_true", dest="assumeyes", action="store_true", dest="assumeyes",
default=False, default=False,
@ -80,10 +52,7 @@ def parse_args():
help="Regular expression used to find GCE resources (default: %default)") help="Regular expression used to find GCE resources (default: %default)")
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
for required in ['service_account_email', 'pem_file', 'project_id']: gce_credentials.check_required(opts, parser)
if getattr(opts, required) is None:
parser.error("Missing required parameter: --%s" % required)
return (opts, args) return (opts, args)
if __name__ == '__main__': if __name__ == '__main__':
@ -91,9 +60,7 @@ if __name__ == '__main__':
(opts, args) = parse_args() (opts, args) = parse_args()
# Connect to GCE # Connect to GCE
gce_cls = get_driver(Provider.GCE) gce = gce_credentials.get_gce_driver(opts)
gce = gce_cls(
opts.service_account_email, opts.pem_file, project=opts.project_id)
try: try:
# Delete matching instances # Delete matching instances

Loading…
Cancel
Save