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:"
......
......@@ -66,9 +66,9 @@ class OS(object):
_os = os
def __init__(self, config):
self._dry_run = config.dry_run
self._logger = config.logger
def __init__(self, conf):
self._dry_run = conf.dry_run
self._logger = conf.logger
add = self._addWrapper
add('chown')
add('chmod')
......@@ -241,21 +241,21 @@ class Computer(object):
# Can't find address
raise NoAddressOnInterface('No valid IPv6 found on %s.' % self.interface.name)
def send(self, config):
def send(self, conf):
"""
Send a marshalled dictionary of the computer object serialized via_getDict.
"""
slap_instance = slap.slap()
connection_dict = {}
if config.key_file and config.cert_file:
connection_dict['key_file'] = config.key_file
connection_dict['cert_file'] = config.cert_file
slap_instance.initializeConnection(config.master_url,
if conf.key_file and conf.cert_file:
connection_dict['key_file'] = conf.key_file
connection_dict['cert_file'] = conf.cert_file
slap_instance.initializeConnection(conf.master_url,
**connection_dict)
slap_computer = slap_instance.registerComputer(self.reference)
if config.dry_run:
if conf.dry_run:
return
try:
slap_computer.updateConfiguration(xml_marshaller.xml_marshaller.dumps(_getDict(self)))
......@@ -925,8 +925,8 @@ class Interface(object):
raise AddressGenerationError(addr)
def parse_computer_definition(config, definition_path):
config.logger.info('Using definition file %r' % definition_path)
def parse_computer_definition(conf, definition_path):
conf.logger.info('Using definition file %r' % definition_path)
computer_definition = ConfigParser.RawConfigParser({
'software_user': 'slapsoft',
})
......@@ -936,20 +936,20 @@ def parse_computer_definition(config, definition_path):
netmask = None
if computer_definition.has_option('computer', 'address'):
address, netmask = computer_definition.get('computer', 'address').split('/')
if config.alter_network and config.interface_name is not None \
and config.ipv4_local_network is not None:
interface = Interface(config.logger, config.interface_name, config.ipv4_local_network,
config.ipv6_interface)
if conf.alter_network and conf.interface_name is not None \
and conf.ipv4_local_network is not None:
interface = Interface(conf.logger, conf.interface_name, conf.ipv4_local_network,
conf.ipv6_interface)
computer = Computer(
reference=config.computer_id,
reference=conf.computer_id,
interface=interface,
addr=address,
netmask=netmask,
ipv6_interface=config.ipv6_interface,
ipv6_interface=conf.ipv6_interface,
software_user=computer_definition.get('computer', 'software_user'),
)
partition_list = []
for partition_number in range(int(config.partition_amount)):
for partition_number in range(int(conf.partition_amount)):
section = 'partition_%s' % partition_number
user = User(computer_definition.get(section, 'user'))
address_list = []
......@@ -958,7 +958,7 @@ def parse_computer_definition(config, definition_path):
address_list.append(dict(addr=address, netmask=netmask))
tap = Tap(computer_definition.get(section, 'network_interface'))
partition = Partition(reference=computer_definition.get(section, 'pathname'),
path=os.path.join(config.instance_root,
path=os.path.join(conf.instance_root,
computer_definition.get(section, 'pathname')),
user=user,
address_list=address_list,
......@@ -968,48 +968,48 @@ def parse_computer_definition(config, definition_path):
return computer
def parse_computer_xml(config, xml_path):
def parse_computer_xml(conf, xml_path):
if os.path.exists(xml_path):
config.logger.info('Loading previous computer data from %r' % xml_path)
conf.logger.info('Loading previous computer data from %r' % xml_path)
computer = Computer.load(xml_path,
reference=config.computer_id,
ipv6_interface=config.ipv6_interface)
reference=conf.computer_id,
ipv6_interface=conf.ipv6_interface)
# Connect to the interface defined by the configuration
computer.interface = Interface(config.logger, config.interface_name, config.ipv4_local_network,
config.ipv6_interface)
computer.interface = Interface(conf.logger, conf.interface_name, conf.ipv4_local_network,
conf.ipv6_interface)
else:
# If no pre-existent configuration found, create a new computer object
config.logger.warning('Creating new data computer with id %r' % config.computer_id)
conf.logger.warning('Creating new data computer with id %r' % conf.computer_id)
computer = Computer(
reference=config.computer_id,
interface=Interface(config.logger, config.interface_name, config.ipv4_local_network,
config.ipv6_interface),
reference=conf.computer_id,
interface=Interface(conf.logger, conf.interface_name, conf.ipv4_local_network,
conf.ipv6_interface),
addr=None,
netmask=None,
ipv6_interface=config.ipv6_interface,
software_user=config.software_user,
ipv6_interface=conf.ipv6_interface,
software_user=conf.software_user,
)
partition_amount = int(config.partition_amount)
partition_amount = int(conf.partition_amount)
existing_partition_amount = len(computer.partition_list)
if existing_partition_amount > partition_amount:
raise ValueError('Requested amount of computer partitions (%s) is lower '
'then already configured (%s), cannot continue' % (partition_amount,
len(computer.partition_list)))
config.logger.info('Adding %s new partitions' %
conf.logger.info('Adding %s new partitions' %
(partition_amount - existing_partition_amount))
for nb_iter in range(existing_partition_amount, partition_amount):
# add new ones
user = User("%s%s" % (config.user_base_name, nb_iter))
user = User("%s%s" % (conf.user_base_name, nb_iter))
tap = Tap("%s%s" % (config.tap_base_name, nb_iter))
tap = Tap("%s%s" % (conf.tap_base_name, nb_iter))
path = os.path.join(config.instance_root, "%s%s" % (
config.partition_base_name, nb_iter))
path = os.path.join(conf.instance_root, "%s%s" % (
conf.partition_base_name, nb_iter))
computer.partition_list.append(
Partition(
reference="%s%s" % (config.partition_base_name, nb_iter),
reference="%s%s" % (conf.partition_base_name, nb_iter),
path=path,
user=user,
address_list=None,
......@@ -1019,7 +1019,7 @@ def parse_computer_xml(config, xml_path):
return computer
def write_computer_definition(config, computer):
def write_computer_definition(conf, computer):
computer_definition = ConfigParser.RawConfigParser()
computer_definition.add_section('computer')
if computer.address is not None and computer.netmask is not None:
......@@ -1035,55 +1035,55 @@ def write_computer_definition(config, computer):
computer_definition.set(section, 'user', partition.user.name)
computer_definition.set(section, 'network_interface', partition.tap.name)
computer_definition.set(section, 'pathname', partition.reference)
computer_definition.write(open(config.output_definition_file, 'w'))
config.logger.info('Stored computer definition in %r' % config.output_definition_file)
computer_definition.write(open(conf.output_definition_file, 'w'))
conf.logger.info('Stored computer definition in %r' % conf.output_definition_file)
def random_delay(config):
def random_delay(conf):
# Add delay between 0 and 1 hour
# XXX should be the contrary: now by default, and cron should have
# --maximal-delay=3600
if not config.now:
if not conf.now:
duration = float(60 * 60) * random.random()
print("Sleeping for %s seconds. To disable this feature, " \
"use with --now parameter in manual." % duration)
time.sleep(duration)
def do_format(config):
random_delay(config)
def do_format(conf):
random_delay(conf)
if config.input_definition_file:
computer = parse_computer_definition(config, config.input_definition_file)
if conf.input_definition_file:
computer = parse_computer_definition(conf, conf.input_definition_file)
else:
# no definition file, figure out computer
computer = parse_computer_xml(config, config.computer_xml)
computer = parse_computer_xml(conf, conf.computer_xml)
computer.instance_root = config.instance_root
computer.software_root = config.software_root
config.logger.info('Updating computer')
address = computer.getAddress(config.create_tap)
computer.instance_root = conf.instance_root
computer.software_root = conf.software_root
conf.logger.info('Updating computer')
address = computer.getAddress(conf.create_tap)
computer.address = address['addr']
computer.netmask = address['netmask']
if config.output_definition_file:
write_computer_definition(config, computer)
if conf.output_definition_file:
write_computer_definition(conf, computer)
computer.construct(alter_user=config.alter_user,
alter_network=config.alter_network,
create_tap=config.create_tap)
computer.construct(alter_user=conf.alter_user,
alter_network=conf.alter_network,
create_tap=conf.create_tap)
if getattr(config, 'certificate_repository_path', None):
mkdir_p(config.certificate_repository_path, mode=0o700)
if getattr(conf, 'certificate_repository_path', None):
mkdir_p(conf.certificate_repository_path, mode=0o700)
# Dumping and sending to the erp5 the current configuration
if not config.dry_run:
computer.dump(path_to_xml=config.computer_xml,
path_to_json=config.computer_json,
logger=config.logger)
config.logger.info('Posting information to %r' % config.master_url)
computer.send(config)
config.logger.info('slapformat successfully prepared computer.')
if not conf.dry_run:
computer.dump(path_to_xml=conf.computer_xml,
path_to_json=conf.computer_json,
logger=conf.logger)
conf.logger.info('Posting information to %r' % conf.master_url)
computer.send(conf)
conf.logger.info('slapformat successfully prepared computer.')
class FormatConfig(object):
......@@ -1119,20 +1119,20 @@ class FormatConfig(object):
raise UsageError('Some required binaries are missing or not '
'functional: %s' % (','.join(missing_binary_list), ))
def setConfig(self, options, configuration_parser):
def setConfig(self, args, configp):
"""
Set options given by parameters.
"""
self.key_file = None
self.cert_file = None
# Set options parameters
for option, value in options.__dict__.items():
setattr(self, option, value)
# Set argument parameters
for key, value in args.__dict__.items():
setattr(self, key, value)
# Merges the arguments and configuration
for section in ("slapformat", "slapos"):
configuration_dict = dict(configuration_parser.items(section))
configuration_dict = dict(configp.items(section))
for key in configuration_dict:
if not getattr(self, key, None):
setattr(self, key, configuration_dict[key])
......@@ -1249,15 +1249,15 @@ class FormatConfig(object):
self.output_definition_file = os.path.abspath(self.output_definition_file)
def tracing_monkeypatch(config):
def tracing_monkeypatch(conf):
"""Substitute os module and callAndRead function with tracing wrappers."""
global os
global callAndRead
real_callAndRead = callAndRead
os = OS(config)
if config.dry_run:
os = OS(conf)
if conf.dry_run:
def dry_callAndRead(argument_list, raise_on_error=True):
if argument_list == ['brctl', 'show']:
return real_callAndRead(argument_list, raise_on_error)
......@@ -1275,6 +1275,6 @@ def tracing_monkeypatch(config):
dry_callAndRead = real_callAndRead
def logging_callAndRead(argument_list, raise_on_error=True):
config.logger.debug(' '.join(argument_list))
conf.logger.debug(' '.join(argument_list))
return dry_callAndRead(argument_list, raise_on_error)
callAndRead = logging_callAndRead
......@@ -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)
slapconfig(conf)
print "Node has successfully been configured as %s." % COMP
return 0
......@@ -126,15 +126,15 @@ class TestCall (BasicMixin, unittest.TestCase):
options = ["--logfile /opt/slapos/logfile",
"--pidfile /opt/slapos/pidfile"]
config = '/etc/opt/slapos/slapos.cfg'
config_path = '/etc/opt/slapos/slapos.cfg'
try:
entry.call(fun, config=config, option=options)
entry.call(fun, config_path=config_path, option=options)
except SystemExit, e:
self.assertEqual(e[0], 0)
self.assertNotEqual(original_sysargv, sys.argv)
for x in options:
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):
"""
......@@ -151,16 +151,16 @@ class TestCall (BasicMixin, unittest.TestCase):
return 0
options = [default_present_option, missing_option]
config = '/etc/opt/slapos/slapos.cfg'
config_path = '/etc/opt/slapos/slapos.cfg'
try:
entry.call(fun, config=config, option=options)
entry.call(fun, config_path=config_path, option=options)
except SystemExit, e:
self.assertEqual(e[0], 0)
self.assertNotEqual(original_sysargv, sys.argv)
for x in (missing_option, present_option):
self.assertTrue(x 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):
"""
......@@ -178,9 +178,9 @@ class TestCall (BasicMixin, unittest.TestCase):
return 0
options = [default_present_option]
config = '/etc/opt/slapos/slapos.cfg'
config_path = '/etc/opt/slapos/slapos.cfg'
try:
entry.call(fun, config=config, option=options)
entry.call(fun, config_path=config_path, option=options)
except SystemExit, e:
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