From 9b7a409745abae6a837ff1e177e5220bcac4d938 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Mon, 23 Jan 2017 11:55:27 -0500 Subject: Adding oc_scale to lib_openshift. --- roles/lib_openshift/src/ansible/oc_scale.py | 29 ++++++ roles/lib_openshift/src/class/oc_scale.py | 104 +++++++++++++++++++++ roles/lib_openshift/src/doc/scale | 70 ++++++++++++++ roles/lib_openshift/src/sources.yml | 9 ++ .../src/test/integration/oc_scale.yml | 19 ++++ 5 files changed, 231 insertions(+) create mode 100644 roles/lib_openshift/src/ansible/oc_scale.py create mode 100644 roles/lib_openshift/src/class/oc_scale.py create mode 100644 roles/lib_openshift/src/doc/scale create mode 100755 roles/lib_openshift/src/test/integration/oc_scale.yml (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/ansible/oc_scale.py b/roles/lib_openshift/src/ansible/oc_scale.py new file mode 100644 index 000000000..58f5c2ee3 --- /dev/null +++ b/roles/lib_openshift/src/ansible/oc_scale.py @@ -0,0 +1,29 @@ +# pylint: skip-file +# flake8: noqa + +def main(): + ''' + ansible oc module for scaling + ''' + + module = AnsibleModule( + argument_spec=dict( + kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'), + state=dict(default='present', type='str', choices=['present', 'list']), + debug=dict(default=False, type='bool'), + kind=dict(default='dc', choices=['dc', 'rc'], type='str'), + namespace=dict(default='default', type='str'), + replicas=dict(default=None, type='int'), + name=dict(default=None, type='str'), + ), + supports_check_mode=True, + ) + rval = OCScale.run_ansible(params, module.check_mode) + if 'failed' in rval: + module.fail_json(**rval) + + module.exit_json(**rval) + + +if __name__ == '__main__': + main() diff --git a/roles/lib_openshift/src/class/oc_scale.py b/roles/lib_openshift/src/class/oc_scale.py new file mode 100644 index 000000000..bd8f9da93 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_scale.py @@ -0,0 +1,104 @@ +# pylint: skip-file +# flake8: noqa + +# pylint: disable=too-many-instance-attributes +class OCScale(OpenShiftCLI): + ''' Class to wrap the oc command line tools ''' + + # pylint allows 5 + # pylint: disable=too-many-arguments + def __init__(self, + resource_name, + namespace, + replicas, + kind, + kubeconfig='/etc/origin/master/admin.kubeconfig', + verbose=False): + ''' Constructor for OCScale ''' + super(OCScale, self).__init__(namespace, kubeconfig) + self.kind = kind + self.replicas = replicas + self.name = resource_name + self.namespace = namespace + self.kubeconfig = kubeconfig + self.verbose = verbose + self._resource = None + + @property + def resource(self): + ''' property function for resource var ''' + if not self._resource: + self.get() + return self._resource + + @resource.setter + def resource(self, data): + ''' setter function for resource var ''' + self._resource = data + + def get(self): + '''return replicas information ''' + vol = self._get(self.kind, self.name) + if vol['returncode'] == 0: + if self.kind == 'dc': + self.resource = DeploymentConfig(content=vol['results'][0]) + vol['results'] = [self.resource.get_replicas()] + if self.kind == 'rc': + self.resource = ReplicationController(content=vol['results'][0]) + vol['results'] = [self.resource.get_replicas()] + + return vol + + def put(self): + '''update replicas into dc ''' + self.resource.update_replicas(self.replicas) + return self._replace_content(self.kind, self.name, self.resource.yaml_dict) + + def needs_update(self): + ''' verify whether an update is needed ''' + return self.resource.needs_update_replicas(self.replicas) + + @staticmethod + def run_ansible(params, check_mode): + '''perform the idempotent ansible logic''' + + oc_scale = OCScale(params['name'], + params['namespace'], + params['replicas'], + params['kind'], + params['kubeconfig'], + verbose=params['debug']) + + state = params['state'] + + api_rval = oc_scale.get() + + ##### + # Get + ##### + if state == 'list': + return {'changed': False, 'result': api_rval['results'], 'state': 'list'} # noqa: E501 + + elif state == 'present': + ######## + # Update + ######## + if oc_scale.needs_update(): + if check_mode: + return {'changed': True, 'result': 'CHECK_MODE: Would have updated.'} # noqa: E501 + api_rval = oc_scale.put() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + # return the created object + api_rval = oc_scale.get() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + return {'changed': True, 'result': api_rval['results'], 'state': 'present'} # noqa: E501 + + return {'changed': False, 'result': api_rval['results'], 'state': 'present'} # noqa: E501 + + return {'failed': True, 'msg': 'Unknown state passed. [{}]'.format(state)} diff --git a/roles/lib_openshift/src/doc/scale b/roles/lib_openshift/src/doc/scale new file mode 100644 index 000000000..b2ffc77f9 --- /dev/null +++ b/roles/lib_openshift/src/doc/scale @@ -0,0 +1,70 @@ +# flake8: noqa +# pylint: skip-file + +DOCUMENTATION = ''' +--- +module: oc_scale +short_description: Manage openshift services through the scale parameters +description: + - Manage openshift services through scaling them. +options: + state: + description: + - State represents whether to create, modify, delete, or list + required: true + default: present + choices: ["present", "absent", "list"] + aliases: [] + kubeconfig: + description: + - The path for the kubeconfig file to use for authentication + required: false + default: /etc/origin/master/admin.kubeconfig + aliases: [] + debug: + description: + - Turn on debug output. + required: false + default: False + aliases: [] + name: + description: + - Name of the object that is being queried. + required: false + default: None + aliases: [] + namespace: + description: + - The namespace where the object lives. + required: false + default: str + aliases: [] + kind: + description: + - The kind of object to scale. + required: false + default: None + choices: + - rc + - dc + aliases: [] +author: +- "Kenny Woodson " +extends_documentation_fragment: [] +''' + +EXAMPLES = ''' +- name: scale down a rc to 0 + oc_scale: + name: my-replication-controller + kind: rc + namespace: openshift-infra + replicas: 0 + +- name: scale up a deploymentconfig to 2 + oc_scale: + name: php + kind: dc + namespace: my-php-app + replicas: 2 +''' diff --git a/roles/lib_openshift/src/sources.yml b/roles/lib_openshift/src/sources.yml index 3c20b614d..00543c64b 100644 --- a/roles/lib_openshift/src/sources.yml +++ b/roles/lib_openshift/src/sources.yml @@ -27,6 +27,15 @@ oc_route.py: - lib/route.py - class/oc_route.py - ansible/oc_route.py +oc_scale.py: +- doc/generated +- doc/license +- lib/import.py +- doc/scale +- ../../lib_utils/src/class/yedit.py +- lib/base.py +- class/oc_scale.py +- ansible/oc_scale.py oc_version.py: - doc/generated - doc/license diff --git a/roles/lib_openshift/src/test/integration/oc_scale.yml b/roles/lib_openshift/src/test/integration/oc_scale.yml new file mode 100755 index 000000000..d272357e8 --- /dev/null +++ b/roles/lib_openshift/src/test/integration/oc_scale.yml @@ -0,0 +1,19 @@ +#!/usr/bin/ansible-playbook --module-path=../../../library/ +# ./oc_scale.yml -e "cli_master_test=$OPENSHIFT_MASTER +--- +- hosts: "{{ cli_master_test }}" + gather_facts: no + user: root + tasks: + - name: list oc scale for default router dc + oc_scale: + state: list + name: router + namespace: default + kind: dc + register: scaleout + - debug: var=scaleout + + - assert: + that: "scaleout.results.results[0]['metadata']['name'] == 'test'" + msg: route create failed -- cgit v1.2.3