mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
421 lines
16 KiB
Python
421 lines
16 KiB
Python
11 years ago
|
#!/usr/bin/python
|
||
|
# 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/>.
|
||
|
|
||
|
DOCUMENTATION = '''
|
||
|
---
|
||
11 years ago
|
module: gc_storage
|
||
11 years ago
|
version_added: "1.4"
|
||
|
short_description: This module manages objects/buckets in Google Cloud Storage.
|
||
11 years ago
|
description:
|
||
11 years ago
|
- This module allows users to manage their objects/buckets in Google Cloud Storage. It allows upload and download operations and can set some canned permissions. It also allows retrieval of URLs for objects for use in playbooks, and retrieval of string contents of objects. This module requires setting the default project in GCS prior to playbook usage. See U(https://developers.google.com/storage/docs/reference/v1/apiversion1) for information about setting the default project.
|
||
11 years ago
|
|
||
|
options:
|
||
|
bucket:
|
||
|
description:
|
||
|
- Bucket name.
|
||
|
required: true
|
||
|
default: null
|
||
|
aliases: []
|
||
|
object:
|
||
|
description:
|
||
11 years ago
|
- Keyname of the object inside the bucket. Can be also be used to create "virtual directories" (see examples).
|
||
11 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
|
src:
|
||
|
description:
|
||
|
- The source file path when performing a PUT operation.
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
|
dest:
|
||
|
description:
|
||
|
- The destination file path when downloading an object/key with a GET operation.
|
||
|
required: false
|
||
|
aliases: []
|
||
11 years ago
|
force:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Forces an overwrite either locally on the filesystem or remotely with the object/key. Used with PUT and GET operations.
|
||
11 years ago
|
required: false
|
||
11 years ago
|
default: true
|
||
|
aliases: [ 'overwrite' ]
|
||
11 years ago
|
permission:
|
||
|
description:
|
||
|
- This option let's the user set the canned permissions on the object/bucket that are created. The permissions that can be set are 'private', 'public-read', 'authenticated-read'.
|
||
|
required: false
|
||
|
default: private
|
||
11 years ago
|
expiration:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Time limit (in seconds) for the URL generated and returned by GCA when performing a mode=put or mode=get_url operation. This url is only avaialbe when public-read is the acl for the object.
|
||
11 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
11 years ago
|
mode:
|
||
|
description:
|
||
11 years ago
|
- Switches the module behaviour between upload, download, get_url (return download url) , get_str (download object as string), create (bucket) and delete (bucket).
|
||
11 years ago
|
required: true
|
||
|
default: null
|
||
|
aliases: []
|
||
11 years ago
|
choices: [ 'get', 'put', 'get_url', 'get_str', 'delete', 'create' ]
|
||
11 years ago
|
gcs_secret_key:
|
||
|
description:
|
||
|
- GCS secret key. If not set then the value of the GCS_SECRET_KEY environment variable is used.
|
||
11 years ago
|
required: true
|
||
11 years ago
|
default: null
|
||
|
gcs_access_key:
|
||
|
description:
|
||
|
- GCS access key. If not set then the value of the GCS_ACCESS_KEY environment variable is used.
|
||
11 years ago
|
required: true
|
||
11 years ago
|
default: null
|
||
11 years ago
|
|
||
|
requirements: [ "boto 2.9+" ]
|
||
|
|
||
11 years ago
|
author: benno@ansible.com Note. Most of the code has been taken from the S3 module.
|
||
11 years ago
|
|
||
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
11 years ago
|
# upload some content
|
||
11 years ago
|
- gc_storage: bucket=mybucket object=key.txt src=/usr/local/myfile.txt mode=put permission=public-read
|
||
11 years ago
|
|
||
|
# download some content
|
||
11 years ago
|
- gc_storage: bucket=mybucket object=key.txt dest=/usr/local/myfile.txt mode=get
|
||
11 years ago
|
|
||
11 years ago
|
# Download an object as a string to use else where in your playbook
|
||
11 years ago
|
- gc_storage: bucket=mybucket object=key.txt mode=get_str
|
||
|
|
||
11 years ago
|
# Create an empty bucket
|
||
11 years ago
|
- gc_storage: bucket=mybucket mode=create
|
||
|
|
||
11 years ago
|
# Create a bucket with key as directory
|
||
11 years ago
|
- gc_storage: bucket=mybucket object=/my/directory/path mode=create
|
||
|
|
||
11 years ago
|
# Delete a bucket and all contents
|
||
11 years ago
|
- gc_storage: bucket=mybucket mode=delete
|
||
11 years ago
|
'''
|
||
|
|
||
|
import sys
|
||
|
import os
|
||
|
import urlparse
|
||
|
import hashlib
|
||
|
|
||
|
try:
|
||
|
import boto
|
||
|
except ImportError:
|
||
11 years ago
|
print "failed=True msg='boto 2.9+ required for this module'"
|
||
11 years ago
|
sys.exit(1)
|
||
|
|
||
|
def grant_check(module, gs, obj):
|
||
|
try:
|
||
|
acp = obj.get_acl()
|
||
|
if module.params.get('permission') == 'public-read':
|
||
|
grant = [ x for x in acp.entries.entry_list if x.scope.type == 'AllUsers']
|
||
|
if not grant:
|
||
|
obj.set_acl('public-read')
|
||
11 years ago
|
module.exit_json(changed=True, result="The objects permission as been set to public-read")
|
||
11 years ago
|
if module.params.get('permission') == 'authenticated-read':
|
||
|
grant = [ x for x in acp.entries.entry_list if x.scope.type == 'AllAuthenticatedUsers']
|
||
|
if not grant:
|
||
|
obj.set_acl('authenticated-read')
|
||
11 years ago
|
module.exit_json(changed=True, result="The objects permission as been set to authenticated-read")
|
||
11 years ago
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
return True
|
||
|
|
||
|
|
||
|
|
||
|
def key_check(module, gs, bucket, obj):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key_check = bucket.get_key(obj)
|
||
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
if key_check:
|
||
|
grant_check(module, gs, key_check)
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
def keysum(module, gs, bucket, obj):
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key_check = bucket.get_key(obj)
|
||
11 years ago
|
if not key_check:
|
||
|
return None
|
||
|
md5_remote = key_check.etag[1:-1]
|
||
|
etag_multipart = '-' in md5_remote # Check for multipart, etag is not md5
|
||
|
if etag_multipart is True:
|
||
|
module.fail_json(msg="Files uploaded with multipart of gs are not supported with checksum, unable to compute checksum.")
|
||
11 years ago
|
return md5_remote
|
||
|
|
||
|
def bucket_check(module, gs, bucket):
|
||
|
try:
|
||
|
result = gs.lookup(bucket)
|
||
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
if result:
|
||
|
grant_check(module, gs, result)
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
def create_bucket(module, gs, bucket):
|
||
|
try:
|
||
|
bucket = gs.create_bucket(bucket)
|
||
|
bucket.set_acl(module.params.get('permission'))
|
||
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
if bucket:
|
||
|
return True
|
||
|
|
||
|
def delete_bucket(module, gs, bucket):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
bucket_contents = bucket.list()
|
||
|
for key in bucket_contents:
|
||
|
bucket.delete_key(key.name)
|
||
|
bucket.delete()
|
||
|
return True
|
||
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
|
def delete_key(module, gs, bucket, obj):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
bucket.delete_key(obj)
|
||
|
module.exit_json(msg="Object deleted from bucket ", changed=True)
|
||
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
|
def create_dirkey(module, gs, bucket, obj):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key = bucket.new_key(obj)
|
||
|
key.set_contents_from_string('')
|
||
|
module.exit_json(msg="Virtual directory %s created in bucket %s" % (obj, bucket.name), changed=True)
|
||
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
|
def upload_file_check(src):
|
||
|
if os.path.exists(src):
|
||
|
file_exists is True
|
||
|
else:
|
||
|
file_exists is False
|
||
|
if os.path.isdir(src):
|
||
|
module.fail_json(msg="Specifying a directory is not a valid source for upload.", failed=True)
|
||
|
return file_exists
|
||
|
|
||
|
def path_check(path):
|
||
|
if os.path.exists(path):
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
def upload_gsfile(module, gs, bucket, obj, src, expiry):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key = bucket.new_key(obj)
|
||
|
key.set_contents_from_filename(src)
|
||
|
key.set_acl(module.params.get('permission'))
|
||
|
url = key.generate_url(expiry)
|
||
|
module.exit_json(msg="PUT operation complete", url=url, changed=True)
|
||
|
except gs.provider.storage_copy_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
|
def download_gsfile(module, gs, bucket, obj, dest):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key = bucket.lookup(obj)
|
||
|
key.get_contents_to_filename(dest)
|
||
|
module.exit_json(msg="GET operation complete", changed=True)
|
||
|
except gs.provider.storage_copy_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
|
def download_gsstr(module, gs, bucket, obj):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key = bucket.lookup(obj)
|
||
|
contents = key.get_contents_as_string()
|
||
|
module.exit_json(msg="GET operation complete", contents=contents, changed=True)
|
||
|
except gs.provider.storage_copy_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
|
def get_download_url(module, gs, bucket, obj, expiry):
|
||
|
try:
|
||
|
bucket = gs.lookup(bucket)
|
||
|
key = bucket.lookup(obj)
|
||
|
url = key.generate_url(expiry)
|
||
11 years ago
|
module.exit_json(msg="Download url:", url=url, expiration=expiry, changed=True)
|
||
11 years ago
|
except gs.provider.storage_response_error, e:
|
||
|
module.fail_json(msg= str(e))
|
||
|
|
||
11 years ago
|
def handle_get(module, gs, bucket, obj, overwrite, dest):
|
||
|
md5_remote = keysum(module, gs, bucket, obj)
|
||
|
md5_local = hashlib.md5(open(dest, 'rb').read()).hexdigest()
|
||
11 years ago
|
if md5_local == md5_remote:
|
||
11 years ago
|
module.exit_json(changed=False)
|
||
11 years ago
|
if md5_local != md5_remote and not overwrite:
|
||
|
module.exit_json(msg="WARNING: Checksums do not match. Use overwrite parameter to force download.", failed=True)
|
||
11 years ago
|
else:
|
||
11 years ago
|
download_gsfile(module, gs, bucket, obj, dest)
|
||
11 years ago
|
|
||
11 years ago
|
def handle_put(module, gs, bucket, obj, overwrite, src, expiration):
|
||
11 years ago
|
# Lets check to see if bucket exists to get ground truth.
|
||
|
bucket_rc = bucket_check(module, gs, bucket)
|
||
|
key_rc = key_check(module, gs, bucket, obj)
|
||
|
|
||
|
# Lets check key state. Does it exist and if it does, compute the etag md5sum.
|
||
|
if bucket_rc and key_rc:
|
||
|
md5_remote = keysum(module, gs, bucket, obj)
|
||
|
md5_local = hashlib.md5(open(src, 'rb').read()).hexdigest()
|
||
11 years ago
|
if md5_local == md5_remote:
|
||
|
module.exit_json(msg="Local and remote object are identical", changed=False)
|
||
11 years ago
|
if md5_local != md5_remote and not overwrite:
|
||
|
module.exit_json(msg="WARNING: Checksums do not match. Use overwrite parameter to force upload.", failed=True)
|
||
11 years ago
|
else:
|
||
11 years ago
|
upload_gsfile(module, gs, bucket, obj, src, expiration)
|
||
11 years ago
|
|
||
|
if not bucket_rc:
|
||
|
create_bucket(module, gs, bucket)
|
||
11 years ago
|
upload_gsfile(module, gs, bucket, obj, src, expiration)
|
||
11 years ago
|
|
||
|
# If bucket exists but key doesn't, just upload.
|
||
|
if bucket_rc and not key_rc:
|
||
11 years ago
|
upload_gsfile(module, gs, bucket, obj, src, expiration)
|
||
11 years ago
|
|
||
|
def handle_delete(module, gs, bucket, obj):
|
||
|
if bucket and not obj:
|
||
|
if bucket_check(module, gs, bucket):
|
||
|
module.exit_json(msg="Bucket %s and all keys have been deleted."%bucket, changed=delete_bucket(module, gs, bucket))
|
||
|
else:
|
||
|
module.exit_json(msg="Bucket does not exist.", changed=False)
|
||
|
if bucket and obj:
|
||
|
if bucket_check(module, gs, bucket):
|
||
|
if key_check(module, gs, bucket, obj):
|
||
|
module.exit_json(msg="Object has been deleted.", changed=delete_key(module, gs, bucket, obj))
|
||
|
else:
|
||
|
module.exit_json(msg="Object does not exists.", changed=False)
|
||
|
else:
|
||
|
module.exit_json(msg="Bucket does not exist.", changed=False)
|
||
|
else:
|
||
|
module.fail_json(msg="Bucket or Bucket & object parameter is required.", failed=True)
|
||
|
|
||
|
def handle_create(module, gs, bucket, obj):
|
||
|
if bucket and not obj:
|
||
|
if bucket_check(module, gs, bucket):
|
||
|
module.exit_json(msg="Bucket already exists.", changed=False)
|
||
|
else:
|
||
11 years ago
|
module.exit_json(msg="Bucket created successfully", changed=create_bucket(module, gs, bucket))
|
||
11 years ago
|
if bucket and obj:
|
||
|
if bucket_check(module, gs, bucket):
|
||
|
if obj.endswith('/'):
|
||
|
dirobj = obj
|
||
|
else:
|
||
|
dirobj = obj + "/"
|
||
|
if key_check(module, gs, bucket, dirobj):
|
||
|
module.exit_json(msg="Bucket %s and key %s already exists."% (bucket, obj), changed=False)
|
||
|
else:
|
||
|
create_dirkey(module, gs, bucket, dirobj)
|
||
|
else:
|
||
|
create_bucket(module, gs, bucket)
|
||
|
create_dirkey(module, gs, bucket, dirobj)
|
||
|
|
||
11 years ago
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
|
bucket = dict(required=True),
|
||
|
object = dict(default=None),
|
||
|
src = dict(default=None),
|
||
|
dest = dict(default=None),
|
||
11 years ago
|
expiration = dict(default=600, aliases=['expiry']),
|
||
|
mode = dict(choices=['get', 'put', 'delete', 'create', 'get_url', 'get_str'], required=True),
|
||
11 years ago
|
permission = dict(choices=['private', 'public-read', 'authenticated-read'], default='private'),
|
||
11 years ago
|
gs_secret_key = dict(no_log=True, required=True),
|
||
|
gs_access_key = dict(required=True),
|
||
11 years ago
|
overwrite = dict(default=True, type='bool', aliases=['force']),
|
||
11 years ago
|
),
|
||
|
)
|
||
|
|
||
11 years ago
|
bucket = module.params.get('bucket')
|
||
|
obj = module.params.get('object')
|
||
|
src = module.params.get('src')
|
||
|
dest = module.params.get('dest')
|
||
11 years ago
|
if dest:
|
||
11 years ago
|
dest = os.path.expanduser(dest)
|
||
|
mode = module.params.get('mode')
|
||
11 years ago
|
expiry = module.params.get('expiration')
|
||
11 years ago
|
gs_secret_key = module.params.get('gs_secret_key')
|
||
|
gs_access_key = module.params.get('gs_access_key')
|
||
11 years ago
|
overwrite = module.params.get('overwrite')
|
||
|
|
||
11 years ago
|
if mode == 'put':
|
||
11 years ago
|
if not src or not object:
|
||
11 years ago
|
module.fail_json(msg="When using PUT, src, bucket, object are mandatory parameters")
|
||
11 years ago
|
if mode == 'get':
|
||
11 years ago
|
if not dest or not object:
|
||
11 years ago
|
module.fail_json(msg="When using GET, dest, bucket, object are mandatory parameters")
|
||
11 years ago
|
if obj:
|
||
|
obj = os.path.expanduser(module.params['object'])
|
||
|
|
||
|
try:
|
||
|
gs = boto.connect_gs(gs_access_key, gs_secret_key)
|
||
|
except boto.exception.NoAuthHandlerFound, e:
|
||
|
module.fail_json(msg = str(e))
|
||
|
|
||
|
if mode == 'get':
|
||
11 years ago
|
if not bucket_check(module, gs, bucket) or not key_check(module, gs, bucket, obj):
|
||
|
module.fail_json(msg="Target bucket/key cannot be found", failed=True)
|
||
|
if not path_check(dest):
|
||
11 years ago
|
download_gsfile(module, gs, bucket, obj, dest)
|
||
11 years ago
|
else:
|
||
|
handle_get(module, gs, bucket, obj, overwrite, dest)
|
||
11 years ago
|
|
||
|
if mode == 'put':
|
||
11 years ago
|
if not path_check(src):
|
||
11 years ago
|
module.fail_json(msg="Local object for PUT does not exist", failed=True)
|
||
11 years ago
|
handle_put(module, gs, bucket, obj, overwrite, src, expiry)
|
||
11 years ago
|
|
||
|
# Support for deleting an object if we have both params.
|
||
|
if mode == 'delete':
|
||
11 years ago
|
handle_delete(module, gs, bucket, obj)
|
||
|
|
||
11 years ago
|
if mode == 'create':
|
||
11 years ago
|
handle_create(module, gs, bucket, obj)
|
||
|
|
||
11 years ago
|
if mode == 'get_url':
|
||
11 years ago
|
if bucket and obj:
|
||
11 years ago
|
if bucket_check(module, gs, bucket) and key_check(module, gs, bucket, obj):
|
||
|
get_download_url(module, gs, bucket, obj, expiry)
|
||
11 years ago
|
else:
|
||
11 years ago
|
module.fail_json(msg="Key/Bucket does not exist", failed=True)
|
||
11 years ago
|
else:
|
||
|
module.fail_json(msg="Bucket and Object parameters must be set", failed=True)
|
||
|
|
||
11 years ago
|
# --------------------------- Get the String contents of an Object -------------------------
|
||
11 years ago
|
if mode == 'get_str':
|
||
11 years ago
|
if bucket and obj:
|
||
11 years ago
|
if bucket_check(module, gs, bucket) and key_check(module, gs, bucket, obj):
|
||
|
download_gsstr(module, gs, bucket, obj)
|
||
11 years ago
|
else:
|
||
11 years ago
|
module.fail_json(msg="Key/Bucket does not exist", failed=True)
|
||
11 years ago
|
else:
|
||
|
module.fail_json(msg="Bucket and Object parameters must be set", failed=True)
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
# import module snippets
|
||
11 years ago
|
from ansible.module_utils.basic import *
|
||
11 years ago
|
|
||
|
main()
|