haproxy-tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.7.0"
10
+ gem "yard", "~> 0.7.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.6.4"
13
+ gem "rcov", ">= 0"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jason Wadsworth
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = haproxy-tools
2
+
3
+ Tools for managing HAProxy with Ruby.
4
+
5
+ == Contributing to haproxy-tools
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Jason Wadsworth. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "haproxy-tools"
18
+ gem.homepage = "http://github.com/subakva/haproxy-tools"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{HAProxy Tools for Ruby}
21
+ gem.description = %Q{Ruby tools for HAProxy, including config file management.}
22
+ gem.email = "jdwadsworth@gmail.com"
23
+ gem.authors = ["Jason Wadsworth"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,34 @@
1
+ module HAProxy
2
+ Backend = Struct.new(:name, :servers)
3
+ Frontend = Struct.new(:name, :ip, :port)
4
+ Server = Struct.new(:name, :ip, :port, :options)
5
+
6
+ class Config
7
+ attr_accessor :options, :backends, :frontends, :option_groups
8
+
9
+ def initialize(options = nil)
10
+ self.options = options || {}
11
+ self.backends = []
12
+ self.frontends = []
13
+ self.option_groups = {'global' => {}, 'defaults' => {}}
14
+ end
15
+
16
+ def global
17
+ option_groups['global']
18
+ end
19
+
20
+ def defaults
21
+ option_groups['defaults']
22
+ end
23
+
24
+ def backend(name)
25
+ self.backends.find { |b| b.name == name }
26
+ end
27
+
28
+ class << self
29
+ def parse(filename)
30
+ HAProxy::Parser.new.parse(filename)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,100 @@
1
+ module HAProxy
2
+ class Parser
3
+ attr_accessor :verbose, :options
4
+ attr_accessor :current_backend, :current_section, :current_frontend, :current_section_name
5
+
6
+ def initialize(options = nil)
7
+ options ||= {}
8
+ options = { :verbose => false }.merge(options)
9
+
10
+ self.options = options
11
+ self.verbose = options[:verbose]
12
+ reset_parser_flags
13
+ end
14
+
15
+ def reset_parser_flags
16
+ self.current_section = nil
17
+ self.current_backend = nil
18
+ self.current_frontend = nil
19
+ end
20
+
21
+ # This is starting to suck. Try treetop.
22
+ def parse(filename)
23
+ self.reset_parser_flags
24
+
25
+ config = HAProxy::Config.new
26
+ start_backend(config, 'default')
27
+ start_frontend(config, 'default')
28
+
29
+ lines = File.readlines(filename)
30
+ lines.each do |line|
31
+ line.strip!
32
+ case line.strip
33
+ when /^(global|defaults)$/
34
+ start_section(config, $1)
35
+ when /^frontend\W+([^\W]+)\W+([^\W:]+):(\d+)/
36
+ start_section(config, 'frontend')
37
+ start_frontend(config, $1)
38
+ when /^backend\W+([^\W]+)/
39
+ start_section(config, 'backend')
40
+ start_backend(config, $1)
41
+ when /^server\W+([^\W]+)\W+([\d\.]+):(\d+)(.*)/
42
+ append_server($1, $2, $3, $4)
43
+ when /^([^\W]+)([^#]*)/ # match other name/value pairs; ignore comments
44
+ append_option($1, $2)
45
+ when /^$/
46
+ when /^#.*/
47
+ puts " => Ignoring comment: #{line}" if verbose
48
+ else
49
+ puts " => Skipping non-matching line: #{line}" if verbose
50
+ end
51
+ end
52
+
53
+ config
54
+ end
55
+
56
+ def normalize_option_string(option_string)
57
+ normalized_options = option_string.strip.split
58
+ normalized_options = normalized_options.first if normalized_options.size <= 1
59
+ normalized_options
60
+ end
61
+
62
+ def append_option(name, option_string)
63
+ normalized_options = normalize_option_string(option_string)
64
+
65
+ puts " => Adding #{current_section_name} option : #{name} = #{normalized_options.inspect}" if verbose
66
+
67
+ current_section[name] ||= []
68
+ self.current_section[name] << normalized_options unless normalized_options.nil?
69
+ end
70
+
71
+ def append_server(name, ip, port, option_string)
72
+ puts " => Adding server: #{name}" if verbose
73
+
74
+ server_options = normalize_option_string(option_string)
75
+ self.current_backend.servers[name] = Server.new(name, ip, port, server_options)
76
+ end
77
+
78
+ def start_section(config, name)
79
+ puts " => Starting option_group: #{name}" if verbose
80
+
81
+ config.option_groups[name] ||= {}
82
+ self.current_section_name = name
83
+ self.current_section = config.option_groups[name]
84
+ end
85
+
86
+ def start_frontend(config, name)
87
+ puts " => Starting frontend: #{name}" if verbose
88
+
89
+ self.current_frontend = Frontend.new(name, {})
90
+ config.frontends << self.current_frontend
91
+ end
92
+
93
+ def start_backend(config, name)
94
+ puts " => Starting backend: #{name}" if verbose
95
+
96
+ self.current_backend = Backend.new(name, {})
97
+ config.backends << self.current_backend
98
+ end
99
+ end
100
+ end
@@ -0,0 +1 @@
1
+ require 'haproxy_tools'
@@ -0,0 +1,2 @@
1
+ require 'haproxy/config'
2
+ require 'haproxy/parser'
@@ -0,0 +1,54 @@
1
+ global
2
+ log 127.0.0.1 local2 info
3
+ maxconn 20000
4
+ # ulimit-n 8250
5
+ chroot /home/haproxy
6
+ user haproxy
7
+ group haproxy
8
+ daemon
9
+ quiet
10
+ pidfile /home/haproxy/haproxy.pid
11
+ defaults
12
+ log global
13
+ mode http
14
+ option httplog
15
+ option dontlognull
16
+ retries 3
17
+ redispatch
18
+ maxconn 10000
19
+ contimeout 5000
20
+ clitimeout 60000
21
+ srvtimeout 60000
22
+ stats uri /haproxy-status
23
+ cookie SERVERID insert indirect nocache
24
+
25
+ frontend www *:85
26
+ default_backend www_main
27
+
28
+ acl logs_hostname hdr_beg(Host) -i logs.example
29
+
30
+ acl www_main_media_subdomain hdr_beg(Host) -i m0.example
31
+ acl www_main_media_subdomain hdr_beg(Host) -i m1.example
32
+ acl www_main_media_subdomain hdr_beg(Host) -i m2.example
33
+ acl www_main_media_subdomain hdr_beg(Host) -i m3.example
34
+
35
+ use_backend logs if logs_hostname
36
+
37
+ use_backend www_main if www_main_media_subdomain
38
+
39
+ backend www_main
40
+ mode http
41
+ balance roundrobin
42
+ option httpchk GET /up
43
+ server prd_www_1 10.214.78.95:8000 cookie i-prd_www_1 check inter 3000 rise 2 fall 3 maxconn 1000
44
+ server prd_www_2 10.99.89.233:8000 cookie i-prd_www_2 check inter 3000 rise 2 fall 3 maxconn 1000
45
+ server prd_www_3 10.7.22.40:8000 cookie i-prd_www_3 check inter 3000 rise 2 fall 3 maxconn 1000
46
+ server fake_www_main 127.0.0.1:9999 backup
47
+
48
+ backend logs
49
+ mode http
50
+ balance roundrobin
51
+ option httpchk GET /ping
52
+ server fake_logger 127.0.0.1:9999 backup
53
+ server prd_log_1 10.245.174.75:8000 cookie i-prd_log_1 check inter 3000 rise 2 fall 3 maxconn 1000
54
+ server prd_log_2 10.215.157.10:8000 cookie i-prd_log_2 check inter 3000 rise 2 fall 3 maxconn 1000
@@ -0,0 +1,21 @@
1
+ global
2
+ maxconn 4096 # Total Max Connections. This is dependent on ulimit
3
+ daemon
4
+ nbproc 4 # Number of processing cores. Dual Dual-core Opteron is 4 cores for example.
5
+
6
+ defaults
7
+ mode http
8
+ clitimeout 60000
9
+ srvtimeout 30000
10
+ contimeout 4000
11
+ option httpclose # Disable Keepalive
12
+
13
+ listen http_proxy 55.55.55.55:80
14
+ balance roundrobin # Load Balancing algorithm
15
+ option httpchk
16
+ option forwardfor # This sets X-Forwarded-For
17
+
18
+ ## Define your servers to balance
19
+ server web1 66.66.66.66:80 weight 1 maxconn 512 check
20
+ server web2 77.77.77.77:80 weight 1 maxconn 512 check
21
+ server web3 88.88.88.88:80 weight 1 maxconn 512 check
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe "HAProxy::Config" do
4
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe "HAProxy::Parser" do
4
+ context 'multi-pool config file' do
5
+ before(:each) do
6
+ @parser = HAProxy::Parser.new
7
+ @config = @parser.parse('spec/fixtures/multi-pool.haproxy.cfg')
8
+ end
9
+
10
+ it "parses a named backends from a config file" do
11
+ @config.backends.size.should == 3
12
+ logs_backend = @config.backend('logs')
13
+
14
+ logs_backend.servers.size.should == 3
15
+
16
+ server1 = logs_backend.servers['prd_log_1']
17
+ server1.name.should == 'prd_log_1'
18
+ server1.ip.should == '10.245.174.75'
19
+ server1.port.should == '8000'
20
+
21
+ server2 = logs_backend.servers['fake_logger']
22
+ server2.name.should == 'fake_logger'
23
+ server2.ip.should == '127.0.0.1'
24
+ server2.port.should == '9999'
25
+
26
+ server3 = logs_backend.servers['prd_log_2']
27
+ server3.name.should == 'prd_log_2'
28
+ server3.ip.should == '10.215.157.10'
29
+ server3.port.should == '8000'
30
+ end
31
+ end
32
+
33
+ context 'basic config file' do
34
+ before(:each) do
35
+ @parser = HAProxy::Parser.new
36
+ @config = @parser.parse('spec/fixtures/simple.haproxy.cfg')
37
+ end
38
+
39
+ it "parses global variables from a config file" do
40
+ @config.global.size.should == 3
41
+ @config.global['maxconn'].should == ['4096']
42
+ @config.global['daemon'].should == []
43
+ @config.global['nbproc'].should == ['4']
44
+ end
45
+
46
+ it "parses default variables from a config file" do
47
+ @config.defaults.size.should == 7
48
+ @config.defaults['mode'].should == ['http']
49
+ @config.defaults['clitimeout'].should == ['60000']
50
+ @config.defaults['srvtimeout'].should == ['30000']
51
+ @config.defaults['contimeout'].should == ['4000']
52
+ @config.defaults['listen'].should == [['http_proxy', '55.55.55.55:80']]
53
+ @config.defaults['balance'].should == ['roundrobin']
54
+ @config.defaults['option'].should == ['httpclose','httpchk','forwardfor']
55
+ end
56
+
57
+ it "parses a default backends from a config file" do
58
+ @config.backends.size.should == 1
59
+
60
+ pool = @config.backends.first
61
+
62
+ pool.servers.size.should == 3
63
+
64
+ server1 = pool.servers['web1']
65
+ server1.name.should == 'web1'
66
+ server1.ip.should == '66.66.66.66'
67
+ server1.port.should == '80'
68
+
69
+ server2 = pool.servers['web2']
70
+ server2.name.should == 'web2'
71
+ server2.ip.should == '77.77.77.77'
72
+ server2.port.should == '80'
73
+
74
+ server3 = pool.servers['web3']
75
+ server3.name.should == 'web3'
76
+ server3.ip.should == '88.88.88.88'
77
+ server3.port.should == '80'
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'haproxy-tools'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haproxy-tools
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Wadsworth
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-23 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: rspec
24
+ type: :development
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 19
31
+ segments:
32
+ - 2
33
+ - 7
34
+ - 0
35
+ version: 2.7.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ name: yard
40
+ type: :development
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ - 7
50
+ - 0
51
+ version: 0.7.0
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ prerelease: false
55
+ name: bundler
56
+ type: :development
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 23
63
+ segments:
64
+ - 1
65
+ - 0
66
+ - 0
67
+ version: 1.0.0
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ prerelease: false
71
+ name: jeweler
72
+ type: :development
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ hash: 7
79
+ segments:
80
+ - 1
81
+ - 6
82
+ - 4
83
+ version: 1.6.4
84
+ requirement: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ prerelease: false
87
+ name: rcov
88
+ type: :development
89
+ version_requirements: &id005 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirement: *id005
99
+ description: Ruby tools for HAProxy, including config file management.
100
+ email: jdwadsworth@gmail.com
101
+ executables: []
102
+
103
+ extensions: []
104
+
105
+ extra_rdoc_files:
106
+ - LICENSE.txt
107
+ - README.rdoc
108
+ files:
109
+ - .document
110
+ - .rspec
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.rdoc
114
+ - Rakefile
115
+ - VERSION
116
+ - lib/haproxy-tools.rb
117
+ - lib/haproxy/config.rb
118
+ - lib/haproxy/parser.rb
119
+ - lib/haproxy_tools.rb
120
+ - spec/fixtures/multi-pool.haproxy.cfg
121
+ - spec/fixtures/simple.haproxy.cfg
122
+ - spec/haproxy/config_spec.rb
123
+ - spec/haproxy/parser_spec.rb
124
+ - spec/spec_helper.rb
125
+ has_rdoc: true
126
+ homepage: http://github.com/subakva/haproxy-tools
127
+ licenses:
128
+ - MIT
129
+ post_install_message:
130
+ rdoc_options: []
131
+
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
152
+ requirements: []
153
+
154
+ rubyforge_project:
155
+ rubygems_version: 1.4.2
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: HAProxy Tools for Ruby
159
+ test_files: []
160
+