diff options
| author | Sam Yaple <sam@yaple.net> | 2015-08-29 11:49:57 +0000 |
|---|---|---|
| committer | Sam Yaple <sam@yaple.net> | 2015-09-24 12:33:43 +0000 |
| commit | c86e66c9dc3595199b4b88a526e2c204c12364c8 (patch) | |
| tree | 1688b0749486694ab9116a79186e86e1ca41e1c0 /ansible/library | |
| parent | a83d84b383f2e96e17a5a5d38ffc80d7c045e150 (diff) | |
| download | kolla-c86e66c9dc3595199b4b88a526e2c204c12364c8.tar.gz kolla-c86e66c9dc3595199b4b88a526e2c204c12364c8.zip | |
Ceph Ansible support
Add the initial playbooks for making ceph and ansible play nice
together.
This does not include all of the openstack changes to make things like
nova, glance, and cinder work. This will simply build the ceph cluster
and thats it. The next patchset will do the OpenStack integration.
DocImpact
Change-Id: Ie1697dde5f92e833652933a80f0004f31b641330
Partially-Implements: blueprint ceph-container
Diffstat (limited to 'ansible/library')
| -rw-r--r-- | ansible/library/bslurp.py | 189 | ||||
| -rw-r--r-- | ansible/library/find_disks.py | 86 |
2 files changed, 275 insertions, 0 deletions
diff --git a/ansible/library/bslurp.py b/ansible/library/bslurp.py new file mode 100644 index 000000000..485b4c3b4 --- /dev/null +++ b/ansible/library/bslurp.py @@ -0,0 +1,189 @@ +#!/usr/bin/python + +# Copyright 2015 Sam Yaple +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This module has been relicensed from the source below: +# https://github.com/SamYaple/yaodu/blob/master/ansible/library/bslurp + +DOCUMENTATION = ''' +--- +module: bslurp +short_description: Slurps a file from a remote node +description: + - Used for fetching a binary blob containing the file, then push that file + to other hosts. +options: + src: + description: + - File to fetch. When dest is used, src is expected to be a str with data + required: True + type: str + compress: + description: + - Compress file with zlib + default: True + type: bool + dest: + description: + - Where to write out binary blob + required: False + type: str + mode: + description: + - Destination file permissions + default: '0644' + type: str + sha1: + description: + - sha1 hash of the underlying data + default: None + type: bool +author: Sam Yaple +''' + +EXAMPLES = ''' +Distribute a file from single to many host: + +- hosts: web_servers + tasks: + - name: Pull in web config + bslurp: src="/path/to/file" + register: file_data + run_once: True + - name: Push if changed + bslurp: + src: "{{ file_data.content }}" + dest: "{{ file_data.source }}" + mode: "{{ file_data.mode }}" + sha1: "{{ file_data.sha1 }}" + +Distribute multiple files from single to many host: + +- hosts: web_servers + tasks: + - name: Pull in web config + bslurp: src="{{ item }}" + with_items: + - "/path/to/file1" + - "/path/to/file2" + - "/path/to/file3" + register: file_data + run_once: True + - name: Push if changed + bslurp: + src: "{{ item.content }}" + dest: "{{ item.source }}" + mode: "{{ item.mode }}" + sha1: "{{ item.sha1 }}" + with_items: file_data.results + +Distribute a to file many host without compression; Change permissions on dest: + +- hosts: web_servers + tasks: + - name: Pull in web config + bslurp: src="/path/to/file" + register: file_data + run_once: True + - name: Push if changed + bslurp: + src: "{{ file_data.content }}" + dest: "/new/path/to/file" + mode: "0777" + compress: False + sha1: "{{ file_data.sha1 }}" +''' + +import base64 +import hashlib +import os +import sys +import zlib + +def copy_from_host(module): + compress = module.params.get('compress') + src = module.params.get('src') + + if not os.path.exists(src): + module.fail_json(msg="file not found: {}".format(src)) + if not os.access(src, os.R_OK): + module.fail_json(msg="file is not readable: {}".format(src)) + + mode = oct(os.stat(src).st_mode & 0777) + + with open(src, 'rb') as f: + raw_data = f.read() + + sha1 = hashlib.sha1(raw_data).hexdigest() + data = zlib.compress(raw_data) if compress else raw_data + + module.exit_json(content=base64.b64encode(data), sha1=sha1, mode=mode, + source=src) + + +def copy_to_host(module): + compress = module.params.get('compress') + dest = module.params.get('dest') + mode = int(module.params.get('mode'), 0) + sha1 = module.params.get('sha1') + src = module.params.get('src') + + data = base64.b64decode(src) + raw_data = zlib.decompress(data) if compress else data + + if sha1: + if os.path.exists(dest): + if os.access(dest, os.R_OK): + with open(dest, 'rb') as f: + if hashlib.sha1(f.read()).hexdigest() == sha1: + module.exit_json(changed=False) + else: + module.exit_json(failed=True, changed=False, + msg='file is not accessible: {}'.format(dest)) + + if sha1 != hashlib.sha1(raw_data).hexdigest(): + module.exit_json(failed=True, changed=False, + msg='sha1 sum does not match data') + + with os.fdopen(os.open(dest, os.O_WRONLY | os.O_CREAT, mode), 'wb') as f: + f.write(raw_data) + + module.exit_json(changed=True) + +def main(): + module = AnsibleModule( + argument_spec = dict( + compress = dict(default=True, type='bool'), + dest = dict(type='str'), + mode = dict(default='0644', type='str'), + sha1 = dict(default=None, type='str'), + src = dict(required=True, type='str') + ) + ) + + dest = module.params.get('dest') + + try: + if dest: + copy_to_host(module) + else: + copy_from_host(module) + except Exception as e: + module.exit_json(failed=True, changed=True, msg=repr(e)) + +# import module snippets +from ansible.module_utils.basic import * +if __name__ == '__main__': + main() diff --git a/ansible/library/find_disks.py b/ansible/library/find_disks.py new file mode 100644 index 000000000..fe2a638b2 --- /dev/null +++ b/ansible/library/find_disks.py @@ -0,0 +1,86 @@ +#!/usr/bin/python + +# Copyright 2015 Sam Yaple +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This module has been relicensed from the source below: +# https://github.com/SamYaple/yaodu/blob/master/ansible/library/ceph_osd_list + +DOCUMENTATION = ''' +--- +module: find_disks +short_description: Return list of devices containing a specfied label +description: + - This will return a list of all devices with a GPT partition label with + the name specified. +options: + partition_name: + description: + - Partition name + required: True + type: bool +author: Sam Yaple +''' + +EXAMPLES = ''' +- hosts: ceph-osd + tasks: + - name: Return all valid formated devices with the name KOLLA_CEPH_OSD + ceph_osd_list: + partition_name: 'KOLLA_CEPH_OSD' + register: osds +''' + +import sys +import subprocess + + +def main(): + module = AnsibleModule( + argument_spec = dict( + partition_name = dict(required=True, type='str') + ) + ) + + partition_name = module.params.get('partition_name') + + try: + # This should all really be done differently. Unfortunately there is no + # decent python library for dealing with disks like we need to here. + disks = subprocess.check_output("parted -l", shell=True).split('\n') + ret = list() + + for line in disks: + d = line.split(' ') + if d[0] == 'Disk': + dev = d[1][:-1] + + if line.find(partition_name) != -1: + # This process returns an error code when no results return + # We can ignore that, it is safe + p = subprocess.Popen("blkid " + dev + "*", shell=True, stdout=subprocess.PIPE) + fs_uuid = p.communicate()[0] + # The dev doesn't need to have a uuid, will be '' otherwise + if fs_uuid: + fs_uuid = fs_uuid.split('"')[1] + ret.append({'device': dev, 'fs_uuid': fs_uuid}) + + module.exit_json(disks=ret) + except Exception as e: + module.exit_json(failed=True, msg=repr(e)) + +# import module snippets +from ansible.module_utils.basic import * +if __name__ == '__main__': + main() |
