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