wulffeld-capistrano 2.5.8

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