capistrano 3.3.5 → 3.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/.github/issue_template.md +19 -0
  3. data/.github/pull_request_template.md +22 -0
  4. data/.github/release-drafter.yml +17 -0
  5. data/.github/workflows/push.yml +12 -0
  6. data/.gitignore +8 -5
  7. data/.rubocop.yml +62 -0
  8. data/.travis.yml +25 -7
  9. data/CHANGELOG.md +1 -273
  10. data/CONTRIBUTING.md +63 -93
  11. data/DEVELOPMENT.md +127 -0
  12. data/Dangerfile +1 -0
  13. data/Gemfile +36 -3
  14. data/LICENSE.txt +1 -1
  15. data/README.md +118 -292
  16. data/RELEASING.md +17 -0
  17. data/Rakefile +9 -1
  18. data/UPGRADING-3.7.md +86 -0
  19. data/bin/cap +1 -1
  20. data/capistrano.gemspec +24 -24
  21. data/features/deploy.feature +35 -1
  22. data/features/doctor.feature +11 -0
  23. data/features/installation.feature +8 -3
  24. data/features/sshconnect.feature +11 -0
  25. data/features/stage_failure.feature +9 -0
  26. data/features/step_definitions/assertions.rb +57 -20
  27. data/features/step_definitions/cap_commands.rb +9 -0
  28. data/features/step_definitions/setup.rb +56 -8
  29. data/features/subdirectory.feature +9 -0
  30. data/features/support/env.rb +5 -5
  31. data/features/support/remote_command_helpers.rb +12 -6
  32. data/features/support/vagrant_helpers.rb +17 -11
  33. data/lib/Capfile +1 -1
  34. data/lib/capistrano/all.rb +10 -10
  35. data/lib/capistrano/application.rb +47 -34
  36. data/lib/capistrano/configuration.rb +95 -44
  37. data/lib/capistrano/configuration/empty_filter.rb +9 -0
  38. data/lib/capistrano/configuration/filter.rb +16 -46
  39. data/lib/capistrano/configuration/host_filter.rb +29 -0
  40. data/lib/capistrano/configuration/null_filter.rb +9 -0
  41. data/lib/capistrano/configuration/plugin_installer.rb +51 -0
  42. data/lib/capistrano/configuration/question.rb +31 -9
  43. data/lib/capistrano/configuration/role_filter.rb +29 -0
  44. data/lib/capistrano/configuration/scm_resolver.rb +149 -0
  45. data/lib/capistrano/configuration/server.rb +30 -62
  46. data/lib/capistrano/configuration/servers.rb +38 -12
  47. data/lib/capistrano/configuration/validated_variables.rb +110 -0
  48. data/lib/capistrano/configuration/variables.rb +112 -0
  49. data/lib/capistrano/defaults.rb +26 -4
  50. data/lib/capistrano/deploy.rb +1 -2
  51. data/lib/capistrano/doctor.rb +6 -0
  52. data/lib/capistrano/doctor/environment_doctor.rb +19 -0
  53. data/lib/capistrano/doctor/gems_doctor.rb +45 -0
  54. data/lib/capistrano/doctor/output_helpers.rb +79 -0
  55. data/lib/capistrano/doctor/servers_doctor.rb +105 -0
  56. data/lib/capistrano/doctor/variables_doctor.rb +72 -0
  57. data/lib/capistrano/dotfile.rb +1 -2
  58. data/lib/capistrano/dsl.rb +49 -18
  59. data/lib/capistrano/dsl/env.rb +16 -46
  60. data/lib/capistrano/dsl/paths.rb +11 -25
  61. data/lib/capistrano/dsl/stages.rb +14 -2
  62. data/lib/capistrano/dsl/task_enhancements.rb +7 -12
  63. data/lib/capistrano/framework.rb +1 -1
  64. data/lib/capistrano/i18n.rb +32 -24
  65. data/lib/capistrano/immutable_task.rb +30 -0
  66. data/lib/capistrano/install.rb +1 -1
  67. data/lib/capistrano/plugin.rb +95 -0
  68. data/lib/capistrano/proc_helpers.rb +13 -0
  69. data/lib/capistrano/scm.rb +7 -20
  70. data/lib/capistrano/scm/git.rb +100 -0
  71. data/lib/capistrano/scm/hg.rb +55 -0
  72. data/lib/capistrano/scm/plugin.rb +13 -0
  73. data/lib/capistrano/scm/svn.rb +56 -0
  74. data/lib/capistrano/scm/tasks/git.rake +73 -0
  75. data/lib/capistrano/scm/tasks/hg.rake +53 -0
  76. data/lib/capistrano/scm/tasks/svn.rake +53 -0
  77. data/lib/capistrano/setup.rb +20 -6
  78. data/lib/capistrano/tasks/console.rake +4 -8
  79. data/lib/capistrano/tasks/deploy.rake +105 -74
  80. data/lib/capistrano/tasks/doctor.rake +24 -0
  81. data/lib/capistrano/tasks/framework.rake +13 -14
  82. data/lib/capistrano/tasks/install.rake +14 -15
  83. data/lib/capistrano/templates/Capfile +22 -11
  84. data/lib/capistrano/templates/deploy.rb.erb +18 -27
  85. data/lib/capistrano/templates/stage.rb.erb +36 -20
  86. data/lib/capistrano/upload_task.rb +1 -1
  87. data/lib/capistrano/version.rb +1 -1
  88. data/lib/capistrano/version_validator.rb +5 -10
  89. data/spec/integration/dsl_spec.rb +311 -289
  90. data/spec/integration_spec_helper.rb +3 -5
  91. data/spec/lib/capistrano/application_spec.rb +23 -39
  92. data/spec/lib/capistrano/configuration/empty_filter_spec.rb +17 -0
  93. data/spec/lib/capistrano/configuration/filter_spec.rb +83 -79
  94. data/spec/lib/capistrano/configuration/host_filter_spec.rb +71 -0
  95. data/spec/lib/capistrano/configuration/null_filter_spec.rb +17 -0
  96. data/spec/lib/capistrano/configuration/plugin_installer_spec.rb +98 -0
  97. data/spec/lib/capistrano/configuration/question_spec.rb +58 -26
  98. data/spec/lib/capistrano/configuration/role_filter_spec.rb +80 -0
  99. data/spec/lib/capistrano/configuration/scm_resolver_spec.rb +55 -0
  100. data/spec/lib/capistrano/configuration/server_spec.rb +124 -100
  101. data/spec/lib/capistrano/configuration/servers_spec.rb +143 -123
  102. data/spec/lib/capistrano/configuration_spec.rb +231 -59
  103. data/spec/lib/capistrano/doctor/environment_doctor_spec.rb +44 -0
  104. data/spec/lib/capistrano/doctor/gems_doctor_spec.rb +67 -0
  105. data/spec/lib/capistrano/doctor/output_helpers_spec.rb +47 -0
  106. data/spec/lib/capistrano/doctor/servers_doctor_spec.rb +86 -0
  107. data/spec/lib/capistrano/doctor/variables_doctor_spec.rb +89 -0
  108. data/spec/lib/capistrano/dsl/paths_spec.rb +201 -42
  109. data/spec/lib/capistrano/dsl/task_enhancements_spec.rb +57 -37
  110. data/spec/lib/capistrano/dsl_spec.rb +84 -11
  111. data/spec/lib/capistrano/immutable_task_spec.rb +31 -0
  112. data/spec/lib/capistrano/plugin_spec.rb +84 -0
  113. data/spec/lib/capistrano/scm/git_spec.rb +184 -0
  114. data/spec/lib/capistrano/scm/hg_spec.rb +109 -0
  115. data/spec/lib/capistrano/scm/svn_spec.rb +137 -0
  116. data/spec/lib/capistrano/scm_spec.rb +7 -8
  117. data/spec/lib/capistrano/upload_task_spec.rb +7 -7
  118. data/spec/lib/capistrano/version_validator_spec.rb +61 -46
  119. data/spec/lib/capistrano_spec.rb +2 -3
  120. data/spec/spec_helper.rb +21 -8
  121. data/spec/support/Vagrantfile +18 -8
  122. data/spec/support/tasks/database.rake +3 -3
  123. data/spec/support/tasks/fail.rake +4 -3
  124. data/spec/support/tasks/failed.rake +2 -2
  125. data/spec/support/tasks/plugin.rake +6 -0
  126. data/spec/support/tasks/root.rake +11 -0
  127. data/spec/support/test_app.rb +63 -39
  128. metadata +121 -44
  129. data/features/remote_file_task.feature +0 -14
  130. data/lib/capistrano/git.rb +0 -46
  131. data/lib/capistrano/hg.rb +0 -43
  132. data/lib/capistrano/svn.rb +0 -38
  133. data/lib/capistrano/tasks/git.rake +0 -81
  134. data/lib/capistrano/tasks/hg.rake +0 -52
  135. data/lib/capistrano/tasks/svn.rake +0 -52
  136. data/spec/lib/capistrano/git_spec.rb +0 -81
  137. data/spec/lib/capistrano/hg_spec.rb +0 -81
  138. data/spec/lib/capistrano/svn_spec.rb +0 -79
@@ -2,8 +2,16 @@ Given(/^a test app with the default configuration$/) do
2
2
  TestApp.install
3
3
  end
4
4
 
5
+ Given(/^a test app without any configuration$/) do
6
+ TestApp.create_test_app
7
+ end
8
+
5
9
  Given(/^servers with the roles app and web$/) do
6
- vagrant_cli_command('up') rescue nil
10
+ begin
11
+ vagrant_cli_command("up")
12
+ rescue
13
+ nil
14
+ end
7
15
  end
8
16
 
9
17
  Given(/^a linked file "(.*?)"$/) do |file|
@@ -13,10 +21,16 @@ end
13
21
 
14
22
  Given(/^file "(.*?)" exists in shared path$/) do |file|
15
23
  file_shared_path = TestApp.shared_path.join(file)
16
- run_vagrant_command("mkdir -p #{TestApp.shared_path}")
24
+ run_vagrant_command("mkdir -p #{file_shared_path.dirname}")
17
25
  run_vagrant_command("touch #{file_shared_path}")
18
26
  end
19
27
 
28
+ Given(/^all linked files exists in shared path$/) do
29
+ TestApp.linked_files.each do |linked_file|
30
+ step %Q{file "#{linked_file}" exists in shared path}
31
+ end
32
+ end
33
+
20
34
  Given(/^file "(.*?)" does not exist in shared path$/) do |file|
21
35
  file_shared_path = TestApp.shared_path.join(file)
22
36
  run_vagrant_command("mkdir -p #{TestApp.shared_path}")
@@ -24,7 +38,11 @@ Given(/^file "(.*?)" does not exist in shared path$/) do |file|
24
38
  end
25
39
 
26
40
  Given(/^a custom task to generate a file$/) do
27
- TestApp.copy_task_to_test_app('spec/support/tasks/database.rake')
41
+ TestApp.copy_task_to_test_app("spec/support/tasks/database.rake")
42
+ end
43
+
44
+ Given(/^a task which executes as root$/) do
45
+ TestApp.copy_task_to_test_app("spec/support/tasks/root.rake")
28
46
  end
29
47
 
30
48
  Given(/config stage file has line "(.*?)"/) do |line|
@@ -32,15 +50,45 @@ Given(/config stage file has line "(.*?)"/) do |line|
32
50
  end
33
51
 
34
52
  Given(/^the configuration is in a custom location$/) do
35
- TestApp.move_configuration_to_custom_location('app')
53
+ TestApp.move_configuration_to_custom_location("app")
36
54
  end
37
55
 
38
56
  Given(/^a custom task that will simulate a failure$/) do
39
- safely_remove_file(TestApp.shared_path.join('failed'))
40
- TestApp.copy_task_to_test_app('spec/support/tasks/fail.rake')
57
+ safely_remove_file(TestApp.shared_path.join("failed"))
58
+ TestApp.copy_task_to_test_app("spec/support/tasks/fail.rake")
41
59
  end
42
60
 
43
61
  Given(/^a custom task to run in the event of a failure$/) do
44
- safely_remove_file(TestApp.shared_path.join('failed'))
45
- TestApp.copy_task_to_test_app('spec/support/tasks/failed.rake')
62
+ safely_remove_file(TestApp.shared_path.join("failed"))
63
+ TestApp.copy_task_to_test_app("spec/support/tasks/failed.rake")
64
+ end
65
+
66
+ Given(/^a stage file named (.+)$/) do |filename|
67
+ TestApp.write_local_stage_file(filename)
68
+ end
69
+
70
+ Given(/^I make (\d+) deployments$/) do |count|
71
+ step "all linked files exists in shared path"
72
+
73
+ @release_paths = (1..count.to_i).map do
74
+ TestApp.cap("deploy")
75
+ stdout, _stderr = run_vagrant_command("readlink #{TestApp.current_path}")
76
+
77
+ stdout.strip
78
+ end
79
+ end
80
+
81
+ Given(/^(\d+) valid existing releases$/) do |num|
82
+ a_day = 86_400 # in seconds
83
+ (1...num).each_slice(100) do |num_batch|
84
+ dirs = num_batch.map do |i|
85
+ offset = -(a_day * i)
86
+ TestApp.release_path(TestApp.timestamp(offset))
87
+ end
88
+ run_vagrant_command("mkdir -p #{dirs.join(' ')}")
89
+ end
90
+ end
91
+
92
+ Given(/^an invalid release named "(.+)"$/) do |filename|
93
+ run_vagrant_command("mkdir -p #{TestApp.release_path(filename)}")
46
94
  end
@@ -0,0 +1,9 @@
1
+ Feature: cap can be run from a subdirectory, and will still find the Capfile
2
+
3
+ Background:
4
+ Given a test app with the default configuration
5
+ And servers with the roles app and web
6
+
7
+ Scenario: Running cap from a subdirectory
8
+ When I run cap "git:check" within the "config" directory
9
+ Then the task is successful
@@ -1,11 +1,11 @@
1
- PROJECT_ROOT = File.expand_path('../../../', __FILE__)
2
- VAGRANT_ROOT = File.join(PROJECT_ROOT, 'spec/support')
3
- VAGRANT_BIN = ENV['VAGRANT_BIN'] || "vagrant"
1
+ PROJECT_ROOT = File.expand_path("../../../", __FILE__)
2
+ VAGRANT_ROOT = File.join(PROJECT_ROOT, "spec/support")
3
+ VAGRANT_BIN = ENV["VAGRANT_BIN"] || "vagrant"
4
4
 
5
5
  at_exit do
6
- if ENV['KEEP_RUNNING']
6
+ if ENV["KEEP_RUNNING"]
7
7
  VagrantHelpers.run_vagrant_command("rm -rf /home/vagrant/var")
8
8
  end
9
9
  end
10
10
 
11
- require_relative '../../spec/support/test_app'
11
+ require_relative "../../spec/support/test_app"
@@ -1,22 +1,28 @@
1
1
  module RemoteCommandHelpers
2
2
  def test_dir_exists(path)
3
- exists?('d', path)
3
+ exists?("d", path)
4
4
  end
5
5
 
6
6
  def test_symlink_exists(path)
7
- exists?('L', path)
7
+ exists?("L", path)
8
8
  end
9
9
 
10
10
  def test_file_exists(path)
11
- exists?('f', path)
11
+ exists?("f", path)
12
12
  end
13
13
 
14
14
  def exists?(type, path)
15
- %{[ -#{type} "#{path}" ]}
15
+ %Q{[ -#{type} "#{path}" ]}
16
16
  end
17
17
 
18
- def safely_remove_file(path)
19
- run_vagrant_command("rm #{test_file}") rescue VagrantHelpers::VagrantSSHCommandError
18
+ def symlinked?(symlink_path, target_path)
19
+ "[ #{symlink_path} -ef #{target_path} ]"
20
+ end
21
+
22
+ def safely_remove_file(_path)
23
+ run_vagrant_command("rm #{test_file}")
24
+ rescue
25
+ VagrantHelpers::VagrantSSHCommandError
20
26
  end
21
27
  end
22
28
 
@@ -1,10 +1,12 @@
1
+ require "open3"
2
+
1
3
  module VagrantHelpers
2
4
  extend self
3
5
 
4
6
  class VagrantSSHCommandError < RuntimeError; end
5
7
 
6
8
  at_exit do
7
- if ENV['KEEP_RUNNING']
9
+ if ENV["KEEP_RUNNING"]
8
10
  puts "Vagrant vm will be left up because KEEP_RUNNING is set."
9
11
  puts "Rerun without KEEP_RUNNING set to cleanup the vm."
10
12
  else
@@ -14,22 +16,26 @@ module VagrantHelpers
14
16
 
15
17
  def vagrant_cli_command(command)
16
18
  puts "[vagrant] #{command}"
17
- Dir.chdir(VAGRANT_ROOT) do
18
- `#{VAGRANT_BIN} #{command} 2>&1`.split("\n").each do |line|
19
- puts "[vagrant] #{line}"
20
- end
19
+ stdout, stderr, status = Dir.chdir(VAGRANT_ROOT) do
20
+ Open3.capture3("#{VAGRANT_BIN} #{command}")
21
21
  end
22
- $?
22
+
23
+ (stdout + stderr).each_line { |line| puts "[vagrant] #{line}" }
24
+
25
+ [stdout, stderr, status]
23
26
  end
24
27
 
25
28
  def run_vagrant_command(command)
26
- if (status = vagrant_cli_command("ssh -c #{command.inspect}")).success?
27
- true
28
- else
29
- fail VagrantSSHCommandError, status
30
- end
29
+ stdout, stderr, status = vagrant_cli_command("ssh -c #{command.inspect}")
30
+ return [stdout, stderr] if status.success?
31
+ raise VagrantSSHCommandError, status
31
32
  end
32
33
 
34
+ def puts(message)
35
+ # Attach log messages to the current cucumber feature (`log`),
36
+ # or simply puts to the console (`super`) if we are outside of cucumber.
37
+ respond_to?(:log) ? log(message) : super(message)
38
+ end
33
39
  end
34
40
 
35
41
  World(VagrantHelpers)
data/lib/Capfile CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env cap
2
2
  include Capistrano::DSL
3
- require 'capistrano/install'
3
+ require "capistrano/install"
@@ -1,17 +1,17 @@
1
- require 'rake'
2
- require 'sshkit'
1
+ require "rake"
2
+ require "sshkit"
3
3
 
4
- require 'io/console'
4
+ require "io/console"
5
5
 
6
6
  Rake.application.options.trace = true
7
7
 
8
- require 'capistrano/version'
9
- require 'capistrano/version_validator'
10
- require 'capistrano/i18n'
11
- require 'capistrano/dsl'
12
- require 'capistrano/application'
13
- require 'capistrano/configuration'
8
+ require "capistrano/version"
9
+ require "capistrano/version_validator"
10
+ require "capistrano/i18n"
11
+ require "capistrano/dsl"
12
+ require "capistrano/application"
13
+ require "capistrano/configuration"
14
+ require "capistrano/configuration/scm_resolver"
14
15
 
15
16
  module Capistrano
16
-
17
17
  end
@@ -1,9 +1,8 @@
1
1
  module Capistrano
2
2
  class Application < Rake::Application
3
-
4
3
  def initialize
5
4
  super
6
- @rakefiles = %w{capfile Capfile capfile.rb Capfile.rb} << capfile
5
+ @rakefiles = %w{capfile Capfile capfile.rb Capfile.rb}
7
6
  end
8
7
 
9
8
  def name
@@ -21,11 +20,11 @@ module Capistrano
21
20
  switch =~ /--#{Regexp.union(not_applicable_to_capistrano)}/
22
21
  end
23
22
 
24
- super.push(version, dry_run, roles, hostfilter)
23
+ super.push(version, dry_run, roles, hostfilter, print_config_variables)
25
24
  end
26
25
 
27
26
  def handle_options
28
- options.rakelib = ['rakelib']
27
+ options.rakelib = ["rakelib"]
29
28
  options.trace_output = $stderr
30
29
 
31
30
  OptionParser.new do |opts|
@@ -48,11 +47,10 @@ module Capistrano
48
47
  end
49
48
 
50
49
  standard_rake_options.each { |args| opts.on(*args) }
51
- opts.environment('RAKEOPT')
50
+ opts.environment("RAKEOPT")
52
51
  end.parse!
53
52
  end
54
53
 
55
-
56
54
  def top_level_tasks
57
55
  if tasks_without_stage_dependency.include?(@top_level_tasks.first)
58
56
  @top_level_tasks
@@ -63,13 +61,10 @@ module Capistrano
63
61
 
64
62
  def display_error_message(ex)
65
63
  unless options.backtrace
66
- if loc = Rake.application.find_rakefile_location
67
- whitelist = (@imported.dup << loc[0]).map{|f| File.absolute_path(f, loc[1])}
68
- pattern = %r@^(?!#{whitelist.map{|p| Regexp.quote(p)}.join('|')})@
69
- Rake.application.options.suppress_backtrace_pattern = pattern
70
- end
64
+ Rake.application.options.suppress_backtrace_pattern = backtrace_pattern if backtrace_pattern
71
65
  trace "(Backtrace restricted to imported tasks)"
72
66
  end
67
+
73
68
  super
74
69
  end
75
70
 
@@ -81,60 +76,78 @@ module Capistrano
81
76
  end
82
77
  end
83
78
 
79
+ # allows the `cap install` task to load without a capfile
80
+ def find_rakefile_location
81
+ if (location = super).nil?
82
+ [capfile, Dir.pwd]
83
+ else
84
+ location
85
+ end
86
+ end
87
+
84
88
  private
85
89
 
90
+ def backtrace_pattern
91
+ loc = Rake.application.find_rakefile_location
92
+ return unless loc
93
+
94
+ whitelist = (@imported.dup << loc[0]).map { |f| File.absolute_path(f, loc[1]) }
95
+ /^(?!#{whitelist.map { |p| Regexp.quote(p) }.join('|')})/
96
+ end
97
+
86
98
  def load_imports
87
- if options.show_tasks
88
- invoke 'load:defaults'
89
- set(:stage, '')
99
+ if options.show_tasks && Rake::Task.task_defined?("load:defaults")
100
+ invoke "load:defaults"
101
+ set(:stage, "")
90
102
  Dir[deploy_config_path].each { |f| add_import f }
91
103
  end
92
104
 
93
105
  super
94
106
  end
95
107
 
96
- # allows the `cap install` task to load without a capfile
97
108
  def capfile
98
- File.expand_path(File.join(File.dirname(__FILE__),'..','Capfile'))
109
+ File.expand_path(File.join(File.dirname(__FILE__), "..", "Capfile"))
99
110
  end
100
111
 
101
112
  def version
102
- ['--version', '-V',
113
+ ["--version", "-V",
103
114
  "Display the program version.",
104
- lambda { |value|
105
- puts "Capistrano Version: #{Capistrano::VERSION} (Rake Version: #{RAKEVERSION})"
115
+ lambda do |_value|
116
+ puts "Capistrano Version: #{Capistrano::VERSION} (Rake Version: #{Rake::VERSION})"
106
117
  exit
107
- }
108
- ]
118
+ end]
109
119
  end
110
120
 
111
121
  def dry_run
112
- ['--dry-run', '-n',
122
+ ["--dry-run", "-n",
113
123
  "Do a dry run without executing actions",
114
- lambda { |value|
124
+ lambda do |_value|
115
125
  Configuration.env.set(:sshkit_backend, SSHKit::Backend::Printer)
116
- }
117
- ]
126
+ end]
118
127
  end
119
128
 
120
129
  def roles
121
- ['--roles ROLES', '-r',
130
+ ["--roles ROLES", "-r",
122
131
  "Run SSH commands only on hosts matching these roles",
123
- lambda { |value|
132
+ lambda do |value|
124
133
  Configuration.env.add_cmdline_filter(:role, value)
125
- }
126
- ]
134
+ end]
127
135
  end
128
136
 
129
137
  def hostfilter
130
- ['--hosts HOSTS', '-z',
138
+ ["--hosts HOSTS", "-z",
131
139
  "Run SSH commands only on matching hosts",
132
- lambda { |value|
140
+ lambda do |value|
133
141
  Configuration.env.add_cmdline_filter(:host, value)
134
- }
135
- ]
142
+ end]
136
143
  end
137
144
 
145
+ def print_config_variables
146
+ ["--print-config-variables", "-p",
147
+ "Display the defined config variables before starting the deployment tasks.",
148
+ lambda do |_value|
149
+ Configuration.env.set(:print_config_variables, true)
150
+ end]
151
+ end
138
152
  end
139
-
140
153
  end
@@ -1,15 +1,15 @@
1
- require_relative 'configuration/filter'
2
- require_relative 'configuration/question'
3
- require_relative 'configuration/server'
4
- require_relative 'configuration/servers'
1
+ require_relative "configuration/filter"
2
+ require_relative "configuration/question"
3
+ require_relative "configuration/plugin_installer"
4
+ require_relative "configuration/server"
5
+ require_relative "configuration/servers"
6
+ require_relative "configuration/validated_variables"
7
+ require_relative "configuration/variables"
5
8
 
6
9
  module Capistrano
7
- class Configuration
8
-
9
- def initialize(config = nil)
10
- @config ||= config
11
- end
10
+ class ValidationError < RuntimeError; end
12
11
 
12
+ class Configuration
13
13
  def self.env
14
14
  @env ||= new
15
15
  end
@@ -18,38 +18,53 @@ module Capistrano
18
18
  @env = new
19
19
  end
20
20
 
21
+ extend Forwardable
22
+ attr_reader :variables
23
+ def_delegators :variables,
24
+ :set, :fetch, :fetch_for, :delete, :keys, :validate
25
+
26
+ def initialize(values={})
27
+ @variables = ValidatedVariables.new(Variables.new(values))
28
+ end
29
+
21
30
  def ask(key, default=nil, options={})
22
31
  question = Question.new(key, default, options)
23
32
  set(key, question)
24
33
  end
25
34
 
26
- def set(key, value)
27
- config[key] = value
35
+ def set_if_empty(key, value=nil, &block)
36
+ set(key, value, &block) unless keys.include?(key)
28
37
  end
29
38
 
30
- def set_if_empty(key, value)
31
- config[key] = value unless config.has_key? key
39
+ def append(key, *values)
40
+ set(key, Array(fetch(key)).concat(values))
32
41
  end
33
42
 
34
- def delete(key)
35
- config.delete(key)
43
+ def remove(key, *values)
44
+ set(key, Array(fetch(key)) - values)
36
45
  end
37
46
 
38
- def fetch(key, default=nil, &block)
39
- value = fetch_for(key, default, &block)
40
- while callable_without_parameters?(value)
41
- value = set(key, value.call)
47
+ def any?(key)
48
+ value = fetch(key)
49
+ if value && value.respond_to?(:any?)
50
+ begin
51
+ return value.any?
52
+ rescue ArgumentError # rubocop:disable Lint/HandleExceptions
53
+ # Gracefully ignore values whose `any?` method doesn't accept 0 args
54
+ end
42
55
  end
43
- return value
56
+
57
+ !value.nil?
44
58
  end
45
59
 
46
- def keys
47
- config.keys
60
+ def is_question?(key)
61
+ value = fetch_for(key, nil)
62
+ !value.nil? && value.is_a?(Question)
48
63
  end
49
64
 
50
65
  def role(name, hosts, options={})
51
66
  if name == :all
52
- raise ArgumentError.new("#{name} reserved name for role. Please choose another name")
67
+ raise ArgumentError, "#{name} reserved name for role. Please choose another name"
53
68
  end
54
69
 
55
70
  servers.add_role(name, hosts, options)
@@ -63,6 +78,10 @@ module Capistrano
63
78
  servers.roles_for(names)
64
79
  end
65
80
 
81
+ def role_properties_for(names, &block)
82
+ servers.role_properties_for(names, &block)
83
+ end
84
+
66
85
  def primary(role)
67
86
  servers.fetch_primary(role)
68
87
  end
@@ -75,27 +94,50 @@ module Capistrano
75
94
 
76
95
  def configure_backend
77
96
  backend.configure do |sshkit|
78
- sshkit.format = fetch(:format)
97
+ configure_sshkit_output(sshkit)
79
98
  sshkit.output_verbosity = fetch(:log_level)
80
99
  sshkit.default_env = fetch(:default_env)
81
100
  sshkit.backend = fetch(:sshkit_backend, SSHKit::Backend::Netssh)
82
101
  sshkit.backend.configure do |backend|
83
102
  backend.pty = fetch(:pty)
84
103
  backend.connection_timeout = fetch(:connection_timeout)
85
- backend.ssh_options = fetch(:ssh_options) if fetch(:ssh_options)
104
+ backend.ssh_options = (backend.ssh_options || {}).merge(fetch(:ssh_options, {}))
86
105
  end
87
106
  end
88
107
  end
89
108
 
109
+ def configure_scm
110
+ Capistrano::Configuration::SCMResolver.new.resolve
111
+ end
112
+
90
113
  def timestamp
91
114
  @timestamp ||= Time.now.utc
92
115
  end
93
116
 
117
+ def add_filter(filter=nil, &block)
118
+ if block
119
+ raise ArgumentError, "Both a block and an object were given" if filter
120
+
121
+ filter = Object.new
122
+ def filter.filter(servers)
123
+ block.call(servers)
124
+ end
125
+ elsif !filter.respond_to? :filter
126
+ raise TypeError, "Provided custom filter <#{filter.inspect}> does " \
127
+ "not have a public 'filter' method"
128
+ end
129
+ @custom_filters ||= []
130
+ @custom_filters << filter
131
+ end
132
+
94
133
  def setup_filters
95
- @filters = cmdline_filters.clone
96
- @filters << Filter.new(:role, ENV['ROLES']) if ENV['ROLES']
97
- @filters << Filter.new(:host, ENV['HOSTS']) if ENV['HOSTS']
98
- fh = fetch_for(:filter,{})
134
+ @filters = cmdline_filters
135
+ @filters += @custom_filters if @custom_filters
136
+ @filters << Filter.new(:role, ENV["ROLES"]) if ENV["ROLES"]
137
+ @filters << Filter.new(:host, ENV["HOSTS"]) if ENV["HOSTS"]
138
+ fh = fetch_for(:filter, {}) || {}
139
+ @filters << Filter.new(:host, fh[:hosts]) if fh[:hosts]
140
+ @filters << Filter.new(:role, fh[:roles]) if fh[:roles]
99
141
  @filters << Filter.new(:host, fh[:host]) if fh[:host]
100
142
  @filters << Filter.new(:role, fh[:role]) if fh[:role]
101
143
  end
@@ -104,35 +146,44 @@ module Capistrano
104
146
  cmdline_filters << Filter.new(type, values)
105
147
  end
106
148
 
107
- def filter list
149
+ def filter(list)
108
150
  setup_filters if @filters.nil?
109
- @filters.reduce(list) { |l,f| f.filter l }
151
+ @filters.reduce(list) { |l, f| f.filter l }
110
152
  end
111
153
 
112
- private
154
+ def dry_run?
155
+ fetch(:sshkit_backend) == SSHKit::Backend::Printer
156
+ end
113
157
 
114
- def cmdline_filters
115
- @cmdline_filters ||= []
158
+ def install_plugin(plugin, load_hooks: true, load_immediately: false)
159
+ installer.install(plugin,
160
+ load_hooks: load_hooks,
161
+ load_immediately: load_immediately)
162
+ end
163
+
164
+ def scm_plugin_installed?
165
+ installer.scm_installed?
116
166
  end
117
167
 
118
168
  def servers
119
169
  @servers ||= Servers.new
120
170
  end
121
171
 
122
- def config
123
- @config ||= Hash.new
172
+ private
173
+
174
+ def cmdline_filters
175
+ @cmdline_filters ||= []
124
176
  end
125
177
 
126
- def fetch_for(key, default, &block)
127
- if block_given?
128
- config.fetch(key, &block)
129
- else
130
- config.fetch(key, default)
131
- end
178
+ def installer
179
+ @installer ||= PluginInstaller.new
132
180
  end
133
181
 
134
- def callable_without_parameters?(x)
135
- x.respond_to?(:call) && ( !x.respond_to?(:arity) || x.arity == 0)
182
+ def configure_sshkit_output(sshkit)
183
+ format_args = [fetch(:format)]
184
+ format_args.push(fetch(:format_options)) if any?(:format_options)
185
+
186
+ sshkit.use_format(*format_args)
136
187
  end
137
188
  end
138
189
  end