Refactor console scripts into entry points

Change-Id: I521ab193bb65dc4d53e84d4f289194bff85b0685
This commit is contained in:
Ilya Etingof 2018-08-10 12:12:32 +02:00
parent 38395b2307
commit 497e11b102
8 changed files with 94 additions and 67 deletions

0
pyghmi/cmd/__init__.py Normal file
View File

View File

@ -77,7 +77,7 @@ class FakeBmc(bmc.Bmc):
self.sol.send_data(data)
if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(
prog='fakebmc',
description='Pretend to be a BMC',
@ -90,3 +90,7 @@ if __name__ == '__main__':
args = parser.parse_args()
mybmc = FakeBmc({'admin': 'password'}, port=args.port)
mybmc.listen()
if __name__ == '__main__':
sys.exit(main())

View File

@ -29,21 +29,8 @@ import tty
from pyghmi.ipmi import console
import threading
tcattr = termios.tcgetattr(sys.stdin)
newtcattr = tcattr
# TODO(jbjohnso): add our exit handler
newtcattr[-1][termios.VINTR] = 0
newtcattr[-1][termios.VSUSP] = 0
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, newtcattr)
tty.setraw(sys.stdin.fileno())
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK)
sol = None
def _doinput():
def _doinput(sol):
while True:
select.select((sys.stdin,), (), (), 600)
try:
@ -51,6 +38,8 @@ def _doinput():
except (IOError, OSError) as e:
if e.errno == 11:
continue
raise
sol.send_data(data)
@ -64,22 +53,40 @@ def _print(data):
if bailout:
raise Exception(data)
try:
if sys.argv[3] is None:
passwd = os.environ['IPMIPASSWORD']
else:
passwd_file = sys.argv[3]
with open(passwd_file, "r") as f:
passwd = f.read()
sol = console.Console(bmc=sys.argv[1], userid=sys.argv[2], password=passwd,
iohandler=_print, force=True)
inputthread = threading.Thread(target=_doinput)
inputthread.daemon = True
inputthread.start()
sol.main_loop()
except Exception:
def main():
tcattr = termios.tcgetattr(sys.stdin)
newtcattr = tcattr
# TODO(jbjohnso): add our exit handler
newtcattr[-1][termios.VINTR] = 0
newtcattr[-1][termios.VSUSP] = 0
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, newtcattr)
tty.setraw(sys.stdin.fileno())
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK)
termios.tcsetattr(sys.stdin, termios.TCSANOW, tcattr)
sys.exit(0)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK)
try:
if sys.argv[3] is None:
passwd = os.environ['IPMIPASSWORD']
else:
passwd_file = sys.argv[3]
with open(passwd_file, "r") as f:
passwd = f.read()
sol = console.Console(bmc=sys.argv[1], userid=sys.argv[2], password=passwd,
iohandler=_print, force=True)
inputthread = threading.Thread(target=_doinput, args=(sol,))
inputthread.daemon = True
inputthread.start()
sol.main_loop()
except Exception:
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK)
termios.tcsetattr(sys.stdin, termios.TCSANOW, tcattr)
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@ -20,69 +20,76 @@
# it isn't conceived as a general utility to actually use, just help developers
# understand how the ipmi_command class workes.
# """
import functools
import os
import string
import sys
from pyghmi.ipmi import command
if (len(sys.argv) < 3) or 'IPMIPASSWORD' not in os.environ:
print("Usage:")
print(" IPMIPASSWORD=password %s bmc username <cmd> <optarg>"
% sys.argv[0])
sys.exit(1)
password = os.environ['IPMIPASSWORD']
os.environ['IPMIPASSWORD'] = ""
bmc = sys.argv[1]
userid = sys.argv[2]
args = None
if len(sys.argv) >= 5:
args = sys.argv[4:]
ipmicmd = None
def docommand(result, ipmisession):
cmmand = sys.argv[3]
def docommand(args, result, ipmisession):
command = args[0]
args = args[1:]
print("Logged into %s" % ipmisession.bmc)
if 'error' in result:
print(result['error'])
return
if cmmand == 'power':
if command == 'power':
if args:
print(ipmisession.set_power(args[0], wait=True))
else:
value = ipmisession.get_power()
print("%s: %s" % (ipmisession.bmc, value['powerstate']))
elif cmmand == 'bootdev':
elif command == 'bootdev':
if args:
print(ipmisession.set_bootdev(args[0]))
else:
print(ipmisession.get_bootdev())
elif cmmand == 'sensors':
elif command == 'sensors':
for reading in ipmisession.get_sensor_data():
print(reading)
elif cmmand == 'health':
elif command == 'health':
print(ipmisession.get_health())
elif cmmand == 'inventory':
elif command == 'inventory':
for item in ipmisession.get_inventory():
print(item)
elif cmmand == 'leds':
elif command == 'leds':
for led in ipmisession.get_leds():
print(led)
elif cmmand == 'graphical':
elif command == 'graphical':
print(ipmisession.get_graphical_console())
elif cmmand == 'net':
elif command == 'net':
print(ipmisession.get_net_configuration())
elif cmmand == 'raw':
elif command == 'raw':
print(ipmisession.raw_command(
netfn=int(args[0]),
command=int(args[1]),
data=map(lambda x: int(x, 16), args[2:])))
bmcs = string.split(bmc, ",")
for bmc in bmcs:
ipmicmd = command.Command(bmc=bmc, userid=userid, password=password,
onlogon=docommand)
ipmicmd.eventloop()
def main():
if (len(sys.argv) < 3) or 'IPMIPASSWORD' not in os.environ:
print("Usage:")
print(" IPMIPASSWORD=password %s bmc username <cmd> <optarg>" % sys.argv[0])
return 1
password = os.environ['IPMIPASSWORD']
os.environ['IPMIPASSWORD'] = ""
bmc = sys.argv[1]
userid = sys.argv[2]
bmcs = string.split(bmc, ",")
ipmicmd = None
for bmc in bmcs:
# NOTE(etingof): is it right to have `ipmicmd` overridden?
ipmicmd = command.Command(
bmc=bmc, userid=userid, password=password,
onlogon=functools.partial(docommand, sys.argv[3:]))
if ipmicmd:
ipmicmd.eventloop()
if __name__ == '__main__':
sys.exit(main())

View File

@ -127,7 +127,7 @@ class LibvirtBmc(bmc.Bmc):
libvirt.virEventRunDefaultImpl()
if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(
prog='virshbmc',
description='Pretend to be a BMC and proxy to virsh',
@ -156,3 +156,7 @@ if __name__ == '__main__':
domain=args.domain,
port=args.port)
mybmc.listen()
if __name__ == '__main__':
sys.exit(main())

View File

@ -28,3 +28,10 @@ packages =
[global]
setup-hooks =
pbr.hooks.setup_hook
[entry_points]
console_scripts =
pyghmicons = pyghmi.cmd.pyghmicons:main
pyghmiutil = pyghmi.cmd.pyghmiutil:main
virshbmc = pyghmi.cmd.virshbmc:main
fakebmc = pyghmi.cmd.fakebmc:main

View File

@ -20,6 +20,5 @@ import setuptools
setuptools.setup(
license='Apache License, Version 2.0',
scripts=['bin/pyghmicons', 'bin/pyghmiutil', 'bin/virshbmc'],
setup_requires=['pbr'],
pbr=True)

View File

@ -17,7 +17,6 @@ sitepackages = True
[testenv:pep8]
basepython = python3
whitelist_externals = bash
commands = bash -c 'pycodestyle pyghmi bin/*'
[testenv:cover]
basepython = python3