mina-stack 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +62 -0
  6. data/Rakefile +1 -0
  7. data/lib/generators/mina/stack/install_generator.rb +43 -0
  8. data/lib/generators/mina/templates/deploy.rb +74 -0
  9. data/lib/generators/mina/templates/production.rb +13 -0
  10. data/lib/mina-stack/base.rb +30 -0
  11. data/lib/mina-stack/defaults.rb +108 -0
  12. data/lib/mina-stack/setup.rb +50 -0
  13. data/lib/mina-stack/version.rb +5 -0
  14. data/lib/mina-stack.rb +9 -0
  15. data/lib/recipes/bower.rb +43 -0
  16. data/lib/recipes/elastic_search.rb +31 -0
  17. data/lib/recipes/imagemagick.rb +11 -0
  18. data/lib/recipes/libs.rb +14 -0
  19. data/lib/recipes/memcached.rb +21 -0
  20. data/lib/recipes/monit.rb +54 -0
  21. data/lib/recipes/nginx.rb +49 -0
  22. data/lib/recipes/node.rb +12 -0
  23. data/lib/recipes/postgresql.rb +58 -0
  24. data/lib/recipes/private_pub.rb +35 -0
  25. data/lib/recipes/puma.rb +31 -0
  26. data/lib/recipes/rails.rb +22 -0
  27. data/lib/recipes/rbenv.rb +28 -0
  28. data/lib/recipes/redis.rb +23 -0
  29. data/lib/recipes/sidekiq.rb +42 -0
  30. data/lib/recipes/unicorn.rb +21 -0
  31. data/lib/templates/application.yml.erb +4 -0
  32. data/lib/templates/database.yml.erb +8 -0
  33. data/lib/templates/memcached.conf.erb +16 -0
  34. data/lib/templates/monit/memcached.erb +8 -0
  35. data/lib/templates/monit/monitrc.erb +21 -0
  36. data/lib/templates/monit/nginx.erb +5 -0
  37. data/lib/templates/monit/postgresql.erb +5 -0
  38. data/lib/templates/monit/private_pub.erb +6 -0
  39. data/lib/templates/monit/puma.erb +7 -0
  40. data/lib/templates/monit/redis.erb +4 -0
  41. data/lib/templates/monit/sidekiq.erb +6 -0
  42. data/lib/templates/monit/unicorn.erb +14 -0
  43. data/lib/templates/nginx.conf.erb +43 -0
  44. data/lib/templates/private_pub.yml.erb +4 -0
  45. data/lib/templates/puma.rb.erb +17 -0
  46. data/lib/templates/sidekiq.yml.erb +9 -0
  47. data/lib/templates/unicorn.rb.erb +41 -0
  48. data/lib/templates/unicorn_init.erb +86 -0
  49. data/lib/templates/upstart/puma.conf.erb +46 -0
  50. data/lib/templates/upstart/sidekiq.conf.erb +44 -0
  51. data/mina-stack.gemspec +25 -0
  52. data/mina-stack.rb +7 -0
  53. metadata +151 -0
@@ -0,0 +1,49 @@
1
+ ###########################################################################
2
+ # nginx Tasks
3
+ ###########################################################################
4
+
5
+ namespace :nginx do
6
+
7
+ desc "Install latest stable release of nginx"
8
+ task :install do
9
+ invoke :sudo
10
+ queue "sudo add-apt-repository ppa:nginx/stable --yes"
11
+ queue "sudo apt-get -y update"
12
+ queue "sudo apt-get -y install nginx"
13
+ end
14
+
15
+ desc "Upload and update (link) all nginx config file"
16
+ task :setup => [:upload, :link, :reload]
17
+
18
+ desc "Symlink config file"
19
+ task :link do
20
+ invoke :sudo
21
+ queue %{echo "-----> Symlink nginx config file"}
22
+ queue echo_cmd %{sudo ln -fs "#{config_path}/nginx.conf" "#{nginx_config}"}
23
+ queue check_symlink nginx_config
24
+ queue echo_cmd %{sudo ln -fs "#{config_path}/nginx.conf" "#{nginx_config_e}"}
25
+ queue check_symlink nginx_config_e
26
+ end
27
+
28
+ desc "Parses nginx config file and uploads it to server"
29
+ task :upload do
30
+ template 'nginx.conf.erb', "#{config_path}/nginx.conf"
31
+ end
32
+
33
+ desc "Parses config file and outputs it to STDOUT (local task)"
34
+ task :parse do
35
+ puts "#"*80
36
+ puts "# nginx.conf"
37
+ puts "#"*80
38
+ puts erb("#{config_templates_path}/nginx.conf.erb")
39
+ end
40
+
41
+ %w(stop start restart reload status).each do |action|
42
+ desc "#{action.capitalize} Nginx"
43
+ task action.to_sym do
44
+ invoke :sudo
45
+ queue %{echo "-----> #{action.capitalize} Nginx"}
46
+ queue echo_cmd "sudo service nginx #{action}"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,12 @@
1
+ namespace :node do
2
+ desc "Install the latest relase of Node.js"
3
+ task :install do
4
+ invoke :sudo
5
+ queue "sudo apt-get -y update"
6
+ queue "sudo apt-get -y install software-properties-common python-software-properties python g++ make"
7
+ queue "sudo add-apt-repository ppa:chris-lea/node.js --yes"
8
+ queue "sudo apt-get -y update"
9
+ queue "sudo apt-get -y install nodejs"
10
+ end
11
+ task(:setup) { }
12
+ end
@@ -0,0 +1,58 @@
1
+ namespace :postgresql do
2
+
3
+ require 'highline/import'
4
+
5
+ desc "Install the latest stable release of PostgreSQL."
6
+ task :install do
7
+ invoke :sudo
8
+ queue "sudo add-apt-repository ppa:pitti/postgresql --yes"
9
+ queue "sudo apt-get -y update"
10
+ queue "sudo apt-get -y install postgresql libpq-dev"
11
+ end
12
+
13
+ task :setup => [:upload]
14
+
15
+ desc "Create configuration and other files"
16
+ task :upload do
17
+ template "database.yml.erb"
18
+ queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
19
+ end
20
+
21
+ desc "Create database"
22
+ task :create_db do
23
+ queue %{echo "-----> Create database"}
24
+ invoke :sudo
25
+ ask "PostgreSQL password:" do |psql_password|
26
+ psql_password.echo = "x"
27
+ queue echo_cmd %{sudo -u postgres psql -c "create user #{psql_user} with password '#{psql_password}';"}
28
+ queue echo_cmd %{sudo -u postgres psql -c "create database #{psql_database} owner #{psql_user};"}
29
+ end
30
+ end
31
+
32
+ RYAML = <<-BASH
33
+ function ryaml {
34
+ ruby -ryaml -e 'puts ARGV[1..-1].inject(YAML.load(File.read(ARGV[0]))) {|acc, key| acc[key] }' "$@"
35
+ };
36
+ BASH
37
+
38
+ desc 'Pull remote db in development'
39
+ task :pull do
40
+ code = isolate do
41
+ invoke :environment
42
+ queue RYAML
43
+ queue "USERNAME=$(ryaml #{config_path!}/database.yml #{rails_env!} username)"
44
+ queue "PASSWORD=$(ryaml #{config_path!}/database.yml #{rails_env!} password)"
45
+ queue "DATABASE=$(ryaml #{config_path!}/database.yml #{rails_env!} database)"
46
+ queue %{echo "-----> Database $DATABASE will be dumped locally"}
47
+ queue "PGPASSWORD=$PASSWORD pg_dump -w -U $USERNAME $DATABASE -f #{tmp_path}/dump.sql"
48
+ queue "gzip -f #{tmp_path}/dump.sql"
49
+ run!
50
+ end
51
+
52
+ %x[scp #{user}@#{domain}:#{tmp_path}/dump.sql.gz .]
53
+ %x[gunzip -f dump.sql.gz]
54
+ %x[#{RYAML} psql -d $(ryaml config/database.yml development database) -f dump.sql]
55
+ %x[rm dump.sql]
56
+ end
57
+
58
+ end
@@ -0,0 +1,35 @@
1
+ namespace :private_pub do
2
+
3
+ task(:install) { }
4
+
5
+ desc "Setup Private Pub configuration"
6
+ task :setup => [:upload]
7
+
8
+ desc "Create configuration and other files"
9
+ task :upload do
10
+ template "private_pub.yml.erb", private_pub_config
11
+ queue %[echo "-----> Be sure to edit #{private_pub_config}."]
12
+ end
13
+
14
+ desc "Stop Private Pub"
15
+ task :stop do
16
+ queue %[ if [ -f #{private_pub_pid} ]; then
17
+ echo "-----> Stop Private Pub"
18
+ kill -s QUIT `cat #{private_pub_pid}` || true
19
+ fi ]
20
+ end
21
+
22
+ desc "Start Private Pub"
23
+ task :start do
24
+ queue %{
25
+ echo "-----> Start Private Pub"
26
+ #{echo_cmd %[(cd #{deploy_to}/#{current_path}; #{private_pub_cmd} -s #{private_pub_server} -E #{rails_env} -P #{private_pub_pid} >> #{private_pub_log} 2>&1 </dev/null &) ] }
27
+ }
28
+ end
29
+
30
+ desc "Restart Private Pub"
31
+ task :restart do
32
+ invoke :'private_pub:stop'
33
+ invoke :'private_pub:start'
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ namespace :puma do
2
+
3
+ task(:install) { }
4
+
5
+ desc "Setup Puma configuration"
6
+ task :setup => [:upload]
7
+
8
+ desc "Create configuration and other files"
9
+ task :upload do
10
+ invoke :sudo
11
+ template "puma.rb.erb", puma_config
12
+ queue %[echo "-----> Be sure to edit #{puma_config}."]
13
+ template "upstart/puma.conf.erb", "/tmp/puma_conf"
14
+ queue "sudo mv /tmp/puma_conf #{puma_upstart}"
15
+ end
16
+
17
+ %w[start stop restart].each do |command|
18
+ desc "#{command.capitalize} puma"
19
+ task command do
20
+ # need to get rid of sudo here: have to figure out how it works with upstart
21
+ invoke :sudo
22
+ queue "sudo service #{puma_name} #{command}"
23
+ end
24
+ end
25
+
26
+ desc "Phased-restart puma"
27
+ task :'phased-restart' do
28
+ queue "#{pumactl_cmd} -S #{puma_state} phased-restart"
29
+ end
30
+
31
+ end
@@ -0,0 +1,22 @@
1
+ namespace :rails do
2
+
3
+ task(:install) { }
4
+ task(:setup) { }
5
+
6
+ task :log do
7
+ queue 'echo "Contents of the log file are as follows:"'
8
+ queue "tail -f #{logs_path}/#{rails_env}.log"
9
+ end
10
+ end
11
+
12
+
13
+
14
+ # desc "Open the rails console on one of the remote servers"
15
+ # task :console, :roles => :app do
16
+ # queue %{ssh #{domain} -t "#{default_shell} -c 'cd #{current_path} && bundle exec rails c #{rails_env}'"}
17
+ # end
18
+
19
+ # desc "remote rake task"
20
+ # task :rake do
21
+ # run "cd #{deploy_to}/current; RAILS_ENV=#{rails_env} rake #{ENV['TASK']}"
22
+ # end
@@ -0,0 +1,28 @@
1
+ namespace :rbenv do
2
+
3
+ desc "Install rbenv, Ruby, and the Bundler gem"
4
+ task :install do
5
+ invoke :sudo
6
+ queue %{echo "-----> Installing Ruby"}
7
+ queue "sudo apt-get -y install curl git-core"
8
+ queue "curl -L https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash"
9
+ bashrc = <<-BASHRC
10
+ if [ -d $HOME/.rbenv ]; then
11
+ export PATH="$HOME/.rbenv/bin:$PATH"
12
+ eval "$(rbenv init -)"
13
+ fi
14
+ BASHRC
15
+ put bashrc, "/tmp/rbenvrc"
16
+ queue "cat /tmp/rbenvrc ~/.bashrc > ~/.bashrc.tmp"
17
+ queue "mv ~/.bashrc.tmp ~/.bashrc"
18
+ queue %q{export PATH="$HOME/.rbenv/bin:$PATH"}
19
+ queue %q{eval "$(rbenv init -)"}
20
+ queue "sudo apt-get -y install build-essential zlib1g-dev libssl-dev libreadline-gplv2-dev"
21
+ queue "rbenv install #{ruby_version}"
22
+ queue "rbenv global #{ruby_version}"
23
+ queue "gem install bundler --no-ri --no-rdoc"
24
+ queue "rbenv rehash"
25
+ end
26
+
27
+ task(:setup) { }
28
+ end
@@ -0,0 +1,23 @@
1
+ namespace :redis do
2
+
3
+ desc "Install the latest release of Redis"
4
+ task :install do
5
+ invoke :sudo
6
+ queue %{echo "-----> Installing Redis..."}
7
+ queue "sudo add-apt-repository -y ppa:chris-lea/redis-server --yes"
8
+ queue "sudo apt-get -y update"
9
+ queue "sudo apt-get -y install redis-server"
10
+ end
11
+
12
+ task(:setup) { }
13
+
14
+ %w[start stop restart].each do |command|
15
+ desc "#{command} redis"
16
+ task command do
17
+ invoke :sudo
18
+ queue %{echo "-----> Trying to #{command} Redis..."}
19
+ queue "#{sudo} service redis #{command}"
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,42 @@
1
+ namespace :sidekiq do
2
+
3
+ task(:install) { }
4
+
5
+ desc "Setup sidekiq configuration"
6
+ task :setup => [:upload]
7
+
8
+ desc "Create configuration and other files"
9
+ task :upload do
10
+ invoke :sudo
11
+ template "sidekiq.yml.erb", sidekiq_config
12
+ queue %[echo "-----> Be sure to edit #{sidekiq_config}."]
13
+ template "upstart/sidekiq.conf.erb", "/tmp/sidekiq_conf"
14
+ queue "sudo mv /tmp/sidekiq_conf #{sidekiq_upstart}"
15
+ end
16
+
17
+ # %w[quiet].each do |command|
18
+ # desc "#{command.capitalize} sidekiq"
19
+ # task command do
20
+ # queue %{ if [ -f #{sidekiq_pid} ]; then
21
+ # echo "-----> #{command.capitalize} sidekiq"
22
+ # #{echo_cmd %{(cd #{deploy_to}/#{current_path} && #{sidekiqctl_cmd} #{command} #{sidekiq_pid})} }
23
+ # fi }
24
+ # end
25
+ # end
26
+
27
+ # desc "Restart Sidekiq"
28
+ # task :restart do
29
+ # invoke :'sidekiq:stop'
30
+ # invoke :'sidekiq:start'
31
+ # end
32
+
33
+ # %w[start stop].each do |command|
34
+ # desc "#{command.capitalize} sidekiq"
35
+ # task command do
36
+ # # need to get rid of sudo here: have to figure out how it works with upstart
37
+ # invoke :sudo
38
+ # queue "sudo service #{sidekiq_name} #{command}"
39
+ # queue %[echo "-----> #{command.capitalize} sidekiq."]
40
+ # end
41
+ # end
42
+ end
@@ -0,0 +1,21 @@
1
+ namespace :unicorn do
2
+
3
+ task(:install) { }
4
+
5
+ desc "Setup Unicorn initializer and app configuration"
6
+ task :setup do
7
+ invoke :sudo
8
+ template "unicorn.rb.erb", unicorn_config
9
+ template "unicorn_init.erb", "/tmp/unicorn_init"
10
+ queue "chmod +x /tmp/unicorn_init"
11
+ queue "sudo mv /tmp/unicorn_init #{unicorn_script}"
12
+ queue "sudo update-rc.d -f #{unicorn_name} defaults"
13
+ end
14
+
15
+ %w[start stop restart].each do |command|
16
+ desc "#{command} unicorn"
17
+ task command do
18
+ queue "service #{unicorn_name} #{command}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ production:
2
+ S3_BUCKET: folify
3
+ S3_KEY: KEY
4
+ S3_SECRET: Secret
@@ -0,0 +1,8 @@
1
+ production:
2
+ adapter: postgresql
3
+ database: <%= psql_database %>
4
+ username: <%= psql_user %>
5
+ password: <%= psql_password %>
6
+ host: localhost
7
+ encoding: utf8
8
+ pool: 16
@@ -0,0 +1,16 @@
1
+ # run as a daemon
2
+ -d
3
+
4
+ logfile /var/log/memcached.log
5
+
6
+ # memory limit
7
+ -m <%= memcached_memory_limit %>
8
+
9
+ # port
10
+ -p 11211
11
+
12
+ # user
13
+ -u memcache
14
+
15
+ # listen only on localhost (for security)
16
+ -l 127.0.0.1
@@ -0,0 +1,8 @@
1
+ check process memcached with pidfile <%= memcached_pid! %>
2
+ start program = "/etc/init.d/memcached start"
3
+ stop program = "/etc/init.d/memcached stop"
4
+ if failed host 127.0.0.1 port 11211 then restart
5
+ if cpu > 60% for 2 cycles then alert
6
+ if cpu > 98% for 5 cycles then restart
7
+ if 2 restarts within 3 cycles then timeout
8
+ group cache
@@ -0,0 +1,21 @@
1
+ set daemon 30
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 mailserver smtp.gmail.com port 587
12
+ # username "foo@example.com" password "secret"
13
+ # using tlsv1
14
+ # with timeout 30 seconds
15
+
16
+ # set alert foo@example.com
17
+
18
+ set httpd port <%= monit_http_port %>
19
+ allow <%= monit_http_username %>:"<%= monit_http_password %>"
20
+
21
+ include /etc/monit/conf.d/*
@@ -0,0 +1,5 @@
1
+ check process nginx with pidfile <%= nginx_pid %>
2
+ start program = "/etc/init.d/nginx start"
3
+ stop program = "/etc/init.d/nginx stop"
4
+ if children > 250 then restart
5
+ if 5 restarts within 5 cycles then timeout
@@ -0,0 +1,5 @@
1
+ check process postgresql with pidfile <%= postgresql_pid! %>
2
+ start program = "/etc/init.d/postgresql start"
3
+ stop program = "/etc/init.d/postgresql stop"
4
+ if failed host localhost port 5432 protocol pgsql then restart
5
+ if 5 restarts within 5 cycles then timeout
@@ -0,0 +1,6 @@
1
+ check process <%= private_pub_name %>
2
+ with pidfile <%= private_pub_pid %>
3
+ start program = "<%= private_pub_start %>" with timeout 90 seconds
4
+ stop program = "kill -s TERM `cat <%= private_pub_config %>`" with timeout 90 seconds
5
+ if totalmem is greater than 200 MB for 2 cycles then restart # eating up memory?
6
+ group <%= private_pub_name %>
@@ -0,0 +1,7 @@
1
+ check process <%= puma_name %> with pidfile <%= puma_pid %>
2
+ start program = "/sbin/start <%= puma_name %>"
3
+ stop program = "/sbin/stop <%= puma_name %>"
4
+ if cpu > 60% for 2 cycles then alert
5
+ if cpu > 98% for 5 cycles then restart
6
+ if 2 restarts within 3 cycles then timeout
7
+ if totalmem is greater than 300 MB for 10 cycles then restart
@@ -0,0 +1,4 @@
1
+ check process redis
2
+ with pidfile /var/run/redis/redis-server.pid
3
+ if failed host 127.0.0.1 port 6379 then restart
4
+ if 5 restarts within 5 cycles then timeout
@@ -0,0 +1,6 @@
1
+ check process <%= sidekiq_name %>
2
+ with pidfile <%= sidekiq_pid %>
3
+ start program = "service <%= sidekiq_name %> start" with timeout 90 seconds
4
+ stop program = "service <%= sidekiq_name %> stop" with timeout 90 seconds
5
+ if totalmem is greater than 200 MB for 2 cycles then restart # eating up memory?
6
+ group sidekiq
@@ -0,0 +1,14 @@
1
+ check process <%= unicorn_name %> with pidfile <%= unicorn_pid %>
2
+ start program = "/etc/init.d/<%= unicorn_name %> start"
3
+ stop program = "/etc/init.d/<%= unicorn_name %> stop"
4
+
5
+ <% unicorn_workers.times do |n| %>
6
+ <% pid = unicorn_pid.sub(".pid", ".#{n}.pid") %>
7
+ check process <%= unicorn_name %>_worker_<%= n %> with pidfile <%= pid %>
8
+ start program = "/bin/true"
9
+ stop program = "/usr/bin/test -s <%= pid %> && /bin/kill -QUIT `cat <%= pid %>`"
10
+ if mem > 200.0 MB for 1 cycles then restart
11
+ if cpu > 50% for 3 cycles then restart
12
+ if 5 restarts within 5 cycles then timeout
13
+ if changed pid 2 times within 60 cycles then alert
14
+ <% end %>
@@ -0,0 +1,43 @@
1
+ <% rails_socket = server_stack.include?("puma") ? puma_socket : unicorn_socket %>
2
+ upstream <%= app %> {
3
+ server unix://<%= rails_socket %> fail_timeout=0;
4
+ }
5
+
6
+ server {
7
+ # listen 80 default deferred;
8
+ server_name <%= server_name %>;
9
+ keepalive_timeout 5;
10
+
11
+ root <%= deploy_to %>/<%= current_path %>/public;
12
+
13
+ client_max_body_size 4G;
14
+
15
+ if (-f $document_root/system/maintenance.html) {
16
+ return 503;
17
+ }
18
+
19
+ error_page 503 @maintenance;
20
+ location @maintenance {
21
+ rewrite ^(.*)$ /system/maintenance.html last;
22
+ break;
23
+ }
24
+
25
+ location ~ ^/(assets)/ {
26
+ gzip_static on;
27
+ expires max;
28
+ add_header Cache-Control public;
29
+ add_header Last-Modified "";
30
+ add_header ETag "";
31
+ }
32
+
33
+ location @rails {
34
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
35
+ proxy_set_header Host $http_host;
36
+ proxy_redirect off;
37
+ proxy_pass http://<%= app %>;
38
+ }
39
+
40
+ try_files $uri/index.html $uri @rails;
41
+
42
+ error_page 500 502 503 504 /500.html;
43
+ }
@@ -0,0 +1,4 @@
1
+ production:
2
+ server: "http://folify.com:9292/faye"
3
+ secret_token: "secret"
4
+ signature_expiration: 3600 # one hour
@@ -0,0 +1,17 @@
1
+ # Bind to a UNIX socket
2
+ bind "unix://<%= puma_socket %>"
3
+ activate_control_app "unix://<%= sockets_path %>/pumactl.sock"
4
+
5
+ # not deamonizing here - still can use -d flag
6
+ # daemonize true
7
+ environment "<%= rails_env %>"
8
+ directory '<%= "#{deploy_to!}/#{current_path!}" %>'
9
+ pidfile "<%= puma_pid %>"
10
+ state_path "<%= sockets_path %>/puma.state"
11
+ stdout_redirect '<%= puma_log %>', '<%= puma_error_log %>', true
12
+
13
+ workers <%= puma_workers %>
14
+ preload_app!
15
+
16
+ # 0-16 threads for production environment
17
+ # threads 0, 16
@@ -0,0 +1,9 @@
1
+ :verbose: false
2
+ :namespace: sidekiq
3
+ :concurrency: <%= sidekiq_concurrency %>
4
+ :queues:
5
+ - [high, 7]
6
+ - [low, 5]
7
+ - [default, 3]
8
+ :limits:
9
+ low: <%= (sidekiq_concurrency*0.4).to_i %>
@@ -0,0 +1,41 @@
1
+ rails_root = "<%= "#{deploy_to!}/#{current_path!}" %>"
2
+ rails_env = "<%= rails_env %>"
3
+ pid_file = "<%= unicorn_pid %>"
4
+ socket_file= "<%= unicorn_socket %>"
5
+ log_file = "<%= unicorn_log %>"
6
+ err_log = "<%= unicorn_error_log %>"
7
+ old_pid = pid_file + '.oldbin'
8
+
9
+ timeout 60
10
+ worker_processes <%= unicorn_workers %>
11
+
12
+ # Listen on a Unix data socket
13
+ listen socket_file, :backlog => 2048
14
+ pid pid_file
15
+
16
+ stderr_path err_log
17
+ stdout_path log_file
18
+
19
+ preload_app true
20
+
21
+ GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
22
+
23
+ before_exec do |server|
24
+ ENV["BUNDLE_GEMFILE"] = "#{rails_root}/Gemfile"
25
+ end
26
+
27
+ before_fork do |server, worker|
28
+ # Using this method we get 0 downtime deploys.
29
+ if File.exists?(old_pid) && server.pid != old_pid
30
+ begin
31
+ Process.kill("QUIT", File.read(old_pid).to_i)
32
+ rescue Errno::ENOENT, Errno::ESRCH
33
+ # someone else did our job for us
34
+ end
35
+ end
36
+ end
37
+
38
+ after_fork do |server, worker|
39
+ child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
40
+ system("echo #{Process.pid} > #{child_pid}")
41
+ end
@@ -0,0 +1,86 @@
1
+ #!/bin/sh
2
+ ### BEGIN INIT INFO
3
+ # Provides: unicorn
4
+ # Required-Start: $remote_fs $syslog
5
+ # Required-Stop: $remote_fs $syslog
6
+ # Default-Start: 2 3 4 5
7
+ # Default-Stop: 0 1 6
8
+ # Short-Description: Manage unicorn server
9
+ # Description: Start, stop, restart unicorn server for a specific application.
10
+ ### END INIT INFO
11
+ set -e
12
+
13
+ # Feel free to change any of the following variables for your app:
14
+ TIMEOUT=60
15
+ APP_ROOT="<%= "#{deploy_to!}/#{current_path!}" %>"
16
+ PID="<%= unicorn_pid %>"
17
+ CMD="<%= unicorn_cmd %>"
18
+ AS_USER="<%= unicorn_user %>"
19
+ ENVIRONMENT="<%= rails_env %>"
20
+ CONFIG="<%= unicorn_config %>"
21
+ set -u
22
+
23
+ OLD_PIN="$PID.oldbin"
24
+
25
+ sig () {
26
+ test -s "$PID" && kill -$1 `cat $PID`
27
+ }
28
+
29
+ oldsig () {
30
+ test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
31
+ }
32
+
33
+ run () {
34
+ if [ "$(id -un)" = "$AS_USER" ]; then
35
+ eval $1
36
+ else
37
+ su -c "$1" - $AS_USER
38
+ fi
39
+ }
40
+
41
+ case "$1" in
42
+ start)
43
+ sig 0 && echo >&2 "Already running" && exit 0
44
+ run "$CMD"
45
+ ;;
46
+ stop)
47
+ sig QUIT && exit 0
48
+ echo >&2 "Not running"
49
+ ;;
50
+ force-stop)
51
+ sig TERM && exit 0
52
+ echo >&2 "Not running"
53
+ ;;
54
+ restart|reload)
55
+ sig USR2 && echo reloaded OK && exit 0
56
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
57
+ run "$CMD"
58
+ ;;
59
+ upgrade)
60
+ if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
61
+ then
62
+ n=$TIMEOUT
63
+ while test -s $OLD_PIN && test $n -ge 0
64
+ do
65
+ printf '.' && sleep 1 && n=$(( $n - 1 ))
66
+ done
67
+ echo
68
+
69
+ if test $n -lt 0 && test -s $OLD_PIN
70
+ then
71
+ echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
72
+ exit 1
73
+ fi
74
+ exit 0
75
+ fi
76
+ echo >&2 "Couldn't upgrade, starting '$CMD' instead"
77
+ run "$CMD"
78
+ ;;
79
+ reopen-logs)
80
+ sig USR1
81
+ ;;
82
+ *)
83
+ echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
84
+ exit 1
85
+ ;;
86
+ esac