minestrone 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/main.yml +32 -0
  3. data/.gitignore +5 -0
  4. data/Gemfile +10 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +35 -0
  7. data/Rakefile +10 -0
  8. data/bin/capify +89 -0
  9. data/bin/min +5 -0
  10. data/docs/lib-codebase-map.md +162 -0
  11. data/docs/lib-dependency-graph.svg +129 -0
  12. data/lib/minestrone/callback.rb +45 -0
  13. data/lib/minestrone/cli/help.rb +131 -0
  14. data/lib/minestrone/cli/help.txt +72 -0
  15. data/lib/minestrone/cli/options.rb +232 -0
  16. data/lib/minestrone/cli.rb +159 -0
  17. data/lib/minestrone/command.rb +177 -0
  18. data/lib/minestrone/configuration/actions/file_transfer.rb +53 -0
  19. data/lib/minestrone/configuration/actions/inspect.rb +46 -0
  20. data/lib/minestrone/configuration/actions/invocation.rb +202 -0
  21. data/lib/minestrone/configuration/alias_task.rb +29 -0
  22. data/lib/minestrone/configuration/callbacks.rb +129 -0
  23. data/lib/minestrone/configuration/connections.rb +66 -0
  24. data/lib/minestrone/configuration/execution.rb +139 -0
  25. data/lib/minestrone/configuration/loading.rb +207 -0
  26. data/lib/minestrone/configuration/log_formatters.rb +75 -0
  27. data/lib/minestrone/configuration/namespaces.rb +225 -0
  28. data/lib/minestrone/configuration/servers.rb +70 -0
  29. data/lib/minestrone/configuration/variables.rb +115 -0
  30. data/lib/minestrone/configuration.rb +69 -0
  31. data/lib/minestrone/errors.rb +17 -0
  32. data/lib/minestrone/ext/string.rb +7 -0
  33. data/lib/minestrone/extensions.rb +56 -0
  34. data/lib/minestrone/logger.rb +171 -0
  35. data/lib/minestrone/processable.rb +50 -0
  36. data/lib/minestrone/recipes/deploy/assets.rb +194 -0
  37. data/lib/minestrone/recipes/deploy/bundler.rb +81 -0
  38. data/lib/minestrone/recipes/deploy/dependencies.rb +44 -0
  39. data/lib/minestrone/recipes/deploy/local_dependency.rb +45 -0
  40. data/lib/minestrone/recipes/deploy/remote_dependency.rb +119 -0
  41. data/lib/minestrone/recipes/deploy/scm/base.rb +204 -0
  42. data/lib/minestrone/recipes/deploy/scm/git.rb +284 -0
  43. data/lib/minestrone/recipes/deploy/scm/none.rb +54 -0
  44. data/lib/minestrone/recipes/deploy/scm.rb +22 -0
  45. data/lib/minestrone/recipes/deploy/strategy/base.rb +87 -0
  46. data/lib/minestrone/recipes/deploy/strategy/copy.rb +353 -0
  47. data/lib/minestrone/recipes/deploy/strategy/remote_cache.rb +80 -0
  48. data/lib/minestrone/recipes/deploy/strategy.rb +22 -0
  49. data/lib/minestrone/recipes/deploy.rb +639 -0
  50. data/lib/minestrone/recipes/standard.rb +23 -0
  51. data/lib/minestrone/recipes/templates/maintenance.rhtml +53 -0
  52. data/lib/minestrone/server_definition.rb +56 -0
  53. data/lib/minestrone/ssh.rb +81 -0
  54. data/lib/minestrone/task_definition.rb +82 -0
  55. data/lib/minestrone/transfer.rb +205 -0
  56. data/lib/minestrone/version.rb +11 -0
  57. data/lib/minestrone.rb +3 -0
  58. data/minestrone.gemspec +32 -0
  59. data/test/cli/execute_test.rb +130 -0
  60. data/test/cli/help_test.rb +178 -0
  61. data/test/cli/options_test.rb +315 -0
  62. data/test/cli/ui_test.rb +26 -0
  63. data/test/cli_test.rb +17 -0
  64. data/test/command_test.rb +305 -0
  65. data/test/configuration/actions/file_transfer_test.rb +61 -0
  66. data/test/configuration/actions/inspect_test.rb +76 -0
  67. data/test/configuration/actions/invocation_test.rb +258 -0
  68. data/test/configuration/alias_task_test.rb +110 -0
  69. data/test/configuration/callbacks_test.rb +201 -0
  70. data/test/configuration/connections_test.rb +192 -0
  71. data/test/configuration/execution_test.rb +176 -0
  72. data/test/configuration/loading_test.rb +149 -0
  73. data/test/configuration/namespace_dsl_test.rb +325 -0
  74. data/test/configuration/servers_test.rb +100 -0
  75. data/test/configuration/variables_test.rb +191 -0
  76. data/test/configuration_test.rb +77 -0
  77. data/test/deploy/local_dependency_test.rb +61 -0
  78. data/test/deploy/remote_dependency_test.rb +146 -0
  79. data/test/deploy/scm/base_test.rb +55 -0
  80. data/test/deploy/scm/git_test.rb +260 -0
  81. data/test/deploy/scm/none_test.rb +26 -0
  82. data/test/deploy/strategy/copy_test.rb +360 -0
  83. data/test/extensions_test.rb +69 -0
  84. data/test/fixtures/cli_integration.rb +5 -0
  85. data/test/fixtures/config.rb +4 -0
  86. data/test/fixtures/custom.rb +3 -0
  87. data/test/logger_formatting_test.rb +149 -0
  88. data/test/logger_test.rb +134 -0
  89. data/test/recipes_test.rb +26 -0
  90. data/test/server_definition_test.rb +121 -0
  91. data/test/ssh_test.rb +99 -0
  92. data/test/task_definition_test.rb +117 -0
  93. data/test/transfer_test.rb +172 -0
  94. data/test/utils.rb +28 -0
  95. data/test/version_test.rb +11 -0
  96. metadata +258 -0
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minestrone
4
+ class CLI
5
+ module Help
6
+ LINE_PADDING = 7
7
+ MIN_MAX_LEN = 30
8
+ HEADER_LEN = 60
9
+
10
+ def self.included(base) #:nodoc:
11
+ base.send :alias_method, :execute_requested_actions_without_help, :execute_requested_actions
12
+ base.send :alias_method, :execute_requested_actions, :execute_requested_actions_with_help
13
+ end
14
+
15
+ def execute_requested_actions_with_help(config)
16
+ if options[:tasks]
17
+ task_list(config, options[:tasks])
18
+ elsif options[:explain]
19
+ explain_task(config, options[:explain])
20
+ else
21
+ execute_requested_actions_without_help(config)
22
+ end
23
+ end
24
+
25
+ def task_list(config, pattern = true) #:nodoc:
26
+ tool_output = options[:tool]
27
+
28
+ if pattern.is_a?(String)
29
+ tasks = config.task_list(:all).select {|t| t.fully_qualified_name =~ /#{pattern}/}
30
+ end
31
+ if tasks.nil? || tasks.length == 0
32
+ warn "Pattern '#{pattern}' not found. Listing all tasks.\n\n" if !tool_output && !pattern.is_a?(TrueClass)
33
+ tasks = config.task_list(:all)
34
+ end
35
+
36
+ if tasks.empty?
37
+ warn "There are no tasks available. Please specify a recipe file to load." unless tool_output
38
+ else
39
+ all_tasks_length = tasks.length
40
+ if options[:verbose].to_i < 1
41
+ tasks = tasks.reject { |t| t.description.empty? || t.description =~ /^\[internal\]/ }
42
+ end
43
+
44
+ tasks = tasks.sort_by { |task| task.fully_qualified_name }
45
+
46
+ longest = tasks.map { |task| task.fully_qualified_name.length }.max
47
+ max_length = output_columns - longest - LINE_PADDING
48
+ max_length = MIN_MAX_LEN if max_length < MIN_MAX_LEN
49
+
50
+ tasks.each do |task|
51
+ if tool_output
52
+ puts "min #{task.fully_qualified_name}"
53
+ else
54
+ puts "min %-#{longest}s # %s" % [task.fully_qualified_name, task.brief_description(max_length)]
55
+ end
56
+ end
57
+
58
+ unless tool_output
59
+ if all_tasks_length > tasks.length
60
+ puts
61
+ puts "Some tasks were not listed, either because they have no description,"
62
+ puts "or because they are only used internally by other tasks. To see all"
63
+ puts "tasks, type `#{File.basename($0)} -vT'."
64
+ end
65
+
66
+ puts
67
+ puts "Extended help may be available for these tasks."
68
+ puts "Type `#{File.basename($0)} -e taskname' to view it."
69
+ end
70
+ end
71
+ end
72
+
73
+ def explain_task(config, name) #:nodoc:
74
+ task = config.find_task(name)
75
+ if task.nil?
76
+ warn "The task `#{name}' does not exist."
77
+ else
78
+ puts "-" * HEADER_LEN
79
+ puts "min #{name}"
80
+ puts "-" * HEADER_LEN
81
+
82
+ if task.description.empty?
83
+ puts "There is no description for this task."
84
+ else
85
+ puts format_text(task.description)
86
+ end
87
+
88
+ puts
89
+ end
90
+ end
91
+
92
+ def long_help #:nodoc:
93
+ help_text = File.read(File.join(__dir__, "help.txt"))
94
+ self.class.ui.page_at = self.class.ui.output_rows - 2
95
+ self.class.ui.say format_text(help_text)
96
+ end
97
+
98
+ def format_text(text) #:nodoc:
99
+ formatted = "".dup
100
+
101
+ text.each_line do |line|
102
+ indentation = line[/^\s+/] || ""
103
+ indentation_size = indentation.split(//).map { |ch| ch == "\t" ? 8 : 1 }.sum
104
+ line_length = output_columns - indentation_size
105
+ line_length = MIN_MAX_LEN if line_length < MIN_MAX_LEN
106
+
107
+ lines = line.strip.gsub(/(.{1,#{line_length}})(?:\s+|\Z)/, "\\1\n").split(/\n/)
108
+
109
+ if lines.empty?
110
+ formatted << "\n"
111
+ else
112
+ formatted << lines.map { |l| "#{indentation}#{l}\n" }.join
113
+ end
114
+ end
115
+
116
+ formatted
117
+ end
118
+
119
+ def output_columns #:nodoc:
120
+ if ( @output_columns.nil? )
121
+ if ( self.class.ui.output_cols.nil? )
122
+ @output_columns = 80
123
+ else
124
+ @output_columns = self.class.ui.output_cols
125
+ end
126
+ end
127
+ @output_columns
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,72 @@
1
+ -----------------------------
2
+ <%= color('Minestrone', :bold) %>
3
+ -----------------------------
4
+
5
+ Minestrone is a utility for automating the execution of commands across multiple remote machines. It was originally conceived as an aid to deploy Ruby on Rails web applications, but has since evolved to become a much more general-purpose tool.
6
+
7
+ The command-line interface to Minestrone is via the `min' command.
8
+
9
+ min [ option ] ... action ...
10
+
11
+ The following options are understood:
12
+
13
+ <%= color '-d, --debug', :bold %>
14
+ Causes Minestrone to pause and prompt before executing any remote command, giving the user the option to either execute the command, skip the command, or abort execution entirely. This makes it a great way to troubleshoot tasks, or test custom tasks, by executing commands one at a time and checking the server to make sure they worked as expected before moving on to the next command. (Compare this to the --dry-run command.)
15
+
16
+ <%= color '-e, --explain TASK', :bold %>
17
+ Displays the extended description of the given task. Not all tasks will have an extended description, but for those that do, this can provide a wealth of additional usage information, such as describing environment variables or settings that can affect the execution of the task.
18
+
19
+ <%= color '-F, --default-config', :bold %>
20
+ By default, min will search for a config file named `Capfile' or `capfile' in the current directory, or in any parent directory, and will automatically load it. However, if you specify the -f flag (see below), min will use that file instead of the default config. If you want to use both the default config, and files loaded via -f, you can specify -F to force min to search for and load the default config, even if additional files were specified via -f.
21
+
22
+ <%= color '-f, --file FILE', :bold %>
23
+ Causes the named file to be loaded. Minestrone will search both its own recipe directory, as well as the current directory, looking for the named file. An ".rb" extension is optional. The -f option may be given any number of times, but if it is given, it will take the place of the normal `Capfile' or `capfile' detection. Use -F if you want the default capfile to be loaded when you use -f.
24
+
25
+ <%= color '-H, --long-help', :bold %>
26
+ Displays this document and exits.
27
+
28
+ <%= color '-h, --help', :bold %>
29
+ Shows a brief summary of these options and exits.
30
+
31
+ <%= color '-l, --logger [STDERR|STDOUT|file]', :bold %>
32
+ Change the file used to print the output. It offers three options: standard error(stderr), standard output and file. Options are not case sensitive. By default Minestrone uses stderr.
33
+
34
+ <%= color '-n, --dry-run', :bold %>
35
+ Causes Minestrone to simply display each remote command, without executing it. In this sense it is similar to --debug, but without the prompt. Note that commands executed locally are still run--only remote commands are skipped.
36
+
37
+ <%= color '-p, --password', :bold %>
38
+ Normally, min will prompt for the sudo password on-demand, the first time it is needed. This can make it hard to walk away from Minestrone, since you might not know if it will prompt for a password down the road. In such cases, you can use the -p option to force min to prompt for the sudo password immediately.
39
+
40
+ <%= color '-q, --quiet', :bold %>
41
+ Display only critical error messages. All other output is suppressed.
42
+
43
+ <%= color '-S, --set-before NAME=VALUE', :bold %>
44
+ Sets the given variable to the given value, before loading any recipe files. This is useful if you have a recipe file that depends on a certain variable being set, at the time it is loaded.
45
+
46
+ <%= color '-s, --set NAME=VALUE', :bold %>
47
+ Sets the given variable to the given value, after loading all recipe files. This is useful when you want to override the value of a variable which is used in a task. Note that this will set the variables too late for them to affect conditions that are executed as the recipes are loaded.
48
+
49
+ <%= color '-T, --tasks PATTERN', :bold %>
50
+ Displays the list of all tasks (matching optional PATTERN) in all loaded recipe files. If a task has no description, or if the description starts with the [internal] tag, the task will not be listed unless you also specify -v.
51
+
52
+ <%= color '-t, --tool', :bold %>
53
+ Abbreviates the output of -T for integration with other tools. Without -t, -T will list tasks with their summaries, and may include additional instructive text at the bottom. When integrating with other tools (e.g., bash auto-expansion and the like) that additional text can get in the way. This switch makes it easier for those tools to parse the list of tasks. (The -t switch has no effect if the -T switch is not specified.)
54
+
55
+ <%= color '-V, --version', :bold %>
56
+ Shows the current Minestrone version number and exits.
57
+
58
+ <%= color '-v, --verbose', :bold %>
59
+ Increase the verbosity. You can specify this option up to three times to further increase verbosity. By default, min will use maximum verbosity, but if you specify an explicit verbosity, that will be used instead. See also -q.
60
+
61
+ <%= color '-X, --skip-system-config', :bold %>
62
+ By default, min will look for and (if it exists) load the global system configuration file located in /etc/minestrone.conf. If you don't want min to load that file, give this option.
63
+
64
+ <%= color '-x, --skip-user-config', :bold %>
65
+ By default, min will look for and (if it exists) load the user-specific configuration file located in $HOME/.caprc. If you don't want min to load that file, give this option.
66
+
67
+ -----------------------------
68
+ <%= color('Environment Variables', :bold) %>
69
+ -----------------------------
70
+
71
+ <%= color 'SERVER', :bold %>
72
+ Execute the tasks against this server instead of the configured server.
@@ -0,0 +1,232 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ module Minestrone
6
+ class CLI
7
+ module Options
8
+
9
+ # The hash of (parsed) command-line options
10
+ attr_reader :options
11
+
12
+ # Return an OptionParser instance that defines the acceptable command
13
+ # line switches for Minestrone, and what their corresponding behaviors
14
+ # are.
15
+ def option_parser #:nodoc:
16
+ @logger = Logger.new
17
+ @option_parser ||= OptionParser.new do |opts|
18
+ opts.banner = "Usage: #{File.basename($0)} [options] action ..."
19
+
20
+ opts.on("-d", "--debug",
21
+ "Prompts before each remote command execution."
22
+ ) { |value| options[:debug] = true }
23
+
24
+ opts.on("-e", "--explain TASK",
25
+ "Displays help (if available) for the task."
26
+ ) { |value| options[:explain] = value }
27
+
28
+ opts.on("-F", "--default-config",
29
+ "Always use default config, even with -f."
30
+ ) { options[:default_config] = true }
31
+
32
+ opts.on("-f", "--file FILE",
33
+ "A recipe file to load. May be given more than once."
34
+ ) { |value| options[:recipes] << value }
35
+
36
+ opts.on("-H", "--long-help", "Explain these options and environment variables.") do
37
+ long_help
38
+ exit
39
+ end
40
+
41
+ opts.on("-h", "--help", "Display this help message.") do
42
+ puts opts
43
+ exit
44
+ end
45
+
46
+ opts.on("-l", "--logger [STDERR|STDOUT|file]",
47
+ "Choose logger method. STDERR used by default."
48
+ ) do |value|
49
+ options[:output] = if value.nil? || value.upcase == 'STDERR'
50
+ # Using default logger.
51
+ nil
52
+ elsif value.upcase == 'STDOUT'
53
+ $stdout
54
+ else
55
+ value
56
+ end
57
+ end
58
+
59
+ opts.on("-n", "--dry-run",
60
+ "Prints out commands without running them."
61
+ ) { |value| options[:dry_run] = true }
62
+
63
+ opts.on("-p", "--password",
64
+ "Immediately prompt for the sudo password."
65
+ ) { options[:password] = nil }
66
+
67
+ opts.on("-q", "--quiet",
68
+ "Make the output as quiet as possible."
69
+ ) { options[:verbose] = 0 }
70
+
71
+ opts.on("-S", "--set-before NAME=VALUE",
72
+ "Set a variable before the recipes are loaded."
73
+ ) do |pair|
74
+ name, value = pair.split(/=/, 2)
75
+ options[:pre_vars][name.to_sym] = value
76
+ end
77
+
78
+ opts.on("-s", "--set NAME=VALUE",
79
+ "Set a variable after the recipes are loaded."
80
+ ) do |pair|
81
+ name, value = pair.split(/=/, 2)
82
+ options[:vars][name.to_sym] = value
83
+ end
84
+
85
+ opts.on("-T", "--tasks [PATTERN]",
86
+ "List all tasks (matching optional PATTERN) in the loaded recipe files."
87
+ ) do |value|
88
+ options[:tasks] = if value
89
+ value
90
+ else
91
+ true
92
+ end
93
+ options[:verbose] ||= 0
94
+ end
95
+
96
+ opts.on("-t", "--tool",
97
+ "Abbreviates the output of -T for tool integration."
98
+ ) { options[:tool] = true }
99
+
100
+ opts.on("-V", "--version",
101
+ "Display the Minestrone version, and exit."
102
+ ) do
103
+ require 'minestrone/version'
104
+ puts "Minestrone v#{Minestrone::Version}"
105
+ exit
106
+ end
107
+
108
+ opts.on("-v", "--verbose",
109
+ "Be more verbose. May be given more than once."
110
+ ) do
111
+ options[:verbose] ||= 0
112
+ options[:verbose] += 1
113
+ end
114
+
115
+ opts.on("-X", "--skip-system-config",
116
+ "Don't load the system config file (minestrone.conf)"
117
+ ) { options.delete(:sysconf) }
118
+
119
+ opts.on("-x", "--skip-user-config",
120
+ "Don't load the user config file (.caprc)"
121
+ ) { options.delete(:dotfile) }
122
+ end
123
+ end
124
+
125
+ # If the arguments to the command are empty, this will print the
126
+ # allowed options and exit. Otherwise, it will parse the command
127
+ # line and set up any default options.
128
+
129
+ def parse_options!
130
+ @options = {
131
+ :recipes => [],
132
+ :actions => [],
133
+ :vars => {},
134
+ :pre_vars => {},
135
+ :sysconf => default_sysconf,
136
+ :dotfile => default_dotfile
137
+ }
138
+
139
+ if args.empty?
140
+ warn "Please specify at least one action to execute."
141
+ warn option_parser
142
+ exit
143
+ end
144
+
145
+ option_parser.parse!(args)
146
+
147
+ coerce_variable_types!
148
+
149
+ # if no verbosity has been specified, be verbose
150
+ options[:verbose] = 3 if !options.has_key?(:verbose)
151
+
152
+ look_for_default_recipe_file! if options[:default_config] || options[:recipes].empty?
153
+ extract_environment_variables!
154
+
155
+ options[:actions].concat(args)
156
+
157
+ password = options.has_key?(:password)
158
+ options[:password] = Proc.new { self.class.password_prompt }
159
+ options[:password] = options[:password].call if password
160
+ end
161
+
162
+ # Extracts name=value pairs from the remaining command-line arguments
163
+ # and assigns them as environment variables.
164
+
165
+ def extract_environment_variables!
166
+ args.delete_if do |arg|
167
+ next unless arg.match(/^(\w+)=(.*)$/)
168
+ ENV[$1] = $2
169
+ end
170
+ end
171
+
172
+ # Looks for a default recipe file in the current directory.
173
+
174
+ def look_for_default_recipe_file!
175
+ current = Dir.pwd
176
+
177
+ loop do
178
+ %w(Capfile capfile).each do |file|
179
+ if File.file?(file)
180
+ options[:recipes] << file
181
+ @logger.info "Using recipes from #{File.join(current,file)}"
182
+ return
183
+ end
184
+ end
185
+
186
+ pwd = Dir.pwd
187
+ Dir.chdir("..")
188
+ break if pwd == Dir.pwd # if changing the directory made no difference, then we're at the top
189
+ end
190
+
191
+ Dir.chdir(current)
192
+ end
193
+
194
+ def default_sysconf #:nodoc:
195
+ File.join(sysconf_directory, "minestrone.conf")
196
+ end
197
+
198
+ def default_dotfile #:nodoc:
199
+ File.join(home_directory, ".caprc")
200
+ end
201
+
202
+ def sysconf_directory #:nodoc:
203
+ '/etc'
204
+ end
205
+
206
+ def home_directory #:nodoc:
207
+ ENV["HOME"] || "/"
208
+ end
209
+
210
+ def coerce_variable_types!
211
+ [:pre_vars, :vars].each do |collection|
212
+ options[collection].keys.each do |key|
213
+ options[collection][key] = coerce_variable(options[collection][key])
214
+ end
215
+ end
216
+ end
217
+
218
+ def coerce_variable(value)
219
+ case value
220
+ when /^"(.*)"$/ then $1
221
+ when /^'(.*)'$/ then $1
222
+ when /^\d+$/ then value.to_i
223
+ when /^\d+\.\d*$/ then value.to_f
224
+ when "true" then true
225
+ when "false" then false
226
+ when "nil" then nil
227
+ else value
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minestrone'
4
+ require 'minestrone/cli/help'
5
+ require 'minestrone/cli/options'
6
+ require 'minestrone/configuration'
7
+ require 'highline'
8
+
9
+ # work around problem where HighLine detects an eof on $stdin and raises an
10
+ # error.
11
+ HighLine.track_eof = false
12
+
13
+ module Minestrone
14
+
15
+ # The CLI class encapsulates the behavior of minestrone when it is invoked
16
+ # as a command-line utility. This allows other programs to embed Minestrone
17
+ # and preserve its command-line semantics.
18
+
19
+ class CLI
20
+
21
+ # The array of (unparsed) command-line options
22
+ attr_reader :args
23
+
24
+ # Return a new CLI instance with the given arguments pre-parsed and
25
+ # ready for execution.
26
+ def self.parse(args)
27
+ self.new(args).tap { |c| c.parse_options! }
28
+ end
29
+
30
+ # Invoke minestrone using the ARGV array as the option parameters. This
31
+ # is what the command-line minestrone utility does.
32
+ def self.execute
33
+ parse(ARGV).execute!
34
+ end
35
+
36
+ # Return the object that provides UI-specific methods, such as prompts
37
+ # and more.
38
+ def self.ui
39
+ @ui ||= HighLine.new
40
+ end
41
+
42
+ # Prompt for a password using echo suppression.
43
+ def self.password_prompt(prompt = "Password: ")
44
+ ui.ask(prompt) { |q| q.echo = false }
45
+ end
46
+
47
+ # Debug mode prompt
48
+ def self.debug_prompt(cmd)
49
+ ui.say("Preparing to execute command: #{cmd}")
50
+ prompt = "Execute ([Yes], No, Abort) "
51
+ ui.ask("#{prompt}? ") do |q|
52
+ q.overwrite = false
53
+ q.default = 'y'
54
+ q.validate = /(y(es)?)|(no?)|(a(bort)?|\n)/i
55
+ q.responses[:not_valid] = prompt
56
+ end
57
+ end
58
+
59
+ # Create a new CLI instance using the given array of command-line parameters
60
+ # to initialize it. By default, +ARGV+ is used, but you can specify a
61
+ # different set of parameters (such as when embedded `min` in a program):
62
+ #
63
+ # require 'minestrone/cli'
64
+ # Minestrone::CLI.parse(%W(-vvvv -f config/deploy update_code)).execute!
65
+ #
66
+ # Note that you can also embed `min` directly by creating a new Configuration
67
+ # instance and setting it up, The above snippet, redone using the
68
+ # Configuration class directly, would look like:
69
+ #
70
+ # require 'minestrone'
71
+ # require 'minestrone/cli'
72
+ # config = Minestrone::Configuration.new
73
+ # config.logger.level = Minestrone::Logger::TRACE
74
+ # config.set(:password) { Minestrone::CLI.password_prompt } # sudo password
75
+ # config.load "config/deploy"
76
+ # config.update_code
77
+ #
78
+ # There may be times that you want/need the additional control offered by
79
+ # manipulating the Configuration directly, but generally interfacing with
80
+ # the CLI class is recommended.
81
+
82
+ def initialize(args)
83
+ @args = args.dup
84
+ $stdout.sync = true # so that Net::SSH prompts show up
85
+ end
86
+
87
+ # Using the options build when the command-line was parsed, instantiate
88
+ # a new Minestrone configuration, initialize it, and execute the
89
+ # requested actions.
90
+ #
91
+ # Returns the Configuration instance used, if successful.
92
+
93
+ def execute!
94
+ config = instantiate_configuration(options)
95
+
96
+ config.debug = options[:debug]
97
+ config.dry_run = options[:dry_run]
98
+ config.logger.level = options[:verbose]
99
+
100
+ set_pre_vars(config)
101
+ load_recipes(config)
102
+
103
+ config.trigger(:load)
104
+ execute_requested_actions(config)
105
+ config.trigger(:exit)
106
+
107
+ config
108
+ rescue Exception => error
109
+ handle_error(error)
110
+ end
111
+
112
+ def execute_requested_actions(config)
113
+ Array(options[:vars]).each { |name, value| config.set(name, value) }
114
+
115
+ Array(options[:actions]).each do |action|
116
+ config.find_and_execute_task(action, :before => :start, :after => :finish)
117
+ end
118
+ end
119
+
120
+ def set_pre_vars(config) #:nodoc:
121
+ config.set :password, options[:password]
122
+ Array(options[:pre_vars]).each { |name, value| config.set(name, value) }
123
+ end
124
+
125
+ def load_recipes(config) #:nodoc:
126
+ # load the standard recipe definition
127
+ config.load 'standard'
128
+
129
+ # load systemwide config/recipe definition
130
+ config.load(options[:sysconf]) if options[:sysconf] && File.file?(options[:sysconf])
131
+
132
+ # load user config/recipe definition
133
+ config.load(options[:dotfile]) if options[:dotfile] && File.file?(options[:dotfile])
134
+
135
+ Array(options[:recipes]).each { |recipe| config.load(recipe) }
136
+ end
137
+
138
+ # Primarily useful for testing, but subclasses of CLI could conceivably
139
+ # override this method to return a Configuration subclass or replacement.
140
+
141
+ def instantiate_configuration(options = {})
142
+ Minestrone::Configuration.new(options)
143
+ end
144
+
145
+ def handle_error(error) #:nodoc:
146
+ case error
147
+ when Net::SSH::AuthenticationFailed
148
+ abort "authentication failed for `#{error.message}'"
149
+ when Minestrone::Error
150
+ abort(error.message)
151
+ else
152
+ raise error
153
+ end
154
+ end
155
+
156
+ include Options
157
+ include Help # needs to be included last, because it overrides some methods
158
+ end
159
+ end