Module: Capistrano::HAProxy

Defined in:
lib/capistrano-haproxy.rb,
lib/capistrano-haproxy/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.extended(configuration) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/capistrano-haproxy.rb', line 9

def self.extended(configuration)
  configuration.load {
    namespace(:haproxy) {
      _cset(:haproxy_path, "/etc/haproxy")

      _cset(:haproxy_global) {{
#           "chroot" => "/usr/share/haproxy",
        "daemon" => "",
        "group" => fetch(:haproxy_group, "haproxy"),
#           "quiet" => "",
        "spread-checks" => 0,
        "user" => fetch(:haproxy_user, "haproxy"),
        "tune.maxaccept" => 100,
      }}
      _cset(:haproxy_defaults) {{
        "balance" => "roundrobin",
        "grace" => 0,
        "log" => "global",
        "maxconn" => fetch(:haproxy_connections, 65535),
        "mode" => "tcp",
        "option" => [
          "clitcpka",
          "contstats",
          "dontlognull",
          "redispatch",
#             "splice-auto",
          "srvtcpka",
#             "transparent",
        ],
        "retries" => 3,
        "timeout" => [
          "client #{fetch(:haproxy_client_tieout, '1h')}",
          "connect #{fetch(:haproxy_connct_timeout, '3s')}",
          "server #{fetch(:haproxy_server_timeout, '1h')}",
        ],
      }}
      _cset(:haproxy_listens, {})
      #
      # Example:
      #
      # set(:haproxy_listens) {{
      #   "stats 127.0.0.1:8888" => {
      #     :mode => "http",
      #     :stats => "uri /server-status",
      #   },
      #   "mysql 127.0.0.1:3306" => {
      #     :mode => "tcp",
      #     :servers => [
      #       "foo foo.example.com:3306 check inter 5000 weight 1",
      #       "bar bar.example.com:3306 check inter 5000 weight 1 backup",
      #     ],
      #   }
      # }}
      #

      desc("Setup HAProxy.")
      task(:setup, :roles => :app, :except => { :no_release => true }) {
        transaction {
          install
          _update
        }
      }
      after 'deploy:setup', 'haproxy:setup'

      desc("Update HAProxy configuration.")
      task(:update, :roles => :app, :except => { :no_release => true }) {
        transaction {
          _update
        }
      }
      # Do not run automatically during normal `deploy' to avoid slow down.
      # If you want to do so, add following line in your ./config/deploy.rb
      #
      # after 'deploy:finalize_update', 'haproxy:update'

      task(:_update, :roles => :app, :except => { :no_release => true }) {
        configure
        reload
      }

      task(:install, :roles => :app, :except => { :no_release => true }) {
        install_dependencies
        install_service
      }

      _cset(:haproxy_platform) {
        capture((<<-EOS).gsub(/\s+/, ' ')).strip
          if test -f /etc/debian_version; then
            if test -f /etc/lsb-release && grep -i -q DISTRIB_ID=Ubuntu /etc/lsb-release; then
              echo ubuntu;
            else
              echo debian;
            fi;
          elif test -f /etc/redhat-release; then
            echo redhat;
          else
            echo unknown;
          fi;
        EOS
      }
      _cset(:haproxy_dependencies, %w(haproxy))
      task(:install_dependencies, :roles => :app, :except => { :no_release => true }) {
        unless haproxy_dependencies.empty?
          case haproxy_platform
          when /(debian|ubuntu)/i
            run("#{sudo} apt-get install -q -y #{haproxy_dependencies.join(' ')}")
          when /redhat/i
            run("#{sudo} yum install -q -y #{haproxy_dependencies.join(' ')}")
          else
            # nop
          end
        end
      }

      task(:install_service, :roles => :app, :except => { :no_release => true }) {
        # TODO: setup (sysvinit|daemontools|upstart|runit|systemd) service of HAProxy
      }

      _cset(:haproxy_template_path, File.join(File.dirname(__FILE__), 'capistrano-haproxy', 'templates'))
      _cset(:haproxy_configure_files, %w(/etc/default/haproxy haproxy.cfg))
      task(:configure, :roles => :app, :except => { :no_release => true }) {
        haproxy_configure_files.each do |f|
          safe_put(template(f, :path => haproxy_template_path), ( File.expand_path(f) == f ? f : File.join(haproxy_path, f) ),
                   :place => :if_modified, :sudo => true)
        end
      }

      _cset(:haproxy_service_name, 'haproxy')
      desc("Start HAProxy daemon.")
      task(:start, :roles => :app, :except => { :no_release => true }) {
        run("#{sudo} service #{haproxy_service_name} start")
      }

      desc("Stop HAProxy daemon.")
      task(:stop, :roles => :app, :except => { :no_release => true }) {
        run("#{sudo} service #{haproxy_service_name} stop")
      }

      desc("Restart HAProxy daemon.")
      task(:restart, :roles => :app, :except => { :no_release => true }) {
        run("#{sudo} service #{haproxy_service_name} restart || #{sudo} service #{haproxy_service_name} start")
      }

      desc("Reload HAProxy daemon.")
      task(:reload, :roles => :app, :except => { :no_release => true }) {
        run("#{sudo} service #{haproxy_service_name} reload || #{sudo} service #{haproxy_service_name} start")
      }

      desc("Show HAProxy daemon status.")
      task(:status, :roles => :app, :except => { :no_release => true }) {
        run("#{sudo} service #{haproxy_service_name} status")
      }
    }
  }
end