diff options
Diffstat (limited to 'roles/lib_utils')
-rw-r--r-- | roles/lib_utils/library/yedit.py | 108 | ||||
-rw-r--r-- | roles/lib_utils/src/ansible/yedit.py | 44 | ||||
-rw-r--r-- | roles/lib_utils/src/class/yedit.py | 64 | ||||
-rw-r--r-- | roles/lib_utils/src/doc/generated | 9 | ||||
-rw-r--r-- | roles/lib_utils/src/doc/yedit | 6 | ||||
-rwxr-xr-x | roles/lib_utils/src/generate.py | 68 | ||||
-rw-r--r-- | roles/lib_utils/src/sources.yml (renamed from roles/lib_utils/src/generate_sources.yml) | 1 | ||||
-rw-r--r-- | roles/lib_utils/src/test/integration/kube-manager-test.yaml.orig | 52 |
8 files changed, 224 insertions, 128 deletions
diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index fb545c7c8..d882c983e 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -24,7 +24,6 @@ # limitations under the License. # - # pylint: disable=wrong-import-order import json import os @@ -163,6 +162,7 @@ EXAMPLES = ''' # b: # c: d ''' +# noqa: E301,E302 class YeditException(Exception): @@ -170,6 +170,7 @@ class YeditException(Exception): pass +# pylint: disable=too-many-public-methods class Yedit(object): ''' Class to modify yaml files ''' re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$" @@ -590,6 +591,48 @@ class Yedit(object): return (False, self.yaml_dict) + @staticmethod + def get_curr_value(invalue, val_type): + '''return the current value''' + if invalue is None: + return None + + curr_value = invalue + if val_type == 'yaml': + curr_value = yaml.load(invalue) + elif val_type == 'json': + curr_value = json.loads(invalue) + + return curr_value + + @staticmethod + def parse_value(inc_value, vtype=''): + '''determine value type passed''' + true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', + 'on', 'On', 'ON', ] + false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', + 'off', 'Off', 'OFF'] + + # It came in as a string but you didn't specify value_type as string + # we will convert to bool if it matches any of the above cases + if isinstance(inc_value, str) and 'bool' in vtype: + if inc_value not in true_bools and inc_value not in false_bools: + raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' + % (inc_value, vtype)) + elif isinstance(inc_value, bool) and 'str' in vtype: + inc_value = str(inc_value) + + # If vtype is not str then go ahead and attempt to yaml load it. + if isinstance(inc_value, str) and 'str' not in vtype: + try: + inc_value = yaml.load(inc_value) + except Exception: + raise YeditException('Could not determine type of incoming ' + + 'value. value=[%s] vtype=[%s]' + % (type(inc_value), vtype)) + + return inc_value + # pylint: disable=too-many-return-statements,too-many-branches @staticmethod def run_ansible(module): @@ -610,8 +653,8 @@ class Yedit(object): if module.params['state'] == 'list': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['key']: @@ -621,8 +664,8 @@ class Yedit(object): elif module.params['state'] == 'absent': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['update']: @@ -639,8 +682,8 @@ class Yedit(object): elif module.params['state'] == 'present': # check if content is different than what is in the file if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) # We had no edits to make and the contents are the same if yamlfile.yaml_dict == content and \ @@ -653,12 +696,13 @@ class Yedit(object): # we were passed a value; parse it if module.params['value']: - value = parse_value(module.params['value'], - module.params['value_type']) + value = Yedit.parse_value(module.params['value'], + module.params['value_type']) key = module.params['key'] if module.params['update']: # pylint: disable=line-too-long - curr_value = get_curr_value(parse_value(module.params['curr_value']), module.params['curr_value_format']) # noqa: #501 + curr_value = Yedit.get_curr_value(Yedit.parse_value(module.params['curr_value']), # noqa: E501 + module.params['curr_value_format']) # noqa: E501 rval = yamlfile.update(key, value, module.params['index'], curr_value) # noqa: E501 @@ -684,48 +728,6 @@ class Yedit(object): return {'failed': True, 'msg': 'Unkown state passed'} -def get_curr_value(invalue, val_type): - '''return the current value''' - if invalue is None: - return None - - curr_value = invalue - if val_type == 'yaml': - curr_value = yaml.load(invalue) - elif val_type == 'json': - curr_value = json.loads(invalue) - - return curr_value - - -def parse_value(inc_value, vtype=''): - '''determine value type passed''' - true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', - 'on', 'On', 'ON', ] - false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', - 'off', 'Off', 'OFF'] - - # It came in as a string but you didn't specify value_type as string - # we will convert to bool if it matches any of the above cases - if isinstance(inc_value, str) and 'bool' in vtype: - if inc_value not in true_bools and inc_value not in false_bools: - raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' - % (inc_value, vtype)) - elif isinstance(inc_value, bool) and 'str' in vtype: - inc_value = str(inc_value) - - # If vtype is not str then go ahead and attempt to yaml load it. - if isinstance(inc_value, str) and 'str' not in vtype: - try: - inc_value = yaml.load(inc_value) - except Exception: - raise YeditException('Could not determine type of incoming ' + - 'value. value=[%s] vtype=[%s]' - % (type(inc_value), vtype)) - - return inc_value - - # pylint: disable=too-many-branches def main(): ''' ansible oc module for secrets ''' @@ -757,7 +759,7 @@ def main(): rval = Yedit.run_ansible(module) if 'failed' in rval and rval['failed']: - module.fail_json(msg=rval['msg']) + module.fail_json(**rval) module.exit_json(**rval) diff --git a/roles/lib_utils/src/ansible/yedit.py b/roles/lib_utils/src/ansible/yedit.py index a80cd520c..8a1a7c2dc 100644 --- a/roles/lib_utils/src/ansible/yedit.py +++ b/roles/lib_utils/src/ansible/yedit.py @@ -2,48 +2,6 @@ # pylint: skip-file -def get_curr_value(invalue, val_type): - '''return the current value''' - if invalue is None: - return None - - curr_value = invalue - if val_type == 'yaml': - curr_value = yaml.load(invalue) - elif val_type == 'json': - curr_value = json.loads(invalue) - - return curr_value - - -def parse_value(inc_value, vtype=''): - '''determine value type passed''' - true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', - 'on', 'On', 'ON', ] - false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', - 'off', 'Off', 'OFF'] - - # It came in as a string but you didn't specify value_type as string - # we will convert to bool if it matches any of the above cases - if isinstance(inc_value, str) and 'bool' in vtype: - if inc_value not in true_bools and inc_value not in false_bools: - raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' - % (inc_value, vtype)) - elif isinstance(inc_value, bool) and 'str' in vtype: - inc_value = str(inc_value) - - # If vtype is not str then go ahead and attempt to yaml load it. - if isinstance(inc_value, str) and 'str' not in vtype: - try: - inc_value = yaml.load(inc_value) - except Exception: - raise YeditException('Could not determine type of incoming ' + - 'value. value=[%s] vtype=[%s]' - % (type(inc_value), vtype)) - - return inc_value - - # pylint: disable=too-many-branches def main(): ''' ansible oc module for secrets ''' @@ -75,7 +33,7 @@ def main(): rval = Yedit.run_ansible(module) if 'failed' in rval and rval['failed']: - module.fail_json(msg=rval['msg']) + module.fail_json(**rval) module.exit_json(**rval) diff --git a/roles/lib_utils/src/class/yedit.py b/roles/lib_utils/src/class/yedit.py index e110bc11e..b1644f9b2 100644 --- a/roles/lib_utils/src/class/yedit.py +++ b/roles/lib_utils/src/class/yedit.py @@ -1,11 +1,14 @@ # flake8: noqa # pylint: skip-file +# noqa: E301,E302 + class YeditException(Exception): ''' Exception class for Yedit ''' pass +# pylint: disable=too-many-public-methods class Yedit(object): ''' Class to modify yaml files ''' re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$" @@ -426,6 +429,48 @@ class Yedit(object): return (False, self.yaml_dict) + @staticmethod + def get_curr_value(invalue, val_type): + '''return the current value''' + if invalue is None: + return None + + curr_value = invalue + if val_type == 'yaml': + curr_value = yaml.load(invalue) + elif val_type == 'json': + curr_value = json.loads(invalue) + + return curr_value + + @staticmethod + def parse_value(inc_value, vtype=''): + '''determine value type passed''' + true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', + 'on', 'On', 'ON', ] + false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', + 'off', 'Off', 'OFF'] + + # It came in as a string but you didn't specify value_type as string + # we will convert to bool if it matches any of the above cases + if isinstance(inc_value, str) and 'bool' in vtype: + if inc_value not in true_bools and inc_value not in false_bools: + raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' + % (inc_value, vtype)) + elif isinstance(inc_value, bool) and 'str' in vtype: + inc_value = str(inc_value) + + # If vtype is not str then go ahead and attempt to yaml load it. + if isinstance(inc_value, str) and 'str' not in vtype: + try: + inc_value = yaml.load(inc_value) + except Exception: + raise YeditException('Could not determine type of incoming ' + + 'value. value=[%s] vtype=[%s]' + % (type(inc_value), vtype)) + + return inc_value + # pylint: disable=too-many-return-statements,too-many-branches @staticmethod def run_ansible(module): @@ -446,8 +491,8 @@ class Yedit(object): if module.params['state'] == 'list': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['key']: @@ -457,8 +502,8 @@ class Yedit(object): elif module.params['state'] == 'absent': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['update']: @@ -475,8 +520,8 @@ class Yedit(object): elif module.params['state'] == 'present': # check if content is different than what is in the file if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) # We had no edits to make and the contents are the same if yamlfile.yaml_dict == content and \ @@ -489,12 +534,13 @@ class Yedit(object): # we were passed a value; parse it if module.params['value']: - value = parse_value(module.params['value'], - module.params['value_type']) + value = Yedit.parse_value(module.params['value'], + module.params['value_type']) key = module.params['key'] if module.params['update']: # pylint: disable=line-too-long - curr_value = get_curr_value(parse_value(module.params['curr_value']), module.params['curr_value_format']) # noqa: #501 + curr_value = Yedit.get_curr_value(Yedit.parse_value(module.params['curr_value']), # noqa: E501 + module.params['curr_value_format']) # noqa: E501 rval = yamlfile.update(key, value, module.params['index'], curr_value) # noqa: E501 diff --git a/roles/lib_utils/src/doc/generated b/roles/lib_utils/src/doc/generated new file mode 100644 index 000000000..054780313 --- /dev/null +++ b/roles/lib_utils/src/doc/generated @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# pylint: disable=missing-docstring +# ___ ___ _ _ ___ ___ _ _____ ___ ___ +# / __| __| \| | __| _ \ /_\_ _| __| \ +# | (_ | _|| .` | _|| / / _ \| | | _|| |) | +# \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____ +# | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _| +# | |) | (_) | | .` | (_) || | | _|| |) | | | | +# |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_| diff --git a/roles/lib_utils/src/doc/yedit b/roles/lib_utils/src/doc/yedit index e367a389e..16b44943e 100644 --- a/roles/lib_utils/src/doc/yedit +++ b/roles/lib_utils/src/doc/yedit @@ -102,6 +102,12 @@ options: required: false default: true aliases: [] + separator: + description: + - The separator being used when parsing strings. + required: false + default: '.' + aliases: [] author: - "Kenny Woodson <kwoodson@redhat.com>" extends_documentation_fragment: [] diff --git a/roles/lib_utils/src/generate.py b/roles/lib_utils/src/generate.py index f4b46aa91..cece68fb4 100755 --- a/roles/lib_utils/src/generate.py +++ b/roles/lib_utils/src/generate.py @@ -3,42 +3,64 @@ Generate the openshift-ansible/roles/lib_openshift_cli/library/ modules. ''' +import argparse import os +import six import yaml -# pylint: disable=anomalous-backslash-in-string -GEN_STR = "#!/usr/bin/env python\n" + \ - "# pylint: disable=missing-docstring\n" + \ - "# ___ ___ _ _ ___ ___ _ _____ ___ ___\n" + \ - "# / __| __| \| | __| _ \ /_\_ _| __| \\\n" + \ - "# | (_ | _|| .` | _|| / / _ \| | | _|| |) |\n" + \ - "# \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____\n" + \ - "# | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _|\n" + \ - "# | |) | (_) | | .` | (_) || | | _|| |) | | | |\n" + \ - "# |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|\n" - OPENSHIFT_ANSIBLE_PATH = os.path.dirname(os.path.realpath(__file__)) -OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'generate_sources.yml') # noqa: E501 +OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'sources.yml') # noqa: E501 + + +class GenerateAnsibleException(Exception): + '''General Exception for generate function''' + pass + + +def parse_args(): + '''parse arguments to generate''' + parser = argparse.ArgumentParser(description="Generate ansible modules.") + parser.add_argument('--verify', action='store_true', default=False, + help='Verify library code matches the generated code.') + + return parser.parse_args() + + +def generate(parts): + '''generate the source code for the ansible modules''' + + data = six.StringIO() + for fpart in parts: + # first line is pylint disable so skip it + with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd: + for idx, line in enumerate(pfd): + if idx in [0, 1] and 'flake8: noqa' in line or 'pylint: skip-file' in line: # noqa: E501 + continue + + data.write(line) + + return data def main(): ''' combine the necessary files to create the ansible module ''' + args = parse_args() library = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/') sources = yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read()) + for fname, parts in sources.items(): - with open(os.path.join(library, fname), 'w') as afd: + data = generate(parts) + fname = os.path.join(library, fname) + if args.verify: + if not open(fname).read() == data.getvalue(): + raise GenerateAnsibleException('Generated content does not match for %s' % fname) + + continue + + with open(fname, 'w') as afd: afd.seek(0) - afd.write(GEN_STR) - for fpart in parts: - with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd: - # first line is pylint disable so skip it - for idx, line in enumerate(pfd): - if idx in [0, 1] and 'flake8: noqa' in line \ - or 'pylint: skip-file' in line: - continue - - afd.write(line) + afd.write(data.getvalue()) if __name__ == '__main__': diff --git a/roles/lib_utils/src/generate_sources.yml b/roles/lib_utils/src/sources.yml index 83b21de1b..9cf3a0981 100644 --- a/roles/lib_utils/src/generate_sources.yml +++ b/roles/lib_utils/src/sources.yml @@ -1,5 +1,6 @@ --- yedit.py: +- doc/generated - doc/license - class/import.py - doc/yedit diff --git a/roles/lib_utils/src/test/integration/kube-manager-test.yaml.orig b/roles/lib_utils/src/test/integration/kube-manager-test.yaml.orig new file mode 100644 index 000000000..5541c3dae --- /dev/null +++ b/roles/lib_utils/src/test/integration/kube-manager-test.yaml.orig @@ -0,0 +1,52 @@ +apiVersion: v1 +kind: Pod +metadata: + name: kube-controller-manager + namespace: kube-system +spec: + hostNetwork: true + containers: + - name: kube-controller-manager + image: openshift/kube:v1.0.0 + command: + - /hyperkube + - controller-manager + - --master=http://127.0.0.1:8080 + - --leader-elect=true + - --service-account-private-key-file=/etc/kubernetes/ssl/apiserver-key.pem + - --root-ca-file=/etc/k8s/ssl/my.pem + - --my-new-parameter=openshift + livenessProbe: + httpGet: + host: 127.0.0.1 + path: /healthz + port: 10252 + initialDelaySeconds: 15 + timeoutSeconds: 1 + volumeMounts: + - mountPath: /etc/kubernetes/ssl + name: ssl-certs-kubernetes + readOnly: true + - mountPath: /etc/ssl/certs + name: ssl-certs-host + readOnly: 'true' + volumes: + - hostPath: + path: /etc/kubernetes/ssl + name: ssl-certs-kubernetes + - hostPath: + path: /usr/share/ca-certificates + name: ssl-certs-host +yedittest: yedittest +metadata-namespace: openshift-is-awesome +nonexistingkey: +- --my-new-parameter=openshift +a: + b: + c: d +e: + f: + g: + h: + i: + j: k |