diff --git a/confluent_client/bin/confetty b/confluent_client/bin/confetty index 64764a5e..89296ff6 100755 --- a/confluent_client/bin/confetty +++ b/confluent_client/bin/confetty @@ -576,7 +576,7 @@ def conserver_command(filehandle, localcommand): tlvdata.send(session.connection, {'operation': 'break', 'path': currconsole}) print("break sent]\r") - elif localcommand[0] == 'p': # print + elif localcommand[0] == 'p': # power cmdlen += 1 localcommand = get_command_bytes(filehandle, localcommand, cmdlen) @@ -651,6 +651,8 @@ def conserver_command(filehandle, localcommand): else: print("Unknown power state.]\r") + check_power_state() + elif localcommand[0] == '?': print("help]\r") print(". disconnect\r") @@ -757,6 +759,27 @@ elif shellargs: do_command(command, netserver) quitconfetty(fullexit=True, fixterm=False) +powerstate = None +powertime = None + + +def check_power_state(): + global powerstate, powertime + for rsp in session.read('/nodes/' + consolename + '/power/state'): + if type(rsp) == dict and 'state' in rsp: + powerstate = rsp['state']['value'] + powertime = time.time() + if powerstate == 'off': + sys.stdout.write("\r\n[powered off]\r\n") + elif type(rsp) == dict and '_requestdone' in rsp: + break + elif type(rsp) == dict: + updatestatus(rsp) + else: + sys.stdout.write(rsp) + sys.stdout.flush() + + while inconsole or not doexit: if inconsole: rdylist, _, _ = select.select( @@ -810,6 +833,9 @@ while inconsole or not doexit: myinput = check_escape_seq(myinput, fh) if myinput: tlvdata.send(session.connection, myinput) + if powerstate is None or powertime < time.time() - 60: # Check powerstate every 60 seconds + check_power_state() + else: currcommand = prompt() try: diff --git a/confluent_client/bin/nodeshell b/confluent_client/bin/nodeshell new file mode 100755 index 00000000..74a5cf15 --- /dev/null +++ b/confluent_client/bin/nodeshell @@ -0,0 +1,86 @@ +#!/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. + +import optparse +import os +import select +import shlex +import subprocess +import sys + +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 + + +argparser = optparse.OptionParser( + usage="Usage: %prog node commandexpression", + epilog="Expressions are the same as in attributes, e.g. " + "'ipmitool -H {hardwaremanagement.manager}' will be expanded.") +argparser.disable_interspersed_args() +(options, args) = argparser.parse_args() +if len(args) < 2: + argparser.print_help() + sys.exit(1) +c = client.Command() +cmdstr = " ".join(args[1:]) + +nodeforpopen = {} +popens = [] +for exp in c.create('/noderange/{0}/attributes/expression'.format(args[0]), + {'expression': cmdstr}): + ex = exp['databynode'] + for node in ex: + cmd = ex[node]['value'].encode('utf-8') + cmdv = ['ssh', node] + shlex.split(cmd) + nopen = subprocess.Popen( + cmdv, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + popens.append(nopen) + nodeforpopen[nopen] = node + +all = set([]) +pipedesc = {} +exitcode = 0 +for pop in popens: + node = nodeforpopen[pop] + pipedesc[pop.stdout] = { 'node': node, 'popen': pop, 'type': 'stdout'} + pipedesc[pop.stderr] = {'node': node, 'popen': pop, 'type': 'stderr'} + all.add(pop.stdout) + all.add(pop.stderr) +rdy, _, _ = select.select(all, [], [], 10) +while all and rdy: + for r in rdy: + data = r.readline() + desc = pipedesc[r] + if data: + node = desc['node'] + if desc['type'] == 'stdout': + sys.stdout.write('{0}: {1}'.format(node,data)) + else: + sys.stderr.write('{0}: {1}'.format(node, data)) + else: + pop = desc['popen'] + ret = pop.poll() + if ret is not None: + exitcode = exitcode | ret + all.discard(r) + if all: + rdy, _, _ = select.select(all, [], [], 10) +sys.exit(exitcode) \ No newline at end of file