Commit d134c8ef authored by Marco Mariani's avatar Marco Mariani

naming consistency between arguments, configuration parser, merged

options, specific configuration instances.
parent 78fc0861
...@@ -31,15 +31,15 @@ ...@@ -31,15 +31,15 @@
import slapos.slap.slap import slapos.slap.slap
def do_bang(config, message): def do_bang(configp, message):
computer_id = config.get('slapos', 'computer_id') computer_id = configp.get('slapos', 'computer_id')
master_url = config.get('slapos', 'master_url') master_url = configp.get('slapos', 'master_url')
if config.has_option('slapos', 'key_file'): if configp.has_option('slapos', 'key_file'):
key_file = config.get('slapos', 'key_file') key_file = configp.get('slapos', 'key_file')
else: else:
key_file = None key_file = None
if config.has_option('slapos', 'cert_file'): if configp.has_option('slapos', 'cert_file'):
cert_file = config.get('slapos', 'cert_file') cert_file = configp.get('slapos', 'cert_file')
else: else:
cert_file = None cert_file = None
slap = slapos.slap.slap() slap = slapos.slap.slap()
......
...@@ -15,8 +15,8 @@ def maybe_md5(s): ...@@ -15,8 +15,8 @@ def maybe_md5(s):
return re.match('[0-9a-f]{32}', s) return re.match('[0-9a-f]{32}', s)
def do_lookup(config, software_url): def do_lookup(configp, software_url):
cache_dir = config.get('networkcache', 'download-binary-dir-url') cache_dir = configp.get('networkcache', 'download-binary-dir-url')
if maybe_md5(software_url): if maybe_md5(software_url):
md5 = software_url md5 = software_url
......
...@@ -17,5 +17,5 @@ class BangCommand(ConfigCommand): ...@@ -17,5 +17,5 @@ class BangCommand(ConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) configp = self.fetch_config(args)
do_bang(config, args.message) do_bang(configp, args.message)
...@@ -18,5 +18,5 @@ class CacheLookupCommand(ConfigCommand): ...@@ -18,5 +18,5 @@ class CacheLookupCommand(ConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) configp = self.fetch_config(args)
do_lookup(config, args.software_url) do_lookup(configp, args.software_url)
...@@ -42,15 +42,15 @@ class ConfigCommand(Command): ...@@ -42,15 +42,15 @@ class ConfigCommand(Command):
else: else:
return None return None
config = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
if config.read(cfg_path) != [cfg_path]: if configp.read(cfg_path) != [cfg_path]:
# bad permission, etc. # bad permission, etc.
if required: if required:
raise ConfigError('Cannot parse configuration file: %s' % cfg_path) raise ConfigError('Cannot parse configuration file: %s' % cfg_path)
else: else:
return None return None
return config return configp
def fetch_config(self, args): def fetch_config(self, args):
if args.cfg: if args.cfg:
......
...@@ -38,7 +38,7 @@ class ConsoleCommand(ClientConfigCommand): ...@@ -38,7 +38,7 @@ class ConsoleCommand(ClientConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
configuration_parser = self.fetch_config(args) configp = self.fetch_config(args)
config = ClientConfig(args, configuration_parser) conf = ClientConfig(args, configp)
local = init(config) local = init(conf)
do_console(local) do_console(local)
...@@ -56,21 +56,21 @@ class FormatCommand(ConfigCommand): ...@@ -56,21 +56,21 @@ class FormatCommand(ConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
configuration_parser = self.fetch_config(args) configp = self.fetch_config(args)
config = FormatConfig(logger=self.log) conf = FormatConfig(logger=self.log)
try: try:
config.setConfig(args, configuration_parser) conf.setConfig(args, configp)
except UsageError as err: except UsageError as err:
sys.stderr.write(err.message + '\n') sys.stderr.write(err.message + '\n')
sys.stderr.write("For help use --help\n") sys.stderr.write("For help use --help\n")
sys.exit(1) sys.exit(1)
tracing_monkeypatch(config) tracing_monkeypatch(conf)
try: try:
do_format(config=config) do_format(conf=conf)
except: except:
self.log.exception('Uncaught exception:') self.log.exception('Uncaught exception:')
raise raise
...@@ -69,9 +69,9 @@ class RegisterCommand(Command): ...@@ -69,9 +69,9 @@ class RegisterCommand(Command):
# ap.error('Please enter your login with your password') # ap.error('Please enter your login with your password')
try: try:
config = RegisterConfig(logger=self.log) conf = RegisterConfig(logger=self.log)
config.setConfig(args) conf.setConfig(args)
return_code = do_register(config) return_code = do_register(conf)
except SystemExit, err: except SystemExit, err:
# Catch exception raised by optparse # Catch exception raised by optparse
# XXX returning exception with sys.exit? # XXX returning exception with sys.exit?
......
...@@ -22,7 +22,7 @@ class RemoveCommand(ClientConfigCommand): ...@@ -22,7 +22,7 @@ class RemoveCommand(ClientConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
configuration_parser = self.fetch_config(args) configp = self.fetch_config(args)
config = ClientConfig(args, configuration_parser) conf = ClientConfig(args, configp)
local = init(config) local = init(conf)
do_remove(args.software_url, args.node, local) do_remove(args.software_url, args.node, local)
...@@ -55,8 +55,8 @@ class RequestCommand(ClientConfigCommand): ...@@ -55,8 +55,8 @@ class RequestCommand(ClientConfigCommand):
args.node = parse_option_dict(args.node) args.node = parse_option_dict(args.node)
args.configuration = parse_option_dict(args.configuration) args.configuration = parse_option_dict(args.configuration)
configuration_parser = self.fetch_config(args) configp = self.fetch_config(args)
config = ClientConfig(args, configuration_parser) conf = ClientConfig(args, configp)
local = init(config) local = init(conf)
do_request(config, local) do_request(conf, local)
...@@ -70,8 +70,8 @@ class SlapgridCommand(ConfigCommand): ...@@ -70,8 +70,8 @@ class SlapgridCommand(ConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) configp = self.fetch_config(args)
options = merged_options(args, config) options = merged_options(args, configp)
check_missing_parameters(options) check_missing_parameters(options)
check_missing_files(options) check_missing_files(options)
......
...@@ -22,8 +22,8 @@ class SupervisorctlCommand(ConfigCommand): ...@@ -22,8 +22,8 @@ class SupervisorctlCommand(ConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) configp = self.fetch_config(args)
instance_root = config.get('slapos', 'instance_root') instance_root = configp.get('slapos', 'instance_root')
configuration_file = os.path.join(instance_root, 'etc', 'supervisord.conf') configuration_file = os.path.join(instance_root, 'etc', 'supervisord.conf')
launchSupervisord(socket=os.path.join(instance_root, 'supervisord.socket'), launchSupervisord(socket=os.path.join(instance_root, 'supervisord.socket'),
configuration_file=configuration_file, configuration_file=configuration_file,
......
...@@ -12,8 +12,8 @@ class SupervisordCommand(ConfigCommand): ...@@ -12,8 +12,8 @@ class SupervisordCommand(ConfigCommand):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) configp = self.fetch_config(args)
instance_root = config.get('slapos', 'instance_root') instance_root = configp.get('slapos', 'instance_root')
launchSupervisord(socket=os.path.join(instance_root, 'supervisord.socket'), launchSupervisord(socket=os.path.join(instance_root, 'supervisord.socket'),
configuration_file=os.path.join(instance_root, 'etc', 'supervisord.conf'), configuration_file=os.path.join(instance_root, 'etc', 'supervisord.conf'),
logger=self.log) logger=self.log)
...@@ -22,7 +22,7 @@ class SupplyCommand(ClientConfigCommand): ...@@ -22,7 +22,7 @@ class SupplyCommand(ClientConfigCommand):
return ap return ap
def take_action(self, args): def take_action(self, args):
configuration_parser = self.fetch_config(args) configp = self.fetch_config(args)
config = ClientConfig(args, configuration_parser) conf = ClientConfig(args, configp)
local = init(config) local = init(conf)
do_supply(args.software_url, args.node, local) do_supply(args.software_url, args.node, local)
...@@ -42,6 +42,6 @@ def main(*args): ...@@ -42,6 +42,6 @@ def main(*args):
args = ap.parse_args(list(args)) args = ap.parse_args(list(args))
else: else:
args = ap.parse_args() args = ap.parse_args()
config = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
config.readfp(args.configuration_file) configp.readfp(args.configuration_file)
do_bang(config, args.message) do_bang(configp, args.message)
...@@ -12,7 +12,7 @@ def cache_lookup(): ...@@ -12,7 +12,7 @@ def cache_lookup():
ap.add_argument("software_url", help="Your software url or MD5 hash") ap.add_argument("software_url", help="Your software url or MD5 hash")
args = ap.parse_args() args = ap.parse_args()
config = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
config.read(args.configuration_file) configp.read(args.configuration_file)
do_lookup(config, args.software_url) do_lookup(configp, args.software_url)
...@@ -44,6 +44,7 @@ examples : ...@@ -44,6 +44,7 @@ examples :
if not os.path.isfile(args.configuration_file): if not os.path.isfile(args.configuration_file):
ap.error("%s: Not found or not a regular file." % args.configuration_file) ap.error("%s: Not found or not a regular file." % args.configuration_file)
config = ClientConfig(args, get_config_parser(args.configuration_file)) configp = get_config_parser(args.configuration_file)
local = init(config) conf = ClientConfig(args, configp)
local = init(conf)
do_console(local) do_console(local)
...@@ -73,9 +73,9 @@ def checkSlaposCfg(): ...@@ -73,9 +73,9 @@ def checkSlaposCfg():
for element in sys.argv: for element in sys.argv:
if '.cfg' in element: if '.cfg' in element:
if os.path.exists(element): if os.path.exists(element):
config = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
config.read(element) configp.read(element)
if config.has_section('slapos'): if configp.has_section('slapos'):
return True return True
return False return False
...@@ -96,7 +96,7 @@ def checkOption(option): ...@@ -96,7 +96,7 @@ def checkOption(option):
return True return True
def call(fun, config=False, option=None): def call(fun, config_path=False, option=None):
""" """
Add missing options to sys.argv Add missing options to sys.argv
Add config if asked and it is missing Add config if asked and it is missing
...@@ -106,9 +106,9 @@ def call(fun, config=False, option=None): ...@@ -106,9 +106,9 @@ def call(fun, config=False, option=None):
option = [] option = []
for element in option: for element in option:
checkOption(element) checkOption(element)
if config: if config_path:
if not checkSlaposCfg(): if not checkSlaposCfg():
sys.argv = [sys.argv[0]] + [os.path.expanduser(config)] + sys.argv[1:] sys.argv = [sys.argv[0]] + [os.path.expanduser(config_path)] + sys.argv[1:]
fun() fun()
sys.exit(0) sys.exit(0)
...@@ -129,34 +129,34 @@ def dispatch(command, is_node_command): ...@@ -129,34 +129,34 @@ def dispatch(command, is_node_command):
if command == 'register': if command == 'register':
call(register) call(register)
elif command == 'software': elif command == 'software':
call(software, config=GLOBAL_SLAPOS_CONFIGURATION, call(software, config_path=GLOBAL_SLAPOS_CONFIGURATION,
option=['--pidfile /opt/slapos/slapgrid-sr.pid']) option=['--pidfile /opt/slapos/slapgrid-sr.pid'])
elif command == 'instance': elif command == 'instance':
call(instance, config=GLOBAL_SLAPOS_CONFIGURATION, call(instance, config_path=GLOBAL_SLAPOS_CONFIGURATION,
option=['--pidfile /opt/slapos/slapgrid-cp.pid']) option=['--pidfile /opt/slapos/slapgrid-cp.pid'])
elif command == 'report': elif command == 'report':
call(report, config=GLOBAL_SLAPOS_CONFIGURATION, call(report, config_path=GLOBAL_SLAPOS_CONFIGURATION,
option=['--pidfile /opt/slapos/slapgrid-ur.pid']) option=['--pidfile /opt/slapos/slapgrid-ur.pid'])
elif command == 'bang': elif command == 'bang':
call(bang, config=GLOBAL_SLAPOS_CONFIGURATION) call(bang, config_path=GLOBAL_SLAPOS_CONFIGURATION)
elif command == 'format': elif command == 'format':
call(format, config=GLOBAL_SLAPOS_CONFIGURATION, option=['-c', '-v']) call(format, config_path=GLOBAL_SLAPOS_CONFIGURATION, option=['-c', '-v'])
elif command == 'supervisord': elif command == 'supervisord':
call(supervisord, config=GLOBAL_SLAPOS_CONFIGURATION) call(supervisord, config_path=GLOBAL_SLAPOS_CONFIGURATION)
elif command == 'supervisorctl': elif command == 'supervisorctl':
call(supervisorctl, config=GLOBAL_SLAPOS_CONFIGURATION) call(supervisorctl, config_path=GLOBAL_SLAPOS_CONFIGURATION)
elif command in ['start', 'stop', 'restart', 'status', 'tail']: elif command in ['start', 'stop', 'restart', 'status', 'tail']:
# Again, too hackish # Again, too hackish
sys.argv[-2:-2] = [command] sys.argv[-2:-2] = [command]
call(supervisorctl, config=GLOBAL_SLAPOS_CONFIGURATION) call(supervisorctl, config_path=GLOBAL_SLAPOS_CONFIGURATION)
else: else:
return False return False
elif command == 'request': elif command == 'request':
call(request, config=USER_SLAPOS_CONFIGURATION) call(request, config_path=USER_SLAPOS_CONFIGURATION)
elif command == 'supply': elif command == 'supply':
call(supply, config=USER_SLAPOS_CONFIGURATION) call(supply, config_path=USER_SLAPOS_CONFIGURATION)
elif command == 'remove': elif command == 'remove':
call(remove, config=USER_SLAPOS_CONFIGURATION) call(remove, config_path=USER_SLAPOS_CONFIGURATION)
elif command == 'start': elif command == 'start':
raise EntryPointNotImplementedError(command) raise EntryPointNotImplementedError(command)
elif command == 'stop': elif command == 'stop':
...@@ -164,9 +164,9 @@ def dispatch(command, is_node_command): ...@@ -164,9 +164,9 @@ def dispatch(command, is_node_command):
elif command == 'destroy': elif command == 'destroy':
raise EntryPointNotImplementedError(command) raise EntryPointNotImplementedError(command)
elif command == 'console': elif command == 'console':
call(console, config=USER_SLAPOS_CONFIGURATION) call(console, config_path=USER_SLAPOS_CONFIGURATION)
elif command == 'cache-lookup': elif command == 'cache-lookup':
call(cache_lookup, config=GLOBAL_SLAPOS_CONFIGURATION) call(cache_lookup, config_path=GLOBAL_SLAPOS_CONFIGURATION)
else: else:
return False return False
......
...@@ -107,23 +107,23 @@ def main(*args): ...@@ -107,23 +107,23 @@ def main(*args):
else: else:
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
config = FormatConfig(logger=logger) conf = FormatConfig(logger=logger)
configuration_parser = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
if configuration_parser.read(args.configuration_file) != [args.configuration_file]: if configp.read(args.configuration_file) != [args.configuration_file]:
raise UsageError('Cannot find or parse configuration file: %s' % args.configuration_file) raise UsageError('Cannot find or parse configuration file: %s' % args.configuration_file)
try: try:
config.setConfig(args, configuration_parser) conf.setConfig(args, configp)
except UsageError as exc: except UsageError as exc:
sys.stderr.write(exc.message + '\n') sys.stderr.write(exc.message + '\n')
sys.stderr.write("For help use --help\n") sys.stderr.write("For help use --help\n")
sys.exit(1) sys.exit(1)
tracing_monkeypatch(config) tracing_monkeypatch(conf)
try: try:
do_format(config=config) do_format(conf=conf)
except: except:
config.logger.exception('Uncaught exception:') conf.logger.exception('Uncaught exception:')
raise raise
...@@ -97,9 +97,9 @@ def main(): ...@@ -97,9 +97,9 @@ def main():
logger.addHandler(handler) logger.addHandler(handler)
try: try:
config = RegisterConfig(logger=logger) conf = RegisterConfig(logger=logger)
config.setConfig(args) conf.setConfig(args)
return_code = do_register(config) return_code = do_register(conf)
except SystemExit as exc: except SystemExit as exc:
# Catch exception raised by optparse # Catch exception raised by optparse
# XXX returning exception with sys.exit? # XXX returning exception with sys.exit?
......
...@@ -16,6 +16,7 @@ def remove(): ...@@ -16,6 +16,7 @@ def remove():
help='Target node') help='Target node')
args = ap.parse_args() args = ap.parse_args()
config = ClientConfig(args, get_config_parser(args.configuration_file)) configp = get_config_parser(args.configuration_file)
local = init(config) conf = ClientConfig(args, configp)
local = init(conf)
do_remove(args.software_url, args.node, local) do_remove(args.software_url, args.node, local)
...@@ -50,6 +50,7 @@ def request(): ...@@ -50,6 +50,7 @@ def request():
if args.node: if args.node:
args.node = argToDict(args.node) args.node = argToDict(args.node)
config = ClientConfig(args, get_config_parser(args.configuration_file)) configp = get_config_parser(args.configuration_file)
local = init(config) conf = ClientConfig(args, configp)
do_request(config, local) local = init(conf)
do_request(conf, local)
...@@ -115,10 +115,10 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple): ...@@ -115,10 +115,10 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
""" """
args = parse_arguments(*argument_tuple) args = parse_arguments(*argument_tuple)
config = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
config.readfp(args.configuration_file) configp.readfp(args.configuration_file)
options = merged_options(args, config) options = merged_options(args, configp)
logger = setup_logger(options) logger = setup_logger(options)
......
...@@ -19,6 +19,7 @@ def supply(): ...@@ -19,6 +19,7 @@ def supply():
help='Target node') help='Target node')
args = ap.parse_args() args = ap.parse_args()
config = ClientConfig(args, get_config_parser(args.configuration_file)) configp = get_config_parser(args.configuration_file)
local = init(config) conf = ClientConfig(args, configp)
local = init(conf)
do_supply(args.software_url, args.node, local) do_supply(args.software_url, args.node, local)
...@@ -5,9 +5,9 @@ import os ...@@ -5,9 +5,9 @@ import os
def get_config_parser(path): def get_config_parser(path):
configuration_parser = ConfigParser.SafeConfigParser() configp = ConfigParser.SafeConfigParser()
path = os.path.expanduser(path) path = os.path.expanduser(path)
if not os.path.isfile(path): if not os.path.isfile(path):
raise OSError('Specified configuration file %s does not exist. Exiting.' % path) raise OSError('Specified configuration file %s does not exist. Exiting.' % path)
configuration_parser.read(path) configp.read(path)
return configuration_parser return configp
...@@ -37,24 +37,25 @@ from slapos.slap import ResourceNotReady ...@@ -37,24 +37,25 @@ from slapos.slap import ResourceNotReady
class ClientConfig(object): class ClientConfig(object):
def __init__(self, option_dict, configuration_parser=None): def __init__(self, args, configp=None):
# XXX configp cannot possibly be optional
""" """
Set options given by parameters. Set options given by parameters.
""" """
# Set options parameters # Set options parameters
for option, value in option_dict.__dict__.items(): for key, value in args.__dict__.items():
setattr(self, option, value) setattr(self, key, value)
# Merges the arguments and configuration # Merges the arguments and configuration
try: try:
configuration_dict = dict(configuration_parser.items('slapconsole')) configuration_dict = dict(configp.items('slapconsole'))
except ConfigParser.NoSectionError: except ConfigParser.NoSectionError:
pass pass
else: else:
for key in configuration_dict: for key in configuration_dict:
if not getattr(self, key, None): if not getattr(self, key, None):
setattr(self, key, configuration_dict[key]) setattr(self, key, configuration_dict[key])
configuration_dict = dict(configuration_parser.items('slapos')) configuration_dict = dict(configp.items('slapos'))
master_url = configuration_dict.get('master_url', None) master_url = configuration_dict.get('master_url', None)
# Backward compatibility, if no key and certificate given in option # Backward compatibility, if no key and certificate given in option
# take one from slapos configuration # take one from slapos configuration
...@@ -77,18 +78,18 @@ class ClientConfig(object): ...@@ -77,18 +78,18 @@ class ClientConfig(object):
if self.cert_file: if self.cert_file:
self.cert_file = os.path.expanduser(self.cert_file) self.cert_file = os.path.expanduser(self.cert_file)
def init(config): def init(conf):
"""Initialize Slap instance, connect to server and create """Initialize Slap instance, connect to server and create
aliases to common software releases""" aliases to common software releases"""
# XXX check certificate and key existence # XXX check certificate and key existence
slap = slapos.slap.slap() slap = slapos.slap.slap()
slap.initializeConnection(config.master_url, slap.initializeConnection(conf.master_url,
key_file=config.key_file, cert_file=config.cert_file) key_file=conf.key_file, cert_file=conf.cert_file)
local = globals().copy() local = globals().copy()
local['slap'] = slap local['slap'] = slap
# Create aliases as global variables # Create aliases as global variables
try: try:
alias = config.alias.split('\n') alias = conf.alias.split('\n')
except AttributeError: except AttributeError:
alias = [] alias = []
software_list = [] software_list = []
...@@ -113,19 +114,19 @@ def init(config): ...@@ -113,19 +114,19 @@ def init(config):
return local return local
def do_request(config, local): def do_request(conf, local):
# Request instance # Request instance
print("Requesting %s..." % config.reference) print("Requesting %s..." % conf.reference)
if config.software_url in local: if conf.software_url in local:
config.software_url = local[config.software_url] conf.software_url = local[conf.software_url]
try: try:
partition = local['slap'].registerOpenOrder().request( partition = local['slap'].registerOpenOrder().request(
software_release = config.software_url, software_release = conf.software_url,
partition_reference = config.reference, partition_reference = conf.reference,
partition_parameter_kw = config.configuration, partition_parameter_kw = conf.configuration,
software_type = config.type, software_type = conf.type,
filter_kw = config.node, filter_kw = conf.node,
shared = config.slave shared = conf.slave
) )
print "Instance requested.\nState is : %s." % partition.getState() print "Instance requested.\nState is : %s." % partition.getState()
print "Connection parameters of instance are:" print "Connection parameters of instance are:"
......
This diff is collapsed.
...@@ -122,12 +122,11 @@ def check_missing_files(options): ...@@ -122,12 +122,11 @@ def check_missing_files(options):
raise RuntimeError('Directory %r does not exist' % d) raise RuntimeError('Directory %r does not exist' % d)
def merged_options(args, config): def merged_options(args, configp):
options = {} options = dict(configp.items('slapos'))
options = dict(config.items('slapos')) if configp.has_section('networkcache'):
if config.has_section('networkcache'): options.update(dict(configp.items('networkcache')))
options.update(dict(config.items('networkcache')))
for key, value in vars(args).iteritems(): for key, value in vars(args).iteritems():
if value is not None: if value is not None:
options[key] = value options[key] = value
......
...@@ -77,7 +77,7 @@ def get_computer_name(certificate): ...@@ -77,7 +77,7 @@ def get_computer_name(certificate):
return certificate[k:i] return certificate[k:i]
def save_former_config(config): def save_former_config(conf):
"""Save former configuration if found""" """Save former configuration if found"""
# Check for config file in /etc/opt/slapos/ # Check for config file in /etc/opt/slapos/
if os.path.exists('/etc/opt/slapos/slapos.cfg'): if os.path.exists('/etc/opt/slapos/slapos.cfg'):
...@@ -95,7 +95,7 @@ def save_former_config(config): ...@@ -95,7 +95,7 @@ def save_former_config(config):
saved += '.1' saved += '.1'
else: else:
break break
config.logger.info("Former slapos configuration detected in %s moving to %s" % (former, saved)) conf.logger.info("Former slapos configuration detected in %s moving to %s" % (former, saved))
shutil.move(former, saved) shutil.move(former, saved)
...@@ -111,11 +111,11 @@ def get_slapos_conf_example(): ...@@ -111,11 +111,11 @@ def get_slapos_conf_example():
return path return path
def slapconfig(config): def slapconfig(conf):
"""Base Function to configure slapos in /etc/opt/slapos""" """Base Function to configure slapos in /etc/opt/slapos"""
dry_run = config.dry_run dry_run = conf.dry_run
# Create slapos configuration directory if needed # Create slapos configuration directory if needed
slap_conf_dir = os.path.normpath(config.slapos_configuration) slap_conf_dir = os.path.normpath(conf.slapos_configuration)
# Make sure everybody can read slapos configuration directory: # Make sure everybody can read slapos configuration directory:
# Add +x to directories in path # Add +x to directories in path
...@@ -128,23 +128,23 @@ def slapconfig(config): ...@@ -128,23 +128,23 @@ def slapconfig(config):
directory = os.path.dirname(directory) directory = os.path.dirname(directory)
if not os.path.exists(slap_conf_dir): if not os.path.exists(slap_conf_dir):
config.logger.info("Creating directory: %s" % slap_conf_dir) conf.logger.info("Creating directory: %s" % slap_conf_dir)
if not dry_run: if not dry_run:
os.mkdir(slap_conf_dir, 0o711) os.mkdir(slap_conf_dir, 0o711)
user_certificate_repository_path = os.path.join(slap_conf_dir, 'ssl') user_certificate_repository_path = os.path.join(slap_conf_dir, 'ssl')
if not os.path.exists(user_certificate_repository_path): if not os.path.exists(user_certificate_repository_path):
config.logger.info("Creating directory: %s" % user_certificate_repository_path) conf.logger.info("Creating directory: %s" % user_certificate_repository_path)
if not dry_run: if not dry_run:
os.mkdir(user_certificate_repository_path, 0o711) os.mkdir(user_certificate_repository_path, 0o711)
key_file = os.path.join(user_certificate_repository_path, 'key') key_file = os.path.join(user_certificate_repository_path, 'key')
cert_file = os.path.join(user_certificate_repository_path, 'certificate') cert_file = os.path.join(user_certificate_repository_path, 'certificate')
for src, dst in [ for src, dst in [
(config.key, key_file), (conf.key, key_file),
(config.certificate, cert_file) (conf.certificate, cert_file)
]: ]:
config.logger.info("Copying to %r, and setting minimum privileges" % dst) conf.logger.info("Copying to %r, and setting minimum privileges" % dst)
if not dry_run: if not dry_run:
with open(dst, 'w') as destination: with open(dst, 'w') as destination:
destination.write(''.join(src)) destination.write(''.join(src))
...@@ -153,41 +153,41 @@ def slapconfig(config): ...@@ -153,41 +153,41 @@ def slapconfig(config):
certificate_repository_path = os.path.join(slap_conf_dir, 'ssl', 'partition_pki') certificate_repository_path = os.path.join(slap_conf_dir, 'ssl', 'partition_pki')
if not os.path.exists(certificate_repository_path): if not os.path.exists(certificate_repository_path):
config.logger.info("Creating directory: %s" % certificate_repository_path) conf.logger.info("Creating directory: %s" % certificate_repository_path)
if not dry_run: if not dry_run:
os.mkdir(certificate_repository_path, 0o711) os.mkdir(certificate_repository_path, 0o711)
# Put slapos configuration file # Put slapos configuration file
slap_conf_file = os.path.join(slap_conf_dir, 'slapos.cfg') slap_conf_file = os.path.join(slap_conf_dir, 'slapos.cfg')
config.logger.info("Creating slap configuration: %s" % slap_conf_file) conf.logger.info("Creating slap configuration: %s" % slap_conf_file)
# Get example configuration file # Get example configuration file
slapos_cfg_example = get_slapos_conf_example() slapos_cfg_example = get_slapos_conf_example()
conf_parser = ConfigParser.RawConfigParser() new_configp = ConfigParser.RawConfigParser()
conf_parser.read(slapos_cfg_example) new_configp.read(slapos_cfg_example)
os.remove(slapos_cfg_example) os.remove(slapos_cfg_example)
for section, key, value in [ for section, key, value in [
('slapos', 'computer_id', config.computer_id), ('slapos', 'computer_id', conf.computer_id),
('slapos', 'master_url', config.master_url), ('slapos', 'master_url', conf.master_url),
('slapos', 'key_file', key_file), ('slapos', 'key_file', key_file),
('slapos', 'cert_file', cert_file), ('slapos', 'cert_file', cert_file),
('slapos', 'certificate_repository_path', certificate_repository_path), ('slapos', 'certificate_repository_path', certificate_repository_path),
('slapformat', 'interface_name', config.interface_name), ('slapformat', 'interface_name', conf.interface_name),
('slapformat', 'ipv4_local_network', config.ipv4_local_network), ('slapformat', 'ipv4_local_network', conf.ipv4_local_network),
('slapformat', 'partition_amount', config.partition_number), ('slapformat', 'partition_amount', conf.partition_number),
('slapformat', 'create_tap', config.create_tap) ('slapformat', 'create_tap', conf.create_tap)
]: ]:
conf_parser.set(section, key, value) new_configp.set(section, key, value)
if config.ipv6_interface: if conf.ipv6_interface:
conf_parser.set('slapformat', 'ipv6_interface', config.ipv6_interface) new_configp.set('slapformat', 'ipv6_interface', conf.ipv6_interface)
if not dry_run: if not dry_run:
with open(slap_conf_file, 'w') as fout: with open(slap_conf_file, 'w') as fout:
conf_parser.write(fout) new_configp.write(fout)
config.logger.info("SlapOS configuration: DONE") conf.logger.info("SlapOS configuration: DONE")
class RegisterConfig(object): class RegisterConfig(object):
...@@ -221,43 +221,43 @@ class RegisterConfig(object): ...@@ -221,43 +221,43 @@ class RegisterConfig(object):
self.logger.debug("Ipv6 Interface: %s" % self.ipv6_interface) self.logger.debug("Ipv6 Interface: %s" % self.ipv6_interface)
def gen_auth(config): def gen_auth(conf):
ask = True ask = True
if config.login: if conf.login:
if config.password: if conf.password:
yield config.login, config.password yield conf.login, conf.password
ask = False ask = False
else: else:
yield config.login, getpass.getpass() yield conf.login, getpass.getpass()
while ask: while ask:
yield raw_input('SlapOS Master Login: '), getpass.getpass() yield raw_input('SlapOS Master Login: '), getpass.getpass()
def do_register(config): def do_register(conf):
"""Register new computer on SlapOS Master and generate slapos.cfg""" """Register new computer on SlapOS Master and generate slapos.cfg"""
for login, password in gen_auth(config): for login, password in gen_auth(conf):
if check_credentials(config.master_url_web, login, password): if check_credentials(conf.master_url_web, login, password):
break break
config.logger.warning('Wrong login/password') conf.logger.warning('Wrong login/password')
else: else:
return 1 return 1
# Get source code of page having certificate and key # Get source code of page having certificate and key
certificate_key = get_certificates(config.master_url_web, config.node_name, login, password) certificate_key = get_certificates(conf.master_url_web, conf.node_name, login, password)
# Parse certificate and key and get computer id # Parse certificate and key and get computer id
certificate, key = parse_certificates(certificate_key) certificate, key = parse_certificates(certificate_key)
COMP = get_computer_name(certificate) COMP = get_computer_name(certificate)
# Getting configuration parameters # Getting configuration parameters
config.COMPConfig(slapos_configuration='/etc/opt/slapos/', conf.COMPConfig(slapos_configuration='/etc/opt/slapos/',
computer_id=COMP, computer_id=COMP,
certificate=certificate, certificate=certificate,
key=key) key=key)
# Save former configuration # Save former configuration
if not config.dry_run: if not conf.dry_run:
save_former_config(config) save_former_config(conf)
# Prepare Slapos Configuration # Prepare Slapos Configuration
slapconfig(config) slapconfig(conf)
print "Node has successfully been configured as %s." % COMP print "Node has successfully been configured as %s." % COMP
return 0 return 0
...@@ -126,15 +126,15 @@ class TestCall (BasicMixin, unittest.TestCase): ...@@ -126,15 +126,15 @@ class TestCall (BasicMixin, unittest.TestCase):
options = ["--logfile /opt/slapos/logfile", options = ["--logfile /opt/slapos/logfile",
"--pidfile /opt/slapos/pidfile"] "--pidfile /opt/slapos/pidfile"]
config = '/etc/opt/slapos/slapos.cfg' config_path = '/etc/opt/slapos/slapos.cfg'
try: try:
entry.call(fun, config=config, option=options) entry.call(fun, config_path=config_path, option=options)
except SystemExit, e: except SystemExit, e:
self.assertEqual(e[0], 0) self.assertEqual(e[0], 0)
self.assertNotEqual(original_sysargv, sys.argv) self.assertNotEqual(original_sysargv, sys.argv)
for x in options: for x in options:
self.assertTrue(x in " ".join(sys.argv)) self.assertTrue(x in " ".join(sys.argv))
self.assertEqual(config, sys.argv[1]) self.assertEqual(config_path, sys.argv[1])
def test_config_and_missing_option_are_added(self): def test_config_and_missing_option_are_added(self):
""" """
...@@ -151,16 +151,16 @@ class TestCall (BasicMixin, unittest.TestCase): ...@@ -151,16 +151,16 @@ class TestCall (BasicMixin, unittest.TestCase):
return 0 return 0
options = [default_present_option, missing_option] options = [default_present_option, missing_option]
config = '/etc/opt/slapos/slapos.cfg' config_path = '/etc/opt/slapos/slapos.cfg'
try: try:
entry.call(fun, config=config, option=options) entry.call(fun, config_path=config_path, option=options)
except SystemExit, e: except SystemExit, e:
self.assertEqual(e[0], 0) self.assertEqual(e[0], 0)
self.assertNotEqual(original_sysargv, sys.argv) self.assertNotEqual(original_sysargv, sys.argv)
for x in (missing_option, present_option): for x in (missing_option, present_option):
self.assertTrue(x in " ".join(sys.argv)) self.assertTrue(x in " ".join(sys.argv))
self.assertFalse(default_present_option in " ".join(sys.argv)) self.assertFalse(default_present_option in " ".join(sys.argv))
self.assertEqual(config, sys.argv[1]) self.assertEqual(config_path, sys.argv[1])
def test_present_config_and_option_are_not_added(self): def test_present_config_and_option_are_not_added(self):
""" """
...@@ -178,9 +178,9 @@ class TestCall (BasicMixin, unittest.TestCase): ...@@ -178,9 +178,9 @@ class TestCall (BasicMixin, unittest.TestCase):
return 0 return 0
options = [default_present_option] options = [default_present_option]
config = '/etc/opt/slapos/slapos.cfg' config_path = '/etc/opt/slapos/slapos.cfg'
try: try:
entry.call(fun, config=config, option=options) entry.call(fun, config_path=config_path, option=options)
except SystemExit, e: except SystemExit, e:
self.assertEqual(e[0], 0) self.assertEqual(e[0], 0)
......
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