magic_recipes_two 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.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/lib/capistrano/magic_recipes/assets.rb +1 -0
  5. data/lib/capistrano/magic_recipes/base_helpers.rb +63 -0
  6. data/lib/capistrano/magic_recipes/db.rb +1 -0
  7. data/lib/capistrano/magic_recipes/exception_pages.rb +1 -0
  8. data/lib/capistrano/magic_recipes/inform_slack.rb +1 -0
  9. data/lib/capistrano/magic_recipes/monit.rb +1 -0
  10. data/lib/capistrano/magic_recipes/nginx.rb +1 -0
  11. data/lib/capistrano/magic_recipes/redis.rb +1 -0
  12. data/lib/capistrano/magic_recipes/secrets.rb +1 -0
  13. data/lib/capistrano/magic_recipes/sidekiq.rb +1 -0
  14. data/lib/capistrano/magic_recipes/thin.rb +1 -0
  15. data/lib/capistrano/magic_recipes/version.rb +5 -0
  16. data/lib/capistrano/magic_recipes.rb +0 -0
  17. data/lib/capistrano/tasks/assets.rake +45 -0
  18. data/lib/capistrano/tasks/db.rake +20 -0
  19. data/lib/capistrano/tasks/exception_pages.rake +18 -0
  20. data/lib/capistrano/tasks/inform_slack.rake +37 -0
  21. data/lib/capistrano/tasks/monit.rake +173 -0
  22. data/lib/capistrano/tasks/monit_sidekiq.rake +44 -0
  23. data/lib/capistrano/tasks/nginx.rake +105 -0
  24. data/lib/capistrano/tasks/redis.rake +32 -0
  25. data/lib/capistrano/tasks/secrets.rake +43 -0
  26. data/lib/capistrano/tasks/sidekiq.rake +217 -0
  27. data/lib/capistrano/tasks/thin.rake +48 -0
  28. data/lib/generators/capistrano/magic_recipes/templates/capistrano3_nginx_conf.erb +68 -0
  29. data/lib/generators/capistrano/magic_recipes/templates/monit/monitrc.erb +67 -0
  30. data/lib/generators/capistrano/magic_recipes/templates/monit/nginx.erb +9 -0
  31. data/lib/generators/capistrano/magic_recipes/templates/monit/postgresql.erb +6 -0
  32. data/lib/generators/capistrano/magic_recipes/templates/monit/redis.erb +10 -0
  33. data/lib/generators/capistrano/magic_recipes/templates/monit/sidekiq.erb +30 -0
  34. data/lib/generators/capistrano/magic_recipes/templates/monit/thin.erb +13 -0
  35. data/lib/generators/capistrano/magic_recipes/templates/monit/website.erb +9 -0
  36. data/lib/generators/capistrano/magic_recipes/templates/nginx.conf.erb +139 -0
  37. data/lib/generators/capistrano/magic_recipes/templates/postgresql.yml.erb +8 -0
  38. data/lib/generators/capistrano/magic_recipes/templates/secrets_yml.erb +2 -0
  39. data/lib/generators/capistrano/magic_recipes/templates/thin_app_yml.erb +19 -0
  40. data/lib/magic_recipes_two/version.rb +3 -0
  41. data/lib/magic_recipes_two.rb +2 -0
  42. data/lib/tasks/magic_recipes_two_tasks.rake +4 -0
  43. metadata +183 -0
@@ -0,0 +1,43 @@
1
+ require 'capistrano/magic_recipes/base_helpers'
2
+ include Capistrano::MagicRecipes::BaseHelpers
3
+
4
+ namespace :load do
5
+ task :defaults do
6
+ set :secrets_roles, -> { :app }
7
+ set :secrets_key_base, -> { generate_secrect_key }
8
+ set :secrets_key_name, -> { "#{ fetch(:application) }_#{ fetch(:stage) }_SECRET_KEY_BASE".gsub(/-/, "_").gsub(/[^a-zA-Z_]/, "").upcase }
9
+ set :secrets_user_path, -> { "/home/#{fetch(:user)}" }
10
+ end
11
+ end
12
+
13
+ namespace :secrets do
14
+
15
+
16
+ desc "upload secrets yaml"
17
+ task :upload do
18
+ on release_roles fetch(:secrets_roles) do
19
+ within shared_path do
20
+ magic_template("secrets_yml", '/tmp/secrets.yml')
21
+ execute :sudo, :mv, '/tmp/secrets.yml', "config/secrets.yml"
22
+ end
23
+ end
24
+ end
25
+
26
+
27
+ desc "set secret-key in .bashrc"
28
+ task :export do
29
+ on release_roles fetch(:secrets_roles) do
30
+ within fetch(:secrets_user_path) do
31
+ execute :sudo, "echo 'export #{fetch(:secrets_key_name)}=#{fetch(:secrets_key_base)}' | cat >> .bashrc"
32
+ execute "export #{fetch(:secrets_key_name)}=#{fetch(:secrets_key_base)}"
33
+ end
34
+ end
35
+ end
36
+
37
+ desc 'secrets setup task (upload and set)'
38
+ task :setup do
39
+ invoke "secrets:export"
40
+ invoke "secrets:upload"
41
+ end
42
+
43
+ end
@@ -0,0 +1,217 @@
1
+ # https://github.com/seuros/capistrano-sidekiq
2
+ namespace :load do
3
+ task :defaults do
4
+ set :sidekiq_default_hooks, -> { true }
5
+
6
+ set :sidekiq_pid, -> { File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') }
7
+ set :sidekiq_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
8
+ set :sidekiq_log, -> { File.join(shared_path, 'log', 'sidekiq.log') }
9
+ set :sidekiq_timeout, -> { 10 }
10
+ set :sidekiq_roles, -> { :app }
11
+ set :sidekiq_processes, -> { 1 }
12
+ # Rbenv and RVM integration
13
+ set :rbenv_map_bins, fetch(:rbenv_map_bins).to_a.concat(%w(sidekiq sidekiqctl))
14
+ set :rvm_map_bins, fetch(:rvm_map_bins).to_a.concat(%w(sidekiq sidekiqctl))
15
+ end
16
+ end
17
+
18
+
19
+ namespace :deploy do
20
+ before :starting, :check_sidekiq_hooks do
21
+ invoke 'sidekiq:add_default_hooks' if fetch(:sidekiq_default_hooks)
22
+ end
23
+ after :publishing, :restart_sidekiq do
24
+ invoke 'sidekiq:restart' if fetch(:sidekiq_default_hooks)
25
+ end
26
+ end
27
+
28
+
29
+ namespace :sidekiq do
30
+ def for_each_process(reverse = false, &block)
31
+ pids = processes_pids
32
+ pids.reverse! if reverse
33
+ pids.each_with_index do |pid_file, idx|
34
+ within current_path do
35
+ yield(pid_file, idx)
36
+ end
37
+ end
38
+ end
39
+
40
+ def processes_pids
41
+ pids = []
42
+ fetch(:sidekiq_processes).times do |idx|
43
+ pids.push (idx.zero? && fetch(:sidekiq_processes) <= 1) ?
44
+ fetch(:sidekiq_pid) :
45
+ fetch(:sidekiq_pid).gsub(/\.pid$/, "-#{idx}.pid")
46
+
47
+ end
48
+ pids
49
+ end
50
+
51
+ def pid_process_exists?(pid_file)
52
+ pid_file_exists?(pid_file) and test(*("kill -0 $( cat #{pid_file} )").split(' '))
53
+ end
54
+
55
+ def pid_file_exists?(pid_file)
56
+ test(*("[ -f #{pid_file} ]").split(' '))
57
+ end
58
+
59
+ def stop_sidekiq(pid_file)
60
+ if fetch(:stop_sidekiq_in_background, fetch(:sidekiq_run_in_background))
61
+ if fetch(:sidekiq_use_signals)
62
+ background "kill -TERM `cat #{pid_file}`"
63
+ else
64
+ background :bundle, :exec, :sidekiqctl, 'stop', "#{pid_file}", fetch(:sidekiq_timeout)
65
+ end
66
+ else
67
+ execute :bundle, :exec, :sidekiqctl, 'stop', "#{pid_file}", fetch(:sidekiq_timeout)
68
+ end
69
+ end
70
+
71
+ def quiet_sidekiq(pid_file)
72
+ if fetch(:sidekiq_use_signals)
73
+ background "kill -USR1 `cat #{pid_file}`"
74
+ else
75
+ begin
76
+ execute :bundle, :exec, :sidekiqctl, 'quiet', "#{pid_file}"
77
+ rescue SSHKit::Command::Failed
78
+ # If gems are not installed eq(first deploy) and sidekiq_default_hooks as active
79
+ warn 'sidekiqctl not found (ignore if this is the first deploy)'
80
+ end
81
+ end
82
+ end
83
+
84
+ def start_sidekiq(pid_file, idx = 0)
85
+ args = []
86
+ args.push "--index #{idx}"
87
+ args.push "--pidfile #{pid_file}"
88
+ args.push "--environment #{fetch(:sidekiq_env)}"
89
+ args.push "--logfile #{fetch(:sidekiq_log)}" if fetch(:sidekiq_log)
90
+ args.push "--require #{fetch(:sidekiq_require)}" if fetch(:sidekiq_require)
91
+ args.push "--tag #{fetch(:sidekiq_tag)}" if fetch(:sidekiq_tag)
92
+ Array(fetch(:sidekiq_queue)).each do |queue|
93
+ args.push "--queue #{queue}"
94
+ end
95
+ args.push "--config #{fetch(:sidekiq_config)}" if fetch(:sidekiq_config)
96
+ args.push "--concurrency #{fetch(:sidekiq_concurrency)}" if fetch(:sidekiq_concurrency)
97
+ # use sidekiq_options for special options
98
+ args.push fetch(:sidekiq_options) if fetch(:sidekiq_options)
99
+
100
+ if defined?(JRUBY_VERSION)
101
+ args.push '>/dev/null 2>&1 &'
102
+ warn 'Since JRuby doesn\'t support Process.daemon, Sidekiq will not be running as a daemon.'
103
+ else
104
+ args.push '--daemon'
105
+ end
106
+
107
+ if fetch(:start_sidekiq_in_background, fetch(:sidekiq_run_in_background))
108
+ background :bundle, :exec, :sidekiq, args.compact.join(' ')
109
+ else
110
+ execute :bundle, :exec, :sidekiq, args.compact.join(' ')
111
+ end
112
+ end
113
+
114
+ task :add_default_hooks do
115
+ after 'deploy:starting', 'sidekiq:quiet'
116
+ after 'deploy:updated', 'sidekiq:stop'
117
+ after 'deploy:reverted', 'sidekiq:stop'
118
+ after 'deploy:published', 'sidekiq:start'
119
+ end
120
+
121
+ desc 'Quiet sidekiq (stop processing new tasks)'
122
+ task :quiet do
123
+ on roles fetch(:sidekiq_roles) do
124
+ if test("[ -d #{current_path} ]") # fixes #11
125
+ for_each_process(true) do |pid_file, idx|
126
+ if pid_process_exists?(pid_file)
127
+ quiet_sidekiq(pid_file)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ desc 'Stop sidekiq'
135
+ task :stop do
136
+ on roles fetch(:sidekiq_roles) do
137
+ if test("[ -d #{current_path} ]")
138
+ for_each_process(true) do |pid_file, idx|
139
+ if pid_process_exists?(pid_file)
140
+ stop_sidekiq(pid_file)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+
147
+ desc 'Start sidekiq'
148
+ task :start do
149
+ on roles fetch(:sidekiq_roles) do
150
+ for_each_process do |pid_file, idx|
151
+ start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file)
152
+ end
153
+ end
154
+ end
155
+
156
+ desc 'Restart sidekiq'
157
+ task :restart do
158
+ invoke 'sidekiq:stop'
159
+ invoke 'sidekiq:start'
160
+ end
161
+
162
+ desc 'Rolling-restart sidekiq'
163
+ task :rolling_restart do
164
+ on roles fetch(:sidekiq_roles) do
165
+ for_each_process(true) do |pid_file, idx|
166
+ if pid_process_exists?(pid_file)
167
+ stop_sidekiq(pid_file)
168
+ end
169
+ start_sidekiq(pid_file, idx)
170
+ end
171
+ end
172
+ end
173
+
174
+ # Delete any pid file not in use
175
+ task :cleanup do
176
+ on roles fetch(:sidekiq_roles) do
177
+ for_each_process do |pid_file, idx|
178
+ if pid_file_exists?(pid_file)
179
+ execute "rm #{pid_file}" unless pid_process_exists?(pid_file)
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ # TODO : Don't start if all proccess are off, raise warning.
186
+ desc 'Respawn missing sidekiq proccesses'
187
+ task :respawn do
188
+ invoke 'sidekiq:cleanup'
189
+ on roles fetch(:sidekiq_roles) do
190
+ for_each_process do |pid_file, idx|
191
+ unless pid_file_exists?(pid_file)
192
+ start_sidekiq(pid_file, idx)
193
+ end
194
+ end
195
+ end
196
+ end
197
+
198
+ # => def template_sidekiq(from, to, role)
199
+ # => [
200
+ # => File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}-#{fetch(:stage)}.rb"),
201
+ # => File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}.rb"),
202
+ # => File.join('lib', 'capistrano', 'templates', "#{from}-#{fetch(:stage)}.rb"),
203
+ # => File.join('lib', 'capistrano', 'templates', "#{from}.rb.erb"),
204
+ # => File.join('lib', 'capistrano', 'templates', "#{from}.rb"),
205
+ # => File.join('lib', 'capistrano', 'templates', "#{from}.erb"),
206
+ # => File.expand_path("../../templates/#{from}.rb.erb", __FILE__),
207
+ # => File.expand_path("../../templates/#{from}.erb", __FILE__)
208
+ # => ].each do |path|
209
+ # => if File.file?(path)
210
+ # => erb = File.read(path)
211
+ # => upload! StringIO.new(ERB.new(erb).result(binding)), to
212
+ # => break
213
+ # => end
214
+ # => end
215
+ # => end
216
+
217
+ end
@@ -0,0 +1,48 @@
1
+ require 'capistrano/magic_recipes/base_helpers'
2
+ include Capistrano::MagicRecipes::BaseHelpers
3
+
4
+ namespace :load do
5
+ task :defaults do
6
+ set :thin_path, -> { '/etc/thin' }
7
+ set :thin_roles, -> { :web }
8
+ end
9
+ end
10
+
11
+
12
+
13
+ namespace :thin do
14
+
15
+
16
+ desc "rewrite thin-configurations"
17
+ task :reconf => ['nginx:load_vars'] do
18
+ on release_roles fetch(:thin_roles) do
19
+ within current_path do
20
+ magic_template("thin_app_yml", '/tmp/thin_app.yml')
21
+ execute :sudo, :mv, '/tmp/thin_app.yml', "config/thin_app_#{fetch(:stage)}.yml"
22
+ execute :sudo, :rm, ' -f', "#{fetch(:thin_path)}/thin_#{fetch(:application)}_#{fetch(:stage)}*"
23
+ execute :sudo, :ln, ' -sf', "#{current_path}/config/thin_app_#{fetch(:stage)}.yml", "#{fetch(:thin_path)}/thin_#{fetch(:application)}_#{fetch(:stage)}.yml"
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ %w[start stop restart].each do |command|
30
+ desc "#{command} thin"
31
+ task command => ['nginx:load_vars'] do
32
+ on release_roles fetch(:thin_roles) do
33
+ within current_path do
34
+ execute :bundle, :exec, :thin, "#{command} -C config/thin_app_#{fetch(:stage)}.yml"
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+
41
+ end
42
+
43
+ after 'deploy:published', nil do
44
+ on release_roles fetch(:thin_roles) do
45
+ invoke "thin:reconf"
46
+ invoke "thin:restart"
47
+ end
48
+ end
@@ -0,0 +1,68 @@
1
+ <% if fetch(:app_server) && (fetch(:app_server_socket) || fetch(:app_server_port))%>
2
+ # Define App Server Upstream
3
+ upstream <%= fetch(:application) %>-app-server {
4
+ <% if fetch(:app_server_socket) %>
5
+ server unix:<%= fetch(:app_server_socket) %> fail_timeout=0;
6
+ <% elsif fetch(:app_server_port) %>
7
+ server <%= fetch(:app_server_host, '127.0.0.1') %>:<%= fetch(:app_server_port) %> fail_timeout=0;
8
+ <% end %>
9
+ }
10
+ <% end %>
11
+
12
+ # HTTP Server
13
+
14
+ <% if fetch(:nginx_use_ssl) %>
15
+ server {
16
+ listen 80;
17
+ rewrite ^(.*) https://$host$1 permanent;
18
+ }
19
+ <% end %>
20
+
21
+ server {
22
+
23
+ <% if fetch(:nginx_use_ssl) %>
24
+ listen 443;
25
+ ssl on;
26
+ ssl_certificate <%= fetch(:nginx_ssl_certificate_path) %>/<%= fetch(:nginx_ssl_certificate) %>;
27
+ ssl_certificate_key <%= fetch(:nginx_ssl_certificate_key_path) %>/<%= fetch(:nginx_ssl_certificate_key) %>;
28
+ <% else %>
29
+ listen 80;
30
+ <% end %>
31
+
32
+ server_name <%= fetch(:nginx_domains) %>;
33
+ root <%= fetch(:deploy_to) %>/current/<%= fetch(:nginx_static_dir) %>;
34
+
35
+ access_log <%= fetch(:nginx_log_path) %>/nginx-access.log;
36
+ error_log <%= fetch(:nginx_log_path) %>/nginx-error.log;
37
+
38
+ error_page 404 /404.html;
39
+ location /404.html { root <%= fetch(:deploy_to) %>/current/<%= fetch(:nginx_static_dir) %>; }
40
+
41
+ error_page 500 /500.html;
42
+ location /500.html { root <%= fetch (:deploy_to) %>/current/<%= fetch(:nginx_static_dir) %>; }
43
+
44
+ client_max_body_size 4G;
45
+ keepalive_timeout 10;
46
+
47
+ <% if fetch(:app_server) && (fetch(:app_server_socket) || fetch(:app_server_port))%>
48
+ location ^~ /assets/ {
49
+ gzip_static on;
50
+ expires max;
51
+ add_header Cache-Control public;
52
+ }
53
+
54
+ try_files $uri/index.html $uri @<%= fetch(:application) %>-app-server;
55
+
56
+ location @<%= fetch(:application) %>-app-server {
57
+ proxy_set_header X-Real-IP $remote_addr;
58
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
59
+ proxy_set_header X-FORWARDED_PROTO http;
60
+ proxy_set_header Host $http_host;
61
+ <% if fetch(:nginx_use_ssl) %>
62
+ proxy_set_header X-Forwarded-Proto https;
63
+ <% end %>
64
+ proxy_redirect off;
65
+ proxy_pass http://<%= fetch(:application) %>-app-server;
66
+ }
67
+ <% end %>
68
+ }
@@ -0,0 +1,67 @@
1
+ set daemon <%= fetch(:monit_interval) %>
2
+
3
+ set logfile /var/log/monit.log
4
+ set idfile /var/lib/monit/id
5
+ set statefile /var/lib/monit/state
6
+
7
+ set eventqueue
8
+ basedir /var/lib/monit/events
9
+ slots 100
10
+
11
+ set mail-format {
12
+ from: <%= fetch(:monit_mail_from) %>
13
+ reply-to: <%= fetch(:monit_mail_reply_to) %>
14
+ subject: !!! $EVENT on $SERVICE !!!
15
+ message: $EVENT on $SERVICE - $DESCRIPTION.
16
+
17
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18
+ When: $DATE
19
+ What: $EVENT [$ACTION]
20
+ Where: $SERVICE
21
+ More: $DESCRIPTION
22
+ Server: $HOST
23
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24
+
25
+ Your faithful employee,
26
+ M O N I T
27
+
28
+ <% if fetch(:monit_http_client) %><% if fetch(:monit_http_domain) %>
29
+ Web: <%= fetch(:monit_http_use_ssl) ? "https" : "http" %>://<%= fetch(:monit_http_domain) %>:2812/
30
+ <% else %>
31
+ Web: <%= fetch(:monit_http_use_ssl) ? "https" : "http" %>://<%= fetch(:app_server_ip) %>:2812/
32
+ <% end %><% end %>
33
+ }
34
+
35
+ set mailserver <%= fetch(:monit_mail_server) %> port <%= fetch(:monit_mail_port) %>
36
+ username "<%= fetch(:monit_mail_username) %>" password "<%= fetch(:monit_mail_password) %>"
37
+ <% if fetch(:monit_mail_authentication) %>
38
+ using <%= fetch(:monit_mail_authentication) %>
39
+ <% end %>
40
+ with timeout 30 seconds
41
+
42
+ set alert <%= fetch(:monit_mail_to) %>
43
+
44
+ <% if fetch(:monit_http_client) %>
45
+ set httpd port <%= fetch(:monit_http_port) %>
46
+ <% if fetch(:monit_http_domain) %>
47
+ use address <%= fetch(:monit_http_domain) %>
48
+ <% end %>
49
+ # allow careagents.cloudapp.net
50
+ # allow 191.233.74.184
51
+ # allow 127.0.0.1
52
+ <% if fetch(:monit_http_use_ssl) %>
53
+ ssl enable
54
+ pemfile <%= fetch(:monit_http_pemfile) %>
55
+ <% end %>
56
+ allow <%= fetch(:monit_http_username) %>:<%= fetch(:monit_http_password) %>
57
+ <% end %>
58
+
59
+ check system <%= fetch(:application) %>_server
60
+ if loadavg(5min) > 2 for 2 cycles then alert
61
+ if memory > 75% for 2 cycles then alert
62
+ if cpu(user) > 75% for 2 cycles then alert
63
+
64
+ check device HardDrive with path /
65
+ if SPACE usage > 75% then alert
66
+
67
+ include /etc/monit/conf.d/*
@@ -0,0 +1,9 @@
1
+ # Monit configuration for NGINX : <%= fetch(:application) %>
2
+ check process nginx with pidfile /var/run/nginx.pid
3
+ start program = "/etc/init.d/nginx start"
4
+ stop program = "/etc/init.d/nginx stop"
5
+ if children > 250 then restart
6
+ if failed host 127.0.0.1 port 80 then restart
7
+ if cpu is greater than 40% for 2 cycles then alert
8
+ if cpu > 60% for 5 cycles then restart
9
+ if 10 restarts within 10 cycles then timeout
@@ -0,0 +1,6 @@
1
+ # Monit configuration for POSTGRESQL : <%= fetch(:application) %>
2
+ check process postgresql with pidfile <%= fetch(:postgresql_pid) %>
3
+ start program = "/etc/init.d/postgresql start"
4
+ stop program = "/etc/init.d/postgresql stop"
5
+ if failed host localhost port 5432 protocol pgsql then restart
6
+ if 5 restarts within 5 cycles then timeout
@@ -0,0 +1,10 @@
1
+ # Monit configuration for REDIS : <%= fetch(:application) %>
2
+ check process redis with pidfile <%= fetch(:redis_pid) %>
3
+ start program = "/etc/init.d/redis-server start"
4
+ stop program = "/etc/init.d/redis-server stop"
5
+ if 2 restarts within 3 cycles then timeout
6
+ if totalmem > 100 Mb then alert
7
+ if children > 255 for 5 cycles then stop
8
+ if cpu usage > 95% for 3 cycles then restart
9
+ if failed host 127.0.0.1 port 6379 then restart
10
+ if 5 restarts within 5 cycles then timeout
@@ -0,0 +1,30 @@
1
+ # Monit configuration for SIDEKIQ : <%= fetch(:application) %> (<%= fetch(:stage) %>)
2
+ <% processes_pids.each_with_index do |pid_file, idx| %>
3
+ <%
4
+ args = []
5
+ args.push "--index #{idx}"
6
+ args.push "--pidfile #{pid_file}"
7
+ args.push "--environment #{fetch(:sidekiq_env)}"
8
+ args.push "--logfile #{fetch(:sidekiq_log)}" if fetch(:sidekiq_log)
9
+ args.push "--require #{fetch(:sidekiq_require)}" if fetch(:sidekiq_require)
10
+ args.push "--tag #{fetch(:sidekiq_tag)}" if fetch(:sidekiq_tag)
11
+ Array(fetch(:sidekiq_queue)).each do |queue|
12
+ args.push "--queue #{queue}"
13
+ end
14
+ args.push "--config #{fetch(:sidekiq_config)}" if fetch(:sidekiq_config)
15
+ args.push "--concurrency #{fetch(:sidekiq_concurrency)}" if fetch(:sidekiq_concurrency)
16
+ # use sidekiq_options for special options
17
+ args.push fetch(:sidekiq_options) if fetch(:sidekiq_options)
18
+ args.push '--daemon'
19
+ %>
20
+ check process <%= sidekiq_service_name(idx) %>
21
+ with pidfile "<%= pid_file %>"
22
+ start program = "/bin/su - <%= @role.user %> -c 'cd <%= current_path %> ; bundle exec sidekiq <%= args.join(" ") %>'" with timeout 30 seconds
23
+ stop program = "/bin/su - <%= @role.user %> -c 'cd <%= current_path %> ; bundle exec sidekiqctl stop <%= pid_file %>' " with timeout <%= fetch(:sidekiq_timeout).to_i + 10 %> seconds
24
+ group <%= fetch(:sidekiq_monit_group, fetch(:application)) %>-sidekiq
25
+ if totalmem is greater than 200 MB for 2 cycles then restart
26
+ if 3 restarts within 5 cycles then timeout
27
+ alert <%= fetch(:monit_mail_to) %> only on { pid }
28
+ if 2 restarts within 3 cycles then alert
29
+ if changed pid 2 times within 10 cycles then alert
30
+ <% end %>
@@ -0,0 +1,13 @@
1
+ # Monit configuration for THIN : <%= fetch(:application) %> (<%= fetch(:stage) %>)
2
+ <% fetch(:app_instances, 1).times do |n| %>
3
+ check process <%= fetch(:application) %>_<%= fetch(:stage) %>_thin_<%= n %> with pidfile <%= deploy_to %>/shared/pids/thin_<%= fetch(:application) %>_<%= fetch(:stage) %>.<%= n %>.pid
4
+ group thin-<%= fetch(:application) %>
5
+ group thin-<%= fetch(:stage) %>
6
+ start program = "/bin/su - <%= fetch(:user) %> -c 'cd <%= current_path %> ; bundle exec thin start -C config/thin_app_<%= fetch(:stage) %>.yml -o <%= n %>' "
7
+ stop program = "/bin/su - <%= fetch(:user) %> -c 'cd <%= current_path %> ; bundle exec thin stop -C config/thin_app_<%= fetch(:stage) %>.yml -o <%= n %>' "
8
+ if mem > 200.0 MB for 1 cycles then restart
9
+ if cpu > 50% for 3 cycles then restart
10
+ if 5 restarts within 5 cycles then timeout
11
+ alert <%= fetch(:monit_mail_to) %> only on { pid }
12
+ if changed pid 2 times within 20 cycles then alert
13
+ <% end %>
@@ -0,0 +1,9 @@
1
+ # Check domains on port 80 <%= "(and 443 if ssl is on)" if fetch(:nginx_use_ssl) %> for <%= fetch(:application) %> [<%= fetch(:stage) %>]
2
+ <% Array(fetch(:nginx_domains)).each do |domain| %>
3
+ <% domain = domain.gsub(/^\*?\./, "") %>
4
+ check host <%= domain %> with address <%= fetch(:app_server_ip) %>
5
+ if failed port 80 protocol http then alert
6
+ # <% if fetch(:nginx_use_ssl) %>if failed port 443 type TCPSSL protocol http then alert<% end %>
7
+
8
+
9
+ <% end %>
@@ -0,0 +1,139 @@
1
+ upstream thin_<%= fetch(:application) %>_<%= fetch(:stage) %>_cluster {
2
+ <% fetch(:app_instances).to_i.times do |i| %>
3
+ server unix:/tmp/thin.<%= fetch(:application) %>.<%= fetch(:stage) %>.<%= i %>.sock max_fails=1 fail_timeout=15s;
4
+ <% end %>
5
+ }
6
+
7
+ # HTTP Server
8
+ <% if fetch(:nginx_use_ssl) %>
9
+ <% if fetch(:nginx_major_domain) %>
10
+ server {
11
+ listen 80<%= ' default_server' if fetch(:default_site) %>;
12
+ server_name <%= Array(fetch(:nginx_domains)).map{ |d| d.gsub(/^\*?\./, "") }.join("\n ") %>
13
+ <%= ".#{fetch(:nginx_major_domain).gsub(/^\*?\./, "")}" %>;
14
+
15
+ # return 301 https://<%= fetch(:nginx_major_domain).gsub(/^\*?\./, "") %>$request_uri;
16
+
17
+ location ^~ /assets/ico/ {
18
+ root <%= current_path %>/public;
19
+ gzip_static on;
20
+ expires max;
21
+ add_header Cache-Control public;
22
+ }
23
+ location / {
24
+ return 301 https://<%= fetch(:nginx_major_domain).gsub(/^\*?\./, "") %>$request_uri;
25
+ }
26
+
27
+ }
28
+ server {
29
+ listen 80;
30
+ server_name <%= Array(fetch(:nginx_domains)).map{ |d| "~^(?<sub>\w+)#{ Regexp.escape( ".#{d.gsub(/^\*?\./, "")}" ) }" }.join("\n ") %>
31
+ <%= "~^#{Regexp.escape("www.")}(?<sub>\w+)#{ Regexp.escape( ".#{fetch(:nginx_major_domain).gsub(/^\*?\./, "")}" ) }" %>
32
+ <%= "~^(?<sub>\w+)#{ Regexp.escape( ".#{fetch(:nginx_major_domain).gsub(/^\*?\./, "")}" ) }" %>;
33
+
34
+ # return 301 https://$sub.<%= fetch(:nginx_major_domain).gsub(/^\*?\./, "") %>$request_uri;
35
+
36
+ location ^~ /assets/ico/ {
37
+ root <%= current_path %>/public;
38
+ gzip_static on;
39
+ expires max;
40
+ add_header Cache-Control public;
41
+ }
42
+ location / {
43
+ return 301 https://$sub.<%= fetch(:nginx_major_domain).gsub(/^\*?\./, "") %>$request_uri;
44
+ }
45
+
46
+ }
47
+ <% else %>
48
+ server {
49
+ listen 80;
50
+ server_name <%= Array(fetch(:nginx_domains)).map{ |d| d[0] == "." ? d : ".#{d}"}.join("\n ") %>;
51
+ # return 301 https://$host$request_uri;
52
+
53
+ location ^~ /assets/ico/ {
54
+ root <%= current_path %>/public;
55
+ gzip_static on;
56
+ expires max;
57
+ add_header Cache-Control public;
58
+ }
59
+ location / {
60
+ return 301 https://$host$request_uri;
61
+ }
62
+
63
+ }
64
+ <% end %>
65
+ <% end %>
66
+
67
+ <% if fetch(:nginx_major_domain) %>
68
+ server {
69
+ listen 443;
70
+ server_name <%= Array(fetch(:nginx_domains)).map{ |d| d.gsub(/^\*?\./, "") }.join("\n ") %>;
71
+ return 301 https://<%= fetch(:nginx_major_domain).gsub(/^\*?\./, "") %>$request_uri;
72
+ ssl on;
73
+ ssl_certificate <%= fetch(:nginx_ssl_certificate_path) %>/<%= fetch(:nginx_old_ssl_certificate) %>;
74
+ ssl_certificate_key <%= fetch(:nginx_ssl_certificate_key_path) %>/<%= fetch(:nginx_old_ssl_certificate_key) %>;
75
+ }
76
+ server {
77
+ listen 443;
78
+ server_name <%= Array(fetch(:nginx_domains)).map{ |d| "~^(?<sub>\w+)\.#{ Regexp.escape( d.gsub(/^\*?\./, "") ) }" }.join("\n ") %>;
79
+ return 301 https://$sub.<%= fetch(:nginx_major_domain).gsub(/^\*?\./, "") %>$request_uri;
80
+ ssl on;
81
+ ssl_certificate <%= fetch(:nginx_ssl_certificate_path) %>/<%= fetch(:nginx_old_ssl_certificate) %>;
82
+ ssl_certificate_key <%= fetch(:nginx_ssl_certificate_key_path) %>/<%= fetch(:nginx_old_ssl_certificate_key) %>;
83
+ }
84
+ <% end %>
85
+
86
+ server {
87
+ <% if fetch(:nginx_use_ssl) %>
88
+ listen 443<%= ' default_server ssl' if fetch(:default_site) %>;
89
+ ssl on;
90
+ ssl_certificate <%= fetch(:nginx_ssl_certificate_path) %>/<%= fetch(:nginx_ssl_certificate) %>;
91
+ ssl_certificate_key <%= fetch(:nginx_ssl_certificate_key_path) %>/<%= fetch(:nginx_ssl_certificate_key) %>;
92
+ <% else %>
93
+ listen 80<%= ' default deferred' if fetch(:default_site) %>;
94
+ <% end %>
95
+ <% if fetch(:nginx_major_domain) %>
96
+ server_name <%= ".#{fetch(:nginx_major_domain).gsub(/^\*?\./, "")}" %>;
97
+ <% else %>
98
+ server_name <%= Array( fetch(:nginx_domains) ).join("\n ") %>;
99
+ <% end %>
100
+
101
+ if ($host ~* ^www\.(.*)) {
102
+ set $host_without_www $1;
103
+ rewrite ^(.*) http://$host_without_www$1 permanent;
104
+ }
105
+
106
+ root <%= current_path %>/public;
107
+
108
+ access_log <%= fetch(:nginx_log_path) %>/nginx-access.log;
109
+ error_log <%= fetch(:nginx_log_path) %>/nginx-error.log;
110
+
111
+ error_page 404 /404.html;
112
+ location /404.html { root <%= fetch(:deploy_to) %>/current/<%= fetch(:nginx_static_dir) %>; }
113
+
114
+ error_page 500 502 503 504 /500.html;
115
+ location /500.html { root <%= fetch (:deploy_to) %>/current/<%= fetch(:nginx_static_dir) %>; }
116
+
117
+ client_max_body_size 4G;
118
+ keepalive_timeout 10;
119
+
120
+ location ^~ /assets/ {
121
+ gzip_static on;
122
+ expires max;
123
+ add_header Cache-Control public;
124
+ }
125
+
126
+ try_files $uri/index.html $uri @thin_<%= fetch(:application) %>_<%= fetch(:stage) %>;
127
+
128
+ location @thin_<%= fetch(:application) %>_<%= fetch(:stage) %> {
129
+ proxy_set_header X-Real-IP $remote_addr;
130
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
131
+ proxy_set_header X-FORWARDED_PROTO http;
132
+ proxy_set_header Host $host:$server_port;
133
+ <% if fetch(:nginx_use_ssl) %>
134
+ proxy_set_header X-Forwarded-Proto https;
135
+ <% end %>
136
+ proxy_redirect off;
137
+ proxy_pass http://thin_<%= fetch(:application) %>_<%= fetch(:stage) %>_cluster;
138
+ }
139
+ }
@@ -0,0 +1,8 @@
1
+ <%= fetch :stage %>:
2
+ adapter: postgresql
3
+ encoding: <%= fetch :pg_encoding %>
4
+ database: <%= fetch :pg_database %>
5
+ pool: <%= fetch :pg_pool %>
6
+ username: <%= fetch :pg_user %>
7
+ password: '<%= fetch :pg_password %>'
8
+ host: <%= fetch :pg_host %>