All Projects → imjoey → pyhaproxy

imjoey / pyhaproxy

Licence: MIT license
Python library to parse haproxy configurations

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to pyhaproxy

read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+20%)
Mutual labels:  parse, configuration
Netcopa
Network Configuration Parser
Stars: ✭ 112 (+124%)
Mutual labels:  parse, configuration
habitat
Easily configure settings for Crystal projects
Stars: ✭ 73 (+46%)
Mutual labels:  configuration
Project
Infrastructure
Stars: ✭ 43 (-14%)
Mutual labels:  configuration
network tech
Cisco config syntax and snippets for Sublime Text
Stars: ✭ 82 (+64%)
Mutual labels:  configuration
kfc-plugins
Kotlin/JS Fast Configuration
Stars: ✭ 37 (-26%)
Mutual labels:  configuration
config
Config component, strictly typed
Stars: ✭ 14 (-72%)
Mutual labels:  configuration
mtgsqlive
MTGJSON build scripts to generate alternative data formats
Stars: ✭ 40 (-20%)
Mutual labels:  parse
tomlj
A Java parser for Tom's Obvious, Minimal Language (TOML).
Stars: ✭ 72 (+44%)
Mutual labels:  configuration
marc4js
A Node.js API for handling MARC
Stars: ✭ 35 (-30%)
Mutual labels:  parse
libdvbtee
dvbtee: a digital television streamer / parser / service information aggregator supporting various interfaces including telnet CLI & http control
Stars: ✭ 65 (+30%)
Mutual labels:  parse
dotfiles
🏡 There's no place like ~/
Stars: ✭ 59 (+18%)
Mutual labels:  configuration
Selenium-Foundation
Selenium Foundation is an automation framework designed to extend and enhance the capabilities provided by Selenium (WebDriver).
Stars: ✭ 51 (+2%)
Mutual labels:  configuration
nginx-conf
Nginx configuration
Stars: ✭ 18 (-64%)
Mutual labels:  configuration
envkey-node
EnvKey's official Node.js client library
Stars: ✭ 46 (-8%)
Mutual labels:  configuration
onion
Layer based configuration for golang
Stars: ✭ 104 (+108%)
Mutual labels:  configuration
CoSky
High-performance, low-cost microservice governance platform. Service Discovery and Configuration Service | 高性能、低成本微服务治理平台
Stars: ✭ 57 (+14%)
Mutual labels:  configuration
configuration-service
Configuration Service is a distributed configuration provider for .NET Core.
Stars: ✭ 62 (+24%)
Mutual labels:  configuration
parse-commit-message
(!! moved to tunnckoCore/opensource !! try `parse-commit-message@canary`) Parse, stringify or validate a commit messages that follows Conventional Commits Specification
Stars: ✭ 31 (-38%)
Mutual labels:  parse
zcolors
🌈 Z Colors uses your $LS_COLORS to generate a coherent theme for Git and your Zsh prompt, command line and completions.
Stars: ✭ 38 (-24%)
Mutual labels:  configuration

Pyhaproxy PyPi Build Status

It's a Python library to parse haproxy config file. Thanks to canopy, which I use for auto-generating Python codes by PEG grammar. But the 'Extension methods for node' feature in canopy seems broken, I always encounter the MRO errors when running parse function. So I modify the generated codes which mainly rename the TreeNode* to specified treenode name, eg: GlobalSection, GlobalHeader, BackendHeader, and also complement missing attributes.

Install

This project uses nose for unit testing, but with no more Python libraries dependencies for running.

  • Suppose that you have virtualenv installed, if not, please go here to install
  • Create a virutalenv and activate it,
$ virtualenv --no-site-packages pyhaproxy
$ source pyhaproxy/bin/activate
  • User pip (pip install pyhaproxy) or setuptools (easy_install pyhaproxy) to install it
  • Install nose dependency for unittest
(pyhaproxy)$ pip install -r requirements.txt

Example

Here is the simple example to show how to use it. See unittest file test.py for more usage details.

#!/usr/bin/env python
# -*- coding: utf8 -*-

from pyhaproxy.parse import Parser
from pyhaproxy.render import Render
import pyhaproxy.config as config


# Build the configuration instance by calling Parser('config_file').build_configuration()
cfg_parser = Parser('haproxy.cfg')
configuration = cfg_parser.build_configuration()

# Get the global section
print configuration.globall  # the `global` is keyword of Python, so name it `globall`
print configuration.globall.options()  # get the 'option ...' config lines
print configuration.globall.configs()  # get config lines except 'option ...' ones


# Get all the frontend sections
frontend_sections = configuration.frontends

# Get frontend sections specifics
for fe_section in frontend_sections:
    # Get the name, host, port of the frontend section
    print fe_section.name, fe_section.host, fe_section.port
    print fe_section.options()
    print fe_section.configs()


# Find the frontend by name
the_fe_section = configuration.frontend(the_fe_section_name)

'''To get other sections is ditto.
'''


# Operates the ACL in a frontend, it much likes operating a list

#   Get all the ACLs defined in the frontend section
acls = the_fe_section.acls()   # return list(config.Acl)

#   Find the specified ACL
acl_instance = the_fe_section.acl(the_acl_name)   # return config.Acl

#   Modify existing ACL
acl_instance.value = 'hdr(host) -i modified.example.com'

#   Append the ACL into the frontend section
#       for version <= 0.2.4
the_fe_section.acls().append(config.Acl(acl_name, acl_value))
#       for version > 0.2.4
the_fe_section.add_acl(config.Acl(acl_name, acl_value))        

#   Remove ACL
#       for version <= 0.2.4
acl_instance = the_fe_section.acl(the_acl_name)
the_fe_section.acls().remove(acl_instance)
#       for version > 0.2.4
the_fe_section.remove_acl(the_acl_name)           


# Operates the use_backend / default_backend configs in a frontend

#   Get all the backend configs
usebackends = the_fe_section.usebackends()  # return list(config.UseBackend)
for usebe in usebackends:
    # Get the using backend name, operator, condition
    print usebe.backend_name, usebe.operator, usebe.backend_condition
    # Determine if it's `default_backend` line
    print usebe.is_default
#   Add a new `use_backend` or `default_backend` line
#       for version <= 0.2.4
the_fe_section.usebackends().append(config.UseBackend(backend_name, operator, backend_condition, is_default))
#       for version > 0.2.4
the_fe_section.add_usebackend(config.UseBackend(backend_name, operator, backend_condition, is_default))


# Operates the Server in a backend
the_be_section = configuration.backend(the_be_section_name)
#   Get all the Server lines in backend section
servers = the_be_section.servers()  # return list(config.Server)
#   Find the specified Server
the_server = the_be_section.server(the_server_name)  # return config.Server
#   Get the Server name, host, port
print the_server.name, the_server.host, the_server.port
#   Get the Server attributes, for line: `server web_server_1 10.1.1.2:80 cookie 1 check inter 2000 rise 3`
print the_server.attributes  # it's is ['cookie', 1, 'check', 'inter', 2000, 'rise', 3]
#   Remove the Server by name
#       for version <= 0.2.4
the_be_section.servers().remove(the_server)
#       for version > 0.2.4
the_be_section.remove_server(server_name)


# Render out to the cfg file
cfg_render = Render(configuration)
cfg_render.dumps_to('./hatest.cfg')  # you will see hatest.cfg which is same to the `haproxy.cfg` parsed previously

Finished

  • Parse global section
  • Parse frontend sections
  • Parse bind config lines
  • Parse backend sections
  • Parse defaults sections
  • Parse userlist sections
  • Parse listen sections
  • Parse acl config lines
  • Parse use_backend and default_backend config lines
  • Render global section
  • Render frontend section
  • Render backend sections
  • Render defaults sections
  • Render userlist sections
  • Render listen sections

TODO

  • Link backend with frontend by acl

Unittest

Use nose unit test framework

(pyhaproxy)$ nosetests -sv test.py

Thanks

  • Inspired by @subakva 's haproxy-tools
  • Use canopy of @jcoglan for PEG parsing and Python code auto-generating
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].