capistrano-vps 0.10.0

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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +105 -0
  6. data/Rakefile +2 -0
  7. data/bootstrap.sh +64 -0
  8. data/capistrano-vps.gemspec +19 -0
  9. data/lib/capistrano-vps.rb +1 -0
  10. data/lib/capistrano-vps/recipes/base.rb +32 -0
  11. data/lib/capistrano-vps/recipes/check.rb +15 -0
  12. data/lib/capistrano-vps/recipes/elasticsearch.rb +29 -0
  13. data/lib/capistrano-vps/recipes/haproxy.rb +30 -0
  14. data/lib/capistrano-vps/recipes/imagemagick.rb +10 -0
  15. data/lib/capistrano-vps/recipes/libcurl.rb +11 -0
  16. data/lib/capistrano-vps/recipes/libxml.rb +11 -0
  17. data/lib/capistrano-vps/recipes/mongodb.rb +21 -0
  18. data/lib/capistrano-vps/recipes/nginx.rb +29 -0
  19. data/lib/capistrano-vps/recipes/nodejs.rb +11 -0
  20. data/lib/capistrano-vps/recipes/postgresql_client.rb +114 -0
  21. data/lib/capistrano-vps/recipes/postgresql_server.rb +41 -0
  22. data/lib/capistrano-vps/recipes/rabbitmq.rb +23 -0
  23. data/lib/capistrano-vps/recipes/rbenv.rb +50 -0
  24. data/lib/capistrano-vps/recipes/redis.rb +28 -0
  25. data/lib/capistrano-vps/recipes/resque.rb +27 -0
  26. data/lib/capistrano-vps/recipes/ssh_agent.rb +17 -0
  27. data/lib/capistrano-vps/recipes/templates/haproxy.erb +1 -0
  28. data/lib/capistrano-vps/recipes/templates/haproxy_conf.erb +58 -0
  29. data/lib/capistrano-vps/recipes/templates/nginx_haproxy.erb +44 -0
  30. data/lib/capistrano-vps/recipes/templates/nginx_unicorn.erb +37 -0
  31. data/lib/capistrano-vps/recipes/templates/postgresql.yml.erb +8 -0
  32. data/lib/capistrano-vps/recipes/templates/redis.conf.erb +1 -0
  33. data/lib/capistrano-vps/recipes/templates/resque.rake.erb +97 -0
  34. data/lib/capistrano-vps/recipes/templates/unicorn.rb.erb +32 -0
  35. data/lib/capistrano-vps/recipes/templates/unicorn_init.erb +84 -0
  36. data/lib/capistrano-vps/recipes/thin.rb +35 -0
  37. data/lib/capistrano-vps/recipes/unicorn.rb +30 -0
  38. data/lib/capistrano-vps/recipes/web.rb +15 -0
  39. data/lib/capistrano-vps/recipes/wkhtmltoimage.rb +16 -0
  40. data/lib/capistrano-vps/version.rb +3 -0
  41. data/lib/rails/generators/vps/recipes/install/install_generator.rb +25 -0
  42. data/lib/rails/generators/vps/recipes/install/templates/deploy.rb +46 -0
  43. data/lib/rails/generators/vps/recipes/install/templates/production.rb +1 -0
  44. data/lib/rails/generators/vps/recipes/install/templates/staging.rb +1 -0
  45. metadata +99 -0
@@ -0,0 +1,8 @@
1
+ production:
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ database: "<%= postgresql_database %>"
5
+ pool: 5
6
+ username: "<%= postgresql_user %>"
7
+ password: "<%= postgresql_password %>"
8
+ host: "<%= postgresql_host %>"
@@ -0,0 +1 @@
1
+ redis.conf.erb
@@ -0,0 +1,97 @@
1
+ # Start a worker with proper env vars and output redirection
2
+ def run_worker(queue, count = 1)
3
+ puts "Starting #{count} worker(s) with QUEUE: #{queue}"
4
+ ops = {:pgroup => true, :err => [(Rails.root + "log/workers_error.log").to_s, "a"],
5
+ :out => [(Rails.root + "log/workers.log").to_s, "a"]}
6
+ env_vars = {"QUEUE" => queue.to_s}
7
+ count.times {
8
+ ## Using Kernel.spawn and Process.detach because regular system() call would
9
+ ## cause the processes to quit when capistrano finishes
10
+ pid = spawn(env_vars, "rake resque:work", ops)
11
+ Process.detach(pid)
12
+ }
13
+ end
14
+
15
+ # Start a scheduler, requires resque_scheduler >= 2.0.0.f
16
+ def run_scheduler
17
+ puts "Starting resque scheduler"
18
+ env_vars = {
19
+ "BACKGROUND" => "1",
20
+ "PIDFILE" => (Rails.root + "tmp/pids/resque_scheduler.pid").to_s,
21
+ "VERBOSE" => "1"
22
+ }
23
+ ops = {:pgroup => true, :err => [(Rails.root + "log/scheduler_error.log").to_s, "a"],
24
+ :out => [(Rails.root + "log/scheduler.log").to_s, "a"]}
25
+ pid = spawn(env_vars, "rake resque:scheduler", ops)
26
+ Process.detach(pid)
27
+ end
28
+
29
+ namespace :resque do
30
+ task :setup => :environment
31
+
32
+ desc "Restart running workers"
33
+ task :restart_workers => :environment do
34
+ Rake::Task['resque:stop_workers'].invoke
35
+ Rake::Task['resque:start_workers'].invoke
36
+ end
37
+
38
+ desc "Quit running workers"
39
+ task :stop_workers => :environment do
40
+ pids = Array.new
41
+ Resque.workers.each do |worker|
42
+ pids.concat(worker.worker_pids)
43
+ end
44
+ if pids.empty?
45
+ puts "No workers to kill"
46
+ else
47
+ syscmd = "kill -s QUIT #{pids.join(' ')}"
48
+ puts "Running syscmd: #{syscmd}"
49
+ system(syscmd)
50
+ end
51
+ end
52
+
53
+ desc "Start workers"
54
+ task :start_workers => :environment do
55
+ run_worker("*", 2)
56
+ run_worker("high", 1)
57
+ end
58
+
59
+ desc "Restart scheduler"
60
+ task :restart_scheduler => :environment do
61
+ Rake::Task['resque:stop_scheduler'].invoke
62
+ Rake::Task['resque:start_scheduler'].invoke
63
+ end
64
+
65
+ desc "Quit scheduler"
66
+ task :stop_scheduler => :environment do
67
+ pidfile = Rails.root + "tmp/pids/resque_scheduler.pid"
68
+ if !File.exists?(pidfile)
69
+ puts "Scheduler not running"
70
+ else
71
+ pid = File.read(pidfile).to_i
72
+ syscmd = "kill -s QUIT #{pid}"
73
+ puts "Running syscmd: #{syscmd}"
74
+ system(syscmd)
75
+ FileUtils.rm_f(pidfile)
76
+ end
77
+ end
78
+
79
+ desc "Start scheduler"
80
+ task :start_scheduler => :environment do
81
+ run_scheduler
82
+ end
83
+
84
+ desc "Reload schedule"
85
+ task :reload_schedule => :environment do
86
+ pidfile = Rails.root + "tmp/pids/resque_scheduler.pid"
87
+
88
+ if !File.exists?(pidfile)
89
+ puts "Scheduler not running"
90
+ else
91
+ pid = File.read(pidfile).to_i
92
+ syscmd = "kill -s USR2 #{pid}"
93
+ puts "Running syscmd: #{syscmd}"
94
+ system(syscmd)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,32 @@
1
+ working_directory "<%= current_path %>"
2
+ pid "<%= unicorn_pid %>"
3
+ stderr_path "<%= unicorn_log %>"
4
+ stdout_path "<%= unicorn_log %>"
5
+
6
+ listen "/tmp/unicorn.<%= application %>.sock", :backlog => 64
7
+ worker_processes <%= unicorn_workers %>
8
+ timeout 30
9
+ # To save some memory and improve performance
10
+ preload_app true
11
+ GC.respond_to?(:copy_on_write_friendly=) and
12
+ GC.copy_on_write_friendly = true
13
+
14
+ before_exec do |server|
15
+ ENV['BUNDLE_GEMFILE'] = "<%= current_path %>/Gemfile"
16
+ end
17
+
18
+ stderr_path "<%= current_path %>/log/unicorn.stderr.log"
19
+ stdout_path "<%= current_path %>/log/unicorn.stdout.log"
20
+
21
+ before_fork do |server, worker|
22
+ # the following is highly recomended for Rails + "preload_app true"
23
+ # as there's no need for the master process to hold a connection
24
+ defined?(ActiveRecord::Base) and
25
+ ActiveRecord::Base.connection.disconnect!
26
+ end
27
+
28
+ after_fork do |server, worker|
29
+ # the following is *required* for Rails + "preload_app true",
30
+ defined?(ActiveRecord::Base) and
31
+ ActiveRecord::Base.establish_connection
32
+ end
@@ -0,0 +1,84 @@
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=${TIMEOUT-60}
15
+ APP_ROOT=<%= current_path %>
16
+ PID=<%= unicorn_pid %>
17
+ CMD="cd <%= current_path %>; bundle exec unicorn -D -c <%= unicorn_config %> -E production"
18
+ AS_USER=<%= unicorn_user %>
19
+ set -u
20
+
21
+ OLD_PIN="$PID.oldbin"
22
+
23
+ sig () {
24
+ test -s "$PID" && kill -$1 `cat $PID`
25
+ }
26
+
27
+ oldsig () {
28
+ test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
29
+ }
30
+
31
+ run () {
32
+ if [ "$(id -un)" = "$AS_USER" ]; then
33
+ eval $1
34
+ else
35
+ su -c "$1" - $AS_USER
36
+ fi
37
+ }
38
+
39
+ case "$1" in
40
+ start)
41
+ sig 0 && echo >&2 "Already running" && exit 0
42
+ run "$CMD"
43
+ ;;
44
+ stop)
45
+ sig QUIT && exit 0
46
+ echo >&2 "Not running"
47
+ ;;
48
+ force-stop)
49
+ sig TERM && exit 0
50
+ echo >&2 "Not running"
51
+ ;;
52
+ restart|reload)
53
+ sig HUP && echo reloaded OK && exit 0
54
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
55
+ run "$CMD"
56
+ ;;
57
+ upgrade)
58
+ if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
59
+ then
60
+ n=$TIMEOUT
61
+ while test -s $OLD_PIN && test $n -ge 0
62
+ do
63
+ printf '.' && sleep 1 && n=$(( $n - 1 ))
64
+ done
65
+ echo
66
+
67
+ if test $n -lt 0 && test -s $OLD_PIN
68
+ then
69
+ echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
70
+ exit 1
71
+ fi
72
+ exit 0
73
+ fi
74
+ echo >&2 "Couldn't upgrade, starting '$CMD' instead"
75
+ run "$CMD"
76
+ ;;
77
+ reopen-logs)
78
+ sig USR1
79
+ ;;
80
+ *)
81
+ echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
82
+ exit 1
83
+ ;;
84
+ esac
@@ -0,0 +1,35 @@
1
+ Capistrano::Configuration.instance(true).load do
2
+ set_default(:thin_user) { user }
3
+ set_default(:thin_pid) { "#{current_path}/tmp/pids/thin.pid" }
4
+ set_default(:thin_log) { "#{shared_path}/log/thin.log" }
5
+ set_default(:thin_servers, 2)
6
+ set_default(:thin_port, 9001)
7
+ set_default(:thin_command, 'bundle exec thin')
8
+ namespace :thin do
9
+ desc "Start thin"
10
+ task :start, :roles => :app do
11
+ set :start_command, "#{thin_command} start -e #{rails_env} --port #{thin_port} -a 127.0.0.1 -P #{thin_pid} --server #{thin_servers} -d"
12
+ run "cd #{deploy_to}/current; #{start_command}", :roles => :app
13
+ end
14
+
15
+ desc "Stop thin"
16
+ task :stop, :roles => :app do
17
+ run "cd #{deploy_to}/current; #{thin_command} stop -P #{thin_pid} -s #{thin_servers} --port 9001"
18
+ end
19
+
20
+ desc "Restart thin"
21
+ task :restart, :roles => :app do
22
+ set :restart_command, "#{thin_command} restart -e #{rails_env} --port #{thin_port} --onebyone -a 127.0.0.1 -P #{thin_pid} --server #{thin_servers}"
23
+ run "cd #{deploy_to}/current; #{restart_command}", :roles => :app
24
+ puts "WEBSITE HAS BEEN DEPLOYED"
25
+ end
26
+ after "deploy:start", "thin:restart"
27
+ after "deploy:restart", "thin:restart"
28
+
29
+ end
30
+
31
+
32
+
33
+ end
34
+
35
+
@@ -0,0 +1,30 @@
1
+ Capistrano::Configuration.instance(true).load do
2
+ set_default(:unicorn_user) { user }
3
+ set_default(:unicorn_pid) { "#{current_path}/tmp/pids/unicorn.pid" }
4
+ set_default(:unicorn_config) { "#{shared_path}/config/unicorn.rb" }
5
+ set_default(:unicorn_log) { "#{shared_path}/log/unicorn.log" }
6
+ set_default(:unicorn_workers, 4)
7
+ namespace :unicorn do
8
+ desc "Setup Unicorn initializer and app configuration"
9
+ task :setup, roles: :app do
10
+ run "mkdir -p #{shared_path}/config"
11
+ template "unicorn.rb.erb", unicorn_config
12
+ template "unicorn_init.erb", "/tmp/unicorn_init"
13
+ run "chmod +x /tmp/unicorn_init"
14
+ run "#{sudo} mv /tmp/unicorn_init /etc/init.d/unicorn_#{application}"
15
+ run "#{sudo} update-rc.d -f unicorn_#{application} defaults"
16
+ template "nginx_unicorn.erb", "/tmp/nginx_conf"
17
+ run "#{sudo} mv /tmp/nginx_conf /etc/nginx/sites-enabled/#{application}"
18
+ nginx.restart
19
+ end
20
+ after "deploy:setup", "unicorn:setup"
21
+
22
+ %w[start stop restart upgrade].each do |command|
23
+ desc "#{command} unicorn"
24
+ task command, roles: :app do
25
+ run "service unicorn_#{application} #{command}"
26
+ end
27
+ after "deploy:#{command}", "unicorn:#{command}"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ Capistrano::Configuration.instance(true).load do
2
+ namespace :deploy do
3
+ namespace :web do
4
+ desc "This will disable the application and show a warning screen"
5
+ task :disable do
6
+ run "cp #{current_path}/config/maintenance.html #{current_path}/public/maintenance.html", :roles => :app
7
+ end
8
+
9
+ desc "This will disable the application and show a warning screen"
10
+ task :enable do
11
+ run "rm #{current_path}/public/maintenance.html", :roles => :app
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ Capistrano::Configuration.instance(true).load do
2
+ namespace :html2image do
3
+
4
+ desc "Install the latest release of Imagemagick"
5
+ task :install, roles: :app do
6
+
7
+ run "wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-amd64.tar.bz2 -O wkhtmltoimage.tar.bz2"
8
+ run "tar xvjf wkhtmltoimage.tar.bz2"
9
+ run "rm wkhtmltoimage.tar.bz2"
10
+ run "#{sudo} mv wkhtmltoimage-amd64 /usr/local/bin/wkhtmltoimage"
11
+
12
+
13
+ end
14
+ after "cap_vps:prepare", "html2image:install"
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module CapistranoVps
2
+ VERSION = "0.10.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'rails/generators'
3
+
4
+ module Vps
5
+ module Recipes
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../templates", __FILE__)
9
+ desc 'Generates deployment files and stages'
10
+ def create_deploy_file
11
+ template 'deploy.rb', File.join('config', 'deploy.rb')
12
+ end
13
+
14
+ def create_staging_file
15
+ template 'staging.rb', File.join('config', 'deploy', 'staging.rb')
16
+ end
17
+
18
+ def create_production_file
19
+ template 'production.rb', File.join('config', 'deploy', 'production.rb')
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ # =============================================================================
2
+ # GENERAL SETTINGS
3
+ # =============================================================================
4
+ set :application, "blog"
5
+ set :user, "deployer"
6
+
7
+ set :deploy_to, "/home/#{user}/apps/#{application}"
8
+ set :use_sudo, false
9
+ set :deploy_via, :remote_cache
10
+ set :scm, :git
11
+ set :repository, "git@gitlab.com:sbusso/#{application}.git"
12
+ set :git_enable_submodules, 1
13
+ set :keep_releases, 3
14
+ set :branch, "master"
15
+
16
+ default_run_options[:pty] = true
17
+ ssh_options[:forward_agent] = true
18
+ # ssh_options[:paranoid] = false
19
+
20
+ after "deploy", "deploy:cleanup" # keep only the last 3 releases
21
+
22
+ # =============================================================================
23
+ # STAGE SETTINGS
24
+ # =============================================================================
25
+
26
+ # set :default_stage, "experimental"
27
+ set :stages, %w(production staging)
28
+ set :default_stage, "staging"
29
+ require 'capistrano/ext/multistage'
30
+
31
+ # =============================================================================
32
+ # RECIPE INCLUDES
33
+ # =============================================================================
34
+
35
+ require "bundler/capistrano"
36
+ require "capistrano-vps/recipes/base"
37
+ require "capistrano-vps/recipes/nginx"
38
+ require "capistrano-vps/recipes/unicorn"
39
+ require "capistrano-vps/recipes/postgresql"
40
+ require "capistrano-vps/recipes/nodejs"
41
+ require "capistrano-vps/recipes/redis"
42
+ require "capistrano-vps/recipes/rbenv"
43
+ require "capistrano-vps/recipes/libxml"
44
+ # require "capistrano-vps/recipes/python"
45
+ require "capistrano-vps/recipes/elasticsearch"
46
+ # require "capistrano-vps/recipes/check"
@@ -0,0 +1 @@
1
+ server "#production_address#", :web, :app, :db, primary: true
@@ -0,0 +1 @@
1
+ server "#staging_address#", :web, :app, :db, primary: true
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-vps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephane Busso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: keeping vps recipes for ruby application
28
+ email: stephane.busso@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - bootstrap.sh
39
+ - capistrano-vps.gemspec
40
+ - lib/capistrano-vps.rb
41
+ - lib/capistrano-vps/recipes/base.rb
42
+ - lib/capistrano-vps/recipes/check.rb
43
+ - lib/capistrano-vps/recipes/elasticsearch.rb
44
+ - lib/capistrano-vps/recipes/haproxy.rb
45
+ - lib/capistrano-vps/recipes/imagemagick.rb
46
+ - lib/capistrano-vps/recipes/libcurl.rb
47
+ - lib/capistrano-vps/recipes/libxml.rb
48
+ - lib/capistrano-vps/recipes/mongodb.rb
49
+ - lib/capistrano-vps/recipes/nginx.rb
50
+ - lib/capistrano-vps/recipes/nodejs.rb
51
+ - lib/capistrano-vps/recipes/postgresql_client.rb
52
+ - lib/capistrano-vps/recipes/postgresql_server.rb
53
+ - lib/capistrano-vps/recipes/rabbitmq.rb
54
+ - lib/capistrano-vps/recipes/rbenv.rb
55
+ - lib/capistrano-vps/recipes/redis.rb
56
+ - lib/capistrano-vps/recipes/resque.rb
57
+ - lib/capistrano-vps/recipes/ssh_agent.rb
58
+ - lib/capistrano-vps/recipes/templates/haproxy.erb
59
+ - lib/capistrano-vps/recipes/templates/haproxy_conf.erb
60
+ - lib/capistrano-vps/recipes/templates/nginx_haproxy.erb
61
+ - lib/capistrano-vps/recipes/templates/nginx_unicorn.erb
62
+ - lib/capistrano-vps/recipes/templates/postgresql.yml.erb
63
+ - lib/capistrano-vps/recipes/templates/redis.conf.erb
64
+ - lib/capistrano-vps/recipes/templates/resque.rake.erb
65
+ - lib/capistrano-vps/recipes/templates/unicorn.rb.erb
66
+ - lib/capistrano-vps/recipes/templates/unicorn_init.erb
67
+ - lib/capistrano-vps/recipes/thin.rb
68
+ - lib/capistrano-vps/recipes/unicorn.rb
69
+ - lib/capistrano-vps/recipes/web.rb
70
+ - lib/capistrano-vps/recipes/wkhtmltoimage.rb
71
+ - lib/capistrano-vps/version.rb
72
+ - lib/rails/generators/vps/recipes/install/install_generator.rb
73
+ - lib/rails/generators/vps/recipes/install/templates/deploy.rb
74
+ - lib/rails/generators/vps/recipes/install/templates/production.rb
75
+ - lib/rails/generators/vps/recipes/install/templates/staging.rb
76
+ homepage: http://activelabs.fr
77
+ licenses: []
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: 'bootstrap from railscast #335'
99
+ test_files: []