예제 #1
0
    def run_ansible(params, check_mode=False):
        '''run the oc_obj module'''

        ocobj = OCList(params['namespace'],
                       params['selector'],
                       oc_binary=params['oc_binary'],
                       verbose=params['debug'],
                       field_selector=params['field_selector'])

        state = params['state']

        if state == 'present':
            ########
            # Create
            ########
            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}

            # Remove files
            if params['files'] and params['delete_after']:
                Utils.cleanup(params['files'])

            return {'changed': True, 'results': api_rval, 'state': state}

        # catch all
        return {'failed': True, 'msg': "Unknown State passed"}
예제 #2
0
    def create(self,
               files=None,
               contents=None,
               from_literal=None,
               cert=None,
               key=None,
               force=False):
        '''Create a secret '''
        cmd = ['create', 'secret']
        if self.type is not None:
            cmd.append(self.type)
        cmd.append(self.name)

        if from_literal is not None:
            cmd.extend(self.from_literal_to_params(from_literal))
        elif self.type == 'tls':
            certInput = '--cert=%s' % cert
            keyInput = '--key=%s' % key
            cmd.append(certInput)
            cmd.append(keyInput)
        else:
            if not files:
                files = Utils.create_tmp_files_from_contents(contents)
            secrets = ["--from-file=%s" % (sfile['path']) for sfile in files]
            cmd.extend(secrets)

        results = self.openshift_cmd(cmd)

        return results
예제 #3
0
    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

        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)
예제 #4
0
    def prep_secret(self, files=None, contents=None, force=False):
        ''' return what the secret would look like if created
            This is accomplished by passing -ojson.  This will most likely change in the future
        '''
        if not files:
            files = Utils.create_tmp_files_from_contents(contents)

        secrets = ["%s=%s" % (sfile['name'], sfile['path']) for sfile in files]
        cmd = ['-ojson', 'secrets', 'new', self.name]
        if self.type is not None:
            cmd.extend(["--type=%s" % (self.type)])
            if force:
                cmd.append('--confirm')
        cmd.extend(secrets)

        return self.openshift_cmd(cmd, output=True)
예제 #5
0
    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])

        # The purpose of this change is twofold:
        # - we need a check to only use the ruamel specific dumper if ruamel is loaded
        # - the dumper or the flow style change is needed so openshift is able to parse
        # the resulting yaml, at least until gopkg.in/yaml.v2 is updated
        if hasattr(yaml, 'RoundTripDumper'):
            content['data'] = yaml.dump(content['data'],
                                        Dumper=yaml.RoundTripDumper)
        else:
            content['data'] = yaml.safe_dump(content['data'],
                                             default_flow_style=False)

        content_file = Utils.create_tmp_files_from_contents(content)[0]

        return self._create(content_file['path'])
예제 #6
0
 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=self.verbose)
예제 #7
0
    def run_ansible(params, check_mode):
        ''' run the oc_image module'''

        ocimage = OCImage(params['namespace'],
                          params['registry_url'],
                          params['image_name'],
                          params['image_tag'],
                          kubeconfig=params['kubeconfig'],
                          verbose=params['debug'])

        state = params['state']

        api_rval = ocimage.get()

        #####
        # Get
        #####
        if state == 'list':
            if api_rval['returncode'] != 0:
                return {"failed": True, "msg": api_rval}
            return {
                "changed": False,
                "ansible_module_results": api_rval,
                "state": "list"
            }

        ########
        # Create
        ########
        if state == 'present':

            if not Utils.exists(api_rval['results'], params['image_name']):

                if check_mode:
                    return {
                        "changed": False,
                        "msg": 'CHECK_MODE: Would have performed a create'
                    }

                api_rval = ocimage.create(params['registry_url'],
                                          params['image_name'],
                                          params['image_tag'])

                if api_rval['returncode'] != 0:
                    return {"failed": True, "msg": api_rval}

                # return the newly created object
                api_rval = ocimage.get()

                if api_rval['returncode'] != 0:
                    return {"failed": True, "msg": api_rval}

                return {
                    "changed": True,
                    "ansible_module_results": api_rval,
                    "state": "present"
                }

            # image exists, no change
            return {
                "changed": False,
                "ansible_module_results": api_rval,
                "state": "present"
            }

        return {
            "failed": True,
            "changed": False,
            "msg": "Unknown state passed. {0}".format(state)
        }
예제 #8
0
    def run_ansible(params, check_mode):
        '''run the oc_secret module'''

        ocsecret = OCSecret(params['namespace'],
                            params['name'],
                            params['type'],
                            params['decode'],
                            oc_binary=params['oc_binary'],
                            verbose=params['debug'])

        state = params['state']

        api_rval = ocsecret.get()

        #####
        # Get
        #####
        if state == 'list':
            return {
                'changed': False,
                'ansible_module_results': api_rval,
                state: 'list'
            }

        if not params['name']:
            return {
                'failed': True,
                'msg': 'Please specify a name when state is absent|present.'
            }

        ########
        # 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': 'Would have performed a delete.'
                }

            api_rval = ocsecret.delete()
            return {
                'changed': True,
                'ansible_module_results': api_rval,
                'state': 'absent'
            }

        if state == 'present':
            if params['files']:
                files = params['files']
            elif params['contents']:
                files = Utils.create_tmp_files_from_contents(
                    params['contents'])
            else:
                files = [{'name': 'null', 'path': os.devnull}]

            ########
            # Create
            ########
            if not Utils.exists(api_rval['results'], params['name']):

                if check_mode:
                    return {
                        'changed': True,
                        'msg': 'Would have performed a create.'
                    }

                api_rval = ocsecret.create(files,
                                           params['contents'],
                                           params['from_literal'],
                                           params['cert'],
                                           params['key'],
                                           force=params['force'])

                # Remove files
                if files and params['delete_after']:
                    Utils.cleanup([ftmp['path'] for ftmp in files])

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                return {
                    'changed': True,
                    'ansible_module_results': api_rval,
                    'state': 'present'
                }

            ########
            # Update
            ########
            # if update is set to false, return
            update = params['update']
            if not update:
                return {
                    'changed': False,
                    'ansible_module_results': api_rval,
                    'state': state
                }

            secret = ocsecret.prep_secret(params['files'],
                                          params['contents'],
                                          force=params['force'])

            if secret['returncode'] != 0:
                return {'failed': True, 'msg': secret}

            if Utils.check_def_equal(secret['results'],
                                     api_rval['results'][0]):

                # Remove files
                if files and params['delete_after']:
                    Utils.cleanup([ftmp['path'] for ftmp in files])

                return {
                    'changed': False,
                    'ansible_module_results': secret['results'],
                    'state': 'present'
                }

            if check_mode:
                return {
                    'changed': True,
                    'msg': 'Would have performed an update.'
                }

            api_rval = ocsecret.update(files, force=params['force'])

            # Remove files
            if secret and params['delete_after']:
                Utils.cleanup([ftmp['path'] for ftmp in files])

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {
                'changed': True,
                'ansible_module_results': api_rval,
                'state': 'present'
            }

        return {
            'failed': True,
            'changed': False,
            'msg': 'Unknown state passed. %s' % state,
            'state': 'unknown'
        }
예제 #9
0
    def run_ansible(params, check_mode=False):
        '''run the oc_obj module'''

        ocobj = OCObject(params['kind'],
                         params['namespace'],
                         params['name'],
                         params['selector'],
                         oc_binary=params['oc_binary'],
                         verbose=params['debug'],
                         all_namespaces=params['all_namespaces'],
                         field_selector=params['field_selector'])

        state = params['state']

        api_rval = ocobj.get()

        #####
        # Get
        #####
        if state == 'list':
            if api_rval['returncode'] != 0:
                return {'changed': False, 'failed': True, 'msg': api_rval}
            return {'changed': False, 'results': api_rval, 'state': state}

        ########
        # Delete
        ########
        if state == 'absent':
            # verify it's not in our results
            if (params['name'] is not None or params['selector'] is not None) and \
               (len(api_rval['results']) == 0 or \
               (not api_rval['results'][0]) or \
               ('items' in api_rval['results'][0] and len(api_rval['results'][0]['items']) == 0)):
                return {'changed': False, 'state': state}

            if check_mode:
                return {
                    'changed': True,
                    'msg': 'CHECK_MODE: Would have performed a delete'
                }

            api_rval = ocobj.delete()

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {'changed': True, 'results': api_rval, 'state': state}

        # create/update: Must define a name beyond this point
        if not params['name']:
            return {
                'failed': True,
                'msg': 'Please specify a name when state is present.'
            }

        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': state}

            ########
            # Update
            ########
            # if update is set to false, return
            update = params['update']
            if not update:
                if params['files'] and params['delete_after']:
                    Utils.cleanup(params['files'])

                return {
                    'changed': False,
                    'results': api_rval['results'][0],
                    'state': state
                }

            # if a file path is passed, use it.
            needs_update = ocobj.needs_update(params['files'],
                                              params['content'])
            if not isinstance(needs_update, bool):
                return {'failed': True, 'msg': update}

            # No changes
            if not needs_update:
                if params['files'] and params['delete_after']:
                    Utils.cleanup(params['files'])

                return {
                    'changed': False,
                    'results': api_rval['results'][0],
                    'state': state
                }

            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': state}
예제 #10
0
    def run_ansible(params, check_mode):
        '''run the oc_configmap module'''

        oc_cm = OCConfigMap(params['name'],
                            params['from_file'],
                            params['from_literal'],
                            params['state'],
                            params['namespace'],
                            oc_binary=params['oc_binary'],
                            verbose=params['debug'])

        state = params['state']

        api_rval = oc_cm.get()

        if 'failed' in api_rval:
            return {'failed': True, 'msg': api_rval}

        #####
        # Get
        #####
        if state == 'list':
            return {'changed': False, 'results': api_rval, 'state': state}

        if not params['name']:
            return {'failed': True,
                    'msg': 'Please specify a name when state is absent|present.'}

        ########
        # 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 = oc_cm.delete()

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {'changed': True, 'results': api_rval, 'state': state}

        ########
        # Create
        ########
        if state == 'present':
            if not Utils.exists(api_rval['results'], params['name']):

                if check_mode:
                    return {'changed': True, 'msg': 'Would have performed a create.'}

                api_rval = oc_cm.create()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                api_rval = oc_cm.get()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                return {'changed': True, 'results': api_rval, 'state': state}

            ########
            # Update
            ########
            # if update is set to false, return
            update = params['update']
            if not update:
                return {'changed': False, 'results': api_rval, 'state': state}
            
            if oc_cm.needs_update():

                api_rval = oc_cm.update()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                api_rval = oc_cm.get()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                return {'changed': True, 'results': api_rval, 'state': state}

            return {'changed': False, 'results': api_rval, 'state': state}

        return {'failed': True, 'msg': 'Unknown state passed. {}'.format(state)}
예제 #11
0
 def needs_update(self):
     '''compare the current configmap with the proposed and return if they are equal'''
     return not Utils.check_def_equal(self.inc_configmap, self.configmap, debug=self.verbose)