Begin to write slapos command dispatcher (draft)

parent 560385d5
...@@ -47,6 +47,9 @@ setup(name=name, ...@@ -47,6 +47,9 @@ setup(name=name,
# accessing templates # accessing templates
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
# One entry point to control them all
'slapos = slapos.entry:main',
# Deprecated entry points
'slapconsole = slapos.console:run', 'slapconsole = slapos.console:run',
'slapos-request = slapos.console:request', 'slapos-request = slapos.console:request',
'slapformat = slapos.format:main', 'slapformat = slapos.format:main',
...@@ -58,7 +61,6 @@ setup(name=name, ...@@ -58,7 +61,6 @@ setup(name=name,
'slapgrid-supervisord = slapos.grid.svcbackend:supervisord', 'slapgrid-supervisord = slapos.grid.svcbackend:supervisord',
'slapproxy = slapos.proxy:main', 'slapproxy = slapos.proxy:main',
'bang = slapos.bang:main', 'bang = slapos.bang:main',
'slapos = slapos.entry:main',
] ]
}, },
test_suite="slapos.tests", test_suite="slapos.tests",
......
...@@ -26,19 +26,98 @@ ...@@ -26,19 +26,98 @@
# #
############################################################################## ##############################################################################
import argparse
import sys import sys
from register.register import main as node_register from slapos.bang import main as bang
from slapos.console import run as console
from slapos.console import request as request
from slapos.format import main as format
from slapos.grid.slapgrid import runComputerPartition as instance
from slapos.grid.slapgrid import runSoftwareRelease as software
from slapos.grid.slapgrid import runUsageReport as report
from slapos.grid.svcbackend import supervisord
from slapos.grid.svcbackend import supervisorctl
from slapos.register.register import main as register
class EntryPointNotImplementedError(NotImplementedError):
def __init__(self, *args, **kw_args):
NotImplementedError.__init__(self, *args, **kw_args)
def showUsage():
# We are out of option. We have to admit it: no other option than error.
# XXX Real error message
sys.exit(1)
def dispatch(command, is_node):
""" Dispatch to correct SlapOS module.
Here we could use introspection to get rid of the big "if" statements,
but we want to control every input.
"""
if is_node:
if command in 'register':
register()
elif command == 'software':
software()
elif command == 'instance':
instance()
elif command == 'report':
report()
elif command == 'bang':
bang()
elif command == 'format':
format()
elif command in ['start', 'stop', 'status', 'tail']:
supervisord()
supervisorctl()
else:
supervisord()
elif command == 'request':
request()
elif command == 'supply':
raise EntryPointNotImplementedError(command)
elif command == 'start':
raise EntryPointNotImplementedError(command)
elif command == 'stop':
raise EntryPointNotImplementedError(command)
elif command == 'console':
console()
else:
return False
def main(): def main():
if len(sys.argv) < 3: """
print "Usage: slapos node register NODE_NAME [options]" Main entry point of SlapOS Node. Used to dispatch commands to python
print "%s: error: Incorrect number of arguments" % sys.argv[0] module responsible of the operation.
return 0 """
"Run default configuration." description = "XXX TODO"
if sys.argv[1] == "node" and sys.argv[2] == "register": # Parse arguments
sys.argv=sys.argv[2:] parser = argparse.ArgumentParser(description=description)
node_register() parser.add_argument('command')
else : parser.add_argument('argument_list', nargs=argparse.REMAINDER)
print "Usage: slapos node register NODE_NAME [options]"
print "%s: error: Incorrect arguments" % sys.argv[0] # If "node" arg is the first: we strip it and set a switch
# XXX do it with argparse
if len(sys.argv) > 1 and sys.argv[1] == "node":
sys.argv=sys.argv[1:]
is_node = True
else:
is_node = False
namespace = parser.parse_args()
# Set sys.argv for the sub-entry point that we will call
command_line = [namespace.command]
command_line.extend(namespace.argument_list)
sys.argv = command_line
# If configuration file is not given: define it arbitrarily
# If client commands: use ~/.slapos.cfg
# If node commands: use /etc/opt/slapos/slapos.cfg
# XXX TODO
try:
if not dispatch(namespace.command, is_node):
parser.print_help()
except EntryPointNotImplementedError, exception:
# XXX more graceful
print 'Not implemented: %s' % exception
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment