2017-04-27 20:44:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2016-2017 Lenovo
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
from collections import deque
|
2017-04-27 20:44:19 +00:00
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import select
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2017-04-27 20:44:19 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2017-04-27 20:44:19 +00:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
path = os.path.realpath(os.path.join(path, '..', 'lib', 'python'))
|
|
|
|
if path.startswith('/opt'):
|
|
|
|
sys.path.append(path)
|
|
|
|
|
|
|
|
import confluent.client as client
|
2017-11-06 20:47:59 +00:00
|
|
|
import confluent.sortutil as sortutil
|
2017-04-27 20:44:19 +00:00
|
|
|
|
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
def run():
|
2017-11-06 19:51:59 +00:00
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
argparser = optparse.OptionParser(
|
2017-11-06 19:51:59 +00:00
|
|
|
usage="Usage: %prog noderange commandexpression",
|
2017-06-20 18:56:24 +00:00
|
|
|
epilog="Expressions are the same as in attributes, e.g. "
|
|
|
|
"'ipmitool -H {hardwaremanagement.manager}' will be expanded.")
|
2017-11-06 19:51:59 +00:00
|
|
|
argparser.add_option('-c', '--count', type='int', default=168,
|
|
|
|
help='Number of commands to run at a time')
|
|
|
|
# among other things, FD_SETSIZE limits. Besides, spawning too many
|
|
|
|
# processes can be unkind for the unaware on memory pressure and such...
|
2017-06-20 18:56:24 +00:00
|
|
|
argparser.disable_interspersed_args()
|
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
if len(args) < 2:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2017-11-13 16:49:40 +00:00
|
|
|
client.check_globbing(args[0])
|
2017-11-06 19:51:59 +00:00
|
|
|
concurrentprocs = options.count
|
2017-06-20 18:56:24 +00:00
|
|
|
c = client.Command()
|
|
|
|
cmdstr = " ".join(args[1:])
|
2017-04-27 20:44:19 +00:00
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
currprocs = 0
|
|
|
|
all = set([])
|
|
|
|
pipedesc = {}
|
|
|
|
pendingexecs = deque()
|
2017-08-03 12:39:04 +00:00
|
|
|
exitcode = 0
|
|
|
|
|
2017-04-27 20:44:19 +00:00
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
for exp in c.create('/noderange/{0}/attributes/expression'.format(args[0]),
|
|
|
|
{'expression': cmdstr}):
|
2017-08-03 12:39:04 +00:00
|
|
|
if 'error' in exp:
|
|
|
|
sys.stderr.write(exp['error'] + '\n')
|
|
|
|
exitcode |= exp.get('errorcode', 1)
|
|
|
|
ex = exp.get('databynode', ())
|
2017-06-20 18:56:24 +00:00
|
|
|
for node in ex:
|
|
|
|
cmd = ex[node]['value'].encode('utf-8')
|
2017-08-03 12:33:29 +00:00
|
|
|
cmdv = ['ssh', node, cmd]
|
2017-06-20 18:56:24 +00:00
|
|
|
if currprocs < concurrentprocs:
|
|
|
|
currprocs += 1
|
|
|
|
run_cmdv(node, cmdv, all, pipedesc)
|
2017-04-27 20:44:19 +00:00
|
|
|
else:
|
2017-06-20 18:56:24 +00:00
|
|
|
pendingexecs.append((node, cmdv))
|
2017-08-03 13:41:21 +00:00
|
|
|
if not all or exitcode:
|
2017-08-03 12:39:04 +00:00
|
|
|
sys.exit(exitcode)
|
2017-06-20 18:56:24 +00:00
|
|
|
rdy, _, _ = select.select(all, [], [], 10)
|
|
|
|
while all:
|
2017-11-06 20:47:59 +00:00
|
|
|
pernodeout = {}
|
2017-06-20 18:56:24 +00:00
|
|
|
for r in rdy:
|
|
|
|
desc = pipedesc[r]
|
2017-11-06 20:47:59 +00:00
|
|
|
node = desc['node']
|
|
|
|
data = True
|
|
|
|
while data and select.select([r], [], [], 0):
|
|
|
|
data = r.readline()
|
|
|
|
if data:
|
|
|
|
if desc['type'] == 'stdout':
|
|
|
|
if node not in pernodeout:
|
|
|
|
pernodeout[node] = []
|
|
|
|
pernodeout[node].append(data)
|
|
|
|
else:
|
|
|
|
sys.stderr.write('{0}: {1}'.format(node, data))
|
|
|
|
sys.stderr.flush()
|
2017-06-20 18:56:24 +00:00
|
|
|
else:
|
2017-11-06 20:47:59 +00:00
|
|
|
pop = desc['popen']
|
|
|
|
ret = pop.poll()
|
|
|
|
if ret is not None:
|
|
|
|
exitcode = exitcode | ret
|
|
|
|
all.discard(r)
|
2018-01-11 17:40:53 +00:00
|
|
|
r.close()
|
2017-11-06 20:47:59 +00:00
|
|
|
if desc['type'] == 'stdout' and pendingexecs:
|
|
|
|
node, cmdv = pendingexecs.popleft()
|
|
|
|
run_cmdv(node, cmdv, all, pipedesc)
|
|
|
|
for node in sortutil.natural_sort(pernodeout):
|
|
|
|
for line in pernodeout[node]:
|
2017-11-15 14:41:01 +00:00
|
|
|
sys.stdout.write('{0}: {1}'.format(node, line))
|
2017-11-06 20:47:59 +00:00
|
|
|
sys.stdout.flush()
|
2017-06-20 18:56:24 +00:00
|
|
|
if all:
|
|
|
|
rdy, _, _ = select.select(all, [], [], 10)
|
|
|
|
sys.exit(exitcode)
|
|
|
|
|
|
|
|
|
|
|
|
def run_cmdv(node, cmdv, all, pipedesc):
|
|
|
|
nopen = subprocess.Popen(
|
|
|
|
cmdv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
pipedesc[nopen.stdout] = {'node': node, 'popen': nopen,
|
|
|
|
'type': 'stdout'}
|
|
|
|
pipedesc[nopen.stderr] = {'node': node, 'popen': nopen,
|
|
|
|
'type': 'stderr'}
|
|
|
|
all.add(nopen.stdout)
|
|
|
|
all.add(nopen.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-01-11 17:40:53 +00:00
|
|
|
run()
|