コード例 #1
0
 async def on_asterisk_event(self, manager, event):
     event = dict(event)
     trace_events = __opts__.get('ami_trace_events')
     if trace_events:
         if isinstance(trace_events, bool):
             log.info('AMI event: %s', json.dumps(event, indent=2))
         elif isinstance(trace_events,
                         list) and event['Event'] in trace_events:
             log.info('AMI event: %s', json.dumps(event, indent=2))
     # Inject system name in every message
     event['SystemName'] = __grains__['id']
     # Send event to Salt's event map
     __salt__['event.fire'](event, 'AMI/{}'.format(event['Event']))
コード例 #2
0
    def on_message(self, message):
        """Listens for a "websocket client ready" message.
        Once that message is received an asynchronous job
        is stated that yields messages to the client.
        These messages make up salt's
        "real time" event stream.
        """
        logger.debug('Got websocket message {0}'.format(message))
        if message == 'websocket client ready':
            if self.connected:
                # TBD: Add ability to run commands in this branch
                logger.debug('Websocket already connected, returning')
                return

            self.connected = True

            while True:
                try:
                    event = yield self.application.event_listener.get_event(
                        self)
                    self.write_message(json.dumps(event))
                except Exception as err:
                    logger.info(
                        'Error! Ending server side websocket connection. Reason = {0}'
                        .format(str(err)))
                    break

            self.close()
        else:
            # TBD: Add logic to run salt commands here
            pass
コード例 #3
0
ファイル: openvswitch.py プロジェクト: morinap/salt-1
def db_set(table, record, column, value, if_exists=False):
    '''
    Sets a column's value for a specific record.

    Args:
        table: A string - name of the database table.
        record: A string - identifier of the record.
        column: A string - name of the column.
        value: A string - the value to be set
        if_exists: A boolean - if True, it is not an error if the record does
            not exist.

    Returns:
        None on success and an error message on failure.

    CLI Example:
    .. code-block:: bash

       salt '*' openvswitch.db_set Interface br0 mac 02:03:04:05:06:07
    '''
    cmd = ['ovs-vsctl']
    if if_exists:
        cmd += ['--if-exists']
    cmd += ['set', table, record, '{0}={1}'.format(column, json.dumps(value))]
    result = __salt__['cmd.run_all'](cmd)
    if result['retcode'] != 0:
        return result['stderr']
    else:
        return None
コード例 #4
0
ファイル: ssdp.py プロジェクト: saorisakura/salt
    def datagram_received(self, data, addr):
        '''
        On datagram receive.

        :param data:
        :param addr:
        :return:
        '''
        message = data.decode()
        if message.startswith(self.signature):
            try:
                timestamp = float(message[len(self.signature):])
            except TypeError:
                self.log.debug('Received invalid timestamp in package from {}'.format("{}:{}".format(*addr)))
                if self.disable_hidden:
                    self._sendto('{0}:E:{1}'.format(self.signature, 'Invalid timestamp'), addr)
                return

            if datetime.datetime.fromtimestamp(timestamp) < (datetime.datetime.now() - datetime.timedelta(seconds=20)):
                if self.disable_hidden:
                    self._sendto('{0}:E:{1}'.format(self.signature, 'Timestamp is too old'), addr)
                self.log.debug('Received outdated package from {}'.format("{}:{}".format(*addr)))
                return

            self.log.debug('Received "{}" from {}'.format(message, "{}:{}".format(*addr)))
            self._sendto('{0}:@:{1}'.format(self.signature, json.dumps(self.answer)), addr)
        else:
            if self.disable_hidden:
                self._sendto('{0}:E:{1}'.format(self.signature, 'Invalid packet signature').encode(), addr)
            self.log.debug('Received bad signature from {}:{}'.format(*addr))
コード例 #5
0
ファイル: zabbix_host.py プロジェクト: morinap/salt-1
    def _interface_format(interfaces_data):
        '''
        Formats interfaces from SLS file into valid JSON usable for zabbix API.
        Completes JSON with default values.

        :param interfaces_data: list of interfaces data from SLS file

        '''

        if not interfaces_data:
            return list()

        interface_attrs = ('ip', 'dns', 'main', 'type', 'useip', 'port')
        interfaces_json = loads(dumps(interfaces_data))
        interfaces_dict = dict()

        for interface in interfaces_json:
            for intf in interface:
                intf_name = intf
                interfaces_dict[intf_name] = dict()
                for intf_val in interface[intf]:
                    for key, value in intf_val.items():
                        if key in interface_attrs:
                            interfaces_dict[intf_name][key] = value

        interfaces_list = list()
        interface_ports = {
            'agent': ['1', '10050'],
            'snmp': ['2', '161'],
            'ipmi': ['3', '623'],
            'jmx': ['4', '12345']
        }

        for key, value in interfaces_dict.items():
            # Load interface values or default values
            interface_type = interface_ports[value['type'].lower()][0]
            main = '1' if six.text_type(value.get(
                'main', 'true')).lower() == 'true' else '0'
            useip = '1' if six.text_type(value.get(
                'useip', 'true')).lower() == 'true' else '0'
            interface_ip = value.get('ip', '')
            dns = value.get('dns', key)
            port = six.text_type(
                value.get('port', interface_ports[value['type'].lower()][1]))

            interfaces_list.append({
                'type': interface_type,
                'main': main,
                'useip': useip,
                'ip': interface_ip,
                'dns': dns,
                'port': port
            })

        interfaces_list = interfaces_list
        interfaces_list_sorted = sorted(interfaces_list,
                                        key=lambda k: k['main'],
                                        reverse=True)

        return interfaces_list_sorted
コード例 #6
0
def get_banned():
    """
    Get banned IP addresses.

    CLI Example: salt asterisk asterisk.get_banned
    """
    result = []
    data = ipset_list('blacklist')
    lines = data.split('\n')
    for line in lines:
        # Try IP style
        found = RE_IPSET_ENTRY.search(line)
        if found:
            address, timeout, packets, bytes, comment = found.group(1), \
                found.group(2), found.group(3), \
                found.group(4), found.group(5)
            result.append({
                'address': found.group(1),
                'timeout': int(found.group(2)),
                'packets': int(found.group(3)),
                'bytes': int(found.group(4)),
                'comment': found.group(5)
            })
    log.debug('Banned entries: {}'.format(json.dumps(
        ['{}: {}'.format(k['address'], k['comment']) for k in result],
        indent=2)))
    return result
コード例 #7
0
ファイル: saltcheck.py プロジェクト: yuriks/salt
 def _load_file_salt_rendered(self, filepath):
     '''
     loads in one test file
     '''
     # use the salt renderer module to interpret jinja and etc
     tests = _render_file(filepath)
     # use json as a convenient way to convert the OrderedDicts from salt renderer
     mydict = loads(dumps(tests), object_pairs_hook=OrderedDict)
     for key, value in mydict.items():
         self.test_dict[key] = value
     return
コード例 #8
0
ファイル: zabbix_user.py プロジェクト: morinap/salt-1
    def _media_format(medias_data):
        '''
        Formats medias from SLS file into valid JSON usable for zabbix API.
        Completes JSON with default values.

        :param medias_data: list of media data from SLS file

        '''
        if not medias_data:
            return list()
        medias_json = loads(dumps(medias_data))
        medias_attr = ('active', 'mediatype', 'period', 'severity', 'sendto')
        media_type = {'mail': 1, 'jabber': 2, 'sms': 3}
        media_severities = ('D', 'H', 'A', 'W', 'I', 'N')

        medias_dict = dict()
        for media in medias_json:
            for med in media:
                medias_dict[med] = dict()
                for medattr in media[med]:
                    for key, value in medattr.items():
                        if key in medias_attr:
                            medias_dict[med][key] = value

        medias_list = list()
        for key, value in medias_dict.items():
            # Load media values or default values
            active = '0' if six.text_type(value.get(
                'active', 'true')).lower() == 'true' else '1'
            mediatype_sls = six.text_type(value.get('mediatype',
                                                    'mail')).lower()
            mediatypeid = six.text_type(media_type.get(mediatype_sls, 1))
            period = value.get('period', '1-7,00:00-24:00')
            sendto = value.get('sendto', key)

            severity_sls = value.get('severity', 'HD')
            severity_bin = six.text_type()
            for sev in media_severities:
                if sev in severity_sls:
                    severity_bin += '1'
                else:
                    severity_bin += '0'
            severity = six.text_type(int(severity_bin, 2))

            medias_list.append({
                'active': active,
                'mediatypeid': mediatypeid,
                'period': period,
                'sendto': sendto,
                'severity': severity
            })
        return medias_list
コード例 #9
0
def _ordered_dict_to_dict(probes):
    '''Mandatory to be dict type in order to be used in the NAPALM Jinja template.'''

    return loads(dumps(probes))
コード例 #10
0
ファイル: saltcheck.py プロジェクト: yuriks/salt
    def run_test(self, test_dict):
        '''
        Run a single saltcheck test
        '''
        start = time.time()
        global_output_details = __salt__['config.get']('saltcheck_output_details', False)
        output_details = test_dict.get('output_details', global_output_details)
        if self.__is_valid_test(test_dict):
            skip = test_dict.get('skip', False)
            if skip:
                return {'status': 'Skip', 'duration': 0.0}
            mod_and_func = test_dict['module_and_function']
            assertion_section = test_dict.get('assertion_section', None)
            assertion_section_delimiter = test_dict.get('assertion_section_delimiter', DEFAULT_TARGET_DELIM)
            args = test_dict.get('args', None)
            kwargs = test_dict.get('kwargs', None)
            pillar_data = test_dict.get('pillar_data', test_dict.get('pillar-data', None))
            if pillar_data:
                if not kwargs:
                    kwargs = {}
                kwargs['pillar'] = pillar_data
            else:
                # make sure we clean pillar from previous test
                if kwargs:
                    kwargs.pop('pillar', None)

            if mod_and_func in ["saltcheck.state_apply"]:
                assertion = "assertNotEmpty"
            else:
                assertion = test_dict['assertion']
            expected_return = test_dict.get('expected_return', test_dict.get('expected-return', None))
            assert_print_result = test_dict.get('print_result', True)

            actual_return = self._call_salt_command(mod_and_func,
                                                    args,
                                                    kwargs,
                                                    assertion_section,
                                                    assertion_section_delimiter)
            if assertion not in ["assertIn", "assertNotIn", "assertEmpty", "assertNotEmpty",
                                 "assertTrue", "assertFalse"]:
                expected_return = self._cast_expected_to_returned_type(expected_return, actual_return)
            if assertion == "assertEqual":
                assertion_desc = "=="
                value = self.__assert_equal(expected_return, actual_return, assert_print_result)
            elif assertion == "assertNotEqual":
                assertion_desc = "!="
                value = self.__assert_not_equal(expected_return, actual_return, assert_print_result)
            elif assertion == "assertTrue":
                assertion_desc = "True is"
                value = self.__assert_true(actual_return)
            elif assertion == "assertFalse":
                assertion_desc = "False is"
                value = self.__assert_false(actual_return)
            elif assertion == "assertIn":
                assertion_desc = "IN"
                value = self.__assert_in(expected_return, actual_return, assert_print_result)
            elif assertion == "assertNotIn":
                assertion_desc = "NOT IN"
                value = self.__assert_not_in(expected_return, actual_return, assert_print_result)
            elif assertion == "assertGreater":
                assertion_desc = ">"
                value = self.__assert_greater(expected_return, actual_return)
            elif assertion == "assertGreaterEqual":
                assertion_desc = ">="
                value = self.__assert_greater_equal(expected_return, actual_return)
            elif assertion == "assertLess":
                assertion_desc = "<"
                value = self.__assert_less(expected_return, actual_return)
            elif assertion == "assertLessEqual":
                assertion_desc = "<="
                value = self.__assert_less_equal(expected_return, actual_return)
            elif assertion == "assertEmpty":
                assertion_desc = "IS EMPTY"
                value = self.__assert_empty(actual_return)
            elif assertion == "assertNotEmpty":
                assertion_desc = "IS NOT EMPTY"
                value = self.__assert_not_empty(actual_return)
            else:
                value = "Fail - bad assertion"
        else:
            value = "Fail - invalid test"
        end = time.time()
        result = {}
        result['status'] = value

        if output_details:
            if assertion_section:
                assertion_section_repr_title = '.{0}'.format('assertion_section')
                assertion_section_repr_value = '.{0}'.format(assertion_section)
            else:
                assertion_section_repr_title = ''
                assertion_section_repr_value = ''
            result['module.function [args]{0}'.format(
                assertion_section_repr_title
            )] = '{0} {1}{2}'.format(
                mod_and_func,
                dumps(args),
                assertion_section_repr_value,
            )
            result['saltcheck assertion'] = '{0}{1} {2}'.format(
                ('' if expected_return is None else '{0} '.format(expected_return)),
                assertion_desc,
                ('hidden' if not assert_print_result else actual_return)
            )

        result['duration'] = round(end - start, 4)
        return result
コード例 #11
0
def _ordered_dict_to_dict(probes):

    '''.'''

    return loads(dumps(probes))
コード例 #12
0
    def _run_assertions(
        self,
        mod_and_func,
        args,
        data,
        module_output,
        output_details,
        assert_print_result,
    ):
        """
        Run assertion against input
        """
        value = {}

        assertion_section = data.get("assertion_section", None)
        assertion_section_delimiter = data.get("assertion_section_delimiter",
                                               DEFAULT_TARGET_DELIM)

        if assertion_section:
            module_output = salt.utils.data.traverse_dict_and_list(
                module_output,
                assertion_section,
                default=False,
                delimiter=assertion_section_delimiter,
            )

        if mod_and_func in ["saltcheck.state_apply"]:
            assertion = "assertNotEmpty"
        else:
            assertion = data["assertion"]
        expected_return = data.get("expected_return",
                                   data.get("expected-return", None))

        if assertion not in [
                "assertIn",
                "assertNotIn",
                "assertEmpty",
                "assertNotEmpty",
                "assertTrue",
                "assertFalse",
        ]:
            expected_return = self._cast_expected_to_returned_type(
                expected_return, module_output)
        if assertion == "assertEqual":
            assertion_desc = "=="
            value["status"] = self.__assert_equal(expected_return,
                                                  module_output,
                                                  assert_print_result)
        elif assertion == "assertNotEqual":
            assertion_desc = "!="
            value["status"] = self.__assert_not_equal(expected_return,
                                                      module_output,
                                                      assert_print_result)
        elif assertion == "assertTrue":
            assertion_desc = "True is"
            value["status"] = self.__assert_true(module_output)
        elif assertion == "assertFalse":
            assertion_desc = "False is"
            value["status"] = self.__assert_false(module_output)
        elif assertion == "assertIn":
            assertion_desc = "IN"
            value["status"] = self.__assert_in(expected_return, module_output,
                                               assert_print_result)
        elif assertion == "assertNotIn":
            assertion_desc = "NOT IN"
            value["status"] = self.__assert_not_in(expected_return,
                                                   module_output,
                                                   assert_print_result)
        elif assertion == "assertGreater":
            assertion_desc = ">"
            value["status"] = self.__assert_greater(expected_return,
                                                    module_output)
        elif assertion == "assertGreaterEqual":
            assertion_desc = ">="
            value["status"] = self.__assert_greater_equal(
                expected_return, module_output)
        elif assertion == "assertLess":
            assertion_desc = "<"
            value["status"] = self.__assert_less(expected_return,
                                                 module_output)
        elif assertion == "assertLessEqual":
            assertion_desc = "<="
            value["status"] = self.__assert_less_equal(expected_return,
                                                       module_output)
        elif assertion == "assertEmpty":
            assertion_desc = "IS EMPTY"
            value["status"] = self.__assert_empty(module_output)
        elif assertion == "assertNotEmpty":
            assertion_desc = "IS NOT EMPTY"
            value["status"] = self.__assert_not_empty(module_output)
        else:
            value["status"] = "Fail - bad assertion"

        if output_details:
            if assertion_section:
                assertion_section_repr_title = " {}".format(
                    "assertion_section")
                assertion_section_repr_value = " {}".format(assertion_section)
            else:
                assertion_section_repr_title = ""
                assertion_section_repr_value = ""
            value["module.function [args]{}".format(
                assertion_section_repr_title)] = "{} {}{}".format(
                    mod_and_func,
                    dumps(args),
                    assertion_section_repr_value,
                )
            value["saltcheck assertion"] = "{}{} {}".format(
                ("" if expected_return is None else
                 "{} ".format(expected_return)),
                assertion_desc,
                ("hidden" if not assert_print_result else module_output),
            )

        return value
コード例 #13
0
    def run_test(self, test_dict):
        """
        Run a single saltcheck test
        """
        start = time.time()
        global_output_details = __salt__["config.get"](
            "saltcheck_output_details", False
        )
        output_details = test_dict.get("output_details", global_output_details)
        if self.__is_valid_test(test_dict):
            skip = test_dict.get("skip", False)
            if skip:
                return {"status": "Skip", "duration": 0.0}
            mod_and_func = test_dict["module_and_function"]
            assertion_section = test_dict.get("assertion_section", None)
            assertion_section_delimiter = test_dict.get(
                "assertion_section_delimiter", DEFAULT_TARGET_DELIM
            )
            args = test_dict.get("args", None)
            kwargs = test_dict.get("kwargs", None)
            pillar_data = test_dict.get(
                "pillar_data", test_dict.get("pillar-data", None)
            )
            if pillar_data:
                if not kwargs:
                    kwargs = {}
                kwargs["pillar"] = pillar_data
            else:
                # make sure we clean pillar from previous test
                if kwargs:
                    kwargs.pop("pillar", None)

            if mod_and_func in ["saltcheck.state_apply"]:
                assertion = "assertNotEmpty"
            else:
                assertion = test_dict["assertion"]
            expected_return = test_dict.get(
                "expected_return", test_dict.get("expected-return", None)
            )
            assert_print_result = test_dict.get("print_result", True)

            actual_return = self._call_salt_command(
                mod_and_func,
                args,
                kwargs,
                assertion_section,
                assertion_section_delimiter,
            )
            if assertion not in [
                "assertIn",
                "assertNotIn",
                "assertEmpty",
                "assertNotEmpty",
                "assertTrue",
                "assertFalse",
            ]:
                expected_return = self._cast_expected_to_returned_type(
                    expected_return, actual_return
                )
            if assertion == "assertEqual":
                assertion_desc = "=="
                value = self.__assert_equal(
                    expected_return, actual_return, assert_print_result
                )
            elif assertion == "assertNotEqual":
                assertion_desc = "!="
                value = self.__assert_not_equal(
                    expected_return, actual_return, assert_print_result
                )
            elif assertion == "assertTrue":
                assertion_desc = "True is"
                value = self.__assert_true(actual_return)
            elif assertion == "assertFalse":
                assertion_desc = "False is"
                value = self.__assert_false(actual_return)
            elif assertion == "assertIn":
                assertion_desc = "IN"
                value = self.__assert_in(
                    expected_return, actual_return, assert_print_result
                )
            elif assertion == "assertNotIn":
                assertion_desc = "NOT IN"
                value = self.__assert_not_in(
                    expected_return, actual_return, assert_print_result
                )
            elif assertion == "assertGreater":
                assertion_desc = ">"
                value = self.__assert_greater(expected_return, actual_return)
            elif assertion == "assertGreaterEqual":
                assertion_desc = ">="
                value = self.__assert_greater_equal(expected_return, actual_return)
            elif assertion == "assertLess":
                assertion_desc = "<"
                value = self.__assert_less(expected_return, actual_return)
            elif assertion == "assertLessEqual":
                assertion_desc = "<="
                value = self.__assert_less_equal(expected_return, actual_return)
            elif assertion == "assertEmpty":
                assertion_desc = "IS EMPTY"
                value = self.__assert_empty(actual_return)
            elif assertion == "assertNotEmpty":
                assertion_desc = "IS NOT EMPTY"
                value = self.__assert_not_empty(actual_return)
            else:
                value = "Fail - bad assertion"
        else:
            value = "Fail - invalid test"
        end = time.time()
        result = {}
        result["status"] = value

        if output_details:
            if assertion_section:
                assertion_section_repr_title = ".{0}".format("assertion_section")
                assertion_section_repr_value = ".{0}".format(assertion_section)
            else:
                assertion_section_repr_title = ""
                assertion_section_repr_value = ""
            result[
                "module.function [args]{0}".format(assertion_section_repr_title)
            ] = "{0} {1}{2}".format(
                mod_and_func, dumps(args), assertion_section_repr_value,
            )
            result["saltcheck assertion"] = "{0}{1} {2}".format(
                ("" if expected_return is None else "{0} ".format(expected_return)),
                assertion_desc,
                ("hidden" if not assert_print_result else actual_return),
            )

        result["duration"] = round(end - start, 4)
        return result
コード例 #14
0
def _ordered_dict_to_dict(config):
    '''
    Forced the datatype to dict, in case OrderedDict is used.
    '''

    return loads(dumps(config))