コード例 #1
0
ファイル: haproxy.py プロジェクト: jrassier/haproxyadmin
    def get_map(self, mapid, value):
        """Lookup the value in the map.

        :param mapid: map id or a file.
        :type mapid: ``integer`` or a file path passed as ``string``
        :param value: value to lookup.
        :type value: ``string``
        :return: matching patterns associated with map.
        :rtype: ``string``

        Usage::

          >>> from haproxyadmin import haproxy
          >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy')
          >>> hap.show_map(0)
          ['0x1a78980 11 new2', '0x1b15c00 22 0']
          >>> hap.get_map(0, '11')
          'type=str, case=sensitive, found=yes, idx=tree, key="11", value="new2", type="str"'
          >>> hap.get_map(0, '10')
          'type=str, case=sensitive, found=no'
        """
        if isint(mapid):
            cmd = "get map #{} {}".format(mapid, value)
        elif os.path.isfile(mapid):
            cmd = "get map {} {}".format(mapid, value)
        else:
            raise ValueError("Invalid input")

        get_results = cmd_across_all_procs(self._hap_processes, 'command', cmd)
        get_info_proc1 = get_results[0][1]
        if not check_output(get_info_proc1):
            raise CommandFailed(get_info_proc1[0])

        return get_info_proc1
コード例 #2
0
def check_command(results):
    """Check if command was successfully executed.

    After a command is executed. We care about the following cases:

        * The same output is returned by all processes
        * If output matches to a list of outputs which indicate that
          command was valid

    :param results: a list of tuples with 2 elements.

          #. process number of HAProxy
          #. message returned by HAProxy

    :type results: ``list``
    :return: ``True`` if command was successfully executed otherwise ``False``.
    :rtype: ``bool``
    :raise: :class:`.MultipleCommandResults` when output differers.
    """
    if elements_of_list_same([msg[1] for msg in results]):
        msg = results[0][1]
        if msg in SUCCESS_OUTPUT_STRINGS:
            return True
        else:
            raise CommandFailed(msg)
    else:
        raise MultipleCommandResults(results)
コード例 #3
0
ファイル: haproxy.py プロジェクト: crovner/haproxyadmin
    def show_acl(self, aclid=None):
        """Dump info about acls.

        Without argument, the list of all available acls is returned.
        If a aclid is specified, its contents are dumped.

        :param aclid: (optional) acl id or a file
        :type aclid: ``integer`` or a file path passed as ``string``
        :return: a list with the acls
        :rtype: ``list``

        Usage::

          >>> from haproxyadmin import haproxy
          >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy')
          >>> hap.show_acl(aclid=6)
          ['0x1d09730 ver%3A27%3Bvar%3A0']
          >>> hap.show_acl()
          ['# id (file) description',
          "1 () acl 'ssl_fc' file '/etc/haproxy/haproxy.cfg' line 83",
          "2 () acl 'src' file '/etc/haproxy/haproxy.cfg' line 95",
          "3 () acl 'path_beg' file '/etc/haproxy/haproxy.cfg' line 97",
          ]
        """
        if aclid is not None:
            if isint(aclid):
                cmd = "show acl #{}".format(aclid)
            else:
                cmd = "show acl {}".format(aclid)
        else:
            cmd = "show acl"

        acl_info = cmd_across_all_procs(self._hap_processes,
                                        'command',
                                        cmd,
                                        full_output=True)
        # ACL can't be different per process thus we only return the acl
        # content found in 1st process.
        acl_info_proc1 = acl_info[0][1]

        if not check_output(acl_info_proc1):
            raise CommandFailed(acl_info_proc1[0])

        if len(acl_info_proc1) == 1 and not acl_info_proc1[0]:
            return []
        else:
            return acl_info_proc1
コード例 #4
0
ファイル: haproxy.py プロジェクト: crovner/haproxyadmin
    def show_map(self, mapid=None):
        """Dump info about maps.

        Without argument, the list of all available maps is returned.
        If a mapid is specified, its contents are dumped.

        :param mapid: (optional) map id or a file.
        :type mapid: ``integer`` or a file path passed as ``string``
        :return: a list with the maps.
        :rtype: ``list``

        Usage::

          >>> from haproxyadmin import haproxy
          >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy')
          >>> hap.show_map()
          ['# id (file) description',
           "0 (/etc/haproxy/v-m1-bk) pattern loaded ...... line 82",
           ]
          >>> hap.show_map(mapid=0)
          ['0x1a78ab0 0 www.foo.com-0', '0x1a78b20 1 www.foo.com-1']
        """
        if mapid is not None:
            if isint(mapid):
                cmd = "show map #{}".format(mapid)
            else:
                cmd = "show map {}".format(mapid)
        else:
            cmd = "show map"
        map_info = cmd_across_all_procs(self._hap_processes,
                                        'command',
                                        cmd,
                                        full_output=True)
        # map can't be different per process thus we only return the map
        # content found in 1st process.
        map_info_proc1 = map_info[0][1]

        if not check_output(map_info_proc1):
            raise CommandFailed(map_info_proc1[0])

        if len(map_info_proc1) == 1 and not map_info_proc1[0]:
            return []
        else:
            return map_info_proc1
コード例 #5
0
def check_command_addr_port(change_type, results):
    """Check if command to set port or address was successfully executed.

    Unfortunately, haproxy returns many different combinations of output when
    we change the address or the port of the server and trying to determine
    if address or port was successfully changed isn't that trivial.

    So, after we change address or port, we check if the same output is
    returned by all processes and we also check if a collection of specific
    strings are part of the output. This is a suboptimal solution, but I
    couldn't come up with something more elegant.

    :param change_type: either ``addr`` or ``port``
    :type change_type: ``string``
    :param results: a list of tuples with 2 elements.

          #. process number of HAProxy
          #. message returned by HAProxy
    :type results: ``list``
    :return: ``True`` if command was successfully executed otherwise ``False``.
    :rtype: ``bool``
    :raise: :class:`.MultipleCommandResults`, :class:`.CommandFailed` and
      :class:`ValueError`.
    """
    if change_type == 'addr':
        _match = SUCCESS_STRING_ADDRESS
    elif change_type == 'port':
        _match = SUCCESS_STRING_PORT
    else:
        raise ValueError('invalid value for change_type')

    if elements_of_list_same([msg[1] for msg in results]):
        msg = results[0][1]
        if re.match(_match, msg):
            return True
        else:
            raise CommandFailed(msg)
    else:
        raise MultipleCommandResults(results)