2013-10-10 00:14:34 +00:00
|
|
|
#!/usr/bin/env python
|
2014-04-07 20:43:39 +00:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2014 IBM Corporation
|
2016-07-14 13:15:49 +00:00
|
|
|
# Copyright 2015-2016 Lenovo
|
2014-04-07 20:43:39 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2013-10-10 00:14:34 +00:00
|
|
|
|
2013-10-11 00:04:59 +00:00
|
|
|
# ultimately, this should provide an interactive cli for navigating confluent
|
|
|
|
# tree and doing console with a socket. <ESC>]0;<string><BELL> can be used to
|
|
|
|
# present info such as whether it is in a console or other mode and, if in
|
|
|
|
# console, how many other connections are live and looking
|
|
|
|
# this means 'wcons' simply needs to make a terminal run and we'll take care of
|
|
|
|
# the title while providing more info
|
|
|
|
# this also means the socket interface needs to have ways to convey more
|
|
|
|
# interesting pieces of data (like concurrent connection count)
|
|
|
|
# socket will probably switch to a TLV scheme:
|
|
|
|
# 32 bit TL, 8 bits of type code and 24 bit size
|
|
|
|
# type codes:
|
|
|
|
# 0: string data
|
|
|
|
# 1: json data
|
|
|
|
# 24 bit size allows the peer to avoid having to do any particular parsing to
|
|
|
|
# understand message boundaries (which is a significant burden on the xCAT
|
|
|
|
# protocol)
|
|
|
|
|
2013-10-12 14:44:46 +00:00
|
|
|
# When in a console client mode, will recognize two escape sequences by
|
|
|
|
# default:
|
2014-04-21 14:17:13 +00:00
|
|
|
# Ctrl-E, c, ?: mimic conserver behavior
|
2013-10-12 14:44:46 +00:00
|
|
|
# ctrl-]: go to interactive prompt (telnet escape, but not telnet prompt)
|
|
|
|
# esc-( would interfere with normal esc use too much
|
|
|
|
# ~ I will not use for now...
|
|
|
|
|
2014-10-06 19:12:16 +00:00
|
|
|
import math
|
2014-02-09 15:43:26 +00:00
|
|
|
import getpass
|
2013-10-10 00:14:34 +00:00
|
|
|
import optparse
|
2013-10-10 23:23:06 +00:00
|
|
|
import os
|
|
|
|
import select
|
2014-02-09 22:35:49 +00:00
|
|
|
import shlex
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2015-02-02 22:15:14 +00:00
|
|
|
import socket
|
2013-10-10 00:14:34 +00:00
|
|
|
import sys
|
2014-10-06 19:12:16 +00:00
|
|
|
import time
|
2015-07-30 21:08:52 +00:00
|
|
|
try:
|
|
|
|
import fcntl
|
|
|
|
import termios
|
|
|
|
import tty
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-02-22 23:12:21 +00:00
|
|
|
exitcode = 0
|
2014-02-10 23:28:45 +00:00
|
|
|
consoleonly = False
|
2014-04-02 20:18:30 +00:00
|
|
|
consolename = ""
|
2015-01-22 16:52:50 +00:00
|
|
|
didconsole = False
|
2014-02-09 22:35:49 +00:00
|
|
|
target = "/"
|
2014-02-09 15:43:26 +00:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
2014-07-14 18:54:12 +00:00
|
|
|
path = os.path.realpath(os.path.join(path, '..', 'lib', 'python'))
|
|
|
|
if path.startswith('/opt'):
|
|
|
|
sys.path.append(path)
|
2014-02-09 15:43:26 +00:00
|
|
|
|
2014-10-07 18:46:53 +00:00
|
|
|
import confluent.termhandler as termhandler
|
2014-05-06 17:37:31 +00:00
|
|
|
import confluent.tlvdata as tlvdata
|
|
|
|
import confluent.client as client
|
2014-02-09 15:43:26 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
conserversequence = '\x05c' # ctrl-e, c
|
2017-11-13 19:04:09 +00:00
|
|
|
clearpowermessage = False
|
2013-10-12 14:44:46 +00:00
|
|
|
|
2015-02-10 22:18:16 +00:00
|
|
|
oldtcattr = None
|
|
|
|
fd = sys.stdin
|
2015-07-30 21:08:52 +00:00
|
|
|
try:
|
|
|
|
if fd.isatty():
|
|
|
|
oldtcattr = termios.tcgetattr(fd.fileno())
|
|
|
|
except NameError:
|
|
|
|
pass
|
2014-04-21 14:17:13 +00:00
|
|
|
netserver = None
|
2014-10-06 19:12:16 +00:00
|
|
|
laststate = {}
|
2013-10-12 14:44:46 +00:00
|
|
|
|
2014-04-23 20:11:26 +00:00
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
def print_help():
|
|
|
|
print("confetty provides a filesystem like interface to confluent. "
|
|
|
|
"Navigation is done using the same commands as would be used in a "
|
|
|
|
"filesystem. Tab completion is supported to aid in navigation,"
|
|
|
|
"as is up arrow to recall previous commands and control-r to search"
|
|
|
|
"previous command history, similar to using bash\n\n"
|
|
|
|
"The supported commands are:\n"
|
|
|
|
"cd [location] - Set the current command context, similar to a "
|
|
|
|
"working directory.\n"
|
|
|
|
"show [resource] - Present the information about the specified "
|
|
|
|
"resource, or current context if omitted.\n"
|
|
|
|
"create [resource] attributename=value attributename=value - Create "
|
|
|
|
"a new instance of a resource.\n"
|
|
|
|
"remove [resource] - Remove a resource from a list\n"
|
|
|
|
"set [resource] attributename=value attributename=value - Change "
|
|
|
|
"the specified attributes value for the given resource name\n"
|
|
|
|
"unset [resource] attributename - Clear any value for the given "
|
|
|
|
"attribute names on a resource.\n"
|
|
|
|
"start [resource] - When used on a text session resource, it "
|
|
|
|
"enters remote terminal mode. In this mode, use 'ctrl-e, c, ?' for "
|
|
|
|
"help"
|
|
|
|
)
|
|
|
|
#TODO(jjohnson2): lookup context help for 'target' variable, perhaps
|
|
|
|
#common with the api document
|
|
|
|
|
|
|
|
|
2014-10-06 19:12:16 +00:00
|
|
|
def updatestatus(stateinfo={}):
|
2017-11-13 19:04:09 +00:00
|
|
|
global powerstate, powertime, clearpowermessage
|
2014-04-02 20:18:30 +00:00
|
|
|
status = consolename
|
|
|
|
info = []
|
2016-03-13 22:48:58 +00:00
|
|
|
for statekey in stateinfo:
|
2014-10-06 19:12:16 +00:00
|
|
|
laststate[statekey] = stateinfo[statekey]
|
|
|
|
if ('connectstate' in laststate and
|
|
|
|
laststate['connectstate'] != 'connected'):
|
2014-10-28 20:52:59 +00:00
|
|
|
info.append(laststate['connectstate'])
|
2016-03-13 22:48:58 +00:00
|
|
|
if laststate['connectstate'] == 'closed':
|
|
|
|
quitconfetty(fullexit=consoleonly)
|
2014-10-06 19:12:16 +00:00
|
|
|
if 'error' in laststate:
|
|
|
|
info.append(laststate['error'])
|
2014-11-04 15:12:37 +00:00
|
|
|
# error will be repeated if relevant
|
|
|
|
# avoid keeping it around as stale
|
|
|
|
del laststate['error']
|
2017-11-06 15:24:32 +00:00
|
|
|
if 'state' in stateinfo: # currently only read power means anything
|
|
|
|
newpowerstate = stateinfo['state']['value']
|
|
|
|
if newpowerstate != powerstate and newpowerstate == 'off':
|
|
|
|
sys.stdout.write("\x1b[2J\x1b[;H[powered off]\r\n")
|
2017-11-13 19:04:09 +00:00
|
|
|
clearpowermessage = True
|
|
|
|
if newpowerstate == 'on' and clearpowermessage:
|
|
|
|
sys.stdout.write("\x1b[2J\x1b[;H")
|
|
|
|
clearpowermessage = False
|
2017-11-06 15:24:32 +00:00
|
|
|
powerstate = newpowerstate
|
2014-10-06 19:12:16 +00:00
|
|
|
if 'clientcount' in laststate and laststate['clientcount'] != 1:
|
|
|
|
info.append('clients: %d' % laststate['clientcount'])
|
2014-10-28 20:45:55 +00:00
|
|
|
if 'bufferage' in stateinfo and stateinfo['bufferage'] is not None:
|
2014-10-06 19:12:16 +00:00
|
|
|
laststate['showtime'] = time.time() - stateinfo['bufferage']
|
|
|
|
if 'showtime' in laststate:
|
|
|
|
showtime = laststate['showtime']
|
|
|
|
age = time.time() - laststate['showtime']
|
2017-06-20 18:56:24 +00:00
|
|
|
if age > 86400: # older than one day
|
2014-10-06 19:12:16 +00:00
|
|
|
# disambiguate by putting date in and time
|
|
|
|
info.append(time.strftime('%m-%dT%H:%M', time.localtime(showtime)))
|
|
|
|
else:
|
|
|
|
info.append(time.strftime('%H:%M', time.localtime(showtime)))
|
2014-04-02 20:18:30 +00:00
|
|
|
if info:
|
|
|
|
status += ' [' + ','.join(info) + ']'
|
2017-06-27 18:04:26 +00:00
|
|
|
if os.environ.get('TERM', '') not in ('linux'):
|
2014-10-06 20:51:56 +00:00
|
|
|
sys.stdout.write('\x1b]0;console: %s\x07' % status)
|
2014-04-02 20:18:30 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2014-04-23 20:11:26 +00:00
|
|
|
def recurse_format(datum, levels=0):
|
|
|
|
ret = ''
|
|
|
|
import json
|
2015-01-19 19:45:24 +00:00
|
|
|
return json.dumps(datum, ensure_ascii=False, indent=1)
|
2014-04-23 20:11:26 +00:00
|
|
|
if isinstance(datum, dict):
|
|
|
|
for key in datum.iterkeys():
|
|
|
|
if datum[key] is None:
|
|
|
|
continue
|
|
|
|
ret += key + ':'
|
|
|
|
if type(datum[key]) in (str, unicode):
|
|
|
|
ret += datum[key] + '\n'
|
|
|
|
else:
|
|
|
|
ret += recurse_format(datum[key], levels + 1)
|
|
|
|
elif isinstance(datum, list):
|
|
|
|
if type(datum[0]) in (str, unicode):
|
|
|
|
ret += '[' + ",".join(datum) + ']\n'
|
|
|
|
else:
|
|
|
|
ret += '['
|
|
|
|
elems = []
|
|
|
|
for elem in datum:
|
|
|
|
elems.append('{' + recurse_format(elem, levels + 1) + '}')
|
|
|
|
ret += ','.join(elems)
|
|
|
|
ret += (' ' * levels) + ']\n'
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
def prompt():
|
2017-06-27 18:04:26 +00:00
|
|
|
if os.environ.get('TERM', '') not in ('linux'):
|
2014-10-06 20:51:56 +00:00
|
|
|
sys.stdout.write('\x1b]0;confetty: %s\x07' % target)
|
2014-02-22 17:13:51 +00:00
|
|
|
try:
|
|
|
|
return raw_input(target + ' -> ')
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print ""
|
|
|
|
return ""
|
|
|
|
except EOFError: # ctrl-d
|
|
|
|
print("exit")
|
|
|
|
return "exit"
|
|
|
|
# sys.stdout.write(target + ' -> ')
|
|
|
|
# sys.stdout.flush()
|
|
|
|
# username = raw_input("Name: ")
|
|
|
|
|
|
|
|
valid_commands = [
|
|
|
|
'start',
|
|
|
|
'cd',
|
|
|
|
'show',
|
|
|
|
'set',
|
2014-02-23 02:53:34 +00:00
|
|
|
'unset',
|
2014-02-22 17:13:51 +00:00
|
|
|
'create',
|
2014-04-10 17:48:29 +00:00
|
|
|
'remove',
|
|
|
|
'rm',
|
|
|
|
'delete',
|
2017-06-20 18:56:24 +00:00
|
|
|
'help',
|
2014-02-22 17:13:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
candidates = None
|
2014-03-04 22:12:19 +00:00
|
|
|
session = None
|
2014-02-22 17:13:51 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2014-02-22 17:13:51 +00:00
|
|
|
def completer(text, state):
|
|
|
|
try:
|
|
|
|
return rcompleter(text, state)
|
|
|
|
except:
|
2015-02-03 16:01:05 +00:00
|
|
|
pass
|
|
|
|
#import traceback
|
|
|
|
#traceback.print_exc()
|
2014-02-22 17:13:51 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2014-02-22 17:13:51 +00:00
|
|
|
def rcompleter(text, state):
|
|
|
|
global candidates
|
|
|
|
global valid_commands
|
|
|
|
cline = readline.get_line_buffer()
|
2015-02-03 16:01:05 +00:00
|
|
|
cline = cline[:readline.get_endidx()]
|
2014-02-22 17:13:51 +00:00
|
|
|
if len(text):
|
|
|
|
cline = cline[:-len(text)]
|
|
|
|
args = shlex.split(cline, posix=True)
|
|
|
|
currpos = len(args)
|
|
|
|
if currpos and cline[-1] == ' ':
|
2014-02-22 19:43:12 +00:00
|
|
|
lastarg = ''
|
2014-02-22 17:13:51 +00:00
|
|
|
currpos += 1
|
2014-02-22 22:31:08 +00:00
|
|
|
elif currpos:
|
2014-02-22 19:43:12 +00:00
|
|
|
lastarg = args[-1]
|
2014-04-21 14:17:13 +00:00
|
|
|
else:
|
|
|
|
lastarg = ''
|
2014-02-22 17:13:51 +00:00
|
|
|
if currpos <= 1:
|
|
|
|
foundcount = 0
|
|
|
|
for cmd in valid_commands:
|
|
|
|
if cmd.startswith(text):
|
|
|
|
if foundcount == state:
|
|
|
|
return cmd
|
|
|
|
else:
|
|
|
|
foundcount += 1
|
2014-02-22 19:43:12 +00:00
|
|
|
candidates = None
|
2014-02-22 17:13:51 +00:00
|
|
|
return None
|
2014-02-22 17:24:22 +00:00
|
|
|
cmd = args[0]
|
2014-02-22 17:13:51 +00:00
|
|
|
if candidates is None:
|
|
|
|
candidates = []
|
2014-02-22 19:43:12 +00:00
|
|
|
targpath = fullpath_target(lastarg)
|
2014-03-04 22:12:19 +00:00
|
|
|
for res in session.read(targpath):
|
2014-02-22 17:13:51 +00:00
|
|
|
if 'item' in res: # a link relation
|
|
|
|
if type(res['item']) == dict:
|
|
|
|
candidates.append(res['item']["href"])
|
|
|
|
else:
|
|
|
|
for item in res['item']:
|
|
|
|
candidates.append(item["href"])
|
|
|
|
foundcount = 0
|
|
|
|
for elem in candidates:
|
2014-02-22 17:24:22 +00:00
|
|
|
if cmd == 'cd' and elem[-1] != '/':
|
|
|
|
continue
|
2014-02-22 17:13:51 +00:00
|
|
|
if elem.startswith(text):
|
|
|
|
if foundcount == state:
|
|
|
|
return elem
|
|
|
|
else:
|
|
|
|
foundcount += 1
|
2014-02-22 19:43:12 +00:00
|
|
|
candidates = None
|
2014-02-22 17:13:51 +00:00
|
|
|
return None
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2013-10-10 00:14:34 +00:00
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
def parse_command(command):
|
2017-03-01 15:31:00 +00:00
|
|
|
try:
|
|
|
|
args = shlex.split(command, posix=True)
|
|
|
|
except ValueError as ve:
|
2017-06-20 18:56:24 +00:00
|
|
|
print('Error: ' + str(ve))
|
2017-03-01 15:31:00 +00:00
|
|
|
return []
|
2014-02-09 22:35:49 +00:00
|
|
|
return args
|
2014-02-22 19:43:12 +00:00
|
|
|
|
2014-02-10 00:26:48 +00:00
|
|
|
|
|
|
|
currchildren = None
|
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2014-05-09 20:38:55 +00:00
|
|
|
def print_result(res):
|
2015-03-19 19:06:31 +00:00
|
|
|
if 'errorcode' in res or 'error' in res:
|
|
|
|
print res['error']
|
2014-05-09 21:18:17 +00:00
|
|
|
return
|
2015-03-23 13:38:56 +00:00
|
|
|
if 'databynode' in res:
|
|
|
|
print_result(res['databynode'])
|
|
|
|
return
|
2017-10-04 13:09:03 +00:00
|
|
|
for key in sorted(res):
|
2014-05-09 20:38:55 +00:00
|
|
|
notes = []
|
|
|
|
if res[key] is None:
|
|
|
|
attrstr = '%s=""' % key
|
|
|
|
elif type(res[key]) == list:
|
|
|
|
attrstr = '%s=%s' % (key, recurse_format(res[key]))
|
2015-04-27 20:57:52 +00:00
|
|
|
elif not isinstance(res[key], dict):
|
2017-09-28 20:20:19 +00:00
|
|
|
try:
|
|
|
|
print '{0}: {1}'.format(key, res[key])
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
print '{0}: {1}'.format(key, repr(res[key]))
|
2015-04-27 20:57:52 +00:00
|
|
|
continue
|
2014-05-09 20:38:55 +00:00
|
|
|
elif 'value' in res[key] and res[key]['value'] is not None:
|
|
|
|
attrstr = '%s="%s"' % (key, res[key]['value'])
|
|
|
|
elif 'value' in res[key] and res[key]['value'] is None:
|
|
|
|
attrstr = '%s=""' % key
|
2015-03-16 14:15:27 +00:00
|
|
|
elif 'isset' in res[key] and res[key]['isset']:
|
|
|
|
attrstr = '%s="********"' % key
|
|
|
|
elif 'isset' in res[key] or not res[key]:
|
|
|
|
attrstr = '%s=""' % key
|
2014-05-09 20:38:55 +00:00
|
|
|
else:
|
2015-03-16 14:15:27 +00:00
|
|
|
sys.stdout.write('{0}: '.format(key))
|
2015-04-27 20:57:52 +00:00
|
|
|
if isinstance(res[key], str) or isinstance(res[key], unicode):
|
|
|
|
print res[key]
|
|
|
|
else:
|
|
|
|
print_result(res[key])
|
2015-03-16 14:15:27 +00:00
|
|
|
continue
|
2014-05-09 20:38:55 +00:00
|
|
|
if res[key] is not None and 'inheritedfrom' in res[key]:
|
|
|
|
notes.append(
|
|
|
|
'Inherited from %s' % res[key]['inheritedfrom'])
|
|
|
|
if res[key] is not None and 'expression' in res[key]:
|
|
|
|
notes.append(
|
|
|
|
('Derived from expression "%s"' %
|
|
|
|
res[key]['expression']))
|
|
|
|
if notes:
|
|
|
|
notestr = '(' + ', '.join(notes) + ')'
|
|
|
|
output = '{0:<40} {1:>39}'.format(attrstr, notestr)
|
|
|
|
else:
|
|
|
|
output = attrstr
|
2015-01-20 21:11:16 +00:00
|
|
|
try:
|
|
|
|
print(output)
|
2015-03-20 21:08:53 +00:00
|
|
|
except (UnicodeDecodeError, UnicodeEncodeError):
|
2015-01-20 21:11:16 +00:00
|
|
|
print(output.encode('utf-8'))
|
2014-05-09 20:38:55 +00:00
|
|
|
|
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
def do_command(command, server):
|
2014-02-22 23:12:21 +00:00
|
|
|
global exitcode
|
2014-02-09 22:35:49 +00:00
|
|
|
global target
|
2014-02-10 23:28:45 +00:00
|
|
|
global currconsole
|
2014-02-10 00:26:48 +00:00
|
|
|
global currchildren
|
2014-02-22 23:12:21 +00:00
|
|
|
exitcode = 0
|
2014-02-09 22:35:49 +00:00
|
|
|
argv = parse_command(command)
|
|
|
|
if len(argv) == 0:
|
|
|
|
return
|
2014-04-11 14:07:35 +00:00
|
|
|
argv[0] = argv[0].lower()
|
2014-02-23 01:10:13 +00:00
|
|
|
if argv[0] == 'exit':
|
2017-06-27 18:04:26 +00:00
|
|
|
if os.environ.get('TERM', '') not in ('linux'):
|
2017-06-20 18:56:24 +00:00
|
|
|
sys.stdout.write('\x1b]0;\x07')
|
2014-02-09 22:35:49 +00:00
|
|
|
sys.exit(0)
|
2017-06-20 18:56:24 +00:00
|
|
|
elif argv[0] in ('help', '?'):
|
|
|
|
return print_help()
|
2014-02-23 01:10:13 +00:00
|
|
|
elif argv[0] == 'cd':
|
2014-02-09 22:35:49 +00:00
|
|
|
otarget = target
|
2014-02-23 00:28:41 +00:00
|
|
|
if len(argv) > 1:
|
|
|
|
target = fullpath_target(argv[1], forcepath=True)
|
2014-04-21 14:17:13 +00:00
|
|
|
else: # cd by itself, go 'home'
|
2014-02-23 00:28:41 +00:00
|
|
|
target = '/'
|
2016-05-09 19:39:05 +00:00
|
|
|
if target[-1] == '/':
|
|
|
|
parentpath = target[:-1]
|
|
|
|
else:
|
|
|
|
parentpath = target
|
|
|
|
if parentpath:
|
|
|
|
childname = '{0}/'.format(parentpath[parentpath.rindex('/') + 1:])
|
|
|
|
parentpath = parentpath[:parentpath.rindex('/') + 1]
|
2016-07-14 13:15:49 +00:00
|
|
|
if parentpath == '/noderange/':
|
|
|
|
for res in session.read(target, server):
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
target = otarget
|
|
|
|
if 'error' in res:
|
|
|
|
sys.stderr.write(target + ': ' + res['error'] + '\n')
|
|
|
|
target = otarget
|
|
|
|
else:
|
|
|
|
foundchild = False
|
|
|
|
for res in session.read(parentpath, server):
|
|
|
|
try:
|
|
|
|
if res['item']['href'] == childname:
|
|
|
|
foundchild = True
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
target = otarget
|
|
|
|
if 'error' in res:
|
|
|
|
sys.stderr.write(target + ': ' + res['error'] + '\n')
|
|
|
|
target = otarget
|
|
|
|
if not foundchild:
|
|
|
|
sys.stderr.write(target + ': Target not found - \n')
|
2016-05-09 19:39:05 +00:00
|
|
|
target = otarget
|
2014-02-22 23:12:21 +00:00
|
|
|
elif argv[0] in ('cat', 'show', 'ls', 'dir'):
|
2014-02-11 00:36:18 +00:00
|
|
|
if len(argv) > 1:
|
|
|
|
targpath = fullpath_target(argv[1])
|
2017-06-20 18:56:24 +00:00
|
|
|
if argv[0] in ('ls', 'dir'):
|
|
|
|
if targpath[-1] != '/':
|
|
|
|
# could still be a directory, fetch the parent..
|
|
|
|
childname = targpath[targpath.rindex('/') + 1:]
|
|
|
|
parentpath = targpath[:targpath.rindex('/') + 1]
|
|
|
|
if parentpath != '/noderange/':
|
|
|
|
# if it were /noderange/, then it's a directory
|
|
|
|
# even though parent won't tell us that
|
|
|
|
for res in session.read(parentpath, server):
|
|
|
|
try:
|
|
|
|
if res['item']['href'] == childname:
|
|
|
|
print(childname)
|
|
|
|
return
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-02-11 00:36:18 +00:00
|
|
|
else:
|
|
|
|
targpath = target
|
2014-03-04 22:12:19 +00:00
|
|
|
for res in session.read(targpath):
|
2014-02-11 00:36:18 +00:00
|
|
|
if 'item' in res: # a link relation
|
|
|
|
if type(res['item']) == dict:
|
|
|
|
print res['item']["href"]
|
|
|
|
else:
|
|
|
|
for item in res['item']:
|
|
|
|
print item["href"]
|
|
|
|
else: # generic attributes to list
|
2014-03-03 20:48:33 +00:00
|
|
|
if 'error' in res:
|
|
|
|
sys.stderr.write(res['error'] + '\n')
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
continue
|
2014-05-09 20:38:55 +00:00
|
|
|
print_result(res)
|
2014-02-22 23:12:21 +00:00
|
|
|
elif argv[0] == 'start':
|
2014-02-10 14:16:29 +00:00
|
|
|
targpath = fullpath_target(argv[1])
|
2014-04-02 20:18:30 +00:00
|
|
|
nodename = targpath.split('/')[-3]
|
2014-02-10 23:28:45 +00:00
|
|
|
currconsole = targpath
|
2015-02-02 22:15:14 +00:00
|
|
|
startrequest = {'operation': 'start', 'path': targpath,
|
|
|
|
'parameters': {}}
|
|
|
|
for param in argv[2:]:
|
|
|
|
(parmkey, parmval) = param.split("=")
|
|
|
|
startrequest['parameters'][parmkey] = parmval
|
2014-04-21 14:17:13 +00:00
|
|
|
tlvdata.send(
|
2015-02-02 22:15:14 +00:00
|
|
|
session.connection, startrequest)
|
2014-03-07 00:49:46 +00:00
|
|
|
status = tlvdata.recv(session.connection)
|
2014-02-10 14:16:29 +00:00
|
|
|
if 'error' in status:
|
2014-02-22 23:12:21 +00:00
|
|
|
if 'errorcode' in status:
|
|
|
|
exitcode = status['errorcode']
|
|
|
|
sys.stderr.write('Error: ' + status['error'] + '\n')
|
2014-08-27 18:29:53 +00:00
|
|
|
while '_requestdone' not in status:
|
|
|
|
status = tlvdata.recv(session.connection)
|
2014-02-10 14:16:29 +00:00
|
|
|
return
|
2014-04-02 20:18:30 +00:00
|
|
|
startconsole(nodename)
|
2014-02-10 14:16:29 +00:00
|
|
|
return
|
2014-02-23 01:10:13 +00:00
|
|
|
elif argv[0] == 'set':
|
|
|
|
setvalues(argv[1:])
|
2014-04-10 17:48:29 +00:00
|
|
|
elif argv[0] == 'create':
|
|
|
|
createresource(argv[1:])
|
|
|
|
elif argv[0] in ('rm', 'delete', 'remove'):
|
|
|
|
delresource(argv[1])
|
2014-02-23 02:53:34 +00:00
|
|
|
elif argv[0] in ('unset', 'clear'):
|
|
|
|
clearvalues(argv[1], argv[2:])
|
2014-10-28 17:42:21 +00:00
|
|
|
elif argv[0] == 'shutdown':
|
|
|
|
shutdown()
|
2014-02-22 23:12:21 +00:00
|
|
|
else:
|
|
|
|
sys.stderr.write("%s: command not found...\n" % argv[0])
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2015-04-27 20:57:52 +00:00
|
|
|
|
2014-10-28 17:42:21 +00:00
|
|
|
def shutdown():
|
|
|
|
tlvdata.send(session.connection, {'operation': 'shutdown', 'path': '/'})
|
2014-02-23 02:53:34 +00:00
|
|
|
|
2015-04-27 20:57:52 +00:00
|
|
|
|
2014-04-10 17:48:29 +00:00
|
|
|
def createresource(args):
|
|
|
|
resname = args[0]
|
|
|
|
attribs = args[1:]
|
|
|
|
keydata = parameterize_attribs(attribs)
|
|
|
|
if keydata is None:
|
|
|
|
return
|
|
|
|
targpath = fullpath_target(resname)
|
2017-08-16 19:06:48 +00:00
|
|
|
if targpath.startswith('/noderange//'):
|
2017-08-14 18:41:43 +00:00
|
|
|
collection = targpath
|
|
|
|
else:
|
|
|
|
collection, _, resname = targpath.rpartition('/')
|
|
|
|
keydata['name'] = resname
|
2014-04-10 17:48:29 +00:00
|
|
|
makecall(session.create, (collection, keydata))
|
|
|
|
|
|
|
|
|
|
|
|
def makecall(callout, args):
|
2014-04-21 14:17:13 +00:00
|
|
|
global exitcode
|
2014-04-10 17:48:29 +00:00
|
|
|
for response in callout(*args):
|
2017-06-20 18:56:24 +00:00
|
|
|
if 'deleted' in response:
|
|
|
|
print("Deleted: " + response['deleted'])
|
|
|
|
if 'created' in response:
|
|
|
|
print("Created: " + response['created'])
|
2014-04-10 17:48:29 +00:00
|
|
|
if 'error' in response:
|
|
|
|
if 'errorcode' in response:
|
|
|
|
exitcode = response['errorcode']
|
|
|
|
sys.stderr.write('Error: ' + response['error'] + '\n')
|
|
|
|
|
|
|
|
|
2014-02-23 02:53:34 +00:00
|
|
|
def clearvalues(resource, attribs):
|
2014-04-21 14:17:13 +00:00
|
|
|
global exitcode
|
2014-02-23 02:53:34 +00:00
|
|
|
targpath = fullpath_target(resource)
|
|
|
|
keydata = {}
|
|
|
|
for attrib in attribs:
|
|
|
|
keydata[attrib] = None
|
2014-03-04 22:12:19 +00:00
|
|
|
for res in session.update(targpath, keydata):
|
2014-02-23 02:53:34 +00:00
|
|
|
if 'error' in res:
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
sys.stderr.write('Error: ' + res['error'] + '\n')
|
|
|
|
|
|
|
|
|
2014-04-10 17:48:29 +00:00
|
|
|
def delresource(resname):
|
|
|
|
resname = fullpath_target(resname)
|
|
|
|
makecall(session.delete, (resname,))
|
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2014-02-23 01:10:13 +00:00
|
|
|
def setvalues(attribs):
|
2014-04-21 14:17:13 +00:00
|
|
|
global exitcode
|
2014-02-23 01:10:13 +00:00
|
|
|
if '=' in attribs[0]: # going straight to attribute
|
|
|
|
resource = attribs[0][:attribs[0].index("=")]
|
2014-02-23 02:22:45 +00:00
|
|
|
if '/' in resource:
|
|
|
|
lastslash = resource.rindex('/')
|
|
|
|
attribs[0] = attribs[0][lastslash + 1:]
|
2014-02-23 01:10:13 +00:00
|
|
|
else: # an actual resource
|
|
|
|
resource = attribs[0]
|
|
|
|
attribs = attribs[1:]
|
2014-04-10 21:05:57 +00:00
|
|
|
keydata = parameterize_attribs(attribs)
|
2014-04-10 17:48:29 +00:00
|
|
|
if not keydata:
|
|
|
|
return
|
2014-02-23 01:10:13 +00:00
|
|
|
targpath = fullpath_target(resource)
|
2014-03-04 22:12:19 +00:00
|
|
|
for res in session.update(targpath, keydata):
|
2014-02-23 01:22:23 +00:00
|
|
|
if 'error' in res:
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
sys.stderr.write('Error: ' + res['error'] + '\n')
|
2014-05-09 20:38:55 +00:00
|
|
|
print_result(res)
|
2014-02-23 01:10:13 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2014-04-10 17:48:29 +00:00
|
|
|
def parameterize_attribs(attribs):
|
|
|
|
keydata = {}
|
|
|
|
for attrib in attribs:
|
|
|
|
if '=' not in attrib:
|
|
|
|
sys.stderr.write("Invalid syntax %s\n" % attrib)
|
|
|
|
return None
|
|
|
|
key = attrib[:attrib.index("=")]
|
|
|
|
value = attrib[attrib.index("=") + 1:]
|
|
|
|
if key == 'groups':
|
|
|
|
value = value.split(',')
|
|
|
|
keydata[key] = value
|
|
|
|
return keydata
|
2014-02-23 01:10:13 +00:00
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
def fullpath_target(currpath, forcepath=False):
|
2014-02-09 22:35:49 +00:00
|
|
|
global target
|
2014-04-21 14:17:13 +00:00
|
|
|
if currpath == '':
|
2014-02-22 19:43:12 +00:00
|
|
|
return target
|
2014-04-21 14:17:13 +00:00
|
|
|
pathcomponents = currpath.split("/")
|
2014-02-22 22:31:08 +00:00
|
|
|
if pathcomponents[-1] == "": # preserve path
|
2014-04-21 14:17:13 +00:00
|
|
|
forcepath = True
|
2014-02-09 22:35:49 +00:00
|
|
|
if pathcomponents[0] == "": # absolute path
|
2014-04-21 14:17:13 +00:00
|
|
|
ntarget = currpath
|
2014-02-09 22:35:49 +00:00
|
|
|
else:
|
|
|
|
targparts = target.split("/")[:-1]
|
|
|
|
for component in pathcomponents:
|
2014-04-21 14:17:13 +00:00
|
|
|
if component in ('.', ''): # ignore these
|
2014-02-09 22:35:49 +00:00
|
|
|
continue
|
|
|
|
elif component == '..':
|
|
|
|
if len(targparts) > 0:
|
|
|
|
del targparts[-1]
|
|
|
|
else:
|
|
|
|
targparts.append(component)
|
2014-02-24 16:02:09 +00:00
|
|
|
if forcepath and (len(targparts) == 0 or targparts[-1] != ""):
|
2014-02-11 00:36:18 +00:00
|
|
|
targparts.append('')
|
2014-02-10 14:16:29 +00:00
|
|
|
ntarget = '/'.join(targparts)
|
2014-02-11 00:36:18 +00:00
|
|
|
if forcepath and (len(ntarget) == 0 or ntarget[-1] != '/'):
|
2014-02-10 14:16:29 +00:00
|
|
|
ntarget += '/'
|
|
|
|
return ntarget
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2014-04-02 20:18:30 +00:00
|
|
|
def startconsole(nodename):
|
2014-02-10 14:16:29 +00:00
|
|
|
global inconsole
|
2014-04-02 20:18:30 +00:00
|
|
|
global consolename
|
2015-01-22 16:52:50 +00:00
|
|
|
global didconsole
|
|
|
|
didconsole = True
|
2014-04-02 20:18:30 +00:00
|
|
|
consolename = nodename
|
2014-02-09 22:35:49 +00:00
|
|
|
tty.setraw(sys.stdin.fileno())
|
2014-02-22 17:13:51 +00:00
|
|
|
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK)
|
2014-02-10 14:16:29 +00:00
|
|
|
inconsole = True
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2015-01-22 16:52:50 +00:00
|
|
|
def quitconfetty(code=0, fullexit=False, fixterm=True):
|
2014-02-10 23:28:45 +00:00
|
|
|
global inconsole
|
|
|
|
global currconsole
|
2015-01-22 16:52:50 +00:00
|
|
|
global didconsole
|
|
|
|
if fixterm or didconsole:
|
|
|
|
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK)
|
2015-02-10 22:18:16 +00:00
|
|
|
if oldtcattr is not None:
|
|
|
|
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, oldtcattr)
|
2017-06-20 18:56:24 +00:00
|
|
|
# Request default color scheme, to undo potential weirdness of terminal
|
|
|
|
sys.stdout.write('\x1b[m')
|
2014-04-21 14:17:13 +00:00
|
|
|
if fullexit:
|
2017-06-27 18:04:26 +00:00
|
|
|
if os.environ.get('TERM', '') not in ('linux'):
|
2017-06-20 18:56:24 +00:00
|
|
|
sys.stdout.write('\x1b]0;\x07')
|
2014-02-10 23:28:45 +00:00
|
|
|
sys.exit(code)
|
|
|
|
else:
|
2014-04-09 19:37:35 +00:00
|
|
|
tlvdata.send(session.connection, {'operation': 'stop',
|
|
|
|
'path': currconsole})
|
2014-02-10 23:28:45 +00:00
|
|
|
inconsole = False
|
2013-10-12 14:44:46 +00:00
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
|
2015-09-09 02:52:11 +00:00
|
|
|
def get_session_node(shellargs):
|
|
|
|
# straight to node console
|
|
|
|
if len(shellargs) == 1 and ' ' not in shellargs[0]:
|
|
|
|
return shellargs[0]
|
|
|
|
if len(shellargs) == 2 and shellargs[0] == 'start':
|
|
|
|
args = [s for s in shellargs[1].split('/') if s]
|
|
|
|
if len(args) == 4 and args[0] == 'nodes' and args[2] == 'console' and \
|
|
|
|
args[3] == 'session':
|
|
|
|
return args[1]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-01-27 18:44:12 +00:00
|
|
|
def conserver_command(filehandle, localcommand):
|
2014-04-09 19:37:35 +00:00
|
|
|
# x - conserver has that as 'show baud', I am inclined to replace that with
|
|
|
|
# 'request exclusive'
|
|
|
|
# b - conserver has that as 'broadcast message', I'm tempted to use that
|
|
|
|
# for break
|
|
|
|
# r - replay
|
|
|
|
# p - replay (this is the one I've always used)
|
|
|
|
# f - force attach read/write
|
|
|
|
# a - attach read/write
|
|
|
|
# s - spy mode
|
|
|
|
# l[n] - send a particular break, tempted to do l0 for compatibility
|
|
|
|
# o - reopen tty, this should mean reconnect console
|
|
|
|
# d - down a console... never used this...
|
|
|
|
# L - toggle logging
|
|
|
|
# w - who is on console
|
2017-01-27 18:44:12 +00:00
|
|
|
|
|
|
|
cmdlen = 1
|
|
|
|
localcommand = get_command_bytes(filehandle, localcommand, cmdlen)
|
|
|
|
|
|
|
|
if localcommand[0] == '.':
|
2013-10-12 14:44:46 +00:00
|
|
|
print("disconnect]\r")
|
2014-04-21 14:17:13 +00:00
|
|
|
quitconfetty(fullexit=consoleonly)
|
2017-01-27 18:44:12 +00:00
|
|
|
elif localcommand[0] == 'o':
|
2014-08-28 17:58:31 +00:00
|
|
|
tlvdata.send(session.connection, {'operation': 'reopen',
|
|
|
|
'path': currconsole})
|
|
|
|
print('reopen]\r')
|
2017-01-27 18:44:12 +00:00
|
|
|
elif localcommand[0] == 'b':
|
2014-04-09 19:37:35 +00:00
|
|
|
tlvdata.send(session.connection, {'operation': 'break',
|
|
|
|
'path': currconsole})
|
|
|
|
print("break sent]\r")
|
2017-05-02 18:54:18 +00:00
|
|
|
elif localcommand[0] == 'p': # power
|
2017-01-27 18:44:12 +00:00
|
|
|
cmdlen += 1
|
|
|
|
localcommand = get_command_bytes(filehandle, localcommand, cmdlen)
|
|
|
|
|
|
|
|
if localcommand[1] == 'o': # off
|
|
|
|
print("powering off...")
|
|
|
|
session.simple_noderange_command(consolename, '/power/state', 'off')
|
|
|
|
print("complete]\r")
|
|
|
|
elif localcommand[1] == 's': # shutdown
|
|
|
|
print("shutting down...")
|
|
|
|
session.simple_noderange_command(consolename, '/power/state', 'shutdown')
|
|
|
|
print("complete]\r")
|
|
|
|
elif localcommand[1] == 'b': # boot
|
|
|
|
cmdlen += 1
|
|
|
|
localcommand = get_command_bytes(filehandle, localcommand, cmdlen)
|
|
|
|
|
|
|
|
if localcommand[2] == 's': # boot to setup
|
|
|
|
print("booting to setup...")
|
|
|
|
|
|
|
|
bootmode = 'uefi'
|
|
|
|
bootdev = 'setup'
|
|
|
|
|
|
|
|
rc = session.simple_noderange_command(consolename, '/boot/nextdevice', bootdev, bootmode=bootmode)
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
print("Error]\r")
|
|
|
|
else:
|
|
|
|
rc = session.simple_noderange_command(consolename, '/power/state', 'boot')
|
|
|
|
if rc:
|
|
|
|
print("Error]\r")
|
|
|
|
else:
|
|
|
|
print("complete]\r")
|
|
|
|
|
|
|
|
elif localcommand[2] == 'n': # boot to network
|
|
|
|
print("booting to network...")
|
|
|
|
|
|
|
|
bootmode = 'uefi'
|
|
|
|
bootdev = 'network'
|
|
|
|
|
|
|
|
rc = session.simple_noderange_command(consolename, '/boot/nextdevice', bootdev, bootmode=bootmode)
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
print("Error]\r")
|
|
|
|
else:
|
|
|
|
rc = session.simple_noderange_command(consolename, '/power/state', 'boot')
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
print("Error]\r")
|
|
|
|
else:
|
|
|
|
print("complete]\r")
|
|
|
|
|
|
|
|
elif localcommand[2] == '\x0d': # boot to default
|
|
|
|
print("booting to default...")
|
|
|
|
|
|
|
|
bootmode = 'uefi'
|
|
|
|
bootdev = 'default'
|
|
|
|
|
|
|
|
rc = session.simple_noderange_command(consolename, '/boot/nextdevice', bootdev, bootmode=bootmode)
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
print("Error]\r")
|
|
|
|
else:
|
|
|
|
rc = session.simple_noderange_command(consolename, '/power/state', 'boot')
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
print("Error]\r")
|
|
|
|
else:
|
|
|
|
print("complete]\r")
|
|
|
|
|
|
|
|
else:
|
2017-01-27 19:25:42 +00:00
|
|
|
print("Unknown boot state.]\r")
|
2017-01-27 18:44:12 +00:00
|
|
|
|
|
|
|
else:
|
2017-01-27 19:25:42 +00:00
|
|
|
print("Unknown power state.]\r")
|
2017-01-27 18:44:12 +00:00
|
|
|
|
2017-11-06 15:24:32 +00:00
|
|
|
check_power_state()
|
2017-11-06 16:09:55 +00:00
|
|
|
elif localcommand[0] == 'r':
|
|
|
|
sys.stdout.write('\x1b7\x1b[999;999H\x1b[6n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
reply = ''
|
|
|
|
while 'R' not in reply:
|
|
|
|
try:
|
|
|
|
reply += sys.stdin.read(1)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
reply = reply.replace('\x1b[', '')
|
|
|
|
reply = reply.replace('R', '')
|
|
|
|
height, width = reply.split(';')
|
|
|
|
sys.stdout.write('\x1b8')
|
|
|
|
sys.stdout.flush()
|
|
|
|
print('sending stty commands]')
|
|
|
|
return 'stty columns {0}\rstty rows {1}\r'.format(width, height)
|
2017-01-27 18:44:12 +00:00
|
|
|
elif localcommand[0] == '?':
|
2013-10-12 14:44:46 +00:00
|
|
|
print("help]\r")
|
2017-06-20 18:56:24 +00:00
|
|
|
print(". exit console\r")
|
2017-01-27 19:25:42 +00:00
|
|
|
print("b break\r")
|
|
|
|
print("o reopen\r")
|
|
|
|
print("po power off\r")
|
|
|
|
print("ps shutdown\r")
|
|
|
|
print("pbs boot to setup\r")
|
|
|
|
print("pbn boot to network\r")
|
|
|
|
print("pb<ent> boot to default\r")
|
2017-11-06 16:09:55 +00:00
|
|
|
print("r send stty command to resize terminal\r")
|
2013-10-12 14:44:46 +00:00
|
|
|
print("<cr> abort command\r")
|
2017-01-27 18:44:12 +00:00
|
|
|
elif localcommand[0] == '\x0d':
|
2013-10-12 14:44:46 +00:00
|
|
|
print("ignored]\r")
|
2014-04-21 14:17:13 +00:00
|
|
|
else: # not a command at all..
|
2013-10-12 14:44:46 +00:00
|
|
|
print("unknown -- use '?']\r")
|
|
|
|
|
|
|
|
|
2017-01-27 18:44:12 +00:00
|
|
|
def get_command_bytes(filehandle, localcommand, cmdlen):
|
|
|
|
while len(localcommand) < cmdlen:
|
2018-04-09 14:48:06 +00:00
|
|
|
try:
|
|
|
|
ready, _, _ = select.select((filehandle,), (), (), 1)
|
|
|
|
except select.error:
|
|
|
|
ready = ()
|
2017-01-27 18:44:12 +00:00
|
|
|
if ready:
|
|
|
|
localcommand += filehandle.read()
|
|
|
|
return localcommand
|
|
|
|
|
|
|
|
|
2014-04-21 14:17:13 +00:00
|
|
|
def check_escape_seq(currinput, filehandle):
|
|
|
|
while conserversequence.startswith(currinput):
|
|
|
|
if currinput.startswith(conserversequence): # We have full sequence
|
2013-10-12 14:44:46 +00:00
|
|
|
sys.stdout.write("[")
|
|
|
|
sys.stdout.flush()
|
2014-04-21 14:17:13 +00:00
|
|
|
return conserver_command(
|
|
|
|
filehandle, currinput[len(conserversequence):])
|
2018-04-09 14:48:06 +00:00
|
|
|
try:
|
|
|
|
ready, _, _ = select.select((filehandle,), (), (), 3)
|
|
|
|
except select.error:
|
|
|
|
ready = ()
|
2013-10-12 14:44:46 +00:00
|
|
|
if not ready: # 3 seconds of no typing
|
|
|
|
break
|
2014-04-21 14:17:13 +00:00
|
|
|
currinput += filehandle.read()
|
|
|
|
return currinput
|
2013-10-12 14:44:46 +00:00
|
|
|
|
2013-10-10 00:14:34 +00:00
|
|
|
parser = optparse.OptionParser()
|
2014-04-21 14:17:13 +00:00
|
|
|
parser.add_option("-s", "--server", dest="netserver",
|
|
|
|
help="Confluent instance to connect to",
|
|
|
|
metavar="SERVER:PORT")
|
2014-10-07 18:46:53 +00:00
|
|
|
parser.add_option("-c", "--control", dest="controlpath",
|
|
|
|
help="Path to offer terminal control",
|
|
|
|
metavar="PATH")
|
2014-04-21 14:17:13 +00:00
|
|
|
opts, shellargs = parser.parse_args()
|
2015-02-02 22:15:14 +00:00
|
|
|
|
|
|
|
username = None
|
|
|
|
passphrase = None
|
|
|
|
def server_connect():
|
|
|
|
global session, username, passphrase
|
|
|
|
if opts.controlpath:
|
|
|
|
termhandler.TermHandler(opts.controlpath)
|
|
|
|
if opts.netserver: # going over a TLS network
|
|
|
|
session = client.Command(opts.netserver)
|
|
|
|
elif 'CONFLUENT_HOST' in os.environ:
|
|
|
|
session = client.Command(os.environ['CONFLUENT_HOST'])
|
|
|
|
else: # unix domain
|
|
|
|
session = client.Command()
|
|
|
|
|
|
|
|
# Next stop, reading and writing from whichever of stdin and server goes first.
|
|
|
|
#see pyghmi code for solconnect.py
|
|
|
|
if not session.authenticated and username is not None:
|
|
|
|
session.authenticate(username, passphrase)
|
|
|
|
if not session.authenticated and 'CONFLUENT_USER' in os.environ:
|
|
|
|
username = os.environ['CONFLUENT_USER']
|
|
|
|
passphrase = os.environ['CONFLUENT_PASSPHRASE']
|
|
|
|
session.authenticate(username, passphrase)
|
|
|
|
while not session.authenticated:
|
|
|
|
username = raw_input("Name: ")
|
|
|
|
passphrase = getpass.getpass("Passphrase: ")
|
|
|
|
session.authenticate(username, passphrase)
|
|
|
|
|
|
|
|
|
2015-02-03 18:28:21 +00:00
|
|
|
try:
|
|
|
|
server_connect()
|
|
|
|
except EOFError, KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|
|
|
|
except socket.gaierror:
|
|
|
|
sys.stderr.write('Could not connect to confluent\n')
|
|
|
|
sys.exit(1)
|
|
|
|
# clear on start can help with readable of TUI, but it
|
2013-10-12 14:44:46 +00:00
|
|
|
# can be annoying, so for now don't do it.
|
|
|
|
# sys.stdout.write('\x1b[H\x1b[J')
|
|
|
|
# sys.stdout.flush()
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2015-06-26 19:26:44 +00:00
|
|
|
if sys.stdout.isatty():
|
2015-09-09 02:52:11 +00:00
|
|
|
import readline
|
|
|
|
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
readline.parse_and_bind("set bell-style none")
|
2017-06-20 18:56:24 +00:00
|
|
|
dl = readline.get_completer_delims().replace('-', '')
|
|
|
|
readline.set_completer_delims(dl)
|
2015-09-09 02:52:11 +00:00
|
|
|
readline.set_completer(completer)
|
|
|
|
|
2013-10-12 14:44:46 +00:00
|
|
|
doexit = False
|
2014-02-09 22:35:49 +00:00
|
|
|
inconsole = False
|
|
|
|
pendingcommand = ""
|
2015-09-09 02:52:11 +00:00
|
|
|
session_node = get_session_node(shellargs)
|
|
|
|
if session_node is not None:
|
2014-02-10 23:28:45 +00:00
|
|
|
consoleonly = True
|
2015-09-09 02:52:11 +00:00
|
|
|
do_command("start /nodes/%s/console/session" % session_node, netserver)
|
2014-10-06 19:52:06 +00:00
|
|
|
doexit = True
|
2014-04-23 20:19:22 +00:00
|
|
|
elif shellargs:
|
|
|
|
command = " ".join(shellargs)
|
|
|
|
do_command(command, netserver)
|
2015-01-22 16:52:50 +00:00
|
|
|
quitconfetty(fullexit=True, fixterm=False)
|
2015-02-10 22:18:16 +00:00
|
|
|
|
2017-05-02 18:54:18 +00:00
|
|
|
powerstate = None
|
|
|
|
powertime = None
|
2017-05-02 19:30:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_power_state():
|
2017-11-06 15:24:32 +00:00
|
|
|
tlvdata.send(
|
|
|
|
session.connection,
|
|
|
|
{'operation': 'retrieve',
|
|
|
|
'path': '/nodes/' + consolename + '/power/state'})
|
|
|
|
return
|
2017-05-02 19:30:39 +00:00
|
|
|
|
|
|
|
|
2014-10-28 20:45:55 +00:00
|
|
|
while inconsole or not doexit:
|
2014-02-22 17:13:51 +00:00
|
|
|
if inconsole:
|
2018-04-09 14:48:06 +00:00
|
|
|
try:
|
|
|
|
rdylist, _, _ = select.select(
|
|
|
|
(sys.stdin, session.connection), (), (), 10)
|
|
|
|
except select.error:
|
|
|
|
rdylist = ()
|
2014-02-22 17:13:51 +00:00
|
|
|
for fh in rdylist:
|
2014-03-04 22:12:19 +00:00
|
|
|
if fh == session.connection:
|
2014-02-22 17:13:51 +00:00
|
|
|
# this only should get called in the
|
|
|
|
# case of a console session
|
|
|
|
# each command should slurp up all relevant
|
2014-03-04 22:12:19 +00:00
|
|
|
# recv potential
|
2014-02-22 17:13:51 +00:00
|
|
|
#fh.read()
|
|
|
|
try:
|
2014-03-04 22:12:19 +00:00
|
|
|
data = tlvdata.recv(fh)
|
2014-02-22 17:13:51 +00:00
|
|
|
except Exception:
|
|
|
|
data = None
|
|
|
|
if type(data) == dict:
|
2014-04-02 20:18:30 +00:00
|
|
|
updatestatus(data)
|
2014-02-22 17:13:51 +00:00
|
|
|
continue
|
|
|
|
if data is not None:
|
2017-11-13 19:04:09 +00:00
|
|
|
if clearpowermessage:
|
|
|
|
sys.stdout.write("\x1b[2J\x1b[;H")
|
|
|
|
clearpowermessage = False
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
sys.stdout.write(data)
|
|
|
|
except IOError: # Some times circumstances are bad
|
|
|
|
# resort to byte at a time...
|
|
|
|
for d in data:
|
|
|
|
sys.stdout.write(d)
|
2014-10-06 19:12:16 +00:00
|
|
|
now = time.time()
|
|
|
|
if ('showtime' not in laststate or
|
|
|
|
(now // 60) != laststate['showtime'] // 60):
|
|
|
|
# don't bother churning if minute does not change
|
|
|
|
laststate['showtime'] = now
|
|
|
|
updatestatus()
|
2014-02-22 17:13:51 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
else:
|
2015-02-02 22:15:14 +00:00
|
|
|
deadline = 5
|
|
|
|
connected = False
|
|
|
|
while not connected and deadline > 0:
|
|
|
|
try:
|
|
|
|
server_connect()
|
|
|
|
connected = True
|
2015-03-26 18:03:56 +00:00
|
|
|
except (socket.gaierror, socket.error):
|
2015-02-02 22:15:14 +00:00
|
|
|
pass
|
|
|
|
if not connected:
|
|
|
|
time.sleep(1)
|
|
|
|
deadline -=1
|
|
|
|
if connected:
|
|
|
|
do_command(
|
|
|
|
"start /nodes/%s/console/session skipreplay=True" % consolename,
|
|
|
|
netserver)
|
|
|
|
else:
|
|
|
|
doexit = True
|
|
|
|
inconsole = False
|
|
|
|
sys.stdout.write("\r\n[remote disconnected]\r\n")
|
2014-02-22 17:13:51 +00:00
|
|
|
break
|
2013-10-10 23:23:06 +00:00
|
|
|
else:
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
myinput = fh.read()
|
|
|
|
myinput = check_escape_seq(myinput, fh)
|
|
|
|
if myinput:
|
|
|
|
tlvdata.send(session.connection, myinput)
|
|
|
|
except IOError:
|
|
|
|
pass
|
2017-11-13 19:22:30 +00:00
|
|
|
if powerstate is None or powertime < time.time() - 10: # Check powerstate every 10 seconds
|
2017-11-06 15:24:32 +00:00
|
|
|
powertime = time.time()
|
|
|
|
powerstate = True
|
|
|
|
check_power_state()
|
2014-02-22 17:13:51 +00:00
|
|
|
else:
|
2014-04-21 14:17:13 +00:00
|
|
|
currcommand = prompt()
|
2015-02-03 18:28:21 +00:00
|
|
|
try:
|
|
|
|
do_command(currcommand, netserver)
|
|
|
|
except socket.error:
|
|
|
|
try:
|
|
|
|
server_connect()
|
|
|
|
do_command(currcommand, netserver)
|
|
|
|
except socket.error:
|
|
|
|
doexit = True
|
|
|
|
sys.stdout.write('Lost connection to server')
|
2014-04-21 14:17:13 +00:00
|
|
|
quitconfetty(fullexit=True)
|