capistrano-haproxy 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-haproxy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yamashita Yuu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # capistrano-haproxy
2
+
3
+ a capistrano recipe to setup [HAProxy](http://haproxy.1wt.eu/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'capistrano-haproxy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install capistrano-haproxy
18
+
19
+ ## Usage
20
+
21
+ This recipe will setup HAProxy during `deploy:setup` task.
22
+
23
+ To enable this recipe, add following in your `config/deploy.rb`.
24
+
25
+ # in "config/deploy.rb"
26
+ require "capistrano-haproxy"
27
+ set(:haproxy_listens) {{
28
+ "stats 127.0.0.1:8888" => {
29
+ :mode => "http",
30
+ :stats => "uri /server-status",
31
+ }
32
+ }}
33
+
34
+ Following options are available to configure your HAProxy.
35
+
36
+ * `:haproxy_path` - The base path of HAProxy configurations. Use `/etc/haproxy` by default.
37
+ * `:haproxy_global` - The key-value map of `global` options of HAProxy.
38
+ * `:haproxy_defaults` - The key-value map of `defaults` options of HAProxy.
39
+ * `:haproxy_listens` - The definitions of listeners of HAProxy.
40
+ * `:haproxy_dependencies` - The packages of HAProxy.
41
+ * `:haproxy_template_path` - The local path to the configuration templates.
42
+ * `:haproxy_configure_files` - The configuration files of HAProxy.
43
+ * `:haproxy_service_name` - The name of HAProxy service. Use `haproxy` by default.
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
52
+
53
+ ## Author
54
+
55
+ - YAMASHITA Yuu (https://github.com/yyuu)
56
+ - Geisha Tokyo Entertainment Inc. (http://www.geishatokyo.com/)
57
+
58
+ ## License
59
+
60
+ MIT
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano-haproxy/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "capistrano-haproxy"
8
+ gem.version = Capistrano::HAProxy::VERSION
9
+ gem.authors = ["Yamashita Yuu"]
10
+ gem.email = ["yamashita@geishatokyo.com"]
11
+ gem.description = %q{a capistrano recipe to setup HAProxy.}
12
+ gem.summary = %q{a capistrano recipe to setup HAProxy.}
13
+ gem.homepage = "https://github.com/yyuu/capistrano-haproxy"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency("capistrano")
21
+ end
@@ -0,0 +1,200 @@
1
+ require "capistrano-haproxy/version"
2
+ require "erb"
3
+ require "tempfile"
4
+ require "uri"
5
+
6
+ module Capistrano
7
+ module HAProxy
8
+ def self.extended(configuration)
9
+ configuration.load {
10
+ namespace(:haproxy) {
11
+ _cset(:haproxy_path, "/etc/haproxy")
12
+
13
+ _cset(:haproxy_global) {{
14
+ # "chroot" => "/usr/share/haproxy",
15
+ "daemon" => "",
16
+ "group" => fetch(:haproxy_group, "haproxy"),
17
+ # "quiet" => "",
18
+ "spread-checks" => 0,
19
+ "user" => fetch(:haproxy_user, "haproxy"),
20
+ "tune.maxaccept" => 100,
21
+ }}
22
+ _cset(:haproxy_defaults) {{
23
+ "balance" => "roundrobin",
24
+ "grace" => 0,
25
+ "log" => "global",
26
+ "maxconn" => fetch(:haproxy_connections, 65535),
27
+ "mode" => "tcp",
28
+ "option" => [
29
+ "clitcpka",
30
+ "contstats",
31
+ "dontlognull",
32
+ "redispatch",
33
+ # "splice-auto",
34
+ "srvtcpka",
35
+ # "transparent",
36
+ ],
37
+ "retries" => 3,
38
+ "timeout" => [
39
+ "client #{fetch(:haproxy_client_tieout, '1h')}",
40
+ "connect #{fetch(:haproxy_connct_timeout, '3s')}",
41
+ "server #{fetch(:haproxy_server_timeout, '1h')}",
42
+ ],
43
+ }}
44
+ _cset(:haproxy_listens, {})
45
+ #
46
+ # Example:
47
+ #
48
+ # set(:haproxy_listens) {{
49
+ # "stats 127.0.0.1:8888" => {
50
+ # :mode => "http",
51
+ # :stats => "uri /server-status",
52
+ # },
53
+ # "mysql 127.0.0.1:3306" => {
54
+ # :mode => "tcp",
55
+ # :servers => [
56
+ # "foo foo.example.com:3306 check inter 5000 weight 1",
57
+ # "bar bar.example.com:3306 check inter 5000 weight 1 backup",
58
+ # ],
59
+ # }
60
+ # }}
61
+ #
62
+
63
+ desc("Setup HAProxy.")
64
+ task(:setup, :roles => :app, :except => { :no_release => true }) {
65
+ transaction {
66
+ install
67
+ _update
68
+ }
69
+ }
70
+ after 'deploy:setup', 'haproxy:setup'
71
+
72
+ desc("Update HAProxy configuration.")
73
+ task(:update, :roles => :app, :except => { :no_release => true }) {
74
+ transaction {
75
+ _update
76
+ }
77
+ }
78
+ # Do not run automatically during normal `deploy' to avoid slow down.
79
+ # If you want to do so, add following line in your ./config/deploy.rb
80
+ #
81
+ # after 'deploy:update', 'haproxy:update'
82
+
83
+ task(:_update, :roles => :app, :except => { :no_release => true }) {
84
+ configure
85
+ reload
86
+ }
87
+
88
+ task(:install, :roles => :app, :except => { :no_release => true }) {
89
+ install_dependencies
90
+ install_service
91
+ }
92
+
93
+ _cset(:haproxy_platform) {
94
+ capture((<<-EOS).gsub(/\s+/, ' ')).strip
95
+ if test -f /etc/debian_version; then
96
+ if test -f /etc/lsb-release && grep -i -q DISTRIB_ID=Ubuntu /etc/lsb-release; then
97
+ echo ubuntu;
98
+ else
99
+ echo debian;
100
+ fi;
101
+ elif test -f /etc/redhat-release; then
102
+ echo redhat;
103
+ else
104
+ echo unknown;
105
+ fi;
106
+ EOS
107
+ }
108
+ _cset(:haproxy_dependencies, %w(haproxy))
109
+ task(:install_dependencies, :roles => :app, :except => { :no_release => true }) {
110
+ unless haproxy_dependencies.empty?
111
+ case haproxy_platform
112
+ when /(debian|ubuntu)/i
113
+ run("#{sudo} apt-get install -q -y #{haproxy_dependencies.join(' ')}")
114
+ when /redhat/i
115
+ run("#{sudo} yum install -q -y #{haproxy_dependencies.join(' ')}")
116
+ else
117
+ # nop
118
+ end
119
+ end
120
+ }
121
+
122
+ task(:install_service, :roles => :app, :except => { :no_release => true }) {
123
+ # TODO: setup (sysvinit|daemontools|upstart|runit|systemd) service of HAProxy
124
+ }
125
+
126
+ def tempfile(name)
127
+ f = Tempfile.new(name)
128
+ path = f.path
129
+ f.close(true) # close and remove tempfile immediately
130
+ path
131
+ end
132
+
133
+ def template(file)
134
+ if File.file?(file)
135
+ File.read(file)
136
+ elsif File.file?("#{file}.erb")
137
+ ERB.new(File.read("#{file}.erb")).result(binding)
138
+ else
139
+ abort("No such template: #{file} or #{file}.erb")
140
+ end
141
+ end
142
+
143
+ _cset(:haproxy_template_path, File.join(File.dirname(__FILE__), 'capistrano-haproxy', 'templates'))
144
+ _cset(:haproxy_configure_files, %w(/etc/default/haproxy haproxy.cfg))
145
+ task(:configure, :roles => :app, :except => { :no_release => true }) {
146
+ srcs = haproxy_configure_files.map { |file| File.join(haproxy_template_path, file) }
147
+ tmps = haproxy_configure_files.map { |file| tempfile('capistrano-haproxy') }
148
+ dsts = haproxy_configure_files.map { |file| File.expand_path(file) == file ? file : File.join(haproxy_path, file) }
149
+ begin
150
+ srcs.zip(tmps) do |src, tmp|
151
+ put(template(src), tmp)
152
+ end
153
+ execute = []
154
+ dirs = dsts.map { |path| File.dirname(path) }.uniq
155
+ execute << "#{sudo} mkdir -p #{dirs.map { |dir| dir.dump }.join(' ')}" unless dirs.empty?
156
+ tmps.zip(dsts) do |tmp, dst|
157
+ execute << "( diff -u #{dst.dump} #{tmp.dump} || #{sudo} mv -f #{tmp.dump} #{dst.dump} )"
158
+ end
159
+ run(execute.join(' && ')) unless execute.empty?
160
+ ensure
161
+ run("rm -f #{tmps.map { |t| t.dump }.join(' ')}") unless tmps.empty?
162
+ end
163
+ }
164
+
165
+ _cset(:haproxy_service_name, 'haproxy')
166
+ desc("Start HAProxy daemon.")
167
+ task(:start, :roles => :app, :except => { :no_release => true }) {
168
+ run("#{sudo} service #{haproxy_service_name} start")
169
+ }
170
+
171
+ desc("Stop HAProxy daemon.")
172
+ task(:stop, :roles => :app, :except => { :no_release => true }) {
173
+ run("#{sudo} service #{haproxy_service_name} stop")
174
+ }
175
+
176
+ desc("Restart HAProxy daemon.")
177
+ task(:restart, :roles => :app, :except => { :no_release => true }) {
178
+ run("#{sudo} service #{haproxy_service_name} restart || #{sudo} service #{haproxy_service_name} start")
179
+ }
180
+
181
+ desc("Reload HAProxy daemon.")
182
+ task(:reload, :roles => :app, :except => { :no_release => true }) {
183
+ run("#{sudo} service #{haproxy_service_name} reload || #{sudo} service #{haproxy_service_name} start")
184
+ }
185
+
186
+ desc("Show HAProxy daemon status.")
187
+ task(:status, :roles => :app, :except => { :no_release => true }) {
188
+ run("#{sudo} service #{haproxy_service_name} status")
189
+ }
190
+ }
191
+ }
192
+ end
193
+ end
194
+ end
195
+
196
+ if Capistrano::Configuration.instance
197
+ Capistrano::Configuration.instance.extend(Capistrano::HAProxy)
198
+ end
199
+
200
+ # vim:set ft=ruby ts=2 sw=2 :
@@ -0,0 +1,10 @@
1
+ # Set ENABLED to 1 if you want the init script to start haproxy.
2
+ ENABLED=1
3
+ # Add extra flags here.
4
+ #EXTRAOPTS="-de -m 16"
5
+ EXTRAOPTS=<%= fetch(:haproxy_extra_opts, []).join(" ").dump %>
6
+
7
+ # Bootstrap script
8
+ <%= fetch(:haproxy_bootstrap_script, '').strip %>
9
+
10
+ # vim:set ft=eruby :
@@ -0,0 +1,12 @@
1
+ global<% haproxy_global.each { |name, global_defs| [ global_defs ].flatten.each { |d| %>
2
+ <%= name %> <%= d %><% } } %>
3
+
4
+ defaults<% haproxy_defaults.each { |name, defaults_defs| [ defaults_defs ].flatten.each { |d| %>
5
+ <%= name %> <%= d %><% } } %>
6
+
7
+ <% haproxy_listens.each { |listen_name, listen_defs| %>
8
+ listen <%= listen_name %><% listen_defs.each { |param_name, param_defs| [ param_defs ].flatten.each { |d| %>
9
+ <%= param_name %> <%= d %><% } } %>
10
+ <% } %>
11
+
12
+ # vim:set ft=eruby :
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module HAProxy
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-haproxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yamashita Yuu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capistrano
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: a capistrano recipe to setup HAProxy.
31
+ email:
32
+ - yamashita@geishatokyo.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - capistrano-haproxy.gemspec
43
+ - lib/capistrano-haproxy.rb
44
+ - lib/capistrano-haproxy/templates/etc/default/haproxy.erb
45
+ - lib/capistrano-haproxy/templates/haproxy.cfg.erb
46
+ - lib/capistrano-haproxy/version.rb
47
+ homepage: https://github.com/yyuu/capistrano-haproxy
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: a capistrano recipe to setup HAProxy.
71
+ test_files: []