Commit 896755e6 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Add default option and configuration file if missing

parent 198cc830
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
############################################################################## ##############################################################################
import argparse import argparse
import ConfigParser
import os
import sys import sys
from slapos.bang import main as bang from slapos.bang import main as bang
from slapos.console import run as console from slapos.console import run as console
...@@ -43,10 +45,51 @@ class EntryPointNotImplementedError(NotImplementedError): ...@@ -43,10 +45,51 @@ class EntryPointNotImplementedError(NotImplementedError):
def __init__(self, *args, **kw_args): def __init__(self, *args, **kw_args):
NotImplementedError.__init__(self, *args, **kw_args) NotImplementedError.__init__(self, *args, **kw_args)
def checkSlaposCfg ():
"""
Check if a slapos configuration file was given as a argument.
If a slapos configuration file is given it return True else False
"""
for element in sys.argv:
if '.cfg' in element:
if os.path.exists(element):
configuration = ConfigParser.SafeConfigParser()
configuration.read(element)
if configuration.has_section('slapos'):
return True
return False
def checkOption(option):
"""
Check if a given option is already in call line
Add it and its values if missing
"""
option = option.split()
key = option[0]
for element in sys.argv:
if key in element:
return True
sys.argv.append(key)
if len(option) > 1 :
sys.argv = sys.argv + option[1:]
return True
def call(fun, config=False, option=[]):
"""
Add missing options to argv
Add config if asked and it is missing
Call function fun
"""
for element in option:
checkOption(element)
if config:
if not checkSlaposCfg():
sys.argv.append(config)
fun()
def showUsage(): def showUsage():
# We are out of option. We have to admit it: no other option than error. # We are out of option. We have to admit it: no other option than error.
# XXX Real error message # XXX Real error message
sys.exit(1) sys.exit(1)
def dispatch(command, is_node): def dispatch(command, is_node):
...@@ -54,19 +97,27 @@ def dispatch(command, is_node): ...@@ -54,19 +97,27 @@ def dispatch(command, is_node):
Here we could use introspection to get rid of the big "if" statements, Here we could use introspection to get rid of the big "if" statements,
but we want to control every input. but we want to control every input.
""" """
# XXX console_config =
if is_node: if is_node:
config = '/etc/opt/slapos/slapos.cfg'
if command in 'register': if command in 'register':
register() call(register)
elif command == 'software': elif command == 'software':
software() call(software, config=config,
option=['--logfile /opt/slapos/slapgrid-sr.log',
'--pidfile /opt/slapos/slapgrid-sr.pid'])
elif command == 'instance': elif command == 'instance':
instance() call(instance, config=config,
option=['--logfile /opt/slapos/slapgrid-cp.log',
'--pidfile /opt/slapos/slapgrid-cp.pid'])
elif command == 'report': elif command == 'report':
report() call(report, config=config,
option=['--logfile /opt/slapos/slapgrid-ur.log'])
elif command == 'bang': elif command == 'bang':
bang() call(bang, config=True)
elif command == 'format': elif command == 'format':
format() call(format, config=config,
option=['--log_file /opt/slapos/slapformat.log'])
elif command in ['start', 'stop', 'status', 'tail']: elif command in ['start', 'stop', 'status', 'tail']:
supervisord() supervisord()
supervisorctl() supervisorctl()
...@@ -105,12 +156,12 @@ def main(): ...@@ -105,12 +156,12 @@ def main():
is_node = False is_node = False
namespace = parser.parse_args() namespace = parser.parse_args()
# Set sys.argv for the sub-entry point that we will call # Set sys.argv for the sub-entry point that we will call
command_line = [namespace.command] command_line = [namespace.command]
command_line.extend(namespace.argument_list) command_line.extend(namespace.argument_list)
sys.argv = command_line sys.argv = command_line
dispatch(namespace.command,is_node)
# If configuration file is not given: define it arbitrarily # If configuration file is not given: define it arbitrarily
# If client commands: use ~/.slapos.cfg # If client commands: use ~/.slapos.cfg
# If node commands: use /etc/opt/slapos/slapos.cfg # If node commands: use /etc/opt/slapos/slapos.cfg
......
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