Commit 77035bfa authored by Marco Mariani's avatar Marco Mariani

cli refactoring: console

parent b41b981f
...@@ -77,6 +77,7 @@ setup(name=name, ...@@ -77,6 +77,7 @@ setup(name=name,
'node bang = slapos.cli.bang:BangCommand', 'node bang = slapos.cli.bang:BangCommand',
'node format = slapos.cli.format:FormatCommand', 'node format = slapos.cli.format:FormatCommand',
'node register = slapos.cli.register:RegisterCommand', 'node register = slapos.cli.register:RegisterCommand',
'console = slapos.cli.console:ConsoleCommand',
] ]
}, },
test_suite="slapos.tests", test_suite="slapos.tests",
......
...@@ -10,6 +10,8 @@ class ConfigError(Exception): ...@@ -10,6 +10,8 @@ class ConfigError(Exception):
pass pass
class ConfigCommand(Command): class ConfigCommand(Command):
"Base class for commands that require a configuration file" "Base class for commands that require a configuration file"
...@@ -61,3 +63,10 @@ class ConfigCommand(Command): ...@@ -61,3 +63,10 @@ class ConfigCommand(Command):
self.log.debug('Loading config: %s' % cfg_path) self.log.debug('Loading config: %s' % cfg_path)
return self._get_config(cfg_path, required=True) return self._get_config(cfg_path, required=True)
class ClientConfigCommand(ConfigCommand):
# XXX does not fallback to SLAPOS_CONFIGURATION
default_config_var = 'SLAPOS_CLIENT_CONFIGURATION'
default_config_path = '~/.slapos/slapos.cfg'
# -*- coding: utf-8 -*-
import logging
from slapos.cli.config import ClientConfigCommand
from slapos.client import init, do_console, ClientConfig
class ConsoleCommand(ClientConfigCommand):
"""
slapconsole allows you interact with slap API. You can play with the global
"slap" object and with the global "request" method.
examples :
>>> # Request instance
>>> request(kvm, "myuniquekvm")
>>> # Request software installation on owned computer
>>> supply(kvm, "mycomputer")
>>> # Fetch instance informations on already launched instance
>>> request(kvm, "myuniquekvm").getConnectionParameter("url")
"""
# XXX TODO: docstring is printed without newlines
log = logging.getLogger(__name__)
def take_action(self, args):
configuration_parser = self.fetch_config(args)
config = ClientConfig(args, configuration_parser)
local = init(config)
do_console(local)
...@@ -115,8 +115,18 @@ def check_request_args(): ...@@ -115,8 +115,18 @@ def check_request_args():
return args return args
class Config: def get_config_parser(path):
def __init__(self, option_dict, configuration_file_path=None): configuration_parser = ConfigParser.SafeConfigParser()
path = os.path.expanduser(path)
if not os.path.isfile(path):
raise OSError('Specified configuration file %s does not exist. Exiting.' % path)
configuration_parser.read(path)
return configuration_parser
class ClientConfig(object):
def __init__(self, option_dict, configuration_parser=None):
""" """
Set options given by parameters. Set options given by parameters.
""" """
...@@ -124,14 +134,6 @@ class Config: ...@@ -124,14 +134,6 @@ class Config:
for option, value in option_dict.__dict__.items(): for option, value in option_dict.__dict__.items():
setattr(self, option, value) setattr(self, option, value)
# Load configuration file
configuration_parser = ConfigParser.SafeConfigParser()
if configuration_file_path:
configuration_file_path = os.path.expanduser(configuration_file_path)
if not os.path.isfile(configuration_file_path):
raise OSError('Specified configuration file %s does not exist.'
' Exiting.' % configuration_file_path)
configuration_parser.read(configuration_file_path)
# Merges the arguments and configuration # Merges the arguments and configuration
try: try:
configuration_dict = dict(configuration_parser.items('slapconsole')) configuration_dict = dict(configuration_parser.items('slapconsole'))
...@@ -194,7 +196,7 @@ def request(): ...@@ -194,7 +196,7 @@ def request():
# Parse arguments and inititate needed parameters # Parse arguments and inititate needed parameters
# XXX-Cedric: move argument parsing to main entry point # XXX-Cedric: move argument parsing to main entry point
options = check_request_args() options = check_request_args()
config = Config(options, options.configuration_file) config = ClientConfig(options, get_config_parser(options.configuration_file))
local = init(config) local = init(config)
# Request instance # Request instance
print("Requesting %s..." % config.reference) print("Requesting %s..." % config.reference)
...@@ -257,7 +259,7 @@ def supply(): ...@@ -257,7 +259,7 @@ def supply():
help="Target node") help="Target node")
args = parser.parse_args() args = parser.parse_args()
config = Config(args, args.configuration_file) config = ClientConfig(args, get_config_parser(args.configuration_file))
_supply(args.software_url, args.node, init(config)) _supply(args.software_url, args.node, init(config))
def remove(): def remove():
...@@ -274,27 +276,11 @@ def remove(): ...@@ -274,27 +276,11 @@ def remove():
help="Target node") help="Target node")
args = parser.parse_args() args = parser.parse_args()
config = Config(args, args.configuration_file) config = ClientConfig(args, get_config_parser(args.configuration_file))
_supply(args.software_url, args.node, init(config), remove=True) _supply(args.software_url, args.node, init(config), remove=True)
def slapconsole(): def do_console(local):
"""Ran when invoking slapconsole"""
# Parse arguments
usage = """usage: %s [options] CONFIGURATION_FILE
slapconsole allows you interact with slap API. You can play with the global
"slap" object and with the global "request" method.
examples :
>>> # Request instance
>>> request(kvm, "myuniquekvm")
>>> # Request software installation on owned computer
>>> supply(kvm, "mycomputer")
>>> # Fetch instance informations on already launched instance
>>> request(kvm, "myuniquekvm").getConnectionParameter("url")""" % sys.argv[0]
config = Config(*Parser(usage=usage).check_args())
local = init(config)
# try to enable readline with completion and history # try to enable readline with completion and history
try: try:
import readline import readline
...@@ -317,3 +303,23 @@ examples : ...@@ -317,3 +303,23 @@ examples :
__import__("code").interact(banner="", local=local) __import__("code").interact(banner="", local=local)
def slapconsole():
"""Ran when invoking slapconsole"""
# Parse arguments
usage = """usage: %s [options] CONFIGURATION_FILE
slapconsole allows you interact with slap API. You can play with the global
"slap" object and with the global "request" method.
examples :
>>> # Request instance
>>> request(kvm, "myuniquekvm")
>>> # Request software installation on owned computer
>>> supply(kvm, "mycomputer")
>>> # Fetch instance informations on already launched instance
>>> request(kvm, "myuniquekvm").getConnectionParameter("url")""" % sys.argv[0]
options, configuration_file_path = Parser(usage=usage).check_args()
config = ClientConfig(options, get_config_parser(configuration_file_path))
local = init(config)
do_console(local)
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