haproxy_parser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/haproxy_parser.gemspec +27 -0
- data/lib/haproxy_parser/builders/backend.rb +52 -0
- data/lib/haproxy_parser/builders/base.rb +46 -0
- data/lib/haproxy_parser/builders/bind_parser.rb +26 -0
- data/lib/haproxy_parser/builders/frontend.rb +53 -0
- data/lib/haproxy_parser/builders/global.rb +26 -0
- data/lib/haproxy_parser/builders/server_parser.rb +57 -0
- data/lib/haproxy_parser/builders.rb +3 -0
- data/lib/haproxy_parser/config.rb +115 -0
- data/lib/haproxy_parser/digger.rb +23 -0
- data/lib/haproxy_parser/format_checker.rb +42 -0
- data/lib/haproxy_parser/line_parser.rb +17 -0
- data/lib/haproxy_parser/section_collector.rb +67 -0
- data/lib/haproxy_parser/sections/backend.rb +6 -0
- data/lib/haproxy_parser/sections/base.rb +45 -0
- data/lib/haproxy_parser/sections/default.rb +6 -0
- data/lib/haproxy_parser/sections/frontend.rb +6 -0
- data/lib/haproxy_parser/sections/global.rb +6 -0
- data/lib/haproxy_parser/server.rb +4 -0
- data/lib/haproxy_parser/version.rb +3 -0
- data/lib/haproxy_parser.rb +5 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e44a573347a42f5fdda76ec3a531198bd30e6945
|
4
|
+
data.tar.gz: c7270719b00ca109d384f0f09334ef9470714a1b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ebc03c7cf8f22e9a6413f41287efd9ab693b54c9de15f4adec9d8c1d1221630e313920ae2579932ba4a80c85a64519586209657f76224f57a1f18558bd1854e5
|
7
|
+
data.tar.gz: 7808574f1e5e2c18f52c2d5682c0bf198ef4f641b2747ea97bbc8f94fe98c3aa8ec59945257da727277a73c6c16a24a37c14c75cccf90915604bdd1a002a8f8e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at aueno@idcf.jp. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Akito Ueno
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# HaproxyParser
|
2
|
+
HaproxyParser could get some paramerters from haproxy.cfg.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'haproxy_parser'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install haproxy_parser
|
19
|
+
|
20
|
+
## Requirements
|
21
|
+
You must install haproxy command your machine. Because this tool uses haproxy command when it check format of a haproxy coniguration file.
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
### haproxy.cfg
|
25
|
+
In first, you should prepare haproxy configuration file from which you want to get information like below:
|
26
|
+
```
|
27
|
+
global
|
28
|
+
log 127.0.0.1 local2
|
29
|
+
|
30
|
+
chroot /var/lib/haproxy
|
31
|
+
pidfile /var/run/haproxy.pid
|
32
|
+
maxconn 4000
|
33
|
+
user haproxy
|
34
|
+
group haproxy
|
35
|
+
daemon
|
36
|
+
|
37
|
+
# turn on stats unix socket
|
38
|
+
stats socket /var/lib/haproxy/stats
|
39
|
+
|
40
|
+
defaults
|
41
|
+
mode http
|
42
|
+
log global
|
43
|
+
option httplog
|
44
|
+
option dontlognull
|
45
|
+
option http-server-close
|
46
|
+
option forwardfor except 127.0.0.0/8
|
47
|
+
option redispatch
|
48
|
+
retries 3
|
49
|
+
timeout http-request 10s
|
50
|
+
timeout queue 1m
|
51
|
+
timeout connect 10s
|
52
|
+
timeout client 1m
|
53
|
+
timeout server 1m
|
54
|
+
timeout http-keep-alive 10s
|
55
|
+
timeout check 10s
|
56
|
+
maxconn 3000
|
57
|
+
|
58
|
+
frontend frontend_http_80
|
59
|
+
bind *:5000
|
60
|
+
default_backend backend_http_80
|
61
|
+
|
62
|
+
backend backend_http_80
|
63
|
+
balance roundrobin
|
64
|
+
server 10.21.0.160:80 10.21.0.160:80 check
|
65
|
+
```
|
66
|
+
|
67
|
+
## Check Format
|
68
|
+
```
|
69
|
+
config = HaproxyParser.new(
|
70
|
+
haproxy_config_path
|
71
|
+
).check_format!
|
72
|
+
```
|
73
|
+
|
74
|
+
### Execute Parse
|
75
|
+
You can parse by calling method like below.
|
76
|
+
```
|
77
|
+
config = HaproxyParser.new(
|
78
|
+
haproxy_config_path
|
79
|
+
).parse
|
80
|
+
```
|
81
|
+
|
82
|
+
### Global Parameters
|
83
|
+
After calling `parse` method, you can access global informations through `global` method.
|
84
|
+
```
|
85
|
+
config.global.nbproc # => 1(default value)
|
86
|
+
config.global.maxconn # => 2000(default value)
|
87
|
+
```
|
88
|
+
|
89
|
+
### Frontend Parameters
|
90
|
+
Like `Global Parameters`, you can access frontend informations through `frontend` method.
|
91
|
+
```
|
92
|
+
config.frontend.name # => "frontend_http_80"
|
93
|
+
config.frontend.ipaddress # => "*"
|
94
|
+
...
|
95
|
+
```
|
96
|
+
|
97
|
+
### Backend Parameters
|
98
|
+
Backend Parameters are refered thorough `frontend` method.
|
99
|
+
So you are not able to access Backend Parameter which are not refered from any `frontend`.
|
100
|
+
|
101
|
+
```
|
102
|
+
config.frontends[0].backend.servers.each do |server|
|
103
|
+
server.name # => "10.21.0.160:80"
|
104
|
+
server.ipaddress # => "10.21.0.160"
|
105
|
+
server.port # => "80"
|
106
|
+
....
|
107
|
+
end
|
108
|
+
|
109
|
+
config.frontends[0].backend.balance # => "roundrobin"
|
110
|
+
```
|
111
|
+
|
112
|
+
## Development
|
113
|
+
|
114
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/haproxy_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
121
|
+
|
122
|
+
|
123
|
+
## License
|
124
|
+
|
125
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
126
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "haproxy_parser"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'haproxy_parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "haproxy_parser"
|
8
|
+
spec.version = HaproxyParser::VERSION
|
9
|
+
spec.authors = ["Akito Ueno"]
|
10
|
+
spec.email = ["aueno@idcf.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{This tool is a parser of haproxy.cfg.}
|
13
|
+
spec.description = %q{This tool parses haproxy.cfg, and gets parameters in sections which include frontend, backend, server.}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "activesupport"
|
26
|
+
spec.add_development_dependency "systemu"
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "haproxy_parser/builders/base"
|
2
|
+
require "haproxy_parser/builders/server_parser"
|
3
|
+
|
4
|
+
module HaproxyParser
|
5
|
+
module Builders
|
6
|
+
class Backend < Base
|
7
|
+
attr_accessor :frontend
|
8
|
+
def initialize(section:, default: nil, frontend:)
|
9
|
+
super(section: section, default: default)
|
10
|
+
@frontend = frontend
|
11
|
+
end
|
12
|
+
|
13
|
+
def mode
|
14
|
+
@mode ||= dig_data("mode")
|
15
|
+
end
|
16
|
+
|
17
|
+
def balance
|
18
|
+
@balance ||= dig_data("balance")
|
19
|
+
end
|
20
|
+
|
21
|
+
def servers
|
22
|
+
@servers ||= [].tap do |arr|
|
23
|
+
if line_servers[0].is_a?(Array)
|
24
|
+
line_servers.each do |line_server|
|
25
|
+
arr << ServerParser.new(
|
26
|
+
line: line_server,
|
27
|
+
frontend_port: frontend.port
|
28
|
+
)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
arr << ServerParser.new(
|
32
|
+
line: line_servers,
|
33
|
+
frontend_port: frontend.port
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def default_values
|
42
|
+
{
|
43
|
+
mode: "tcp"
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def line_servers
|
48
|
+
@line_servers ||= dig_data("server")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "haproxy_parser/digger"
|
2
|
+
module HaproxyParser
|
3
|
+
module Builders
|
4
|
+
class Base
|
5
|
+
attr_reader :section, :default
|
6
|
+
def initialize(section:, default: {})
|
7
|
+
@section = section
|
8
|
+
@default = default
|
9
|
+
end
|
10
|
+
|
11
|
+
def attributes
|
12
|
+
raise(NotImplementedError)
|
13
|
+
end
|
14
|
+
|
15
|
+
def name
|
16
|
+
@name ||= dig_data("name")
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def section_digger
|
22
|
+
@section_digger ||= Digger.new(data: section)
|
23
|
+
end
|
24
|
+
|
25
|
+
def defalut_digger
|
26
|
+
@defalut_digger ||= Digger.new(data: default)
|
27
|
+
end
|
28
|
+
|
29
|
+
def default_values_digger
|
30
|
+
@default_values_digger ||= Digger.new(data: default_values)
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_values
|
34
|
+
raise(NotImplementedError)
|
35
|
+
end
|
36
|
+
|
37
|
+
def dig_data(attr)
|
38
|
+
[
|
39
|
+
section_digger.value(attr),
|
40
|
+
defalut_digger.value(attr),
|
41
|
+
default_values_digger.value(attr)
|
42
|
+
].select { |value| value}[0]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module HaproxyParser
|
2
|
+
module Builders
|
3
|
+
class BindParser
|
4
|
+
attr_reader :line
|
5
|
+
def initialize(line)
|
6
|
+
@line = if line.is_a?(Array)
|
7
|
+
line
|
8
|
+
else
|
9
|
+
[] << line
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def ipaddress
|
14
|
+
@address ||= line[0].split(":")[0]
|
15
|
+
end
|
16
|
+
|
17
|
+
def port
|
18
|
+
@port ||= line[0].split(":")[1]
|
19
|
+
end
|
20
|
+
|
21
|
+
def ssl
|
22
|
+
@ssl ||= line.include?("ssl")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "haproxy_parser/builders/base"
|
2
|
+
require "haproxy_parser/builders/bind_parser"
|
3
|
+
module HaproxyParser
|
4
|
+
module Builders
|
5
|
+
class Frontend < Base
|
6
|
+
attr_accessor :backend
|
7
|
+
def initialize(section:, default: nil, backend: nil)
|
8
|
+
super(section: section, default: default)
|
9
|
+
@backend = backend
|
10
|
+
end
|
11
|
+
|
12
|
+
def backend_name
|
13
|
+
@backend_name ||= if dig_data("use_backend")
|
14
|
+
dig_data("use_backend")
|
15
|
+
else
|
16
|
+
dig_data("default_backend")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def ipaddress
|
21
|
+
@ipaddress ||= bind_parser.ipaddress
|
22
|
+
end
|
23
|
+
|
24
|
+
def port
|
25
|
+
@port ||= bind_parser.port
|
26
|
+
end
|
27
|
+
|
28
|
+
def ssl
|
29
|
+
@ssl ||= bind_parser.ssl
|
30
|
+
end
|
31
|
+
|
32
|
+
def mode
|
33
|
+
@mode ||= dig_data("mode")
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def bind_parser
|
39
|
+
@bind_parser ||= BindParser.new(line_of_bind)
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_values
|
43
|
+
{
|
44
|
+
mode: "tcp"
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def line_of_bind
|
49
|
+
@line_of_bind ||= dig_data("bind")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "haproxy_parser/builders/base"
|
2
|
+
|
3
|
+
module HaproxyParser
|
4
|
+
module Builders
|
5
|
+
class Global < Base
|
6
|
+
def nbproc
|
7
|
+
@nbproc ||= dig_data("nbproc")
|
8
|
+
end
|
9
|
+
|
10
|
+
def maxconn
|
11
|
+
@maxconn ||= dig_data("maxconn")
|
12
|
+
end
|
13
|
+
|
14
|
+
def stats
|
15
|
+
@stats ||= dig_data("stats")
|
16
|
+
end
|
17
|
+
|
18
|
+
def default_values
|
19
|
+
{
|
20
|
+
nbproc: 1,
|
21
|
+
maxconn: 2000
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
require "haproxy_parser/line_parser"
|
4
|
+
module HaproxyParser
|
5
|
+
module Builders
|
6
|
+
class ServerParser
|
7
|
+
attr_reader :line, :frontend_port
|
8
|
+
def initialize(line:, frontend_port:)
|
9
|
+
@line = line
|
10
|
+
@frontend_port = frontend_port
|
11
|
+
end
|
12
|
+
|
13
|
+
def name
|
14
|
+
@name ||= line[0]
|
15
|
+
end
|
16
|
+
|
17
|
+
def ipaddress
|
18
|
+
@ipaddress ||= line[1].split(":")[0]
|
19
|
+
end
|
20
|
+
|
21
|
+
def port
|
22
|
+
@port ||=
|
23
|
+
if line[1].split(":")[1]
|
24
|
+
line[1].split(":")[1]
|
25
|
+
else
|
26
|
+
frontend_port
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
%w(inter rise fall).each do |attr|
|
31
|
+
define_method(attr) do
|
32
|
+
if line_parser.value(attr)
|
33
|
+
line_parser.value(attr)
|
34
|
+
else
|
35
|
+
default_values[attr]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def line_parser
|
43
|
+
@line_parser ||= HaproxyParser::LineParser.new(
|
44
|
+
line
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_values
|
49
|
+
{
|
50
|
+
inter: "2000ms",
|
51
|
+
fall: 3,
|
52
|
+
rise: 2
|
53
|
+
}.with_indifferent_access
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require "systemu"
|
2
|
+
require "haproxy_parser/section_collector"
|
3
|
+
require "haproxy_parser/format_checker"
|
4
|
+
require "haproxy_parser/builders"
|
5
|
+
|
6
|
+
module HaproxyParser
|
7
|
+
class Config
|
8
|
+
SECTIONS = %w(global defaults frontend backend)
|
9
|
+
attr_reader :path
|
10
|
+
def initialize(path)
|
11
|
+
@path = path
|
12
|
+
end
|
13
|
+
|
14
|
+
def check_format!
|
15
|
+
HaproxyParser::FormatChecker.new(
|
16
|
+
path
|
17
|
+
).run
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse
|
21
|
+
check_format!
|
22
|
+
build
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def global
|
27
|
+
@global ||= build_global
|
28
|
+
end
|
29
|
+
|
30
|
+
def frontends
|
31
|
+
@frontends ||= []
|
32
|
+
end
|
33
|
+
|
34
|
+
def backends
|
35
|
+
@backends ||= []
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def build
|
41
|
+
build_global
|
42
|
+
build_frontend_backend
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_global
|
46
|
+
HaproxyParser::Builders::Global.new(
|
47
|
+
section: global_sections[-1].items
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_frontend_backend
|
52
|
+
frontend_sections.each do |frontend_section|
|
53
|
+
built_frontend = HaproxyParser::Builders::Frontend.new(
|
54
|
+
section: frontend_section.items,
|
55
|
+
default: default_section_items
|
56
|
+
)
|
57
|
+
built_backend = build_backend(built_frontend)
|
58
|
+
built_frontend.backend = built_backend
|
59
|
+
frontends << built_frontend
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def build_backend(frontend)
|
64
|
+
name = frontend.backend_name
|
65
|
+
return find_backend(name) if find_backend(name).present?
|
66
|
+
built_backend = HaproxyParser::Builders::Backend.new(
|
67
|
+
section: backend_section(name).items,
|
68
|
+
default: default_section_items,
|
69
|
+
frontend: frontend
|
70
|
+
).tap do |backend|
|
71
|
+
backends << backend
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_backend(name)
|
76
|
+
backends.select { |backend| backend.name == name }[0]
|
77
|
+
end
|
78
|
+
|
79
|
+
def default_section_items
|
80
|
+
@default_section_items ||= default_sections[-1].items
|
81
|
+
end
|
82
|
+
|
83
|
+
def backend_section(name)
|
84
|
+
backend_sections.select do |section|
|
85
|
+
section.name == name
|
86
|
+
end[0]
|
87
|
+
end
|
88
|
+
|
89
|
+
SECTIONS.each do |section|
|
90
|
+
name = section.pluralize
|
91
|
+
class_name = section.singularize
|
92
|
+
method_name = "#{class_name}_sections"
|
93
|
+
require "haproxy_parser/sections/#{class_name}"
|
94
|
+
sym = "@#{method_name}".to_sym
|
95
|
+
define_method(method_name) do
|
96
|
+
if instance_variable_defined?(sym)
|
97
|
+
instance_variable_get(sym)
|
98
|
+
else
|
99
|
+
data = section_collector.public_send(name).map do |item|
|
100
|
+
"HaproxyParser::Sections::#{class_name.camelize}"
|
101
|
+
.constantize
|
102
|
+
.new(item)
|
103
|
+
end
|
104
|
+
instance_variable_set(sym, data)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def section_collector
|
110
|
+
@section_collector ||= SectionCollector.new(
|
111
|
+
path
|
112
|
+
)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
module HaproxyParser
|
4
|
+
class Digger
|
5
|
+
attr_reader :data, :delimter
|
6
|
+
def initialize(data:, delimter: ".")
|
7
|
+
@data = data.with_indifferent_access
|
8
|
+
@delimter = delimter
|
9
|
+
end
|
10
|
+
|
11
|
+
def value(attr)
|
12
|
+
digged_data = data
|
13
|
+
attr.split(delimter).each do |key|
|
14
|
+
unless digged_data.key?(key)
|
15
|
+
digged_data = nil
|
16
|
+
break
|
17
|
+
end
|
18
|
+
digged_data = digged_data[key]
|
19
|
+
end
|
20
|
+
digged_data
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "tempfile"
|
2
|
+
require "systemu"
|
3
|
+
|
4
|
+
module HaproxyParser
|
5
|
+
class FormatChecker
|
6
|
+
FormatError = Class.new(StandardError)
|
7
|
+
attr_accessor :result
|
8
|
+
attr_reader :path
|
9
|
+
def initialize(path)
|
10
|
+
@path = path
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
check
|
15
|
+
raise(FormatError, result) if result
|
16
|
+
end
|
17
|
+
|
18
|
+
def lines
|
19
|
+
@lines ||= [].tap do |arr|
|
20
|
+
File.open(path, "r") do |f|
|
21
|
+
f.each_line do |line|
|
22
|
+
next if /\s+(user|group)\s+/ =~ line
|
23
|
+
arr << line
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def check
|
30
|
+
# create file whose user, group is deleted. #
|
31
|
+
tmp_file = Tempfile.open("haproxy_parser") do |tmp_f|
|
32
|
+
lines.each do |line|
|
33
|
+
tmp_f.print(line)
|
34
|
+
end
|
35
|
+
tmp_f
|
36
|
+
end
|
37
|
+
status,_,stderr = systemu "haproxy -c -f #{tmp_file.path}"
|
38
|
+
tmp_file.unlink
|
39
|
+
@result = stderr unless status.success?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module HaproxyParser
|
2
|
+
class LineParser
|
3
|
+
attr_reader :list
|
4
|
+
def initialize(list)
|
5
|
+
@list = list
|
6
|
+
end
|
7
|
+
|
8
|
+
def value(attr)
|
9
|
+
nil.tap do |ret|
|
10
|
+
list.each_with_index do |v, i|
|
11
|
+
ret = list[i+1] if attr == v
|
12
|
+
end
|
13
|
+
break ret
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
|
4
|
+
module HaproxyParser
|
5
|
+
class SectionCollector
|
6
|
+
SECTIONS = %w(defaults global frontend backend listen)
|
7
|
+
attr_reader :path
|
8
|
+
def initialize(path)
|
9
|
+
@path = path
|
10
|
+
end
|
11
|
+
|
12
|
+
SECTIONS.each do |section_kind|
|
13
|
+
define_method(section_kind.pluralize) do
|
14
|
+
sections.select do |section|
|
15
|
+
section_kind == section[0][0]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def sections
|
23
|
+
@sections ||= build_sections
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_sections
|
27
|
+
[].tap do |arr|
|
28
|
+
section_indexes.each_with_index do |section_index, i|
|
29
|
+
size = 0
|
30
|
+
case i
|
31
|
+
when section_indexes.size - 1
|
32
|
+
size = lines.size - section_index
|
33
|
+
else
|
34
|
+
size = section_indexes[i + 1] - section_index
|
35
|
+
end
|
36
|
+
arr << lines[section_index, size]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def section_indexes
|
42
|
+
@section_indexes ||= [].tap do |arr|
|
43
|
+
index = 0
|
44
|
+
lines.each do |line|
|
45
|
+
arr << index if SECTIONS.include?(line[0])
|
46
|
+
index += 1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def lines
|
52
|
+
@lines ||= [].tap do |arr|
|
53
|
+
File.open(path, "r") do |f|
|
54
|
+
f.each_line do |line|
|
55
|
+
line_list = line.strip.chomp.sub(/#.*/, "").split(/ +/)
|
56
|
+
next if (line_list.size == 0) || unnecessary?(line_list)
|
57
|
+
arr << line_list
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def unnecessary?(line)
|
64
|
+
line.size == 1 && line[0].empty?
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
|
4
|
+
module HaproxyParser
|
5
|
+
module Sections
|
6
|
+
class Base
|
7
|
+
attr_reader :name, :attr_lines
|
8
|
+
def initialize(lines)
|
9
|
+
@name = lines[0][1]
|
10
|
+
@attr_lines = lines[1..-1]
|
11
|
+
end
|
12
|
+
|
13
|
+
def items
|
14
|
+
@items ||= {}.tap do |data|
|
15
|
+
counter = {}
|
16
|
+
attr_lines.each do |line|
|
17
|
+
if data.key?(line[0])
|
18
|
+
if 1 == counter[line[0]]
|
19
|
+
data[line[0]] = [] << data[line[0]] << attr_value(line)
|
20
|
+
else
|
21
|
+
data[line[0]] << attr_value(line)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
data[line[0]] = attr_value(line)
|
25
|
+
end
|
26
|
+
counter[line[0]] ? counter[line[0]] += 1 : counter[line[0]] = 1
|
27
|
+
end
|
28
|
+
data[:name] = name
|
29
|
+
break data
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def attr_value(line)
|
34
|
+
case line.size
|
35
|
+
when 1
|
36
|
+
""
|
37
|
+
when 2
|
38
|
+
line[1]
|
39
|
+
else
|
40
|
+
line[1..-1]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haproxy_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akito Ueno
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: systemu
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: This tool parses haproxy.cfg, and gets parameters in sections which include
|
84
|
+
frontend, backend, server.
|
85
|
+
email:
|
86
|
+
- aueno@idcf.jp
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- haproxy_parser.gemspec
|
102
|
+
- lib/haproxy_parser.rb
|
103
|
+
- lib/haproxy_parser/builders.rb
|
104
|
+
- lib/haproxy_parser/builders/backend.rb
|
105
|
+
- lib/haproxy_parser/builders/base.rb
|
106
|
+
- lib/haproxy_parser/builders/bind_parser.rb
|
107
|
+
- lib/haproxy_parser/builders/frontend.rb
|
108
|
+
- lib/haproxy_parser/builders/global.rb
|
109
|
+
- lib/haproxy_parser/builders/server_parser.rb
|
110
|
+
- lib/haproxy_parser/config.rb
|
111
|
+
- lib/haproxy_parser/digger.rb
|
112
|
+
- lib/haproxy_parser/format_checker.rb
|
113
|
+
- lib/haproxy_parser/line_parser.rb
|
114
|
+
- lib/haproxy_parser/section_collector.rb
|
115
|
+
- lib/haproxy_parser/sections/backend.rb
|
116
|
+
- lib/haproxy_parser/sections/base.rb
|
117
|
+
- lib/haproxy_parser/sections/default.rb
|
118
|
+
- lib/haproxy_parser/sections/frontend.rb
|
119
|
+
- lib/haproxy_parser/sections/global.rb
|
120
|
+
- lib/haproxy_parser/server.rb
|
121
|
+
- lib/haproxy_parser/version.rb
|
122
|
+
homepage: ''
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.5.1
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: This tool is a parser of haproxy.cfg.
|
146
|
+
test_files: []
|