2014-04-11 22:45:56 +00:00
|
|
|
import collections
|
|
|
|
import os
|
2016-09-16 14:02:18 +00:00
|
|
|
import sys
|
2014-04-11 22:45:56 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
try:
|
|
|
|
from libcloud.compute.types import Provider
|
|
|
|
from libcloud.compute.providers import get_driver
|
|
|
|
_ = 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-04-11 22:45:56 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def add_credentials_options(parser):
|
2016-09-21 21:48:09 +00:00
|
|
|
default_service_account_email = None
|
|
|
|
default_pem_file = None
|
|
|
|
default_project_id = None
|
2014-04-11 22:45:56 +00:00
|
|
|
|
|
|
|
# Load details from credentials.yml
|
|
|
|
if os.path.isfile('credentials.yml'):
|
|
|
|
credentials = yaml.load(open('credentials.yml', 'r'))
|
2016-09-21 21:48:09 +00:00
|
|
|
default_service_account_email = credentials[
|
|
|
|
'gce_service_account_email']
|
2016-09-22 18:40:57 +00:00
|
|
|
default_pem_file = credentials['gce_pem_file']
|
2014-04-11 22:45:56 +00:00
|
|
|
default_project_id = credentials['gce_project_id']
|
|
|
|
|
2016-09-21 21:48:09 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--service_account_email", action="store",
|
|
|
|
dest="service_account_email", default=default_service_account_email,
|
2014-04-11 22:45:56 +00:00
|
|
|
help="GCE service account email. Default is loaded from credentials.yml.")
|
2016-09-21 21:48:09 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--pem_file", action="store", dest="pem_file",
|
2014-04-11 22:45:56 +00:00
|
|
|
default=default_pem_file,
|
|
|
|
help="GCE client key. Default is loaded from credentials.yml.")
|
2016-09-21 21:48:09 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--project_id", action="store", dest="project_id",
|
2014-04-11 22:45:56 +00:00
|
|
|
default=default_project_id,
|
|
|
|
help="Google Cloud project ID. Default is loaded from credentials.yml.")
|
|
|
|
|
|
|
|
|
|
|
|
def check_required(opts, parser):
|
|
|
|
for required in ['service_account_email', 'pem_file', 'project_id']:
|
|
|
|
if getattr(opts, required) is None:
|
|
|
|
parser.error("Missing required parameter: --%s" % required)
|
|
|
|
|
|
|
|
|
|
|
|
def get_gce_driver(opts):
|
|
|
|
# Connect to GCE
|
|
|
|
gce_cls = get_driver(Provider.GCE)
|
2016-09-21 21:48:09 +00:00
|
|
|
return gce_cls(opts.service_account_email, opts.pem_file,
|
|
|
|
project=opts.project_id)
|