Commit f3825592 authored by Marco Mariani's avatar Marco Mariani

cli: group commands - client, node, other

parent 5fb75eb8
...@@ -9,6 +9,7 @@ class BangCommand(ConfigCommand): ...@@ -9,6 +9,7 @@ class BangCommand(ConfigCommand):
""" """
request update on all partitions request update on all partitions
""" """
command_group = 'node'
def get_parser(self, prog_name): def get_parser(self, prog_name):
ap = super(BangCommand, self).get_parser(prog_name) ap = super(BangCommand, self).get_parser(prog_name)
......
...@@ -11,7 +11,9 @@ class ConfigError(Exception): ...@@ -11,7 +11,9 @@ class ConfigError(Exception):
class ConfigCommand(Command): class ConfigCommand(Command):
"Base class for commands that require a configuration file" """
Base class for commands that require a configuration file
"""
default_config_var = 'SLAPOS_CONFIGURATION' default_config_var = 'SLAPOS_CONFIGURATION'
...@@ -57,5 +59,10 @@ class ConfigCommand(Command): ...@@ -57,5 +59,10 @@ class ConfigCommand(Command):
class ClientConfigCommand(ConfigCommand): class ClientConfigCommand(ConfigCommand):
"""
Base class for client commands, that use the client configuration file
"""
default_config_var = 'SLAPOS_CLIENT_CONFIGURATION' default_config_var = 'SLAPOS_CLIENT_CONFIGURATION'
default_config_path = '~/.slapos/slapos-client.cfg' default_config_path = '~/.slapos/slapos-client.cfg'
command_group = 'client'
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse
import collections
import logging import logging
import sys import sys
...@@ -48,6 +50,42 @@ class SlapOSCommandManager(cliff.commandmanager.CommandManager): ...@@ -48,6 +50,42 @@ class SlapOSCommandManager(cliff.commandmanager.CommandManager):
sys.exit(5) sys.exit(5)
class SlapOSHelpAction(argparse.Action):
"""
Adapted from cliff.help.HelpAction, this class detects
and outputs command groups, via the .command_group attribute
of the Command class. Must be a class attribute in case the class
cannot be instantiated ('Could not load' message).
"""
def __call__(self, parser, namespace, values, option_string=None):
app = self.default
parser.print_help(app.stdout)
command_manager = app.command_manager
groups = collections.defaultdict(list)
for name, ep in sorted(command_manager):
command_group, help_line = self._help_line(ep, name)
groups[command_group].append(help_line)
for group in sorted(groups):
app.stdout.write('\n%s commands:\n' % group)
for line in sorted(groups[group]):
app.stdout.write(line)
sys.exit(0)
def _help_line(self, ep, name):
try:
factory = ep.load()
except Exception as err:
return 'Could not load %r\n' % ep
try:
cmd = factory(self, None)
except Exception as err:
return 'Could not instantiate %r: %s\n' % (ep, err)
one_liner = cmd.get_description().split('\n')[0]
group = getattr(factory, 'command_group', 'other')
return group, ' %-13s %s\n' % (name, one_liner)
class SlapOSApp(cliff.app.App): class SlapOSApp(cliff.app.App):
# #
...@@ -83,7 +121,13 @@ class SlapOSApp(cliff.app.App): ...@@ -83,7 +121,13 @@ class SlapOSApp(cliff.app.App):
default=None, default=None,
help='Specify a file to log output (default: console only)', help='Specify a file to log output (default: console only)',
) )
parser.add_argument(
'-h', '--help',
action=SlapOSHelpAction,
nargs=0,
default=self, # tricky
help="show this help message and exit",
)
return parser return parser
def initialize_app(self, argv): def initialize_app(self, argv):
......
...@@ -12,6 +12,7 @@ class FormatCommand(ConfigCommand): ...@@ -12,6 +12,7 @@ class FormatCommand(ConfigCommand):
""" """
create users, partitions and network configuration create users, partitions and network configuration
""" """
command_group = 'node'
def get_parser(self, prog_name): def get_parser(self, prog_name):
ap = super(FormatCommand, self).get_parser(prog_name) ap = super(FormatCommand, self).get_parser(prog_name)
......
...@@ -19,6 +19,7 @@ class RegisterCommand(Command): ...@@ -19,6 +19,7 @@ class RegisterCommand(Command):
""" """
register a node in the SlapOS cloud register a node in the SlapOS cloud
""" """
command_group = 'node'
def get_parser(self, prog_name): def get_parser(self, prog_name):
ap = super(RegisterCommand, self).get_parser(prog_name) ap = super(RegisterCommand, self).get_parser(prog_name)
......
...@@ -9,6 +9,7 @@ from slapos.grid.slapgrid import (merged_options, check_missing_parameters, chec ...@@ -9,6 +9,7 @@ from slapos.grid.slapgrid import (merged_options, check_missing_parameters, chec
class SlapgridCommand(ConfigCommand): class SlapgridCommand(ConfigCommand):
command_group = 'node'
method_name = NotImplemented method_name = NotImplemented
default_pidfile = NotImplemented default_pidfile = NotImplemented
......
...@@ -11,7 +11,10 @@ import supervisor.supervisorctl ...@@ -11,7 +11,10 @@ import supervisor.supervisorctl
class SupervisorctlCommand(ConfigCommand): class SupervisorctlCommand(ConfigCommand):
"""open supervisor console, for process management""" """
open supervisor console, for process management
"""
command_group = 'node'
def get_parser(self, prog_name): def get_parser(self, prog_name):
ap = super(SupervisorctlCommand, self).get_parser(prog_name) ap = super(SupervisorctlCommand, self).get_parser(prog_name)
......
...@@ -8,7 +8,10 @@ from slapos.grid.svcbackend import launchSupervisord ...@@ -8,7 +8,10 @@ from slapos.grid.svcbackend import launchSupervisord
class SupervisordCommand(ConfigCommand): class SupervisordCommand(ConfigCommand):
"""launch, if not already running, supervisor daemon""" """
launch, if not already running, supervisor daemon
"""
command_group = 'node'
@must_be_root @must_be_root
def take_action(self, args): def take_action(self, args):
......
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