From 15d730f3aec1f579dbd3cc5310264c68eb78e242 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Mon, 28 Mar 2016 17:44:48 -0400 Subject: Moving generation of ansible module side by side with module. --- roles/lib_yaml_editor/build/ansible/yedit.py | 66 +++++++++++ roles/lib_yaml_editor/build/generate.py | 42 +++++++ roles/lib_yaml_editor/build/src/base.py | 9 ++ roles/lib_yaml_editor/build/src/yedit.py | 160 +++++++++++++++++++++++++++ roles/lib_yaml_editor/build/test/foo.yml | 1 + roles/lib_yaml_editor/build/test/test.yaml | 15 +++ roles/lib_yaml_editor/library/yedit.py | 42 +++++-- 7 files changed, 324 insertions(+), 11 deletions(-) create mode 100644 roles/lib_yaml_editor/build/ansible/yedit.py create mode 100755 roles/lib_yaml_editor/build/generate.py create mode 100644 roles/lib_yaml_editor/build/src/base.py create mode 100644 roles/lib_yaml_editor/build/src/yedit.py create mode 100644 roles/lib_yaml_editor/build/test/foo.yml create mode 100755 roles/lib_yaml_editor/build/test/test.yaml (limited to 'roles/lib_yaml_editor') diff --git a/roles/lib_yaml_editor/build/ansible/yedit.py b/roles/lib_yaml_editor/build/ansible/yedit.py new file mode 100644 index 000000000..bf868fb71 --- /dev/null +++ b/roles/lib_yaml_editor/build/ansible/yedit.py @@ -0,0 +1,66 @@ +#pylint: skip-file + +def main(): + ''' + ansible oc module for secrets + ''' + + module = AnsibleModule( + argument_spec=dict( + state=dict(default='present', type='str', + choices=['present', 'absent', 'list']), + debug=dict(default=False, type='bool'), + src=dict(default=None, type='str'), + content=dict(default=None, type='dict'), + key=dict(default=None, type='str'), + value=dict(default=None, type='str'), + value_format=dict(default='yaml', choices=['yaml', 'json'], type='str'), + ), + #mutually_exclusive=[["src", "content"]], + + supports_check_mode=True, + ) + state = module.params['state'] + + yamlfile = Yedit(module.params['src'], module.params['content']) + + rval = yamlfile.get() + if not rval and state != 'present': + module.fail_json(msg='Error opening file [%s]. Verify that the' + \ + ' file exists, that it is has correct permissions, and is valid yaml.') + + if state == 'list': + module.exit_json(changed=False, results=rval, state="list") + + if state == 'absent': + rval = yamlfile.delete(module.params['key']) + module.exit_json(changed=rval[0], results=rval[1], state="absent") + + if state == 'present': + + if module.params['value_format'] == 'yaml': + value = yaml.load(module.params['value']) + elif module.params['value_format'] == 'json': + value = json.loads(module.params['value']) + + if rval: + rval = yamlfile.put(module.params['key'], value) + module.exit_json(changed=rval[0], results=rval[1], state="present") + + if not module.params['content']: + rval = yamlfile.create(module.params['key'], value) + else: + yamlfile.write() + rval = yamlfile.get() + module.exit_json(changed=rval[0], results=rval[1], state="present") + + module.exit_json(failed=True, + changed=False, + results='Unknown state passed. %s' % state, + state="unknown") + +# pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled +# import module snippets. This are required +from ansible.module_utils.basic import * + +main() diff --git a/roles/lib_yaml_editor/build/generate.py b/roles/lib_yaml_editor/build/generate.py new file mode 100755 index 000000000..6521ff2c1 --- /dev/null +++ b/roles/lib_yaml_editor/build/generate.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +''' + Generate the openshift-ansible/roles/lib_openshift_cli/library/ modules. +''' + +import os + +# pylint: disable=anomalous-backslash-in-string +GEN_STR = "#!usr/bin/env python\n" + \ + "# ___ ___ _ _ ___ ___ _ _____ ___ ___\n" + \ + "# / __| __| \| | __| _ \ /_\_ _| __| \\\n" + \ + "# | (_ | _|| .` | _|| / / _ \| | | _|| |) |\n" + \ + "# \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____\n" + \ + "# | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _|\n" + \ + "# | |) | (_) | | .` | (_) || | | _|| |) | | | |\n" + \ + "# |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|\n" + +FILES = {'yedit.py': ['src/base.py', 'src/yedit.py', 'ansible/yedit.py'], + } + + +def main(): + ''' combine the necessary files to create the ansible module ''' + openshift_ansible = ('../library/') + for fname, parts in FILES.items(): + with open(os.path.join(openshift_ansible, fname), 'w') as afd: + afd.seek(0) + afd.write(GEN_STR) + for fpart in parts: + with open(fpart) as pfd: + # first line is pylint disable so skip it + for idx, line in enumerate(pfd): + if idx == 0 and 'skip-file' in line: + continue + + afd.write(line) + + +if __name__ == '__main__': + main() + + diff --git a/roles/lib_yaml_editor/build/src/base.py b/roles/lib_yaml_editor/build/src/base.py new file mode 100644 index 000000000..ad8b041cf --- /dev/null +++ b/roles/lib_yaml_editor/build/src/base.py @@ -0,0 +1,9 @@ +# pylint: skip-file + +''' +module for managing yaml files +''' + +import os +import yaml + diff --git a/roles/lib_yaml_editor/build/src/yedit.py b/roles/lib_yaml_editor/build/src/yedit.py new file mode 100644 index 000000000..4f6a91d8b --- /dev/null +++ b/roles/lib_yaml_editor/build/src/yedit.py @@ -0,0 +1,160 @@ +# pylint: skip-file + +class YeditException(Exception): + ''' Exception class for Yedit ''' + pass + +class Yedit(object): + ''' Class to modify yaml files ''' + + def __init__(self, filename=None, content=None): + self.content = content + self.filename = filename + self.__yaml_dict = content + if self.filename and not self.content: + self.get() + elif self.filename and self.content: + self.write() + + @property + def yaml_dict(self): + ''' getter method for yaml_dict ''' + return self.__yaml_dict + + @yaml_dict.setter + def yaml_dict(self, value): + ''' setter method for yaml_dict ''' + self.__yaml_dict = value + + @staticmethod + def remove_entry(data, keys): + ''' remove an item from a dictionary with key notation a.b.c + d = {'a': {'b': 'c'}}} + keys = a.b + item = c + ''' + if "." in keys: + key, rest = keys.split(".", 1) + if key in data.keys(): + Yedit.remove_entry(data[key], rest) + else: + del data[keys] + + @staticmethod + def add_entry(data, keys, item): + ''' Add an item to a dictionary with key notation a.b.c + d = {'a': {'b': 'c'}}} + keys = a.b + item = c + ''' + if "." in keys: + key, rest = keys.split(".", 1) + if key not in data: + data[key] = {} + + if not isinstance(data, dict): + raise YeditException('Invalid add_entry called on a [%s] of type [%s].' % (data, type(data))) + else: + Yedit.add_entry(data[key], rest, item) + + else: + data[keys] = item + + + @staticmethod + def get_entry(data, keys): + ''' Get an item from a dictionary with key notation a.b.c + d = {'a': {'b': 'c'}}} + keys = a.b + return c + ''' + if keys and "." in keys: + key, rest = keys.split(".", 1) + if not isinstance(data[key], dict): + raise YeditException('Invalid get_entry called on a [%s] of type [%s].' % (data, type(data))) + + else: + return Yedit.get_entry(data[key], rest) + + else: + return data.get(keys, None) + + + def write(self): + ''' write to file ''' + if not self.filename: + raise YeditException('Please specify a filename.') + + with open(self.filename, 'w') as yfd: + yfd.write(yaml.safe_dump(self.yaml_dict, default_flow_style=False)) + + def read(self): + ''' write to file ''' + # check if it exists + if not self.exists(): + return None + + contents = None + with open(self.filename) as yfd: + contents = yfd.read() + + return contents + + def exists(self): + ''' return whether file exists ''' + if os.path.exists(self.filename): + return True + + return False + + def get(self): + ''' return yaml file ''' + contents = self.read() + + if not contents: + return None + + # check if it is yaml + try: + self.yaml_dict = yaml.load(contents) + except yaml.YAMLError as _: + # Error loading yaml + return None + + return self.yaml_dict + + def delete(self, key): + ''' put key, value into a yaml file ''' + try: + entry = Yedit.get_entry(self.yaml_dict, key) + except KeyError as _: + entry = None + if not entry: + return (False, self.yaml_dict) + + Yedit.remove_entry(self.yaml_dict, key) + self.write() + return (True, self.get()) + + def put(self, key, value): + ''' put key, value into a yaml file ''' + try: + entry = Yedit.get_entry(self.yaml_dict, key) + except KeyError as _: + entry = None + + if entry == value: + return (False, self.yaml_dict) + + Yedit.add_entry(self.yaml_dict, key, value) + self.write() + return (True, self.get()) + + def create(self, key, value): + ''' create the file ''' + if not self.exists(): + self.yaml_dict = {key: value} + self.write() + return (True, self.get()) + + return (False, self.get()) diff --git a/roles/lib_yaml_editor/build/test/foo.yml b/roles/lib_yaml_editor/build/test/foo.yml new file mode 100644 index 000000000..2a7a89ce2 --- /dev/null +++ b/roles/lib_yaml_editor/build/test/foo.yml @@ -0,0 +1 @@ +foo: barplus diff --git a/roles/lib_yaml_editor/build/test/test.yaml b/roles/lib_yaml_editor/build/test/test.yaml new file mode 100755 index 000000000..ac9c37565 --- /dev/null +++ b/roles/lib_yaml_editor/build/test/test.yaml @@ -0,0 +1,15 @@ +#!/usr/bin/ansible-playbook +--- +- hosts: localhost + gather_facts: no + tasks: + - yedit: + src: /home/kwoodson/git/openshift-ansible/roles/lib_yaml_editor/build/test/foo.yml + key: foo + value: barplus + state: present + register: output + + - debug: + msg: "{{ output }}" + diff --git a/roles/lib_yaml_editor/library/yedit.py b/roles/lib_yaml_editor/library/yedit.py index 9b565d0c7..356cf07a5 100644 --- a/roles/lib_yaml_editor/library/yedit.py +++ b/roles/lib_yaml_editor/library/yedit.py @@ -1,11 +1,20 @@ -#!/usr/bin/env python +#!usr/bin/env python +# ___ ___ _ _ ___ ___ _ _____ ___ ___ +# / __| __| \| | __| _ \ /_\_ _| __| \ +# | (_ | _|| .` | _|| / / _ \| | | _|| |) | +# \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____ +# | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _| +# | |) | (_) | | .` | (_) || | | _|| |) | | | | +# |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_| + ''' -module for openshift cloud secrets +module for managing yaml files ''' import os import yaml + class YeditException(Exception): ''' Exception class for Yedit ''' pass @@ -13,10 +22,14 @@ class YeditException(Exception): class Yedit(object): ''' Class to modify yaml files ''' - def __init__(self, filename): + def __init__(self, filename=None, content=None): + self.content = content self.filename = filename - self.__yaml_dict = None - self.get() + self.__yaml_dict = content + if self.filename and not self.content: + self.get() + elif self.filename and self.content: + self.write() @property def yaml_dict(self): @@ -84,8 +97,11 @@ class Yedit(object): def write(self): ''' write to file ''' + if not self.filename: + raise YeditException('Please specify a filename.') + with open(self.filename, 'w') as yfd: - yfd.write(yaml.dump(self.yaml_dict, default_flow_style=False)) + yfd.write(yaml.safe_dump(self.yaml_dict, default_flow_style=False)) def read(self): ''' write to file ''' @@ -105,6 +121,7 @@ class Yedit(object): return True return False + def get(self): ''' return yaml file ''' contents = self.read() @@ -157,7 +174,6 @@ class Yedit(object): return (False, self.get()) - def main(): ''' ansible oc module for secrets @@ -168,19 +184,19 @@ def main(): state=dict(default='present', type='str', choices=['present', 'absent', 'list']), debug=dict(default=False, type='bool'), - src=dict(default=None, type='str'), + content=dict(default=None, type='dict'), key=dict(default=None, type='str'), value=dict(default=None, type='str'), value_format=dict(default='yaml', choices=['yaml', 'json'], type='str'), ), - mutually_exclusive=[["contents", "files"]], + #mutually_exclusive=[["src", "content"]], supports_check_mode=True, ) state = module.params['state'] - yamlfile = Yedit(module.params['src']) + yamlfile = Yedit(module.params['src'], module.params['content']) rval = yamlfile.get() if not rval and state != 'present': @@ -205,7 +221,11 @@ def main(): rval = yamlfile.put(module.params['key'], value) module.exit_json(changed=rval[0], results=rval[1], state="present") - rval = yamlfile.create(module.params['key'], value) + if not module.params['content']: + rval = yamlfile.create(module.params['key'], value) + else: + yamlfile.write() + rval = yamlfile.get() module.exit_json(changed=rval[0], results=rval[1], state="present") module.exit_json(failed=True, -- cgit v1.2.3