summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/class
diff options
context:
space:
mode:
Diffstat (limited to 'roles/lib_openshift/src/class')
-rw-r--r--roles/lib_openshift/src/class/oc_edit.py94
-rw-r--r--roles/lib_openshift/src/class/oc_obj.py193
-rw-r--r--roles/lib_openshift/src/class/oc_route.py170
-rw-r--r--roles/lib_openshift/src/class/oc_version.py47
4 files changed, 504 insertions, 0 deletions
diff --git a/roles/lib_openshift/src/class/oc_edit.py b/roles/lib_openshift/src/class/oc_edit.py
new file mode 100644
index 000000000..0734e2085
--- /dev/null
+++ b/roles/lib_openshift/src/class/oc_edit.py
@@ -0,0 +1,94 @@
+# pylint: skip-file
+# flake8: noqa
+
+class Edit(OpenShiftCLI):
+ ''' Class to wrap the oc command line tools
+ '''
+ # pylint: disable=too-many-arguments
+ def __init__(self,
+ kind,
+ namespace,
+ resource_name=None,
+ kubeconfig='/etc/origin/master/admin.kubeconfig',
+ separator='.',
+ verbose=False):
+ ''' Constructor for OpenshiftOC '''
+ super(Edit, self).__init__(namespace, kubeconfig)
+ self.namespace = namespace
+ self.kind = kind
+ self.name = resource_name
+ self.kubeconfig = kubeconfig
+ self.separator = separator
+ self.verbose = verbose
+
+ def get(self):
+ '''return a secret by name '''
+ return self._get(self.kind, self.name)
+
+ def update(self, file_name, content, force=False, content_type='yaml'):
+ '''run update '''
+ if file_name:
+ if content_type == 'yaml':
+ data = yaml.load(open(file_name))
+ elif content_type == 'json':
+ data = json.loads(open(file_name).read())
+
+ changes = []
+ yed = Yedit(filename=file_name, content=data, separator=self.separator)
+ for key, value in content.items():
+ changes.append(yed.put(key, value))
+
+ if any([not change[0] for change in changes]):
+ return {'returncode': 0, 'updated': False}
+
+ yed.write()
+
+ atexit.register(Utils.cleanup, [file_name])
+
+ return self._replace(file_name, force=force)
+
+ return self._replace_content(self.kind, self.name, content, force=force, sep=self.separator)
+
+ @staticmethod
+ def run_ansible(params, check_mode):
+ '''run the ansible idempotent code'''
+
+ ocedit = Edit(params['kind'],
+ params['namespace'],
+ params['name'],
+ kubeconfig=params['kubeconfig'],
+ separator=params['separator'],
+ verbose=params['debug'])
+
+ api_rval = ocedit.get()
+
+ ########
+ # Create
+ ########
+ if not Utils.exists(api_rval['results'], params['name']):
+ return {"failed": True, 'msg': api_rval}
+
+ ########
+ # Update
+ ########
+ if check_mode:
+ return {'changed': True, 'msg': 'CHECK_MODE: Would have performed edit'}
+
+ api_rval = ocedit.update(params['file_name'],
+ params['content'],
+ params['force'],
+ params['file_format'])
+
+ if api_rval['returncode'] != 0:
+ return {"failed": True, 'msg': api_rval}
+
+ if 'updated' in api_rval and not api_rval['updated']:
+ return {"changed": False, 'results': api_rval, 'state': 'present'}
+
+ # return the created object
+ api_rval = ocedit.get()
+
+ if api_rval['returncode'] != 0:
+ return {"failed": True, 'msg': api_rval}
+
+ return {"changed": True, 'results': api_rval, 'state': 'present'}
diff --git a/roles/lib_openshift/src/class/oc_obj.py b/roles/lib_openshift/src/class/oc_obj.py
new file mode 100644
index 000000000..9d0b8e45b
--- /dev/null
+++ b/roles/lib_openshift/src/class/oc_obj.py
@@ -0,0 +1,193 @@
+# pylint: skip-file
+# flake8: noqa
+
+# pylint: disable=too-many-instance-attributes
+class OCObject(OpenShiftCLI):
+ ''' Class to wrap the oc command line tools '''
+
+ # pylint allows 5. we need 6
+ # pylint: disable=too-many-arguments
+ def __init__(self,
+ kind,
+ namespace,
+ rname=None,
+ selector=None,
+ kubeconfig='/etc/origin/master/admin.kubeconfig',
+ verbose=False,
+ all_namespaces=False):
+ ''' Constructor for OpenshiftOC '''
+ super(OCObject, self).__init__(namespace, kubeconfig,
+ all_namespaces=all_namespaces)
+ self.kind = kind
+ self.namespace = namespace
+ self.name = rname
+ self.selector = selector
+ self.kubeconfig = kubeconfig
+ self.verbose = verbose
+
+ def get(self):
+ '''return a kind by name '''
+ results = self._get(self.kind, rname=self.name, selector=self.selector)
+ if results['returncode'] != 0 and 'stderr' in results and \
+ '\"%s\" not found' % self.name in results['stderr']:
+ results['returncode'] = 0
+
+ return results
+
+ def delete(self):
+ '''return all pods '''
+ return self._delete(self.kind, self.name)
+
+ def create(self, files=None, content=None):
+ '''
+ Create a config
+
+ NOTE: This creates the first file OR the first conent.
+ TODO: Handle all files and content passed in
+ '''
+ if files:
+ return self._create(files[0])
+
+ content['data'] = yaml.dump(content['data'])
+ content_file = Utils.create_files_from_contents(content)[0]
+
+ return self._create(content_file['path'])
+
+ # pylint: disable=too-many-function-args
+ def update(self, files=None, content=None, force=False):
+ '''update a current openshift object
+
+ This receives a list of file names or content
+ and takes the first and calls replace.
+
+ TODO: take an entire list
+ '''
+ if files:
+ return self._replace(files[0], force)
+
+ if content and 'data' in content:
+ content = content['data']
+
+ return self.update_content(content, force)
+
+ def update_content(self, content, force=False):
+ '''update an object through using the content param'''
+ return self._replace_content(self.kind, self.name, content, force=force)
+
+ def needs_update(self, files=None, content=None, content_type='yaml'):
+ ''' check to see if we need to update '''
+ objects = self.get()
+ if objects['returncode'] != 0:
+ return objects
+
+ # pylint: disable=no-member
+ data = None
+ if files:
+ data = Utils.get_resource_file(files[0], content_type)
+ elif content and 'data' in content:
+ data = content['data']
+ else:
+ data = content
+
+ # if equal then no need. So not equal is True
+ return not Utils.check_def_equal(data, objects['results'][0], skip_keys=None, debug=False)
+
+ # pylint: disable=too-many-return-statements,too-many-branches
+ @staticmethod
+ def run_ansible(params, check_mode=False):
+ '''perform the ansible idempotent code'''
+
+ ocobj = OCObject(params['kind'],
+ params['namespace'],
+ params['name'],
+ params['selector'],
+ kubeconfig=params['kubeconfig'],
+ verbose=params['debug'],
+ all_namespaces=params['all_namespaces'])
+
+ state = params['state']
+
+ api_rval = ocobj.get()
+
+ #####
+ # Get
+ #####
+ if state == 'list':
+ return {'changed': False, 'results': api_rval, 'state': 'list'}
+
+ if not params['name']:
+ return {'failed': True, 'msg': 'Please specify a name when state is absent|present.'} # noqa: E501
+
+ ########
+ # Delete
+ ########
+ if state == 'absent':
+ if not Utils.exists(api_rval['results'], params['name']):
+ return {'changed': False, 'state': 'absent'}
+
+ if check_mode:
+ return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a delete'}
+
+ api_rval = ocobj.delete()
+
+ return {'changed': True, 'results': api_rval, 'state': 'absent'}
+
+ if state == 'present':
+ ########
+ # Create
+ ########
+ if not Utils.exists(api_rval['results'], params['name']):
+
+ if check_mode:
+ return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create'}
+
+ # Create it here
+ api_rval = ocobj.create(params['files'], params['content'])
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ # return the created object
+ api_rval = ocobj.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ # Remove files
+ if params['files'] and params['delete_after']:
+ Utils.cleanup(params['files'])
+
+ return {'changed': True, 'results': api_rval, 'state': "present"}
+
+ ########
+ # Update
+ ########
+ # if a file path is passed, use it.
+ update = ocobj.needs_update(params['files'], params['content'])
+ if not isinstance(update, bool):
+ return {'failed': True, 'msg': update}
+
+ # No changes
+ if not update:
+ if params['files'] and params['delete_after']:
+ Utils.cleanup(params['files'])
+
+ return {'changed': False, 'results': api_rval['results'][0], 'state': "present"}
+
+ if check_mode:
+ return {'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.'}
+
+ api_rval = ocobj.update(params['files'],
+ params['content'],
+ params['force'])
+
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ # return the created object
+ api_rval = ocobj.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ return {'changed': True, 'results': api_rval, 'state': "present"}
diff --git a/roles/lib_openshift/src/class/oc_route.py b/roles/lib_openshift/src/class/oc_route.py
new file mode 100644
index 000000000..42af2c01c
--- /dev/null
+++ b/roles/lib_openshift/src/class/oc_route.py
@@ -0,0 +1,170 @@
+# pylint: skip-file
+# flake8: noqa
+
+
+# pylint: disable=too-many-instance-attributes
+class OCRoute(OpenShiftCLI):
+ ''' Class to wrap the oc command line tools '''
+ kind = 'route'
+
+ def __init__(self,
+ config,
+ verbose=False):
+ ''' Constructor for OCVolume '''
+ super(OCRoute, self).__init__(config.namespace, config.kubeconfig)
+ self.config = config
+ self.namespace = config.namespace
+ self._route = None
+
+ @property
+ def route(self):
+ ''' property function for route'''
+ if not self._route:
+ self.get()
+ return self._route
+
+ @route.setter
+ def route(self, data):
+ ''' setter function for route '''
+ self._route = data
+
+ def exists(self):
+ ''' return whether a route exists '''
+ if self.route:
+ return True
+
+ return False
+
+ def get(self):
+ '''return route information '''
+ result = self._get(self.kind, self.config.name)
+ if result['returncode'] == 0:
+ self.route = Route(content=result['results'][0])
+ elif 'routes \"%s\" not found' % self.config.name in result['stderr']:
+ result['returncode'] = 0
+ result['results'] = [{}]
+
+ return result
+
+ def delete(self):
+ '''delete the object'''
+ return self._delete(self.kind, self.config.name)
+
+ def create(self):
+ '''create the object'''
+ return self._create_from_content(self.config.name, self.config.data)
+
+ def update(self):
+ '''update the object'''
+ # need to update the tls information and the service name
+ return self._replace_content(self.kind, self.config.name, self.config.data)
+
+ def needs_update(self):
+ ''' verify an update is needed '''
+ skip = []
+ return not Utils.check_def_equal(self.config.data, self.route.yaml_dict, skip_keys=skip, debug=True)
+
+ # pylint: disable=too-many-return-statements,too-many-branches
+ @staticmethod
+ def run_ansible(params, files, check_mode=False):
+ ''' run the idempotent asnible code
+
+ params comes from the ansible portion for this module
+ files: a dictionary for the certificates
+ {'cert': {'path': '',
+ 'content': '',
+ 'value': ''
+ }
+ }
+ check_mode: does the module support check mode. (module.check_mode)
+ '''
+
+ rconfig = RouteConfig(params['name'],
+ params['namespace'],
+ params['kubeconfig'],
+ files['destcacert']['value'],
+ files['cacert']['value'],
+ files['cert']['value'],
+ files['key']['value'],
+ params['host'],
+ params['tls_termination'],
+ params['service_name'],
+ params['wildcard_policy'],
+ params['weight'])
+
+ oc_route = OCRoute(rconfig, verbose=params['debug'])
+
+ state = params['state']
+
+ api_rval = oc_route.get()
+
+ #####
+ # Get
+ #####
+ if state == 'list':
+ return {'changed': False,
+ 'results': api_rval['results'],
+ 'state': 'list'}
+
+ ########
+ # Delete
+ ########
+ if state == 'absent':
+ if oc_route.exists():
+
+ if check_mode:
+ return {'changed': False, 'msg': 'CHECK_MODE: Would have performed a delete.'} # noqa: E501
+
+ api_rval = oc_route.delete()
+
+ return {'changed': True, 'results': api_rval, 'state': "absent"} # noqa: E501
+ return {'changed': False, 'state': 'absent'}
+
+ if state == 'present':
+ ########
+ # Create
+ ########
+ if not oc_route.exists():
+
+ if check_mode:
+ return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'} # noqa: E501
+
+ # Create it here
+ api_rval = oc_route.create()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
+
+ # return the created object
+ api_rval = oc_route.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
+
+ return {'changed': True, 'results': api_rval, 'state': "present"} # noqa: E501
+
+ ########
+ # Update
+ ########
+ if oc_route.needs_update():
+
+ if check_mode:
+ return {'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.'} # noqa: E501
+
+ api_rval = oc_route.update()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
+
+ # return the created object
+ api_rval = oc_route.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
+
+ return {'changed': True, 'results': api_rval, 'state': "present"} # noqa: E501
+
+ return {'changed': False, 'results': api_rval, 'state': "present"}
+
+ # catch all
+ return {'failed': True, 'msg': "Unknown State passed"}
diff --git a/roles/lib_openshift/src/class/oc_version.py b/roles/lib_openshift/src/class/oc_version.py
new file mode 100644
index 000000000..7f8c721d8
--- /dev/null
+++ b/roles/lib_openshift/src/class/oc_version.py
@@ -0,0 +1,47 @@
+# flake8: noqa
+# pylint: skip-file
+
+
+# pylint: disable=too-many-instance-attributes
+class OCVersion(OpenShiftCLI):
+ ''' Class to wrap the oc command line tools '''
+ # pylint allows 5
+ # pylint: disable=too-many-arguments
+ def __init__(self,
+ config,
+ debug):
+ ''' Constructor for OCVersion '''
+ super(OCVersion, self).__init__(None, config)
+ self.debug = debug
+
+ def get(self):
+ '''get and return version information '''
+
+ results = {}
+
+ version_results = self._version()
+
+ if version_results['returncode'] == 0:
+ filtered_vers = Utils.filter_versions(version_results['results'])
+ custom_vers = Utils.add_custom_versions(filtered_vers)
+
+ results['returncode'] = version_results['returncode']
+ results.update(filtered_vers)
+ results.update(custom_vers)
+
+ return results
+
+ raise OpenShiftCLIError('Problem detecting openshift version.')
+
+ @staticmethod
+ def run_ansible(params):
+ '''run the idempotent ansible code'''
+ oc_version = OCVersion(params['kubeconfig'], params['debug'])
+
+ if params['state'] == 'list':
+
+ #pylint: disable=protected-access
+ result = oc_version.get()
+ return {'state': params['state'],
+ 'results': result,
+ 'changed': False}