From 07331b47724dbb7cd6952c1a2af54275ace7726e Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 13 Jan 2017 12:37:30 -0500 Subject: lib_openshift modules. This is the first one. oc_route. --- roles/lib_utils/src/ansible/yedit.py | 45 +--------------- roles/lib_utils/src/class/yedit.py | 62 ++++++++++++++++++---- .../src/test/integration/kube-manager-test.yaml | 58 ++++++++++++++++++++ .../test/integration/kube-manager-test.yaml.orig | 52 ++++++++++++++++++ 4 files changed, 164 insertions(+), 53 deletions(-) create mode 100644 roles/lib_utils/src/test/integration/kube-manager-test.yaml create mode 100644 roles/lib_utils/src/test/integration/kube-manager-test.yaml.orig (limited to 'roles/lib_utils/src') diff --git a/roles/lib_utils/src/ansible/yedit.py b/roles/lib_utils/src/ansible/yedit.py index a80cd520c..efe034abf 100644 --- a/roles/lib_utils/src/ansible/yedit.py +++ b/roles/lib_utils/src/ansible/yedit.py @@ -1,49 +1,6 @@ # flake8: noqa # 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 +32,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..4521009ab 100644 --- a/roles/lib_utils/src/class/yedit.py +++ b/roles/lib_utils/src/class/yedit.py @@ -1,6 +1,7 @@ # flake8: noqa # pylint: skip-file + class YeditException(Exception): ''' Exception class for Yedit ''' pass @@ -426,6 +427,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 +489,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 +500,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 +518,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 +532,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/test/integration/kube-manager-test.yaml b/roles/lib_utils/src/test/integration/kube-manager-test.yaml new file mode 100644 index 000000000..aea8e668f --- /dev/null +++ b/roles/lib_utils/src/test/integration/kube-manager-test.yaml @@ -0,0 +1,58 @@ +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 +z: + x: + y: + - 1 + - 2 + - 3 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 -- cgit v1.2.3 From ea33e223e34bb2b8efae6b165f3ac9729357cb46 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 13 Jan 2017 14:29:48 -0500 Subject: Adding oc_edit module to lib_openshift. --- roles/lib_utils/src/ansible/yedit.py | 1 + roles/lib_utils/src/class/yedit.py | 2 + .../src/test/integration/kube-manager-test.yaml | 58 ---------------------- 3 files changed, 3 insertions(+), 58 deletions(-) delete mode 100644 roles/lib_utils/src/test/integration/kube-manager-test.yaml (limited to 'roles/lib_utils/src') diff --git a/roles/lib_utils/src/ansible/yedit.py b/roles/lib_utils/src/ansible/yedit.py index efe034abf..8a1a7c2dc 100644 --- a/roles/lib_utils/src/ansible/yedit.py +++ b/roles/lib_utils/src/ansible/yedit.py @@ -1,6 +1,7 @@ # flake8: noqa # pylint: skip-file + # pylint: disable=too-many-branches def main(): ''' ansible oc module for secrets ''' diff --git a/roles/lib_utils/src/class/yedit.py b/roles/lib_utils/src/class/yedit.py index 4521009ab..b1644f9b2 100644 --- a/roles/lib_utils/src/class/yedit.py +++ b/roles/lib_utils/src/class/yedit.py @@ -1,5 +1,6 @@ # flake8: noqa # pylint: skip-file +# noqa: E301,E302 class YeditException(Exception): @@ -7,6 +8,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/_-]+)).?)+$" diff --git a/roles/lib_utils/src/test/integration/kube-manager-test.yaml b/roles/lib_utils/src/test/integration/kube-manager-test.yaml deleted file mode 100644 index aea8e668f..000000000 --- a/roles/lib_utils/src/test/integration/kube-manager-test.yaml +++ /dev/null @@ -1,58 +0,0 @@ -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 -z: - x: - y: - - 1 - - 2 - - 3 -- cgit v1.2.3 From 24a504f03a3d5edfe8957dcfaa4bde98ae0e60ec Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 17 Jan 2017 15:31:42 -0500 Subject: Adding --verfiy to generate script. --- roles/lib_utils/src/doc/generated | 9 +++++ roles/lib_utils/src/generate.py | 68 +++++++++++++++++++++----------- roles/lib_utils/src/generate_sources.yml | 7 ---- roles/lib_utils/src/sources.yml | 8 ++++ 4 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 roles/lib_utils/src/doc/generated delete mode 100644 roles/lib_utils/src/generate_sources.yml create mode 100644 roles/lib_utils/src/sources.yml (limited to 'roles/lib_utils/src') 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/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/generate_sources.yml deleted file mode 100644 index 83b21de1b..000000000 --- a/roles/lib_utils/src/generate_sources.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- -yedit.py: -- doc/license -- class/import.py -- doc/yedit -- class/yedit.py -- ansible/yedit.py diff --git a/roles/lib_utils/src/sources.yml b/roles/lib_utils/src/sources.yml new file mode 100644 index 000000000..9cf3a0981 --- /dev/null +++ b/roles/lib_utils/src/sources.yml @@ -0,0 +1,8 @@ +--- +yedit.py: +- doc/generated +- doc/license +- class/import.py +- doc/yedit +- class/yedit.py +- ansible/yedit.py -- cgit v1.2.3 From 3fd3cd3a07d9f000c8cb8bd1b7a49ac2af675696 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Wed, 18 Jan 2017 12:06:49 -0500 Subject: Adding a few updates for python27,35 compatibility --- roles/lib_utils/src/doc/yedit | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'roles/lib_utils/src') 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 " extends_documentation_fragment: [] -- cgit v1.2.3