/
cli.rb
287 lines (242 loc) · 9.38 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'optparse'
require 'switchtower'
module SwitchTower
# The CLI class encapsulates the behavior of switchtower when it is invoked
# as a command-line utility. This allows other programs to embed ST and
# preserve it's command-line semantics.
class CLI
# Invoke switchtower using the ARGV array as the option parameters. This
# is what the command-line switchtower utility does.
def self.execute!
new.execute!
end
# The following determines whether or not echo-suppression is available.
# This requires the termios library to be installed (which, unfortunately,
# is not available for Windows).
begin
if !defined?(USE_TERMIOS) || USE_TERMIOS
require 'termios'
else
raise LoadError
end
# Enable or disable stdin echoing to the terminal.
def self.echo(enable)
term = Termios::getattr(STDIN)
if enable
term.c_lflag |= (Termios::ECHO | Termios::ICANON)
else
term.c_lflag &= ~Termios::ECHO
end
Termios::setattr(STDIN, Termios::TCSANOW, term)
end
rescue LoadError
def self.echo(enable)
end
end
# execute the associated block with echo-suppression enabled. Note that
# if termios is not available, echo suppression will not be available
# either.
def self.with_echo
echo(false)
yield
ensure
echo(true)
end
# Prompt for a password using echo suppression.
def self.password_prompt(prompt="Password: ")
sync = STDOUT.sync
begin
with_echo do
STDOUT.sync = true
print(prompt)
STDIN.gets.chomp
end
ensure
STDOUT.sync = sync
puts
end
end
# The array of (unparsed) command-line options
attr_reader :args
# The hash of (parsed) command-line options
attr_reader :options
# Create a new CLI instance using the given array of command-line parameters
# to initialize it. By default, +ARGV+ is used, but you can specify a
# different set of parameters (such as when embedded ST in a program):
#
# require 'switchtower/cli'
# SwitchTower::CLI.new(%w(-vvvv -r config/deploy -a update_code)).execute!
#
# Note that you can also embed ST directly by creating a new Configuration
# instance and setting it up, but you'll often wind up duplicating logic
# defined in the CLI class. The above snippet, redone using the Configuration
# class directly, would look like:
#
# require 'switchtower'
# require 'switchtower/cli'
# config = SwitchTower::Configuration.new
# config.logger_level = SwitchTower::Logger::TRACE
# config.set(:password) { SwitchTower::CLI.password_prompt }
# config.load "standard", "config/deploy"
# config.actor.update_code
#
# There may be times that you want/need the additional control offered by
# manipulating the Configuration directly, but generally interfacing with
# the CLI class is recommended.
def initialize(args = ARGV)
@args = args
@options = { :verbose => 0, :recipes => [], :actions => [], :vars => {},
:pre_vars => {} }
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] [args]"
opts.separator ""
opts.separator "Recipe Options -----------------------"
opts.separator ""
opts.on("-a", "--action ACTION",
"An action to execute. Multiple actions may",
"be specified, and are loaded in the given order."
) { |value| @options[:actions] << value }
opts.on("-p", "--password [PASSWORD]",
"The password to use when connecting. If the switch",
"is given without a password, the password will be",
"prompted for immediately. (Default: prompt for password",
"the first time it is needed.)"
) { |value| @options[:password] = value }
opts.on("-r", "--recipe RECIPE",
"A recipe file to load. Multiple recipes may",
"be specified, and are loaded in the given order."
) { |value| @options[:recipes] << value }
opts.on("-s", "--set NAME=VALUE",
"Specify a variable and it's value to set. This",
"will be set after loading all recipe files."
) do |pair|
name, value = pair.split(/=/, 2)
@options[:vars][name.to_sym] = value
end
opts.on("-S", "--set-before NAME=VALUE",
"Specify a variable and it's value to set. This",
"will be set BEFORE loading all recipe files."
) do |pair|
name, value = pair.split(/=/, 2)
@options[:pre_vars][name.to_sym] = value
end
opts.separator ""
opts.separator "Framework Integration Options --------"
opts.separator ""
opts.on("-A", "--apply-to DIRECTORY",
"Create a minimal set of scripts and recipes to use",
"switchtower with the application at the given",
"directory. (Currently only works with Rails apps.)"
) { |value| @options[:apply_to] = value }
opts.separator ""
opts.separator "Miscellaneous Options ----------------"
opts.separator ""
opts.on("-h", "--help", "Display this help message") do
puts opts
exit
end
opts.on("-P", "--[no-]pretend",
"Run the task(s), but don't actually connect to or",
"execute anything on the servers. (For various reasons",
"this will not necessarily be an accurate depiction",
"of the work that will actually be performed.",
"Default: don't pretend.)"
) { |value| @options[:pretend] = value }
opts.on("-q", "--quiet",
"Make the output as quiet as possible (the default)"
) { @options[:verbose] = 0 }
opts.on("-v", "--verbose",
"Specify the verbosity of the output.",
"May be given multiple times. (Default: silent)"
) { @options[:verbose] += 1 }
opts.on("-V", "--version",
"Display the version info for this utility"
) do
require 'switchtower/version'
puts "SwitchTower v#{SwitchTower::Version::STRING}"
exit
end
opts.separator ""
opts.separator <<-DETAIL.split(/\n/)
You can use the --apply-to switch to generate a minimal set of switchtower
scripts and recipes for an application. Just specify the path to the application
as the argument to --apply-to, like this:
switchtower --apply-to ~/projects/myapp
You'll wind up with a sample deployment recipe in config/deploy.rb, some new
rake tasks in config/tasks, and a switchtower script in your script directory.
(Currently, --apply-to only works with Rails applications.)
DETAIL
if args.empty?
puts opts
exit
else
opts.parse!(args)
end
end
check_options!
password_proc = Proc.new { self.class.password_prompt }
if !@options.has_key?(:password)
@options[:password] = password_proc
elsif !@options[:password]
@options[:password] = password_proc.call
end
end
# Beginning running SwitchTower based on the configured options.
def execute!
if !@options[:recipes].empty?
execute_recipes!
elsif @options[:apply_to]
execute_apply_to!
end
end
private
# Load the recipes specified by the options, and execute the actions
# specified.
def execute_recipes!
config = SwitchTower::Configuration.new
config.logger.level = options[:verbose]
config.set :password, options[:password]
config.set :pretend, options[:pretend]
options[:pre_vars].each { |name, value| config.set(name, value) }
# load the standard recipe definition
config.load "standard"
options[:recipes].each { |recipe| config.load(recipe) }
options[:vars].each { |name, value| config.set(name, value) }
actor = config.actor
options[:actions].each { |action| actor.send action }
end
# Load the Rails generator and apply it to the specified directory.
def execute_apply_to!
require 'switchtower/generators/rails/loader'
Generators::RailsLoader.load! @options
end
APPLY_TO_OPTIONS = [:apply_to]
RECIPE_OPTIONS = [:password]
DEFAULT_RECIPES = %w(stasks config/deploy.rb)
# A sanity check to ensure that a valid operation is specified.
def check_options!
apply_to_given = !(@options.keys & APPLY_TO_OPTIONS).empty?
recipe_given = !(@options.keys & RECIPE_OPTIONS).empty? ||
!@options[:recipes].empty? ||
!@options[:actions].empty?
if apply_to_given && recipe_given
abort "You cannot specify both recipe options and framework integration options."
elsif !apply_to_given
look_for_default_recipe_file! if @options[:recipes].empty?
abort "You must specify at least one recipe" if @options[:recipes].empty?
abort "You must specify at least one action" if @options[:actions].empty?
else
@options[:application] = args.shift
@options[:recipe_file] = args.shift
end
end
def look_for_default_recipe_file!
DEFAULT_RECIPES.each do |file|
if File.exist?(file)
@options[:recipes] << file
break
end
end
end
end
end