-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeset-loop
executable file
·174 lines (150 loc) · 4.28 KB
/
nodeset-loop
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
#!/usr/bin/env ruby
#
# Copyright 2015 Victor Penso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 'ostruct'
require 'getoptlong'
require 'logger'
class System
def self.exec(command)
command + ' 2>&1'
$logger.debug "Exec [#{command}]"
# Execute command as subprocess and return the exit code
pipe = IO.popen(command)
# Get the process ID from the child
pid = pipe.pid
# Read the output from the stream
output = pipe.read
# Wait for successful return and pass back the code to the caller
Process.wait(pid)
state=$?
$logger.debug "Returned with #{state}"
if state == 0
return output
else
$logger.warn "Failed to execute [#{command}]"
return nil
end
end
end
exec_name = File.split(__FILE__)[-1]
HELP = <<EOF
Iterate over a nodeset and exectue a command for each node
#{exec_name} [-dhv] COMMAND
COMMAND
Command to execute on all nodes "{}" is replaced by the
node name.
Options
-------
--debug, -d
Show stacktraces in case of errors.
--help, -h
Show this help information.
--nodes, -w NODESET
Nodeset to execute the command on.
--prefix, -p
Print node name infront of command output.
--version, -v
Print version number.
Environment
-----------
NODES: Nodeset to execute the command on (overwrite with
options -w).
EOF
begin
stdin = $stdin.tty? ? String.new : $stdin.read
options = OpenStruct.new
options.debug = false
options.nodeset = String.new
options.prefix = false
options.space = false
$logger = Logger.new($stderr)
# Adjust the time format used for the logger
$logger.datetime_format = "%Y-%m-%d %H:%M:%S "
$logger.formatter = proc do |severity, datetime, progname, message|
"[#{datetime.strftime($logger.datetime_format)}] #{severity} -- #{message}\n"
end
$logger.level = Logger::INFO
GetoptLong.new(
['--debug','-d',GetoptLong::NO_ARGUMENT],
['--help','-h',GetoptLong::NO_ARGUMENT],
['--log-level','-L',GetoptLong::REQUIRED_ARGUMENT],
['--nodes','-w',GetoptLong::REQUIRED_ARGUMENT],
['--prefix','-p',GetoptLong::NO_ARGUMENT],
['--space','-s',GetoptLong::NO_ARGUMENT],
['--version','-v',GetoptLong::NO_ARGUMENT]
).each do |opt,arg|
case opt
when '--debug'
options.debug = true
$logger.level = Logger::DEBUG
when '--help'
$stdout.puts HELP
exit 0
when '--log-level'
$logger.level = case arg
when 'warn'
Logger::WARN
when 'debug'
Logger::DEBUG
when 'fatal'
Logger::FATAL
else
Logger::INFO
end
when '--nodes'
options.nodeset = arg
when '--prefix'
options.prefix = true
when '--space'
options.space = true
when '--version'
$stdout.puts 0.1
exit 0
end
end
if options.nodeset.empty?
if not stdin.empty?
options.nodeset = stdin
elsif ENV.has_key? 'NODES'
options.nodeset = ENV['NODES']
else
raise 'No nodeset defined!'
end
end
$logger.debug "Options:\n #{options.inspect}"
# Expand the nodeset and transform it into an array
nodeset = `nodeset -e #{options.nodeset}`.split(' ')
$logger.debug "Expanded nodeset:\n #{nodeset.inspect}"
commands = ARGV.join(' ')
nodeset.each do |node|
command = commands.gsub(/\{\}/, node)
$stdout.print "#{node}: " if options.prefix
$stdout.print "-- #{node} --\n" if options.space
$stdout.puts System::exec(command)
end
rescue => exc
$stderr.puts "ERROR: #{exc.message}"
$stderr.puts " use -h for detailed instructions"
if options.debug
$stderr.puts '-- Stack Trace --'
$stderr.puts exc.backtrace
else
$stderr.puts 'You may want run this in debug mode with \'-d\''
end
exit 1
end
exit 0