capistrano3-puma 1.2.1 → 6.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +0,0 @@
1
- namespace :load do
2
- task :defaults do
3
- set :puma_jungle_conf, '/etc/puma.conf'
4
- set :puma_run_path, '/usr/local/bin/run-puma'
5
- end
6
- end
7
-
8
-
9
- namespace :puma do
10
- namespace :jungle do
11
-
12
- desc 'Install Puma jungle'
13
- task :install do
14
- on roles(fetch(:puma_role)) do |role|
15
- @role = role
16
- template_puma 'run-puma', "#{fetch(:tmp_dir)}/run-puma", role
17
- execute "chmod +x #{fetch(:tmp_dir)}/run-puma"
18
- sudo "mv #{fetch(:tmp_dir)}/run-puma #{fetch(:puma_run_path)}"
19
- if test '[ -f /etc/redhat-release ]'
20
- #RHEL flavor OS
21
- rhel_install
22
- elsif test '[ -f /etc/lsb-release ]'
23
- #Debian flavor OS
24
- debian_install
25
- else
26
- #Some other OS
27
- error 'This task is not supported for your OS'
28
- end
29
- sudo "touch #{fetch(:puma_jungle_conf)}"
30
- end
31
- end
32
-
33
-
34
- def debian_install
35
- template_puma 'puma-deb', "#{fetch(:tmp_dir)}/puma", @role
36
- execute "chmod +x #{fetch(:tmp_dir)}/puma"
37
- sudo "mv #{fetch(:tmp_dir)}/puma /etc/init.d/puma"
38
- sudo 'update-rc.d -f puma defaults'
39
-
40
- end
41
-
42
- def rhel_install
43
- template_puma 'puma-rpm', "#{fetch(:tmp_dir)}/puma" , @role
44
- execute "chmod +x #{fetch(:tmp_dir)}/puma"
45
- sudo "mv #{fetch(:tmp_dir)}/puma /etc/init.d/puma"
46
- sudo 'chkconfig --add puma'
47
- end
48
-
49
-
50
- desc 'Setup Puma config and install jungle script'
51
- task :setup do
52
- invoke 'puma:config'
53
- invoke 'puma:jungle:install'
54
- invoke 'puma:jungle:add'
55
- end
56
-
57
- desc 'Add current project to the jungle'
58
- task :add do
59
- on roles(fetch(:puma_role)) do|role|
60
- sudo "/etc/init.d/puma add '#{current_path}' #{fetch(:puma_user, role.user)}"
61
- end
62
- end
63
-
64
- desc 'Remove current project from the jungle'
65
- task :remove do
66
- on roles(fetch(:puma_role)) do
67
- sudo "/etc/init.d/puma remove '#{current_path}'"
68
- end
69
- end
70
-
71
- %w[start stop restart status].each do |command|
72
- desc "#{command} puma"
73
- task command do
74
- on roles(fetch(:puma_role)) do
75
- sudo "service puma #{command} #{current_path}"
76
- end
77
- end
78
- end
79
-
80
- end
81
- end
@@ -1,72 +0,0 @@
1
- namespace :load do
2
- task :defaults do
3
- set :puma_monit_conf_dir, -> { "/etc/monit/conf.d/#{puma_monit_service_name}.conf" }
4
- set :puma_monit_use_sudo, true
5
- set :puma_monit_bin, '/usr/bin/monit'
6
- end
7
- end
8
-
9
- namespace :puma do
10
- namespace :monit do
11
- desc 'Config Puma monit-service'
12
- task :config do
13
- on roles(fetch(:puma_role)) do |role|
14
- @role = role
15
- template_puma 'puma_monit.conf', "#{fetch(:tmp_dir)}/monit.conf", @role
16
- sudo_if_needed "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:puma_monit_conf_dir)}"
17
- sudo_if_needed "#{fetch(:puma_monit_bin)} reload"
18
- end
19
- end
20
-
21
- desc 'Monitor Puma monit-service'
22
- task :monitor do
23
- on roles(fetch(:puma_role)) do
24
- sudo_if_needed "#{fetch(:puma_monit_bin)} monitor #{puma_monit_service_name}"
25
- end
26
- end
27
-
28
- desc 'Unmonitor Puma monit-service'
29
- task :unmonitor do
30
- on roles(fetch(:puma_role)) do
31
- sudo_if_needed "#{fetch(:puma_monit_bin)} unmonitor #{puma_monit_service_name}"
32
- end
33
- end
34
-
35
- desc 'Start Puma monit-service'
36
- task :start do
37
- on roles(fetch(:puma_role)) do
38
- sudo_if_needed "#{fetch(:puma_monit_bin)} start #{puma_monit_service_name}"
39
- end
40
- end
41
-
42
- desc 'Stop Puma monit-service'
43
- task :stop do
44
- on roles(fetch(:puma_role)) do
45
- sudo_if_needed "#{fetch(:puma_monit_bin)} stop #{puma_monit_service_name}"
46
- end
47
- end
48
-
49
- desc 'Restart Puma monit-service'
50
- task :restart do
51
- on roles(fetch(:puma_role)) do
52
- sudo_if_needed "#{fetch(:puma_monit_bin)} restart #{puma_monit_service_name}"
53
- end
54
- end
55
-
56
- before 'deploy:updating', 'puma:monit:unmonitor'
57
- after 'deploy:published', 'puma:monit:monitor'
58
-
59
- def puma_monit_service_name
60
- fetch(:puma_monit_service_name, "puma_#{fetch(:application)}_#{fetch(:stage)}")
61
- end
62
-
63
- def sudo_if_needed(command)
64
- if fetch(:puma_monit_use_sudo)
65
- sudo command
66
- else
67
- execute command
68
- end
69
- end
70
-
71
- end
72
- end
@@ -1,197 +0,0 @@
1
-
2
- namespace :load do
3
- task :defaults do
4
- set :puma_default_hooks, -> { true }
5
- set :puma_role, :app
6
- set :puma_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
7
- # Configure "min" to be the minimum number of threads to use to answer
8
- # requests and "max" the maximum.
9
- set :puma_threads, [0, 16]
10
- set :puma_workers, 0
11
- set :puma_rackup, -> { File.join(current_path, 'config.ru') }
12
- set :puma_state, -> { File.join(shared_path, 'tmp', 'pids', 'puma.state') }
13
- set :puma_pid, -> { File.join(shared_path, 'tmp', 'pids', 'puma.pid') }
14
- set :puma_bind, -> { File.join("unix://#{shared_path}", 'tmp', 'sockets', 'puma.sock') }
15
- set :puma_default_control_app, -> { File.join("unix://#{shared_path}", 'tmp', 'sockets', 'pumactl.sock') }
16
- set :puma_conf, -> { File.join(shared_path, 'puma.rb') }
17
- set :puma_access_log, -> { File.join(shared_path, 'log', 'puma_access.log') }
18
- set :puma_error_log, -> { File.join(shared_path, 'log', 'puma_error.log') }
19
- set :puma_init_active_record, false
20
- set :puma_preload_app, false
21
-
22
- # Rbenv and RVM integration
23
- set :rbenv_map_bins, fetch(:rbenv_map_bins).to_a.concat(%w{ puma pumactl })
24
- set :rvm_map_bins, fetch(:rvm_map_bins).to_a.concat(%w{ puma pumactl })
25
-
26
- # Nginx and puma configuration
27
- set :nginx_config_name, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
28
- set :nginx_sites_available_path, -> { '/etc/nginx/sites-available' }
29
- set :nginx_sites_enabled_path, -> { '/etc/nginx/sites-enabled' }
30
- set :nginx_server_name, -> { "localhost #{fetch(:application)}.local" }
31
- set :nginx_flags, -> { 'fail_timeout=0' }
32
- set :nginx_http_flags, -> { fetch(:nginx_flags) }
33
- set :nginx_socket_flags, -> { fetch(:nginx_flags) }
34
- set :nginx_use_ssl, false
35
- end
36
- end
37
-
38
- namespace :deploy do
39
- before :starting, :check_puma_hooks do
40
- invoke 'puma:add_default_hooks' if fetch(:puma_default_hooks)
41
- end
42
- end
43
-
44
- namespace :puma do
45
-
46
- desc 'Setup Puma config file'
47
- task :config do
48
- on roles(fetch(:puma_role)) do |role|
49
- template_puma 'puma', fetch(:puma_conf), role
50
- end
51
- end
52
-
53
- desc 'Start puma'
54
- task :start do
55
- on roles (fetch(:puma_role)) do |role|
56
- puma_switch_user(role) do
57
- if test "[ -f #{fetch(:puma_conf)} ]"
58
- info "using conf file #{fetch(:puma_conf)}"
59
- else
60
- invoke 'puma:config'
61
- end
62
- within current_path do
63
- with rack_env: fetch(:puma_env) do
64
- execute :bundle, 'exec', :puma, "-C #{fetch(:puma_conf)} --daemon"
65
- end
66
- end
67
- end
68
- end
69
- end
70
-
71
- %w[halt stop status].map do |command|
72
- desc "#{command} puma"
73
- task command do
74
- on roles (fetch(:puma_role)) do |role|
75
- within current_path do
76
- puma_switch_user(role) do
77
- with rack_env: fetch(:puma_env) do
78
- if test "[ -f #{fetch(:puma_pid)} ]"
79
- if test "kill -0 $( cat #{fetch(:puma_pid)} )"
80
- execute :bundle, 'exec', :pumactl, "-S #{fetch(:puma_state)} #{command}"
81
- else
82
- # delete invalid pid file , process is not running.
83
- execute :rm, fetch(:puma_pid)
84
- end
85
- else
86
- #pid file not found, so puma is probably not running or it using another pidfile
87
- warn 'Puma not running'
88
- end
89
- end
90
- end
91
- end
92
- end
93
- end
94
- end
95
-
96
- %w[phased-restart restart].map do |command|
97
- desc "#{command} puma"
98
- task command do
99
- on roles (fetch(:puma_role)) do |role|
100
- within current_path do
101
- puma_switch_user(role) do
102
- with rack_env: fetch(:puma_env) do
103
- if test "[ -f #{fetch(:puma_pid)} ]" and test "kill -0 $( cat #{fetch(:puma_pid)} )"
104
- # NOTE pid exist but state file is nonsense, so ignore that case
105
- execute :bundle, 'exec', :pumactl, "-S #{fetch(:puma_state)} #{command}"
106
- else
107
- # Puma is not running or state file is not present : Run it
108
- invoke 'puma:start'
109
- end
110
- end
111
- end
112
- end
113
- end
114
- end
115
- end
116
-
117
- task :check do
118
- on roles (fetch(:puma_role)) do |role|
119
- #Create puma.rb for new deployments
120
- unless test "[ -f #{fetch(:puma_conf)} ]"
121
- warn 'puma.rb NOT FOUND!'
122
- #TODO DRY
123
- template_puma 'puma', fetch(:puma_conf), role
124
- info 'puma.rb generated'
125
- end
126
- end
127
- end
128
-
129
-
130
- task :smart_restart do
131
- if !puma_preload_app? && puma_workers.to_i > 1
132
- invoke 'puma:phased-restart'
133
- else
134
- invoke 'puma:restart'
135
- end
136
- end
137
-
138
- def puma_switch_user(role, &block)
139
- user = puma_user(role)
140
- if user == role.user
141
- block.call
142
- else
143
- as user do
144
- block.call
145
- end
146
- end
147
- end
148
-
149
- def puma_user(role)
150
- properties = role.properties
151
- properties.fetch(:puma_user) || # local property for puma only
152
- properties.fetch(:run_as) || # global property across multiple capistrano gems
153
- role.user
154
- end
155
-
156
- def puma_workers
157
- fetch(:puma_workers, 0)
158
- end
159
-
160
- def puma_preload_app?
161
- fetch(:puma_preload_app)
162
- end
163
-
164
- def puma_bind
165
- Array(fetch(:puma_bind)).collect do |bind|
166
- "bind '#{bind}'"
167
- end.join("\n")
168
- end
169
-
170
- def template_puma(from, to, role)
171
- [
172
- "lib/capistrano/templates/#{from}-#{role.hostname}-#{fetch(:stage)}.rb",
173
- "lib/capistrano/templates/#{from}-#{role.hostname}.rb",
174
- "lib/capistrano/templates/#{from}-#{fetch(:stage)}.rb",
175
- "lib/capistrano/templates/#{from}.rb.erb",
176
- "lib/capistrano/templates/#{from}.rb",
177
- "lib/capistrano/templates/#{from}.erb",
178
- "config/deploy/templates/#{from}.rb.erb",
179
- "config/deploy/templates/#{from}.rb",
180
- "config/deploy/templates/#{from}.erb",
181
- File.expand_path("../../templates/#{from}.rb.erb", __FILE__),
182
- File.expand_path("../../templates/#{from}.erb", __FILE__)
183
- ].each do |path|
184
- if File.file?(path)
185
- erb = File.read(path)
186
- upload! StringIO.new(ERB.new(erb).result(binding)), to
187
- break
188
- end
189
- end
190
- end
191
-
192
- task :add_default_hooks do
193
- after 'deploy:check', 'puma:check'
194
- after 'deploy:finished', 'puma:smart_restart'
195
- end
196
-
197
- end
@@ -1,38 +0,0 @@
1
- namespace :puma do
2
- namespace :workers do
3
- desc 'Add a worker'
4
- task :count do
5
- on roles (fetch(:puma_role)) do |role|
6
- puma_switch_user(role) do
7
- #TODO
8
- # cleanup
9
- # add host name/ip
10
- workers_count = capture("ps ax | grep -c 'puma: cluster worker: `cat #{fetch(:puma_pid)}`'").to_i - 1
11
- log "Workers count : #{workers_count}"
12
- end
13
- end
14
- end
15
-
16
- # TODO
17
- # Add/remove workers to specific host/s
18
- # Define # of workers to add/remove
19
- # Refactor
20
- desc 'Worker++'
21
- task :more do
22
- on roles (fetch(:puma_role)) do |role|
23
- puma_switch_user(role) do
24
- execute("kill -TTIN `cat #{fetch(:puma_pid)}`")
25
- end
26
- end
27
- end
28
-
29
- desc 'Worker--'
30
- task :less do
31
- on roles (fetch(:puma_role)) do |role|
32
- puma_switch_user(role) do
33
- execute("kill -TTOU `cat #{fetch(:puma_pid)}`")
34
- end
35
- end
36
- end
37
- end
38
- end