-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_command
executable file
·76 lines (71 loc) · 2.3 KB
/
run_command
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'expectacle'
require 'optparse'
require 'yaml'
option = {}
opt = OptionParser.new
opt.on('-h', '--hosts=FILE', 'Set host-list file') do |value|
option[:hosts] = value
end
opt.on('-c', '--commands=FILE', 'Set command-list file') do |value|
option[:commands] = value
end
opt.on('-b', '--base-dir=DIR', 'Set base directory path') do |value|
option[:base_dir] = value
end
opt.on('-p', '--preview', 'Preview parameter') do |value|
option[:preview] = value
end
opt.on('-r', '--run', 'Run(exec) commands to each hosts') do |value|
option[:run] = value
end
opt.on('-s', '--syslog', 'Set syslog logger (localhost syslog)') do |value|
option[:syslog] = value
end
opt.on('-l', '--logfile=FILE', 'Set file to save log message') do |value|
option[:logfile] = value
end
opt.on('-t', '--timeout=SEC', 'Set seconds to timeout') do |value|
option[:timeout] = value
end
opt.on('-q', '--quiet', 'Stop printing results to standard out') do |value|
option[:quiet] = value
end
opt.on('-d', '--debug', 'Set logging level to DEBUG') do |value|
option[:debug] = value
end
opt.parse!(ARGV)
# basic setting
verbose = option.key?(:quiet) ? false : true
timeout = option.key?(:timeout) ? option[:timeout] : 60
logger = if option.key?(:syslog)
:syslog
elsif option.key?(:logfile)
option[:logfile]
else
$stdout
end
base_dir = if option.key?(:base_dir)
option[:base_dir]
else
file_dir = File.dirname(File.expand_path(__FILE__))
File.expand_path('../vendor', file_dir)
end
# create thrower
thrower = Expectacle::Thrower.new(base_dir: base_dir,
verbose: verbose,
timeout: timeout,
logger: logger)
# setup logging level
thrower.logger.level = Logger::DEBUG if option.key?(:debug) && option[:debug]
# run or preview
hosts = YAML.load_file(File.join(thrower.hosts_dir, option[:hosts]))
commands = YAML.load_file(File.join(thrower.commands_dir, option[:commands]))
if option[:preview]
thrower.preview_parameter(hosts, commands)
elsif option[:run]
thrower.run_command_for_all_hosts(hosts, commands)
else
STDERR.puts "#{$PROGRAM_NAME}: Action(preview/run) did not specified."
end