le1t0-capistrano 2.5.18.001
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/CHANGELOG +843 -0
- data/README +102 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/bin/cap +4 -0
- data/bin/capify +86 -0
- data/lib/capistrano.rb +2 -0
- data/lib/capistrano/callback.rb +45 -0
- data/lib/capistrano/cli.rb +47 -0
- data/lib/capistrano/cli/execute.rb +85 -0
- data/lib/capistrano/cli/help.rb +125 -0
- data/lib/capistrano/cli/help.txt +78 -0
- data/lib/capistrano/cli/options.rb +243 -0
- data/lib/capistrano/cli/ui.rb +40 -0
- data/lib/capistrano/command.rb +283 -0
- data/lib/capistrano/configuration.rb +44 -0
- data/lib/capistrano/configuration/actions/file_transfer.rb +52 -0
- data/lib/capistrano/configuration/actions/inspect.rb +46 -0
- data/lib/capistrano/configuration/actions/invocation.rb +295 -0
- data/lib/capistrano/configuration/callbacks.rb +148 -0
- data/lib/capistrano/configuration/connections.rb +204 -0
- data/lib/capistrano/configuration/execution.rb +143 -0
- data/lib/capistrano/configuration/loading.rb +197 -0
- data/lib/capistrano/configuration/namespaces.rb +197 -0
- data/lib/capistrano/configuration/roles.rb +73 -0
- data/lib/capistrano/configuration/servers.rb +98 -0
- data/lib/capistrano/configuration/variables.rb +127 -0
- data/lib/capistrano/errors.rb +19 -0
- data/lib/capistrano/extensions.rb +57 -0
- data/lib/capistrano/logger.rb +59 -0
- data/lib/capistrano/processable.rb +53 -0
- data/lib/capistrano/recipes/compat.rb +32 -0
- data/lib/capistrano/recipes/deploy.rb +589 -0
- data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
- data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
- data/lib/capistrano/recipes/deploy/remote_dependency.rb +105 -0
- data/lib/capistrano/recipes/deploy/scm.rb +19 -0
- data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
- data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
- data/lib/capistrano/recipes/deploy/scm/bzr.rb +86 -0
- data/lib/capistrano/recipes/deploy/scm/cvs.rb +152 -0
- data/lib/capistrano/recipes/deploy/scm/darcs.rb +96 -0
- data/lib/capistrano/recipes/deploy/scm/git.rb +278 -0
- data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
- data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
- data/lib/capistrano/recipes/deploy/scm/perforce.rb +138 -0
- data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
- data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
- data/lib/capistrano/recipes/deploy/strategy/base.rb +79 -0
- data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
- data/lib/capistrano/recipes/deploy/strategy/copy.rb +218 -0
- data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
- data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
- data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +56 -0
- data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
- data/lib/capistrano/recipes/standard.rb +37 -0
- data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
- data/lib/capistrano/role.rb +102 -0
- data/lib/capistrano/server_definition.rb +56 -0
- data/lib/capistrano/shell.rb +260 -0
- data/lib/capistrano/ssh.rb +99 -0
- data/lib/capistrano/task_definition.rb +75 -0
- data/lib/capistrano/transfer.rb +216 -0
- data/lib/capistrano/version.rb +18 -0
- data/test/cli/execute_test.rb +132 -0
- data/test/cli/help_test.rb +165 -0
- data/test/cli/options_test.rb +329 -0
- data/test/cli/ui_test.rb +28 -0
- data/test/cli_test.rb +17 -0
- data/test/command_test.rb +286 -0
- data/test/configuration/actions/file_transfer_test.rb +61 -0
- data/test/configuration/actions/inspect_test.rb +65 -0
- data/test/configuration/actions/invocation_test.rb +225 -0
- data/test/configuration/callbacks_test.rb +220 -0
- data/test/configuration/connections_test.rb +349 -0
- data/test/configuration/execution_test.rb +175 -0
- data/test/configuration/loading_test.rb +132 -0
- data/test/configuration/namespace_dsl_test.rb +311 -0
- data/test/configuration/roles_test.rb +144 -0
- data/test/configuration/servers_test.rb +158 -0
- data/test/configuration/variables_test.rb +184 -0
- data/test/configuration_test.rb +88 -0
- data/test/deploy/local_dependency_test.rb +76 -0
- data/test/deploy/remote_dependency_test.rb +114 -0
- data/test/deploy/scm/accurev_test.rb +23 -0
- data/test/deploy/scm/base_test.rb +55 -0
- data/test/deploy/scm/bzr_test.rb +51 -0
- data/test/deploy/scm/darcs_test.rb +37 -0
- data/test/deploy/scm/git_test.rb +184 -0
- data/test/deploy/scm/mercurial_test.rb +134 -0
- data/test/deploy/scm/none_test.rb +35 -0
- data/test/deploy/scm/subversion_test.rb +32 -0
- data/test/deploy/strategy/copy_test.rb +302 -0
- data/test/extensions_test.rb +69 -0
- data/test/fixtures/cli_integration.rb +5 -0
- data/test/fixtures/config.rb +5 -0
- data/test/fixtures/custom.rb +3 -0
- data/test/logger_test.rb +123 -0
- data/test/role_test.rb +11 -0
- data/test/server_definition_test.rb +121 -0
- data/test/shell_test.rb +90 -0
- data/test/ssh_test.rb +104 -0
- data/test/task_definition_test.rb +116 -0
- data/test/transfer_test.rb +160 -0
- data/test/utils.rb +39 -0
- metadata +289 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
module Capistrano
|
2
|
+
|
3
|
+
Error = Class.new(RuntimeError)
|
4
|
+
|
5
|
+
CaptureError = Class.new(Capistrano::Error)
|
6
|
+
NoSuchTaskError = Class.new(Capistrano::Error)
|
7
|
+
NoMatchingServersError = Class.new(Capistrano::Error)
|
8
|
+
|
9
|
+
class RemoteError < Error
|
10
|
+
attr_accessor :hosts
|
11
|
+
end
|
12
|
+
|
13
|
+
ConnectionError = Class.new(Capistrano::RemoteError)
|
14
|
+
TransferError = Class.new(Capistrano::RemoteError)
|
15
|
+
CommandError = Class.new(Capistrano::RemoteError)
|
16
|
+
|
17
|
+
LocalArgumentError = Class.new(Capistrano::Error)
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Capistrano
|
2
|
+
class ExtensionProxy #:nodoc:
|
3
|
+
def initialize(config, mod)
|
4
|
+
@config = config
|
5
|
+
extend(mod)
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(sym, *args, &block)
|
9
|
+
@config.send(sym, *args, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Holds the set of registered plugins, keyed by name (where the name is a
|
14
|
+
# symbol).
|
15
|
+
EXTENSIONS = {}
|
16
|
+
|
17
|
+
# Register the given module as a plugin with the given name. It will henceforth
|
18
|
+
# be available via a proxy object on Configuration instances, accessible by
|
19
|
+
# a method with the given name.
|
20
|
+
def self.plugin(name, mod)
|
21
|
+
name = name.to_sym
|
22
|
+
return false if EXTENSIONS.has_key?(name)
|
23
|
+
|
24
|
+
methods = Capistrano::Configuration.public_instance_methods +
|
25
|
+
Capistrano::Configuration.protected_instance_methods +
|
26
|
+
Capistrano::Configuration.private_instance_methods
|
27
|
+
|
28
|
+
if methods.any? { |m| m.to_sym == name }
|
29
|
+
raise Capistrano::Error, "registering a plugin named `#{name}' would shadow a method on Capistrano::Configuration with the same name"
|
30
|
+
end
|
31
|
+
|
32
|
+
Capistrano::Configuration.class_eval <<-STR, __FILE__, __LINE__+1
|
33
|
+
def #{name}
|
34
|
+
@__#{name}_proxy ||= Capistrano::ExtensionProxy.new(self, Capistrano::EXTENSIONS[#{name.inspect}])
|
35
|
+
end
|
36
|
+
STR
|
37
|
+
|
38
|
+
EXTENSIONS[name] = mod
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
|
42
|
+
# Unregister the plugin with the given name.
|
43
|
+
def self.remove_plugin(name)
|
44
|
+
name = name.to_sym
|
45
|
+
if EXTENSIONS.delete(name)
|
46
|
+
Capistrano::Configuration.send(:remove_method, name)
|
47
|
+
return true
|
48
|
+
end
|
49
|
+
|
50
|
+
return false
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.configuration(*args) #:nodoc:
|
54
|
+
warn "[DEPRECATION] Capistrano.configuration is deprecated. Use Capistrano::Configuration.instance instead"
|
55
|
+
Capistrano::Configuration.instance(*args)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Capistrano
|
2
|
+
class Logger #:nodoc:
|
3
|
+
attr_accessor :level
|
4
|
+
attr_reader :device
|
5
|
+
|
6
|
+
IMPORTANT = 0
|
7
|
+
INFO = 1
|
8
|
+
DEBUG = 2
|
9
|
+
TRACE = 3
|
10
|
+
|
11
|
+
MAX_LEVEL = 3
|
12
|
+
|
13
|
+
def initialize(options={})
|
14
|
+
output = options[:output] || $stderr
|
15
|
+
if output.respond_to?(:puts)
|
16
|
+
@device = output
|
17
|
+
else
|
18
|
+
@device = File.open(output.to_str, "a")
|
19
|
+
@needs_close = true
|
20
|
+
end
|
21
|
+
|
22
|
+
@options = options
|
23
|
+
@level = 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def close
|
27
|
+
device.close if @needs_close
|
28
|
+
end
|
29
|
+
|
30
|
+
def log(level, message, line_prefix=nil)
|
31
|
+
if level <= self.level
|
32
|
+
indent = "%*s" % [MAX_LEVEL, "*" * (MAX_LEVEL - level)]
|
33
|
+
(RUBY_VERSION >= "1.9" ? message.lines : message).each do |line|
|
34
|
+
if line_prefix
|
35
|
+
device.puts "#{indent} [#{line_prefix}] #{line.strip}\n"
|
36
|
+
else
|
37
|
+
device.puts "#{indent} #{line.strip}\n"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def important(message, line_prefix=nil)
|
44
|
+
log(IMPORTANT, message, line_prefix)
|
45
|
+
end
|
46
|
+
|
47
|
+
def info(message, line_prefix=nil)
|
48
|
+
log(INFO, message, line_prefix)
|
49
|
+
end
|
50
|
+
|
51
|
+
def debug(message, line_prefix=nil)
|
52
|
+
log(DEBUG, message, line_prefix)
|
53
|
+
end
|
54
|
+
|
55
|
+
def trace(message, line_prefix=nil)
|
56
|
+
log(TRACE, message, line_prefix)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Capistrano
|
2
|
+
module Processable
|
3
|
+
module SessionAssociation
|
4
|
+
def self.on(exception, session)
|
5
|
+
unless exception.respond_to?(:session)
|
6
|
+
exception.extend(self)
|
7
|
+
exception.session = session
|
8
|
+
end
|
9
|
+
|
10
|
+
return exception
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_accessor :session
|
14
|
+
end
|
15
|
+
|
16
|
+
def process_iteration(wait=nil, &block)
|
17
|
+
ensure_each_session { |session| session.preprocess }
|
18
|
+
|
19
|
+
return false if block && !block.call(self)
|
20
|
+
|
21
|
+
readers = sessions.map { |session| session.listeners.keys }.flatten.reject { |io| io.closed? }
|
22
|
+
writers = readers.select { |io| io.respond_to?(:pending_write?) && io.pending_write? }
|
23
|
+
|
24
|
+
if readers.any? || writers.any?
|
25
|
+
readers, writers, = IO.select(readers, writers, nil, wait)
|
26
|
+
end
|
27
|
+
|
28
|
+
if readers
|
29
|
+
ensure_each_session do |session|
|
30
|
+
ios = session.listeners.keys
|
31
|
+
session.postprocess(ios & readers, ios & writers)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
def ensure_each_session
|
39
|
+
errors = []
|
40
|
+
|
41
|
+
sessions.each do |session|
|
42
|
+
begin
|
43
|
+
yield session
|
44
|
+
rescue Exception => error
|
45
|
+
errors << SessionAssociation.on(error, session)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
raise errors.first if errors.any?
|
50
|
+
sessions
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# A collection of compatibility scripts, to ease the transition between
|
2
|
+
# Capistrano 1.x and Capistrano 2.x.
|
3
|
+
|
4
|
+
# Depends on the deployment system
|
5
|
+
load 'deploy'
|
6
|
+
|
7
|
+
map = { "diff_from_last_deploy" => "deploy:pending:diff",
|
8
|
+
"update" => "deploy:update",
|
9
|
+
"update_code" => "deploy:update_code",
|
10
|
+
"symlink" => "deploy:symlink",
|
11
|
+
"restart" => "deploy:restart",
|
12
|
+
"rollback" => "deploy:rollback",
|
13
|
+
"cleanup" => "deploy:cleanup",
|
14
|
+
"disable_web" => "deploy:web:disable",
|
15
|
+
"enable_web" => "deploy:web:enable",
|
16
|
+
"cold_deploy" => "deploy:cold",
|
17
|
+
"deploy_with_migrations" => "deploy:migrations" }
|
18
|
+
|
19
|
+
map.each do |old, new|
|
20
|
+
desc "DEPRECATED: See #{new}."
|
21
|
+
eval "task(#{old.inspect}) do
|
22
|
+
warn \"[DEPRECATED] `#{old}' is deprecated. Use `#{new}' instead.\"
|
23
|
+
find_and_execute_task(#{new.inspect})
|
24
|
+
end"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "DEPRECATED: See deploy:start."
|
28
|
+
task :spinner do
|
29
|
+
warn "[DEPRECATED] `spinner' is deprecated. Use `deploy:start' instead."
|
30
|
+
set :runner, fetch(:spinner_user, "app")
|
31
|
+
deploy.start
|
32
|
+
end
|
@@ -0,0 +1,589 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'capistrano/recipes/deploy/scm'
|
3
|
+
require 'capistrano/recipes/deploy/strategy'
|
4
|
+
|
5
|
+
def _cset(name, *args, &block)
|
6
|
+
unless exists?(name)
|
7
|
+
set(name, *args, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# =========================================================================
|
12
|
+
# These variables MUST be set in the client capfiles. If they are not set,
|
13
|
+
# the deploy will fail with an error.
|
14
|
+
# =========================================================================
|
15
|
+
|
16
|
+
_cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
|
17
|
+
_cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }
|
18
|
+
|
19
|
+
# =========================================================================
|
20
|
+
# These variables may be set in the client capfile if their default values
|
21
|
+
# are not sufficient.
|
22
|
+
# =========================================================================
|
23
|
+
|
24
|
+
_cset :scm, :subversion
|
25
|
+
_cset :deploy_via, :checkout
|
26
|
+
|
27
|
+
_cset(:deploy_to) { "/u/apps/#{application}" }
|
28
|
+
_cset(:revision) { source.head }
|
29
|
+
|
30
|
+
# =========================================================================
|
31
|
+
# These variables should NOT be changed unless you are very confident in
|
32
|
+
# what you are doing. Make sure you understand all the implications of your
|
33
|
+
# changes if you do decide to muck with these!
|
34
|
+
# =========================================================================
|
35
|
+
|
36
|
+
_cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
|
37
|
+
_cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
|
38
|
+
|
39
|
+
_cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
|
40
|
+
|
41
|
+
# If overriding release name, please also select an appropriate setting for :releases below.
|
42
|
+
_cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
|
43
|
+
|
44
|
+
_cset :version_dir, "releases"
|
45
|
+
_cset :shared_dir, "shared"
|
46
|
+
_cset :shared_children, %w(system log pids)
|
47
|
+
_cset :current_dir, "current"
|
48
|
+
|
49
|
+
_cset(:releases_path) { File.join(deploy_to, version_dir) }
|
50
|
+
_cset(:shared_path) { File.join(deploy_to, shared_dir) }
|
51
|
+
_cset(:current_path) { File.join(deploy_to, current_dir) }
|
52
|
+
_cset(:release_path) { File.join(releases_path, release_name) }
|
53
|
+
|
54
|
+
_cset(:releases) { capture("ls -x #{releases_path}").split.sort }
|
55
|
+
_cset(:current_release) { File.join(releases_path, releases.last) }
|
56
|
+
_cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
|
57
|
+
|
58
|
+
_cset(:current_revision) { capture("cat #{current_path}/REVISION").chomp }
|
59
|
+
_cset(:latest_revision) { capture("cat #{current_release}/REVISION").chomp }
|
60
|
+
_cset(:previous_revision) { capture("cat #{previous_release}/REVISION").chomp }
|
61
|
+
|
62
|
+
_cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
|
63
|
+
|
64
|
+
# some tasks, like symlink, need to always point at the latest release, but
|
65
|
+
# they can also (occassionally) be called standalone. In the standalone case,
|
66
|
+
# the timestamped release_path will be inaccurate, since the directory won't
|
67
|
+
# actually exist. This variable lets tasks like symlink work either in the
|
68
|
+
# standalone case, or during deployment.
|
69
|
+
_cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
|
70
|
+
|
71
|
+
# =========================================================================
|
72
|
+
# These are helper methods that will be available to your recipes.
|
73
|
+
# =========================================================================
|
74
|
+
|
75
|
+
# Auxiliary helper method for the `deploy:check' task. Lets you set up your
|
76
|
+
# own dependencies.
|
77
|
+
def depend(location, type, *args)
|
78
|
+
deps = fetch(:dependencies, {})
|
79
|
+
deps[location] ||= {}
|
80
|
+
deps[location][type] ||= []
|
81
|
+
deps[location][type] << args
|
82
|
+
set :dependencies, deps
|
83
|
+
end
|
84
|
+
|
85
|
+
# Temporarily sets an environment variable, yields to a block, and restores
|
86
|
+
# the value when it is done.
|
87
|
+
def with_env(name, value)
|
88
|
+
saved, ENV[name] = ENV[name], value
|
89
|
+
yield
|
90
|
+
ensure
|
91
|
+
ENV[name] = saved
|
92
|
+
end
|
93
|
+
|
94
|
+
# logs the command then executes it locally.
|
95
|
+
# returns the command output as a string
|
96
|
+
def run_locally(cmd)
|
97
|
+
logger.trace "executing locally: #{cmd.inspect}" if logger
|
98
|
+
output_on_stdout = `#{cmd}`
|
99
|
+
if $?.to_i > 0 # $? is command exit code (posix style)
|
100
|
+
raise Capistrano::LocalArgumentError, "Command #{cmd} returned status code #{$?}"
|
101
|
+
end
|
102
|
+
output_on_stdout
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
# If a command is given, this will try to execute the given command, as
|
107
|
+
# described below. Otherwise, it will return a string for use in embedding in
|
108
|
+
# another command, for executing that command as described below.
|
109
|
+
#
|
110
|
+
# If :run_method is :sudo (or :use_sudo is true), this executes the given command
|
111
|
+
# via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
|
112
|
+
# passed as the user to sudo as, if using sudo. If the :as key is not given,
|
113
|
+
# it will default to whatever the value of the :admin_runner variable is,
|
114
|
+
# which (by default) is unset.
|
115
|
+
#
|
116
|
+
# THUS, if you want to try to run something via sudo, and what to use the
|
117
|
+
# root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
|
118
|
+
# someone else, you'd just do try_sudo('something', :as => "bob"). If you
|
119
|
+
# always wanted sudo to run as a particular user, you could do
|
120
|
+
# set(:admin_runner, "bob").
|
121
|
+
def try_sudo(*args)
|
122
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
123
|
+
command = args.shift
|
124
|
+
raise ArgumentError, "too many arguments" if args.any?
|
125
|
+
|
126
|
+
as = options.fetch(:as, fetch(:admin_runner, nil))
|
127
|
+
via = fetch(:run_method, :sudo)
|
128
|
+
if command
|
129
|
+
invoke_command(command, :via => via, :as => as)
|
130
|
+
elsif via == :sudo
|
131
|
+
sudo(:as => as)
|
132
|
+
else
|
133
|
+
""
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Same as sudo, but tries sudo with :as set to the value of the :runner
|
138
|
+
# variable (which defaults to "app").
|
139
|
+
def try_runner(*args)
|
140
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
141
|
+
args << options.merge(:as => fetch(:runner, "app"))
|
142
|
+
try_sudo(*args)
|
143
|
+
end
|
144
|
+
|
145
|
+
# =========================================================================
|
146
|
+
# These are the tasks that are available to help with deploying web apps,
|
147
|
+
# and specifically, Rails applications. You can have cap give you a summary
|
148
|
+
# of them with `cap -T'.
|
149
|
+
# =========================================================================
|
150
|
+
|
151
|
+
namespace :deploy do
|
152
|
+
desc <<-DESC
|
153
|
+
Deploys your project. This calls both `update' and `restart'. Note that \
|
154
|
+
this will generally only work for applications that have already been deployed \
|
155
|
+
once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
|
156
|
+
task, which handles the cold start specifically.
|
157
|
+
DESC
|
158
|
+
task :default do
|
159
|
+
update
|
160
|
+
restart
|
161
|
+
end
|
162
|
+
|
163
|
+
desc <<-DESC
|
164
|
+
Prepares one or more servers for deployment. Before you can use any \
|
165
|
+
of the Capistrano deployment tasks with your project, you will need to \
|
166
|
+
make sure all of your servers have been prepared with `cap deploy:setup'. When \
|
167
|
+
you add a new server to your cluster, you can easily run the setup task \
|
168
|
+
on just that server by specifying the HOSTS environment variable:
|
169
|
+
|
170
|
+
$ cap HOSTS=new.server.com deploy:setup
|
171
|
+
|
172
|
+
It is safe to run this task on servers that have already been set up; it \
|
173
|
+
will not destroy any deployed revisions or data.
|
174
|
+
DESC
|
175
|
+
task :setup, :except => { :no_release => true } do
|
176
|
+
dirs = [deploy_to, releases_path, shared_path]
|
177
|
+
dirs += shared_children.map { |d| File.join(shared_path, d) }
|
178
|
+
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
|
179
|
+
end
|
180
|
+
|
181
|
+
desc <<-DESC
|
182
|
+
Copies your project and updates the symlink. It does this in a \
|
183
|
+
transaction, so that if either `update_code' or `symlink' fail, all \
|
184
|
+
changes made to the remote servers will be rolled back, leaving your \
|
185
|
+
system in the same state it was in before `update' was invoked. Usually, \
|
186
|
+
you will want to call `deploy' instead of `update', but `update' can be \
|
187
|
+
handy if you want to deploy, but not immediately restart your application.
|
188
|
+
DESC
|
189
|
+
task :update do
|
190
|
+
transaction do
|
191
|
+
update_code
|
192
|
+
symlink
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
desc <<-DESC
|
197
|
+
Copies your project to the remote servers. This is the first stage \
|
198
|
+
of any deployment; moving your updated code and assets to the deployment \
|
199
|
+
servers. You will rarely call this task directly, however; instead, you \
|
200
|
+
should call the `deploy' task (to do a complete deploy) or the `update' \
|
201
|
+
task (if you want to perform the `restart' task separately).
|
202
|
+
|
203
|
+
You will need to make sure you set the :scm variable to the source \
|
204
|
+
control software you are using (it defaults to :subversion), and the \
|
205
|
+
:deploy_via variable to the strategy you want to use to deploy (it \
|
206
|
+
defaults to :checkout).
|
207
|
+
DESC
|
208
|
+
task :update_code, :except => { :no_release => true } do
|
209
|
+
on_rollback { run "rm -rf #{release_path}; true" }
|
210
|
+
strategy.deploy!
|
211
|
+
finalize_update
|
212
|
+
end
|
213
|
+
|
214
|
+
desc <<-DESC
|
215
|
+
[internal] Touches up the released code. This is called by update_code \
|
216
|
+
after the basic deploy finishes. It assumes a Rails project was deployed, \
|
217
|
+
so if you are deploying something else, you may want to override this \
|
218
|
+
task with your own environment's requirements.
|
219
|
+
|
220
|
+
This task will make the release group-writable (if the :group_writable \
|
221
|
+
variable is set to true, which is the default). It will then set up \
|
222
|
+
symlinks to the shared directory for the log, system, and tmp/pids \
|
223
|
+
directories, and will lastly touch all assets in public/images, \
|
224
|
+
public/stylesheets, and public/javascripts so that the times are \
|
225
|
+
consistent (so that asset timestamping works). This touch process \
|
226
|
+
is only carried out if the :normalize_asset_timestamps variable is \
|
227
|
+
set to true, which is the default.
|
228
|
+
DESC
|
229
|
+
task :finalize_update, :except => { :no_release => true } do
|
230
|
+
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
|
231
|
+
|
232
|
+
# mkdir -p is making sure that the directories are there for some SCM's that don't
|
233
|
+
# save empty folders
|
234
|
+
run <<-CMD
|
235
|
+
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
|
236
|
+
mkdir -p #{latest_release}/public &&
|
237
|
+
mkdir -p #{latest_release}/tmp &&
|
238
|
+
ln -s #{shared_path}/log #{latest_release}/log &&
|
239
|
+
ln -s #{shared_path}/system #{latest_release}/public/system &&
|
240
|
+
ln -s #{shared_path}/pids #{latest_release}/tmp/pids
|
241
|
+
CMD
|
242
|
+
|
243
|
+
if fetch(:normalize_asset_timestamps, true)
|
244
|
+
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
|
245
|
+
asset_paths = %w(images stylesheets javascripts).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
|
246
|
+
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
desc <<-DESC
|
251
|
+
Updates the symlink to the most recently deployed version. Capistrano works \
|
252
|
+
by putting each new release of your application in its own directory. When \
|
253
|
+
you deploy a new version, this task's job is to update the `current' symlink \
|
254
|
+
to point at the new version. You will rarely need to call this task \
|
255
|
+
directly; instead, use the `deploy' task (which performs a complete \
|
256
|
+
deploy, including `restart') or the 'update' task (which does everything \
|
257
|
+
except `restart').
|
258
|
+
DESC
|
259
|
+
task :symlink, :except => { :no_release => true } do
|
260
|
+
on_rollback do
|
261
|
+
if previous_release
|
262
|
+
run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
|
263
|
+
else
|
264
|
+
logger.important "no previous release to rollback to, rollback of symlink skipped"
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
|
269
|
+
end
|
270
|
+
|
271
|
+
desc <<-DESC
|
272
|
+
Copy files to the currently deployed version. This is useful for updating \
|
273
|
+
files piecemeal, such as when you need to quickly deploy only a single \
|
274
|
+
file. Some files, such as updated templates, images, or stylesheets, \
|
275
|
+
might not require a full deploy, and especially in emergency situations \
|
276
|
+
it can be handy to just push the updates to production, quickly.
|
277
|
+
|
278
|
+
To use this task, specify the files and directories you want to copy as a \
|
279
|
+
comma-delimited list in the FILES environment variable. All directories \
|
280
|
+
will be processed recursively, with all files being pushed to the \
|
281
|
+
deployment servers.
|
282
|
+
|
283
|
+
$ cap deploy:upload FILES=templates,controller.rb
|
284
|
+
|
285
|
+
Dir globs are also supported:
|
286
|
+
|
287
|
+
$ cap deploy:upload FILES='config/apache/*.conf'
|
288
|
+
DESC
|
289
|
+
task :upload, :except => { :no_release => true } do
|
290
|
+
files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
|
291
|
+
abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
|
292
|
+
|
293
|
+
files.each { |file| top.upload(file, File.join(current_path, file)) }
|
294
|
+
end
|
295
|
+
|
296
|
+
desc <<-DESC
|
297
|
+
Restarts your application. This works by calling the script/process/reaper \
|
298
|
+
script under the current path.
|
299
|
+
|
300
|
+
If you are deploying a Rails 2.3.x application, you will need to install
|
301
|
+
these http://github.com/rails/irs_process_scripts (more info about why
|
302
|
+
on that page.)
|
303
|
+
|
304
|
+
By default, this will be invoked via sudo as the `app' user. If \
|
305
|
+
you wish to run it as a different user, set the :runner variable to \
|
306
|
+
that user. If you are in an environment where you can't use sudo, set \
|
307
|
+
the :use_sudo variable to false:
|
308
|
+
|
309
|
+
set :use_sudo, false
|
310
|
+
DESC
|
311
|
+
task :restart, :roles => :app, :except => { :no_release => true } do
|
312
|
+
warn "[DEPRECATED] `deploy:restart` is going to be changed to Passenger mod_rails' method after 2.5.9 - see http://is.gd/2BPeA"
|
313
|
+
try_runner "#{current_path}/script/process/reaper"
|
314
|
+
end
|
315
|
+
|
316
|
+
namespace :rollback do
|
317
|
+
desc <<-DESC
|
318
|
+
[internal] Points the current symlink at the previous revision.
|
319
|
+
This is called by the rollback sequence, and should rarely (if
|
320
|
+
ever) need to be called directly.
|
321
|
+
DESC
|
322
|
+
task :revision, :except => { :no_release => true } do
|
323
|
+
if previous_release
|
324
|
+
run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
|
325
|
+
else
|
326
|
+
abort "could not rollback the code because there is no prior release"
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
desc <<-DESC
|
331
|
+
[internal] Removes the most recently deployed release.
|
332
|
+
This is called by the rollback sequence, and should rarely
|
333
|
+
(if ever) need to be called directly.
|
334
|
+
DESC
|
335
|
+
task :cleanup, :except => { :no_release => true } do
|
336
|
+
run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
|
337
|
+
end
|
338
|
+
|
339
|
+
desc <<-DESC
|
340
|
+
Rolls back to the previously deployed version. The `current' symlink will \
|
341
|
+
be updated to point at the previously deployed version, and then the \
|
342
|
+
current release will be removed from the servers. You'll generally want \
|
343
|
+
to call `rollback' instead, as it performs a `restart' as well.
|
344
|
+
DESC
|
345
|
+
task :code, :except => { :no_release => true } do
|
346
|
+
revision
|
347
|
+
cleanup
|
348
|
+
end
|
349
|
+
|
350
|
+
desc <<-DESC
|
351
|
+
Rolls back to a previous version and restarts. This is handy if you ever \
|
352
|
+
discover that you've deployed a lemon; `cap rollback' and you're right \
|
353
|
+
back where you were, on the previously deployed version.
|
354
|
+
DESC
|
355
|
+
task :default do
|
356
|
+
revision
|
357
|
+
restart
|
358
|
+
cleanup
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
desc <<-DESC
|
363
|
+
Run the migrate rake task. By default, it runs this in most recently \
|
364
|
+
deployed version of the app. However, you can specify a different release \
|
365
|
+
via the migrate_target variable, which must be one of :latest (for the \
|
366
|
+
default behavior), or :current (for the release indicated by the \
|
367
|
+
`current' symlink). Strings will work for those values instead of symbols, \
|
368
|
+
too. You can also specify additional environment variables to pass to rake \
|
369
|
+
via the migrate_env variable. Finally, you can specify the full path to the \
|
370
|
+
rake executable by setting the rake variable. The defaults are:
|
371
|
+
|
372
|
+
set :rake, "rake"
|
373
|
+
set :rails_env, "production"
|
374
|
+
set :migrate_env, ""
|
375
|
+
set :migrate_target, :latest
|
376
|
+
DESC
|
377
|
+
task :migrate, :roles => :db, :only => { :primary => true } do
|
378
|
+
rake = fetch(:rake, "rake")
|
379
|
+
rails_env = fetch(:rails_env, "production")
|
380
|
+
migrate_env = fetch(:migrate_env, "")
|
381
|
+
migrate_target = fetch(:migrate_target, :latest)
|
382
|
+
|
383
|
+
directory = case migrate_target.to_sym
|
384
|
+
when :current then current_path
|
385
|
+
when :latest then current_release
|
386
|
+
else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
|
387
|
+
end
|
388
|
+
|
389
|
+
puts "#{migrate_target} => #{directory}"
|
390
|
+
run "cd #{directory}; #{rake} RAILS_ENV=#{rails_env} #{migrate_env} db:migrate"
|
391
|
+
end
|
392
|
+
|
393
|
+
desc <<-DESC
|
394
|
+
Deploy and run pending migrations. This will work similarly to the \
|
395
|
+
`deploy' task, but will also run any pending migrations (via the \
|
396
|
+
`deploy:migrate' task) prior to updating the symlink. Note that the \
|
397
|
+
update in this case it is not atomic, and transactions are not used, \
|
398
|
+
because migrations are not guaranteed to be reversible.
|
399
|
+
DESC
|
400
|
+
task :migrations do
|
401
|
+
set :migrate_target, :latest
|
402
|
+
update_code
|
403
|
+
migrate
|
404
|
+
symlink
|
405
|
+
restart
|
406
|
+
end
|
407
|
+
|
408
|
+
desc <<-DESC
|
409
|
+
Clean up old releases. By default, the last 5 releases are kept on each \
|
410
|
+
server (though you can change this with the keep_releases variable). All \
|
411
|
+
other deployed revisions are removed from the servers. By default, this \
|
412
|
+
will use sudo to clean up the old releases, but if sudo is not available \
|
413
|
+
for your environment, set the :use_sudo variable to false instead.
|
414
|
+
DESC
|
415
|
+
task :cleanup, :except => { :no_release => true } do
|
416
|
+
count = fetch(:keep_releases, 5).to_i
|
417
|
+
if count >= releases.length
|
418
|
+
logger.important "no old releases to clean up"
|
419
|
+
else
|
420
|
+
logger.info "keeping #{count} of #{releases.length} deployed releases"
|
421
|
+
|
422
|
+
directories = (releases - releases.last(count)).map { |release|
|
423
|
+
File.join(releases_path, release) }.join(" ")
|
424
|
+
|
425
|
+
try_sudo "rm -rf #{directories}"
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
desc <<-DESC
|
430
|
+
Test deployment dependencies. Checks things like directory permissions, \
|
431
|
+
necessary utilities, and so forth, reporting on the things that appear to \
|
432
|
+
be incorrect or missing. This is good for making sure a deploy has a \
|
433
|
+
chance of working before you actually run `cap deploy'.
|
434
|
+
|
435
|
+
You can define your own dependencies, as well, using the `depend' method:
|
436
|
+
|
437
|
+
depend :remote, :gem, "tzinfo", ">=0.3.3"
|
438
|
+
depend :local, :command, "svn"
|
439
|
+
depend :remote, :directory, "/u/depot/files"
|
440
|
+
DESC
|
441
|
+
task :check, :except => { :no_release => true } do
|
442
|
+
dependencies = strategy.check!
|
443
|
+
|
444
|
+
other = fetch(:dependencies, {})
|
445
|
+
other.each do |location, types|
|
446
|
+
types.each do |type, calls|
|
447
|
+
if type == :gem
|
448
|
+
dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
|
449
|
+
end
|
450
|
+
|
451
|
+
calls.each do |args|
|
452
|
+
dependencies.send(location).send(type, *args)
|
453
|
+
end
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
457
|
+
if dependencies.pass?
|
458
|
+
puts "You appear to have all necessary dependencies installed"
|
459
|
+
else
|
460
|
+
puts "The following dependencies failed. Please check them and try again:"
|
461
|
+
dependencies.reject { |d| d.pass? }.each do |d|
|
462
|
+
puts "--> #{d.message}"
|
463
|
+
end
|
464
|
+
abort
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
desc <<-DESC
|
469
|
+
Deploys and starts a `cold' application. This is useful if you have not \
|
470
|
+
deployed your application before, or if your application is (for some \
|
471
|
+
other reason) not currently running. It will deploy the code, run any \
|
472
|
+
pending migrations, and then instead of invoking `deploy:restart', it will \
|
473
|
+
invoke `deploy:start' to fire up the application servers.
|
474
|
+
DESC
|
475
|
+
task :cold do
|
476
|
+
update
|
477
|
+
migrate
|
478
|
+
start
|
479
|
+
end
|
480
|
+
|
481
|
+
desc <<-DESC
|
482
|
+
Start the application servers. This will attempt to invoke a script \
|
483
|
+
in your application called `script/spin', which must know how to start \
|
484
|
+
your application listeners. For Rails applications, you might just have \
|
485
|
+
that script invoke `script/process/spawner' with the appropriate \
|
486
|
+
arguments.
|
487
|
+
|
488
|
+
By default, the script will be executed via sudo as the `app' user. If \
|
489
|
+
you wish to run it as a different user, set the :runner variable to \
|
490
|
+
that user. If you are in an environment where you can't use sudo, set \
|
491
|
+
the :use_sudo variable to false.
|
492
|
+
DESC
|
493
|
+
task :start, :roles => :app do
|
494
|
+
warn "[DEPRECATED] `deploy:start` is going to be removed after 2.5.9 - see http://is.gd/2BPeA"
|
495
|
+
run "cd #{current_path} && #{try_runner} nohup script/spin"
|
496
|
+
end
|
497
|
+
|
498
|
+
desc <<-DESC
|
499
|
+
Stop the application servers. This will call script/process/reaper for \
|
500
|
+
both the spawner process, and all of the application processes it has \
|
501
|
+
spawned. As such, it is fairly Rails specific and may need to be \
|
502
|
+
overridden for other systems.
|
503
|
+
|
504
|
+
By default, the script will be executed via sudo as the `app' user. If \
|
505
|
+
you wish to run it as a different user, set the :runner variable to \
|
506
|
+
that user. If you are in an environment where you can't use sudo, set \
|
507
|
+
the :use_sudo variable to false.
|
508
|
+
DESC
|
509
|
+
task :stop, :roles => :app do
|
510
|
+
warn "[DEPRECATED] `deploy:start` is going to be removed after 2.5.9 - see http://is.gd/2BPeA"
|
511
|
+
run "if [ -f #{current_path}/tmp/pids/dispatch.spawner.pid ]; then #{try_runner} #{current_path}/script/process/reaper -a kill -r dispatch.spawner.pid; fi"
|
512
|
+
try_runner "#{current_path}/script/process/reaper -a kill"
|
513
|
+
end
|
514
|
+
|
515
|
+
namespace :pending do
|
516
|
+
desc <<-DESC
|
517
|
+
Displays the `diff' since your last deploy. This is useful if you want \
|
518
|
+
to examine what changes are about to be deployed. Note that this might \
|
519
|
+
not be supported on all SCM's.
|
520
|
+
DESC
|
521
|
+
task :diff, :except => { :no_release => true } do
|
522
|
+
system(source.local.diff(current_revision))
|
523
|
+
end
|
524
|
+
|
525
|
+
desc <<-DESC
|
526
|
+
Displays the commits since your last deploy. This is good for a summary \
|
527
|
+
of the changes that have occurred since the last deploy. Note that this \
|
528
|
+
might not be supported on all SCM's.
|
529
|
+
DESC
|
530
|
+
task :default, :except => { :no_release => true } do
|
531
|
+
from = source.next_revision(current_revision)
|
532
|
+
system(source.local.log(from))
|
533
|
+
end
|
534
|
+
end
|
535
|
+
|
536
|
+
namespace :web do
|
537
|
+
desc <<-DESC
|
538
|
+
Present a maintenance page to visitors. Disables your application's web \
|
539
|
+
interface by writing a "maintenance.html" file to each web server. The \
|
540
|
+
servers must be configured to detect the presence of this file, and if \
|
541
|
+
it is present, always display it instead of performing the request.
|
542
|
+
|
543
|
+
By default, the maintenance page will just say the site is down for \
|
544
|
+
"maintenance", and will be back "shortly", but you can customize the \
|
545
|
+
page by specifying the REASON and UNTIL environment variables:
|
546
|
+
|
547
|
+
$ cap deploy:web:disable \\
|
548
|
+
REASON="hardware upgrade" \\
|
549
|
+
UNTIL="12pm Central Time"
|
550
|
+
|
551
|
+
Further customization will require that you write your own task.
|
552
|
+
DESC
|
553
|
+
task :disable, :roles => :web, :except => { :no_release => true } do
|
554
|
+
require 'erb'
|
555
|
+
on_rollback { run "rm #{shared_path}/system/maintenance.html" }
|
556
|
+
|
557
|
+
warn <<-EOHTACCESS
|
558
|
+
|
559
|
+
# Please add something like this to your site's htaccess to redirect users to the maintenance page.
|
560
|
+
# More Info: http://www.shiftcommathree.com/articles/make-your-rails-maintenance-page-respond-with-a-503
|
561
|
+
|
562
|
+
ErrorDocument 503 /system/maintenance.html
|
563
|
+
RewriteEngine On
|
564
|
+
RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
|
565
|
+
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
|
566
|
+
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
|
567
|
+
RewriteRule ^.*$ - [redirect=503,last]
|
568
|
+
EOHTACCESS
|
569
|
+
|
570
|
+
reason = ENV['REASON']
|
571
|
+
deadline = ENV['UNTIL']
|
572
|
+
|
573
|
+
template = File.read(File.join(File.dirname(__FILE__), "templates", "maintenance.rhtml"))
|
574
|
+
result = ERB.new(template).result(binding)
|
575
|
+
|
576
|
+
put result, "#{shared_path}/system/maintenance.html", :mode => 0644
|
577
|
+
end
|
578
|
+
|
579
|
+
desc <<-DESC
|
580
|
+
Makes the application web-accessible again. Removes the \
|
581
|
+
"maintenance.html" page generated by deploy:web:disable, which (if your \
|
582
|
+
web servers are configured correctly) will make your application \
|
583
|
+
web-accessible again.
|
584
|
+
DESC
|
585
|
+
task :enable, :roles => :web, :except => { :no_release => true } do
|
586
|
+
run "rm #{shared_path}/system/maintenance.html"
|
587
|
+
end
|
588
|
+
end
|
589
|
+
end
|