Commit e1c1f61c authored by Marco Mariani's avatar Marco Mariani

cli refactoring: request

parent dd189996
......@@ -80,6 +80,7 @@ setup(name=name,
'console = slapos.cli.console:ConsoleCommand',
'supply = slapos.cli.supply:SupplyCommand',
'remove = slapos.cli.remove:RemoveCommand',
'request = slapos.cli.request:RequestCommand',
]
},
test_suite="slapos.tests",
......
* enable --debug by default
* multiline help from docstring
instance
software
report
supervisord
supervisorctl
node start == node supervisorctl start
node stop == node supervisorctl stop
node restart == node supervisorctl restart
node status == node supervisorctl status
node tail == node supervisorctl tail
slapos start (NotImplementedError)
slapos stop (NotImplementedError)
slapos destroy (NotImplementedError)
issues:
slapformat --log_file vs cliff --log-file
# -*- coding: utf-8 -*-
import logging
from slapos.cli.config import ClientConfigCommand
from slapos.client import init, do_request, ClientConfig
def parse_option_dict(options):
"""
Parse a list of option strings like foo=bar baz=qux and return a dictionary.
Will raise if keys are repeated.
"""
ret = {}
for option_pair in (options or []):
key, value = option_pair.split('=', 1)
if key in ret:
raise ValueError("Multiple values provided for the same key '%s'" % key)
ret[key] = value
return ret
class RequestCommand(ClientConfigCommand):
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
ap = super(RequestCommand, self).get_parser(prog_name)
ap.add_argument('reference',
help='Your instance reference')
ap.add_argument('software_url',
help='Your software url')
ap.add_argument('--node',
nargs='+',
help="Node request option 'option1=value1 option2=value2'")
ap.add_argument('--type',
help='Define software type to be requested')
ap.add_argument('--slave',
action='store_true',
help='Ask for a slave instance')
ap.add_argument('--configuration',
nargs='+',
help="Give your configuration 'option1=value1 option2=value2'")
return ap
def take_action(self, args):
args.node = parse_option_dict(args.node)
args.configuration = parse_option_dict(args.configuration)
configuration_parser = self.fetch_config(args)
config = ClientConfig(args, configuration_parser)
local = init(config)
do_request(config, local)
......@@ -4,7 +4,8 @@ import argparse
import os
import sys
from slapos.client import ClientConfig, init, do_console, get_config_parser
from slapos.client import ClientConfig, init, do_console
from slapos.cli_legacy.util import get_config_parser
def console():
......
......@@ -33,7 +33,7 @@ import sys
from slapos.cli_legacy.bang import main as bang
from slapos.cli_legacy.console import console
from slapos.client import request
from slapos.cli_legacy.request import request
from slapos.cli_legacy.remove import remove
from slapos.cli_legacy.supply import supply
from slapos.cli_legacy.format import main as format
......
......@@ -2,7 +2,8 @@
import argparse
from slapos.client import ClientConfig, init, do_remove, get_config_parser
from slapos.client import ClientConfig, init, do_remove
from slapos.cli_legacy.util import get_config_parser
def remove():
......
# -*- coding: utf-8 -*-
import argparse
from slapos.client import ClientConfig, init, do_request
from slapos.cli_legacy.util import get_config_parser
def argToDict(element):
"""
convert a table of string 'key=value' to dict
"""
if element is not None:
element_dict = dict([arg.split('=') for arg in element])
return element_dict
def request():
"""Run when invoking slapos request. Request an instance."""
# Parse arguments and inititate needed parameters
# XXX-Cedric: move argument parsing to main entry point
ap = argparse.ArgumentParser()
ap.add_argument('configuration_file',
help='SlapOS configuration file.')
ap.add_argument('reference',
help='Your instance reference')
ap.add_argument('software_url',
help='Your software url')
ap.add_argument('--node',
nargs = '*',
help = 'Node request option '
"'option1=value1 option2=value2'")
ap.add_argument('--type',
type = str,
help = 'Define software type to be requested')
ap.add_argument('--slave',
action = 'store_true', default=False,
help = 'Ask for a slave instance')
ap.add_argument('--configuration',
nargs = '*',
help = 'Give your configuration '
"'option1=value1 option2=value2'")
args = ap.parse_args()
if args.configuration:
args.configuration = argToDict(args.configuration)
if args.node:
args.node = argToDict(args.node)
config = ClientConfig(args, get_config_parser(args.configuration_file))
local = init(config)
do_request(config, local)
......@@ -2,7 +2,8 @@
import argparse
from slapos.client import ClientConfig, init, do_supply, get_config_parser
from slapos.client import ClientConfig, init, do_supply
from slapos.cli_legacy.util import get_config_parser
def supply():
......
# -*- coding: utf-8 -*-
import ConfigParser
import os
def get_config_parser(path):
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
......@@ -27,7 +27,6 @@
#
##############################################################################
import argparse
import atexit
import ConfigParser
import os
......@@ -37,58 +36,6 @@ import slapos.slap.slap
from slapos.slap import ResourceNotReady
def argToDict(element):
"""
convert a table of string 'key=value' to dict
"""
if element is not None:
element_dict = dict([arg.split('=') for arg in element])
return element_dict
def check_request_args():
"""
Parser for request
"""
parser = argparse.ArgumentParser()
parser.add_argument("configuration_file",
help="SlapOS configuration file.")
parser.add_argument("reference",
help="Your instance reference")
parser.add_argument("software_url",
help="Your software url")
parser.add_argument("--node",
nargs = '*',
help = "Node request option "
"'option1=value1 option2=value2'")
parser.add_argument("--type",
type = str,
help = "Define software type to be requested")
parser.add_argument("--slave",
action = "store_true", default=False,
help = "Ask for a slave instance")
parser.add_argument("--configuration",
nargs = '*',
help = "Give your configuration "
"'option1=value1 option2=value2'")
args = parser.parse_args()
# Convert to dict
if args.configuration is not None:
args.configuration = argToDict(args.configuration)
if args.node is not None:
args.node = argToDict(args.node)
return args
def get_config_parser(path):
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):
"""
......@@ -124,6 +71,7 @@ class ClientConfig(object):
else:
setattr(self, 'master_url', master_url)
def init(config):
"""Initialize Slap instance, connect to server and create
aliases to common software releases"""
......@@ -146,22 +94,20 @@ def init(config):
# Create global variable too see available aliases
local['software_list'] = software_list
# Create global shortcut functions to request instance and software
def shorthandRequest(*args, **kwargs):
return slap.registerOpenOrder().request(*args, **kwargs)
def shorthandSupply(*args, **kwargs):
return slap.registerSupply().supply(*args, **kwargs)
local['request'] = shorthandRequest
local['supply'] = shorthandSupply
return local
def request():
"""Run when invoking slapos request. Request an instance."""
# Parse arguments and inititate needed parameters
# XXX-Cedric: move argument parsing to main entry point
options = check_request_args()
config = ClientConfig(options, get_config_parser(options.configuration_file))
local = init(config)
def do_request(config, local):
# Request instance
print("Requesting %s..." % config.reference)
if config.software_url in local:
......@@ -180,7 +126,7 @@ def request():
pprint.pprint(partition.getConnectionParameterDict())
print "You can rerun command to get up-to-date informations."
except ResourceNotReady:
print("Instance requested. Master is provisionning it. Please rerun in a "
print("Instance requested. Master is provisioning it. Please rerun in a "
"couple of minutes to get connection informations.")
exit(2)
......
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