Beispiel #1
0
def _parse_config(conf, slot=None):
    ret = cStringIO()
    if isinstance(conf, str):
        if slot:
            print('{0} {1}'.format(slot, conf), file=ret, end='')
        else:
            print('{0}'.format(conf), file=ret, end='')
    elif isinstance(conf, list):
        for value in conf:
            print(_parse_config(value, str(slot)), file=ret)
    elif isinstance(conf, dict):
        print('<{0} {1}>'.format(
            slot,
            _parse_config(conf['this'])),
            file=ret
        )
        del conf['this']
        for key, value in six.iteritems(conf):
            if isinstance(value, str):
                print('{0} {1}'.format(key, value), file=ret)
            elif isinstance(value, list):
                print(_parse_config(value, key), file=ret)
            elif isinstance(value, dict):
                print(_parse_config(value, key), file=ret)
        print('</{0}>'.format(slot), file=ret, end='')

    ret.seek(0)
    return ret.read()
Beispiel #2
0
def string_io(data=None):  # cStringIO can't handle unicode
    '''
    Pass data through to stringIO module and return result
    '''
    try:
        return cStringIO(bytes(data))
    except (UnicodeEncodeError, TypeError):
        return StringIO(data)
Beispiel #3
0
def string_io(data=None):  # cStringIO can't handle unicode
    '''
    Pass data through to stringIO module and return result
    '''
    try:
        return cStringIO(bytes(data))
    except (UnicodeEncodeError, TypeError):
        return StringIO(data)
Beispiel #4
0
def _parse_config(conf, slot=None):
    '''
    Recursively goes through config structure and builds final Apache configuration

    :param conf: defined config structure
    :param slot: name of section container if needed
    '''
    ret = cStringIO()
    if isinstance(conf, six.string_types):
        if slot:
            print('{0} {1}'.format(slot, conf), file=ret, end='')
        else:
            print('{0}'.format(conf), file=ret, end='')
    elif isinstance(conf, list):
        is_section = False
        for item in conf:
            if 'this' in item:
                is_section = True
                slot_this = six.text_type(item['this'])
        if is_section:
            print('<{0} {1}>'.format(slot, slot_this), file=ret)
            for item in conf:
                for key, val in item.items():
                    if key != 'this':
                        print(_parse_config(val, six.text_type(key)), file=ret)
            print('</{0}>'.format(slot), file=ret)
        else:
            for value in conf:
                print(_parse_config(value, six.text_type(slot)), file=ret)
    elif isinstance(conf, dict):
        try:
            print('<{0} {1}>'.format(slot, conf['this']), file=ret)
        except KeyError:
            raise SaltException(
                'Apache section container "<{0}>" expects attribute. '
                'Specify it using key "this".'.format(slot))
        for key, value in six.iteritems(conf):
            if key != 'this':
                if isinstance(value, six.string_types):
                    print('{0} {1}'.format(key, value), file=ret)
                elif isinstance(value, list):
                    print(_parse_config(value, key), file=ret)
                elif isinstance(value, dict):
                    print(_parse_config(value, key), file=ret)
        print('</{0}>'.format(slot), file=ret)

    ret.seek(0)
    return ret.read()
Beispiel #5
0
    def run_run_plus(self, fun, *arg, **kwargs):
        """
        Execute the runner function and return the return data and output in a dict
        """
        # Late import
        import salt.runner
        import salt.output

        ret = {"fun": fun}
        from_scratch = bool(kwargs.pop("__reload_config", False))
        # Have to create an empty dict and then update it, as the result from
        # self.get_config() is an ImmutableDict which cannot be updated.
        opts = {}
        opts.update(self.get_config("client_config",
                                    from_scratch=from_scratch))
        opts_arg = list(arg)

        if 'asynchronous' in kwargs:
            opts['async'] = True
            kwargs.pop('asynchronous')

        if kwargs:
            opts_arg.append({"__kwarg__": True})
            opts_arg[-1].update(kwargs)
        opts.update({"doc": False, "fun": fun, "arg": opts_arg})
        with RedirectStdStreams():
            runner = salt.runner.Runner(opts)
            ret["return"] = runner.run()
            try:
                ret["jid"] = runner.jid
            except AttributeError:
                ret["jid"] = None

        # Compile output
        # TODO: Support outputters other than nested
        opts["color"] = False
        opts["output_file"] = cStringIO()
        try:
            salt.output.display_output(ret["return"], opts=opts)
            ret["out"] = opts["output_file"].getvalue().splitlines()
        finally:
            opts["output_file"].close()

        log.debug("Result of run_run_plus for fun '%s' with arg '%s': %s", fun,
                  opts_arg, ret)
        return ret
Beispiel #6
0
    def run_run_plus(self, fun, *arg, **kwargs):
        """
        Execute the runner function and return the return data and output in a dict
        """
        output = kwargs.pop("_output", None)
        ret = {"fun": fun}

        # Late import
        import salt.config
        import salt.output
        import salt.runner

        opts = salt.config.client_config(self.get_config_file_path("master"))

        opts_arg = list(arg)
        if kwargs:
            opts_arg.append({"__kwarg__": True})
            opts_arg[-1].update(kwargs)

        opts.update({"doc": False, "fun": fun, "arg": opts_arg})
        with RedirectStdStreams():
            runner = salt.runner.Runner(opts)
            ret["return"] = runner.run()
            try:
                ret["jid"] = runner.jid
            except AttributeError:
                ret["jid"] = None

        # Compile output
        # TODO: Support outputters other than nested
        opts["color"] = False
        opts["output_file"] = cStringIO()
        try:
            salt.output.display_output(ret["return"], opts=opts, out=output)
            out = opts["output_file"].getvalue()
            if output is None:
                out = out.splitlines()
            elif output == "json":
                out = json.loads(out)
            ret["out"] = out
        finally:
            opts["output_file"].close()
        log.debug(
            "Result of run_run_plus for fun '%s' with arg '%s': %s", fun, opts_arg, ret
        )
        return ret
Beispiel #7
0
    def run_run_plus(self, fun, *arg, **kwargs):
        '''
        Execute the runner function and return the return data and output in a dict
        '''
        # Late import
        import salt.runner
        import salt.output
        ret = {'fun': fun}
        from_scratch = bool(kwargs.pop('__reload_config', False))
        # Have to create an empty dict and then update it, as the result from
        # self.get_config() is an ImmutableDict which cannot be updated.
        opts = {}
        opts.update(self.get_config('client_config',
                                    from_scratch=from_scratch))
        opts_arg = list(arg)

        if 'asynchronous' in kwargs:
            opts['async'] = True
            kwargs.pop('asynchronous')

        if kwargs:
            opts_arg.append({'__kwarg__': True})
            opts_arg[-1].update(kwargs)
        opts.update({'doc': False, 'fun': fun, 'arg': opts_arg})
        with RedirectStdStreams():
            runner = salt.runner.Runner(opts)
            ret['return'] = runner.run()
            try:
                ret['jid'] = runner.jid
            except AttributeError:
                ret['jid'] = None

        # Compile output
        # TODO: Support outputters other than nested
        opts['color'] = False
        opts['output_file'] = cStringIO()
        try:
            salt.output.display_output(ret['return'], opts=opts)
            ret['out'] = opts['output_file'].getvalue().splitlines()
        finally:
            opts['output_file'].close()

        log.debug('Result of run_run_plus for fun \'%s\' with arg \'%s\': %s',
                  fun, opts_arg, ret)
        return ret
Beispiel #8
0
    def run_run_plus(self, fun, *arg, **kwargs):
        """
        Execute the runner function and return the return data and output in a dict
        """
        ret = {"fun": fun}

        # Late import
        import salt.config
        import salt.output
        import salt.runner
        from salt.ext.six.moves import cStringIO

        opts = salt.config.master_config(self.get_config_file_path("master"))

        if 'asynchronous' in kwargs:
            opts['async'] = True
            kwargs.pop('asynchronous')

        opts_arg = list(arg)
        if kwargs:
            opts_arg.append({"__kwarg__": True})
            opts_arg[-1].update(kwargs)

        opts.update({"doc": False, "fun": fun, "arg": opts_arg})
        with RedirectStdStreams():
            runner = salt.runner.Runner(opts)
            ret["return"] = runner.run()
            try:
                ret["jid"] = runner.jid
            except AttributeError:
                ret["jid"] = None

        # Compile output
        # TODO: Support outputters other than nested
        opts["color"] = False
        opts["output_file"] = cStringIO()
        try:
            salt.output.display_output(ret["return"], opts=opts)
            ret["out"] = opts["output_file"].getvalue()
        finally:
            opts["output_file"].close()

        return ret
Beispiel #9
0
    def run_run_plus(self, fun, *arg, **kwargs):
        '''
        Execute the runner function and return the return data and output in a dict
        '''
        ret = {'fun': fun}

        # Late import
        import salt.config
        import salt.output
        import salt.runner
        from salt.ext.six.moves import cStringIO

        opts = salt.config.master_config(self.get_config_file_path('master'))

        if 'asynchronous' in kwargs:
            opts['async'] = True
            kwargs.pop('asynchronous')

        opts_arg = list(arg)
        if kwargs:
            opts_arg.append({'__kwarg__': True})
            opts_arg[-1].update(kwargs)

        opts.update({'doc': False, 'fun': fun, 'arg': opts_arg})
        with RedirectStdStreams():
            runner = salt.runner.Runner(opts)
            ret['return'] = runner.run()
            try:
                ret['jid'] = runner.jid
            except AttributeError:
                ret['jid'] = None

        # Compile output
        # TODO: Support outputters other than nested
        opts['color'] = False
        opts['output_file'] = cStringIO()
        try:
            salt.output.display_output(ret['return'], opts=opts)
            ret['out'] = opts['output_file'].getvalue()
        finally:
            opts['output_file'].close()

        return ret
Beispiel #10
0
    def run_run_plus(self, fun, *arg, **kwargs):
        '''
        Execute the runner function and return the return data and output in a dict
        '''
        ret = {'fun': fun}

        # Late import
        import salt.config
        import salt.output
        import salt.runner
        from salt.ext.six.moves import cStringIO

        opts = salt.config.master_config(
            self.get_config_file_path('master')
        )

        opts_arg = list(arg)
        if kwargs:
            opts_arg.append({'__kwarg__': True})
            opts_arg[-1].update(kwargs)

        opts.update({'doc': False, 'fun': fun, 'arg': opts_arg})
        with RedirectStdStreams():
            runner = salt.runner.Runner(opts)
            ret['return'] = runner.run()
            try:
                ret['jid'] = runner.jid
            except AttributeError:
                ret['jid'] = None

        # Compile output
        # TODO: Support outputters other than nested
        opts['color'] = False
        opts['output_file'] = cStringIO()
        try:
            salt.output.display_output(ret['return'], opts=opts)
            ret['out'] = opts['output_file'].getvalue()
        finally:
            opts['output_file'].close()

        return ret
Beispiel #11
0
def _parse_config(conf, slot=None):
    ret = cStringIO()
    if isinstance(conf, str):
        if slot:
            print('{0} {1}'.format(slot, conf), file=ret, end='')
        else:
            print('{0}'.format(conf), file=ret, end='')
    elif isinstance(conf, list):
        print('{0} {1}'.format(slot, ' '.join(conf)), file=ret, end='')
    elif isinstance(conf, dict):
        print('<{0} {1}>'.format(slot, _parse_config(conf['this'])), file=ret)
        del conf['this']
        for key, value in six.iteritems(conf):
            if isinstance(value, str):
                print('{0} {1}'.format(key, value), file=ret)
            elif isinstance(value, list):
                print(_parse_config(value, key), file=ret)
            elif isinstance(value, dict):
                print(_parse_config(value, key), file=ret)
        print('</{0}>'.format(slot), file=ret, end='')

    ret.seek(0)
    return ret.read()