Skip to content

Commit 9593553

Browse files
committed
update to capistrano 3.4
1 parent acf8ca0 commit 9593553

File tree

4 files changed

+154
-266
lines changed

4 files changed

+154
-266
lines changed

Capfile

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
load 'deploy'
1+
# Load DSL and set up stages
2+
require 'capistrano/setup'
23

3-
load 'deploy/assets' # Uncommented if you are using Rails' asset pipeline
4-
load 'config/deploy' # remove this line to skip loading any of the default tasks
4+
# Include default deployment tasks
5+
require 'capistrano/deploy'
6+
7+
# Include tasks from other gems included in your Gemfile
8+
#
9+
# For documentation on these, see for example:
10+
#
11+
# https://github.com/capistrano/rvm
12+
# https://github.com/capistrano/rbenv
13+
# https://github.com/capistrano/chruby
14+
# https://github.com/capistrano/bundler
15+
# https://github.com/capistrano/rails
16+
# https://github.com/capistrano/passenger
17+
#
18+
require 'capistrano/rvm'
19+
# require 'capistrano/rbenv'
20+
# require 'capistrano/chruby'
21+
require 'capistrano/bundler'
22+
require 'capistrano/rails'
23+
require 'capistrano/rails/assets'
24+
require 'capistrano/rails/migrations'
25+
require 'capistrano/passenger'
26+
27+
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
28+
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

config/deploy.rb

+127-77
Original file line numberDiff line numberDiff line change
@@ -18,104 +18,154 @@
1818
# uncomment the :asset_env line and make sure the RAILS_RELATIVE_URL_ROOT is set appropriately.
1919
# 5. Run: cap deploy:setup && cap deploy:check
2020
# 6. Run: cap deploy <=== this is all you need for subsequent deploys
21+
#
22+
# Wish I could find a safer way...
23+
# Allow the deploy user to run sudo commands without password prompts by adding the following file /etc/sudoers.d/01-concerto
24+
# deploy ALL=(ALL) NOPASSWD:/usr/bin/unlink, /usr/sbin/update-rc.d, /usr/bin/whoami, /usr/bin/env, /bin/sh
25+
26+
# config valid only for current version of Capistrano
27+
lock '3.4.0'
28+
29+
set :stage, :production
30+
set :application, 'concerto'
31+
set :repo_url, 'https://github.com/concerto/concerto.git'
32+
set :user, 'deploy'
2133

22-
set :user, "concerto"
2334

24-
set :application, "concerto"
25-
set :repository, "https://github.com/concerto/concerto.git"
2635
##set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT=/#{application}" # only needed if running under sub-uri
2736

37+
38+
role :web, "concerto" # Your HTTP server, Apache/etc
39+
role :app, "concerto" # This may be the same as your `Web` server
40+
role :db, "concerto", primary: true # This is where Rails migrations will run
41+
42+
43+
# Default branch is :master
44+
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
45+
2846
# this code will get the latest official release, unless a branch was specified in the command line
2947
# like: cap -S branch="master" deploy
3048
# master will deploy the most current development version
31-
set :branch do
32-
default_tag = `git tag`.split("\n").last
33-
default_tag
34-
end unless exists?(:branch)
49+
set :branch, ENV['branch'] || `git tag`.split("\n").last
3550

36-
role :web, "concerto" # Your HTTP server, Apache/etc
37-
role :app, "concerto" # This may be the same as your `Web` server
38-
role :db, "concerto", primary: true # This is where Rails migrations will run
51+
# Default deploy_to directory is /var/www/my_app_name
52+
# set :deploy_to, '/var/www/my_app_name'
53+
54+
set :deploy_to, "/var/webapps/#{fetch(:application)}" # make sure this exists and is writable
55+
56+
# Default value for :scm is :git
57+
# set :scm, :git
58+
59+
# Default value for :format is :pretty
60+
# set :format, :pretty
61+
62+
# Default value for :log_level is :debug
63+
# set :log_level, :debug
64+
65+
# Default value for :pty is false
66+
set :pty, true
67+
set :ssh_options, {
68+
forward_agent: true,
69+
user: fetch(:user)
70+
# verbose: :debug
71+
}
3972

40-
set :deploy_to, "/var/webapps/#{application}" # make sure this exists and is writable
41-
##set :deploy_to, "/media/blue2/webapps/#{application}" # make sure this exists and is writable
73+
# Default value for :linked_files is []
74+
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/concerto.yml')
4275

43-
set :use_sudo, false
44-
ssh_options[:forward_agent] = true
45-
default_run_options[:pty] = true # must be true for password prompt from git or ssh to work
76+
# Default value for linked_dirs is []
77+
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
4678

47-
after "deploy:restart", "deploy:cleanup" # remove deploys more than 5 versions old
48-
after "deploy:update_code", "deploy:migrate" # make sure the database is migrated if needed
79+
# Default value for default_env is {}
80+
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
81+
82+
# Default value for keep_releases is 5
83+
# set :keep_releases, 5
4984

5085
namespace :deploy do
51-
%w[start stop restart].each do |command|
52-
task command, roles: :app, except: { no_release: true } do
53-
desc "#{command} concerto background services"
54-
# start, stop, or restart the services if the service control script exists
55-
run "if [ -L /etc/init.d/concerto ]; then #{sudo} invoke-rc.d concerto #{command}; fi"
86+
87+
after :restart, :clear_cache do
88+
on roles(:web), in: :groups, limit: 3, wait: 10 do
89+
# Here we can do anything such as:
90+
# within release_path do
91+
# execute :rake, 'cache:clear'
92+
# end
5693
end
5794
end
58-
59-
# If you are using Passenger mod_rails uncomment this so it restarts your webserver
60-
task :restart, roles: :app, except: { no_release: true } do
61-
desc "restart the application server"
62-
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
63-
end
6495

65-
task :setup_service, roles: :app do
96+
namespace :services do
97+
98+
# start, stop, or restart the services if the service control script exists
99+
%w[start stop restart].each do |command|
100+
desc "#{command} concerto background services"
101+
task command.to_sym do
102+
on roles(:app) do
103+
if test("[ -L /etc/init.d/concerto ]")
104+
as :root do
105+
execute "invoke-rc.d", "concerto", "#{command}"
106+
end
107+
end
108+
end
109+
end
110+
end
111+
66112
desc "install concerto background services"
67-
# Must occur after code is deployed and symlink to current is created
68-
# If the service control script does not yet exist, but the script is in our app directory
69-
# then we link it (to create the service control script) and make sure it's executable
70-
# and not world-writable.
71-
# Otherwise if the service control script already exists, then the script in the app directory
72-
# may have just been replaced, so make sure it's permissions are like we said.
73-
run "if [ ! -L /etc/init.d/concerto -a -f #{current_path}/concerto-init.d ]; then
74-
#{sudo} ln -nfs #{current_path}/concerto-init.d /etc/init.d/concerto &&
75-
#{sudo} chmod +x #{current_path}/concerto-init.d &&
76-
#{sudo} chmod o-w #{current_path}/concerto-init.d &&
77-
#{sudo} update-rc.d concerto defaults ;
78-
elif [ -f #{current_path}/concerto-init.d ]; then
79-
#{sudo} chmod +x #{current_path}/concerto-init.d &&
80-
#{sudo} chmod o-w #{current_path}/concerto-init.d ;
81-
fi"
82-
end
113+
task :setup do
114+
on roles(:app) do
115+
# Must occur after code is deployed and symlink to current is created
116+
# If the service control script does not yet exist, but the script is in our app directory
117+
# then we link it (to create the service control script) and make sure it's executable
118+
# and not world-writable.
119+
# Otherwise if the service control script already exists, then the script in the app directory
120+
# may have just been replaced, so make sure it's permissions are like we said.
121+
if !test("[ -L /etc/init.d/concerto ]") and test("[ -f #{current_path}/concerto-init.d ]")
122+
as :root do
123+
execute :ln, "-nfs", "#{current_path}/concerto-init.d", "/etc/init.d/concerto"
124+
execute :chmod, "+x", "#{current_path}/concerto-init.d"
125+
execute :chmod, "o-w", "#{current_path}/concerto-init.d"
126+
execute "update-rc.d", "concerto", "defaults"
127+
end
128+
elsif test("[ -f #{current_path}/concerto-init.d ]")
129+
as :root do
130+
execute :chmod, "+x", "#{current_path}/concerto-init.d"
131+
execute :chmod, "o-w", "#{current_path}/concerto-init.d"
132+
end
133+
end
134+
end
135+
end
83136

84-
task :service_defaults, roles: :app do
85137
desc "set default directory for concerto background services"
86-
# set the path for finding our app
87-
# set the user that the services will run as (vi su)
88-
run "#{sudo} sh -c 'echo \"CONCERTODIR=#{current_path}\" >/etc/default/concerto'"
89-
run "#{sudo} sh -c 'echo \"USERNAME=#{user}\" >>/etc/default/concerto'"
90-
end
138+
task :defaults do
139+
on roles(:app) do
140+
if !test("[ -f /etc/default/concerto ]")
141+
as :root do
142+
# set the path for finding our app
143+
# set the user that the services will run as (vi su)
144+
execute :echo, "CONCERTODIR=#{current_path}", ">/etc/default/concerto"
145+
execute :echo, "USERNAME=#{fetch(:user)}", ">>/etc/default/concerto"
146+
execute :echo, "SUSHELL=/bin/bash", ">>/etc/default/concerto"
147+
end
148+
end
149+
end
150+
end
91151

92-
task :remove_service, roles: :app do
93152
desc "remove concerto background services"
94-
# if the service control script exists, then remove it and unschedule it
95-
run "if [ -L /etc/init.d/concerto ]; then
96-
#{sudo} unlink /etc/init.d/concerto &&
97-
#{sudo} update-rc.d concerto remove ;
98-
fi"
99-
end
100-
end
101-
before "deploy:remove_service", "deploy:stop" # stop the service before we remove it
102-
before "deploy:update_code", "deploy:stop" # stop the service before we update the code
103-
after "deploy:restart", "deploy:setup_service" # reinstall/reset perms on service after code changes
104-
after "deploy:setup_service", "deploy:start" # restart the service after its been set up
105-
after "deploy:setup", "deploy:service_defaults" # set defaults for the service when we set up a new server
106-
107-
# make sure our vendor/bundle is linked to our shared bundle path
108-
# which is shared among deploys of only our application
109-
namespace :bundler do
110-
task :create_symlink, roles: :app do
111-
shared_dir = File.join(shared_path, 'bundle')
112-
release_dir = File.join(current_release, 'vendor/bundle')
113-
run("ln -s #{shared_dir} #{release_dir}")
153+
task :remove do
154+
on roles(:app) do
155+
# if the service control script exists, then remove it and unschedule it
156+
if test("[ -L /etc/init.d/concerto ]")
157+
as :root do
158+
execute "unlink", "/etc/init.d/concerto"
159+
execute "update-rc.d", "concerto", "remove"
160+
end
161+
end
162+
end
163+
end
114164
end
115165
end
116-
before 'deploy:assets:precompile', 'bundler:create_symlink'
117166

118-
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'deploy')
119-
require "capistrano-tags" # needed to deploy tags that are not also branches
120-
require "bundler/capistrano" # needed to be able to bundle stuff
121-
require "capistrano_database" # needed to create and link database.yml
167+
before "deploy:services:remove", "deploy:services:stop" # stop the service before we remove it
168+
before "deploy:updating", "deploy:services:stop" # stop the service before we update the code
169+
after "deploy:restart", "deploy:services:setup" # reinstall/reset perms on service after code changes
170+
after "deploy:services:setup", "deploy:services:start" # restart the service after its been set up
171+
after "deploy:starting", "deploy:services:defaults" # set defaults for the service when we set up a new server

0 commit comments

Comments
 (0)