|
|
@ -117,16 +117,16 @@ EXAMPLES = '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import libcloud
|
|
|
|
import libcloud
|
|
|
|
from libcloud.compute.types import Provider
|
|
|
|
from libcloud.compute.types import Provider
|
|
|
|
from libcloud.compute.providers import get_driver
|
|
|
|
from libcloud.compute.providers import get_driver
|
|
|
|
from libcloud.common.google import GoogleBaseError
|
|
|
|
from libcloud.common.google import GoogleBaseError
|
|
|
|
from libcloud.common.google import ResourceExistsError
|
|
|
|
from libcloud.common.google import ResourceExistsError
|
|
|
|
from libcloud.common.google import ResourceNotFoundError
|
|
|
|
from libcloud.common.google import ResourceNotFoundError
|
|
|
|
_ = Provider.GCE
|
|
|
|
_ = Provider.GCE
|
|
|
|
has_libcloud = True
|
|
|
|
has_libcloud = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
has_libcloud = False
|
|
|
|
has_libcloud = False
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.gce import gce_connect
|
|
|
|
from ansible.module_utils.gce import gce_connect
|
|
|
@ -136,98 +136,98 @@ GCS_URI = 'https://storage.googleapis.com/'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_image(gce, name, module):
|
|
|
|
def create_image(gce, name, module):
|
|
|
|
"""Create an image with the specified name."""
|
|
|
|
"""Create an image with the specified name."""
|
|
|
|
source = module.params.get('source')
|
|
|
|
source = module.params.get('source')
|
|
|
|
zone = module.params.get('zone')
|
|
|
|
zone = module.params.get('zone')
|
|
|
|
desc = module.params.get('description')
|
|
|
|
desc = module.params.get('description')
|
|
|
|
timeout = module.params.get('timeout')
|
|
|
|
timeout = module.params.get('timeout')
|
|
|
|
family = module.params.get('family')
|
|
|
|
family = module.params.get('family')
|
|
|
|
|
|
|
|
|
|
|
|
if not source:
|
|
|
|
if not source:
|
|
|
|
module.fail_json(msg='Must supply a source', changed=False)
|
|
|
|
module.fail_json(msg='Must supply a source', changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
if source.startswith(GCS_URI):
|
|
|
|
if source.startswith(GCS_URI):
|
|
|
|
# source is a Google Cloud Storage URI
|
|
|
|
# source is a Google Cloud Storage URI
|
|
|
|
volume = source
|
|
|
|
volume = source
|
|
|
|
elif source.startswith('gs://'):
|
|
|
|
elif source.startswith('gs://'):
|
|
|
|
# libcloud only accepts https URI.
|
|
|
|
# libcloud only accepts https URI.
|
|
|
|
volume = source.replace('gs://', GCS_URI)
|
|
|
|
volume = source.replace('gs://', GCS_URI)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
volume = gce.ex_get_volume(source, zone)
|
|
|
|
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
|
|
|
|
module.fail_json(msg='Disk %s not found in zone %s' % (source, zone),
|
|
|
|
|
|
|
|
changed=False)
|
|
|
|
|
|
|
|
except GoogleBaseError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gce_extra_args = {}
|
|
|
|
|
|
|
|
if family is not None:
|
|
|
|
|
|
|
|
gce_extra_args['family'] = family
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
old_timeout = gce.connection.timeout
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
volume = gce.ex_get_volume(source, zone)
|
|
|
|
gce.connection.timeout = timeout
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
gce.ex_create_image(name, volume, desc, use_existing=False, **gce_extra_args)
|
|
|
|
module.fail_json(msg='Disk %s not found in zone %s' % (source, zone),
|
|
|
|
return True
|
|
|
|
changed=False)
|
|
|
|
except ResourceExistsError:
|
|
|
|
|
|
|
|
return False
|
|
|
|
except GoogleBaseError as e:
|
|
|
|
except GoogleBaseError as e:
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
gce_extra_args = {}
|
|
|
|
gce.connection.timeout = old_timeout
|
|
|
|
if family is not None:
|
|
|
|
|
|
|
|
gce_extra_args['family'] = family
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
old_timeout = gce.connection.timeout
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
gce.connection.timeout = timeout
|
|
|
|
|
|
|
|
gce.ex_create_image(name, volume, desc, use_existing=False, **gce_extra_args)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except ResourceExistsError:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
except GoogleBaseError as e:
|
|
|
|
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
gce.connection.timeout = old_timeout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_image(gce, name, module):
|
|
|
|
def delete_image(gce, name, module):
|
|
|
|
"""Delete a specific image resource by name."""
|
|
|
|
"""Delete a specific image resource by name."""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
gce.ex_delete_image(name)
|
|
|
|
gce.ex_delete_image(name)
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
except GoogleBaseError as e:
|
|
|
|
except GoogleBaseError as e:
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(required=True),
|
|
|
|
name=dict(required=True),
|
|
|
|
family=dict(),
|
|
|
|
family=dict(),
|
|
|
|
description=dict(),
|
|
|
|
description=dict(),
|
|
|
|
source=dict(),
|
|
|
|
source=dict(),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
zone=dict(default='us-central1-a'),
|
|
|
|
zone=dict(default='us-central1-a'),
|
|
|
|
service_account_email=dict(),
|
|
|
|
service_account_email=dict(),
|
|
|
|
pem_file=dict(type='path'),
|
|
|
|
pem_file=dict(type='path'),
|
|
|
|
project_id=dict(),
|
|
|
|
project_id=dict(),
|
|
|
|
timeout=dict(type='int', default=180)
|
|
|
|
timeout=dict(type='int', default=180)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if not has_libcloud:
|
|
|
|
if not has_libcloud:
|
|
|
|
module.fail_json(msg='libcloud with GCE support is required.')
|
|
|
|
module.fail_json(msg='libcloud with GCE support is required.')
|
|
|
|
|
|
|
|
|
|
|
|
gce = gce_connect(module)
|
|
|
|
gce = gce_connect(module)
|
|
|
|
|
|
|
|
|
|
|
|
name = module.params.get('name')
|
|
|
|
name = module.params.get('name')
|
|
|
|
state = module.params.get('state')
|
|
|
|
state = module.params.get('state')
|
|
|
|
family = module.params.get('family')
|
|
|
|
family = module.params.get('family')
|
|
|
|
changed = False
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
|
|
|
|
if family is not None and hasattr(libcloud, '__version__') and libcloud.__version__ <= '0.20.1':
|
|
|
|
if family is not None and hasattr(libcloud, '__version__') and libcloud.__version__ <= '0.20.1':
|
|
|
|
module.fail_json(msg="Apache Libcloud 1.0.0+ is required to use 'family' option",
|
|
|
|
module.fail_json(msg="Apache Libcloud 1.0.0+ is required to use 'family' option",
|
|
|
|
changed=False)
|
|
|
|
changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
# user wants to create an image.
|
|
|
|
# user wants to create an image.
|
|
|
|
if state == 'present':
|
|
|
|
if state == 'present':
|
|
|
|
changed = create_image(gce, name, module)
|
|
|
|
changed = create_image(gce, name, module)
|
|
|
|
|
|
|
|
|
|
|
|
# user wants to delete the image.
|
|
|
|
# user wants to delete the image.
|
|
|
|
if state == 'absent':
|
|
|
|
if state == 'absent':
|
|
|
|
changed = delete_image(gce, name, module)
|
|
|
|
changed = delete_image(gce, name, module)
|
|
|
|
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, name=name)
|
|
|
|
module.exit_json(changed=changed, name=name)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|