2015-01-28 01:22:46 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""An Ansible module to utilize GCE image resources."""
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: gce_img
|
2015-01-28 20:19:06 +01:00
|
|
|
version_added: "1.9"
|
2015-01-28 01:22:46 +01:00
|
|
|
short_description: utilize GCE image resources
|
|
|
|
description:
|
|
|
|
- This module can create and delete GCE private images from gzipped
|
|
|
|
compressed tarball containing raw disk data or from existing detached
|
|
|
|
disks in any zone. U(https://cloud.google.com/compute/docs/images)
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2015-01-28 20:19:06 +01:00
|
|
|
- the name of the image to create or delete
|
2015-01-28 01:22:46 +01:00
|
|
|
required: true
|
|
|
|
default: null
|
2015-01-30 09:47:47 +01:00
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- an optional description
|
|
|
|
required: false
|
|
|
|
default: null
|
2016-09-08 21:12:20 +02:00
|
|
|
family:
|
|
|
|
description:
|
|
|
|
- an optional family name
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.2"
|
2015-01-28 01:22:46 +01:00
|
|
|
source:
|
|
|
|
description:
|
|
|
|
- the source disk or the Google Cloud Storage URI to create the image from
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- desired state of the image
|
|
|
|
required: false
|
|
|
|
default: "present"
|
2015-01-28 20:19:06 +01:00
|
|
|
choices: ["present", "absent"]
|
2015-01-28 01:22:46 +01:00
|
|
|
zone:
|
|
|
|
description:
|
|
|
|
- the zone of the disk specified by source
|
|
|
|
required: false
|
|
|
|
default: "us-central1-a"
|
2015-10-11 17:18:38 +02:00
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- timeout for the operation
|
|
|
|
required: false
|
|
|
|
default: 180
|
2015-10-22 14:31:49 +02:00
|
|
|
version_added: "2.0"
|
2015-01-28 01:22:46 +01:00
|
|
|
service_account_email:
|
|
|
|
description:
|
|
|
|
- service account email
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
pem_file:
|
|
|
|
description:
|
|
|
|
- path to the pem file associated with the service account email
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
project_id:
|
|
|
|
description:
|
|
|
|
- your GCE project ID
|
|
|
|
required: false
|
|
|
|
default: null
|
2015-05-11 19:09:56 +02:00
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- "apache-libcloud"
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Peter Tan (@tanpeter)"
|
2015-01-28 01:22:46 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create an image named test-image from the disk 'test-disk' in zone us-central1-a.
|
|
|
|
- gce_img:
|
|
|
|
name: test-image
|
|
|
|
source: test-disk
|
|
|
|
zone: us-central1-a
|
|
|
|
state: present
|
|
|
|
|
2015-01-28 20:19:06 +01:00
|
|
|
# Create an image named test-image from a tarball in Google Cloud Storage.
|
2015-01-28 01:22:46 +01:00
|
|
|
- gce_img:
|
|
|
|
name: test-image
|
2015-01-28 20:19:06 +01:00
|
|
|
source: https://storage.googleapis.com/bucket/path/to/image.tgz
|
|
|
|
|
|
|
|
# Alternatively use the gs scheme
|
|
|
|
- gce_img:
|
|
|
|
name: test-image
|
|
|
|
source: gs://bucket/path/to/image.tgz
|
|
|
|
|
|
|
|
# Delete an image named test-image.
|
|
|
|
- gce_img:
|
|
|
|
name: test-image
|
|
|
|
state: absent
|
2015-01-28 01:22:46 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-28 20:19:06 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
2016-09-08 21:12:20 +02:00
|
|
|
import libcloud
|
2015-01-28 20:19:06 +01:00
|
|
|
from libcloud.compute.types import Provider
|
|
|
|
from libcloud.compute.providers import get_driver
|
|
|
|
from libcloud.common.google import GoogleBaseError
|
2015-01-30 09:47:47 +01:00
|
|
|
from libcloud.common.google import ResourceExistsError
|
2015-01-28 20:19:06 +01:00
|
|
|
from libcloud.common.google import ResourceNotFoundError
|
|
|
|
_ = Provider.GCE
|
|
|
|
has_libcloud = True
|
|
|
|
except ImportError:
|
|
|
|
has_libcloud = False
|
|
|
|
|
|
|
|
|
|
|
|
GCS_URI = 'https://storage.googleapis.com/'
|
|
|
|
|
|
|
|
|
|
|
|
def create_image(gce, name, module):
|
|
|
|
"""Create an image with the specified name."""
|
|
|
|
source = module.params.get('source')
|
|
|
|
zone = module.params.get('zone')
|
2015-01-30 09:47:47 +01:00
|
|
|
desc = module.params.get('description')
|
2015-10-11 17:18:38 +02:00
|
|
|
timeout = module.params.get('timeout')
|
2016-09-08 21:12:20 +02:00
|
|
|
family = module.params.get('family')
|
2015-01-28 20:19:06 +01:00
|
|
|
|
|
|
|
if not source:
|
|
|
|
module.fail_json(msg='Must supply a source', changed=False)
|
|
|
|
|
|
|
|
if source.startswith(GCS_URI):
|
|
|
|
# source is a Google Cloud Storage URI
|
|
|
|
volume = source
|
|
|
|
elif source.startswith('gs://'):
|
|
|
|
# libcloud only accepts https URI.
|
|
|
|
volume = source.replace('gs://', GCS_URI)
|
|
|
|
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, e:
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
|
2016-09-08 21:12:20 +02:00
|
|
|
gce_extra_args = {}
|
|
|
|
if family is not None:
|
|
|
|
gce_extra_args['family'] = family
|
|
|
|
|
2015-10-11 17:18:38 +02:00
|
|
|
old_timeout = gce.connection.timeout
|
2015-01-28 20:19:06 +01:00
|
|
|
try:
|
2015-10-11 17:18:38 +02:00
|
|
|
gce.connection.timeout = timeout
|
2016-09-08 21:12:20 +02:00
|
|
|
gce.ex_create_image(name, volume, desc, use_existing=False, **gce_extra_args)
|
2015-01-30 09:47:47 +01:00
|
|
|
return True
|
|
|
|
except ResourceExistsError:
|
|
|
|
return False
|
2015-01-28 20:19:06 +01:00
|
|
|
except GoogleBaseError, e:
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
2015-10-11 17:18:38 +02:00
|
|
|
finally:
|
|
|
|
gce.connection.timeout = old_timeout
|
2015-01-28 20:19:06 +01:00
|
|
|
|
|
|
|
|
2015-01-30 09:47:47 +01:00
|
|
|
def delete_image(gce, name, module):
|
|
|
|
"""Delete a specific image resource by name."""
|
2015-01-28 20:19:06 +01:00
|
|
|
try:
|
2015-01-30 09:47:47 +01:00
|
|
|
gce.ex_delete_image(name)
|
|
|
|
return True
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
return False
|
2015-01-28 20:19:06 +01:00
|
|
|
except GoogleBaseError, e:
|
|
|
|
module.fail_json(msg=str(e), changed=False)
|
|
|
|
|
2015-01-28 01:22:46 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(required=True),
|
2016-09-08 21:12:20 +02:00
|
|
|
family=dict(),
|
2015-01-30 09:47:47 +01:00
|
|
|
description=dict(),
|
2015-01-28 01:22:46 +01:00
|
|
|
source=dict(),
|
2015-01-28 20:19:06 +01:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
2015-01-28 01:22:46 +01:00
|
|
|
zone=dict(default='us-central1-a'),
|
|
|
|
service_account_email=dict(),
|
2016-04-14 23:37:01 +02:00
|
|
|
pem_file=dict(type='path'),
|
2015-01-28 01:22:46 +01:00
|
|
|
project_id=dict(),
|
2015-10-11 17:18:38 +02:00
|
|
|
timeout=dict(type='int', default=180)
|
2015-01-28 01:22:46 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-01-28 20:19:06 +01:00
|
|
|
if not has_libcloud:
|
|
|
|
module.fail_json(msg='libcloud with GCE support is required.')
|
|
|
|
|
2015-01-28 01:22:46 +01:00
|
|
|
gce = gce_connect(module)
|
|
|
|
|
|
|
|
name = module.params.get('name')
|
|
|
|
state = module.params.get('state')
|
2016-09-08 21:12:20 +02:00
|
|
|
family = module.params.get('family')
|
2015-01-28 01:22:46 +01:00
|
|
|
changed = False
|
|
|
|
|
2016-09-08 21:12:20 +02:00
|
|
|
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",
|
|
|
|
changed=False)
|
|
|
|
|
2015-01-28 01:22:46 +01:00
|
|
|
# user wants to create an image.
|
2015-01-30 09:47:47 +01:00
|
|
|
if state == 'present':
|
|
|
|
changed = create_image(gce, name, module)
|
2015-01-28 01:22:46 +01:00
|
|
|
|
|
|
|
# user wants to delete the image.
|
2015-01-30 09:47:47 +01:00
|
|
|
if state == 'absent':
|
|
|
|
changed = delete_image(gce, name, module)
|
2015-01-28 01:22:46 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=changed, name=name)
|
|
|
|
|
2015-01-28 20:19:06 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.gce import *
|
|
|
|
|
2015-01-28 01:22:46 +01:00
|
|
|
main()
|