Commit 3b8cf767 authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼 Committed by Rafael Monnerat

BUGFIX: fix c931d60c

After c931d60c, default options were changed and we didn't want that.

Rewrite the code so that we have a clearer scheme:
 * all options of format are listed as FormatConfig attributes with default values
 * all values present in configuration file (.cfg) erase the previous options
 * all values present in command line erase the previous options

We use the special default argparse.SUPPRESS that removes totally the arg from arg dict so that only PRESENT options erase something
parent 9d6bd566
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
import logging import logging
import sys import sys
import argparse
from slapos.cli.command import check_root_user from slapos.cli.command import check_root_user
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
...@@ -45,41 +46,45 @@ class FormatCommand(ConfigCommand): ...@@ -45,41 +46,45 @@ class FormatCommand(ConfigCommand):
ap = super(FormatCommand, self).get_parser(prog_name) ap = super(FormatCommand, self).get_parser(prog_name)
ap.add_argument('-x', '--computer_xml', ap.add_argument('-x', '--computer_xml',
default=argparse.SUPPRESS, #can't use default here because it would overwrite .cfg
help="Path to file with computer's XML. If does not exists, will be created") help="Path to file with computer's XML. If does not exists, will be created")
ap.add_argument('--computer_json', ap.add_argument('--computer_json',
default=argparse.SUPPRESS, #can't use default here because it would overwrite .cfg
help="Path to a JSON version of the computer's XML (for development only)") help="Path to a JSON version of the computer's XML (for development only)")
ap.add_argument('-i', '--input_definition_file', ap.add_argument('-i', '--input_definition_file',
default=argparse.SUPPRESS, #can't use default here because it would overwrite .cfg
help="Path to file to read definition of computer instead of " help="Path to file to read definition of computer instead of "
"declaration. Using definition file allows to disable " "declaration. Using definition file allows to disable "
"'discovery' of machine services and allows to define computer " "'discovery' of machine services and allows to define computer "
"configuration in fully controlled manner.") "configuration in fully controlled manner.")
ap.add_argument('-o', '--output_definition_file', ap.add_argument('-o', '--output_definition_file',
default=argparse.SUPPRESS, #can't use default here because it would overwrite .cfg
help="Path to file to write definition of computer from " help="Path to file to write definition of computer from "
"declaration.") "declaration.")
ap.add_argument('--alter_user', ap.add_argument('--alter_user',
choices=['True', 'False'], choices=['True', 'False'],
#default=FormatConfig.alter_user, #can't use default here because it would overwrite .cfg default=argparse.SUPPRESS, #can't use default here because it would overwrite .cfg
help='Shall slapformat alter user database' help='Shall slapformat alter user database'
' (default: {})'.format(FormatConfig.alter_user)) ' (default: {})'.format(FormatConfig.alter_user))
ap.add_argument('--alter_network', ap.add_argument('--alter_network',
choices=['True', 'False'], choices=['True', 'False'],
#default=FormatConfig.alter_network, #can't use default here because it would overwrite .cfg default=argparse.SUPPRESS, #can't use default here because it would overwrite .cfg
help='Shall slapformat alter network configuration' help='Shall slapformat alter network configuration'
' (default: {})'.format(FormatConfig.alter_network)) ' (default: {})'.format(FormatConfig.alter_network))
ap.add_argument('--now', ap.add_argument('--now',
default=False, default=False, # can have a default as it is not in .cfg
action="store_true", action="store_true",
help='Launch slapformat without delay' help='Launch slapformat without delay'
' (default: %(default)s)') ' (default: %(default)s)')
ap.add_argument('-n', '--dry_run', ap.add_argument('-n', '--dry_run',
default=False, default=False, # can have a default as it is not in .cfg
action="store_true", action="store_true",
help="Don't actually do anything" help="Don't actually do anything"
" (default: %(default)s)") " (default: %(default)s)")
......
...@@ -1376,22 +1376,38 @@ def do_format(conf): ...@@ -1376,22 +1376,38 @@ def do_format(conf):
class FormatConfig(object): class FormatConfig(object):
key_file = None """This class represents the options for slapos node format
cert_file = None all the attributes of this class are options
alter_network = 'True' all the attributes can be modified by config file (.cfg)
alter_user = 'True' some attributes can be modified by command line"""
# Network options
alter_network = 'True' # modifiable by cmdline
interface_name = None
ipv6_interface = None
create_tap = True create_tap = True
create_tun = False create_tun = False
computer_xml = None tap_base_name = None
computer_json = None ipv4_local_network = None
input_definition_file = None
log_file = None
output_definition_file = None
dry_run = None
software_user = 'slapsoft'
tap_gateway_interface = '' tap_gateway_interface = ''
use_unique_local_address_block = False use_unique_local_address_block = False
# User options
alter_user = 'True' # modifiable by cmdline
software_user = 'slapsoft'
instance_storage_home = None instance_storage_home = None
partition_base_name = None
user_base_name = None
# Other options
input_definition_file = None # modifiable by cmdline
computer_xml = None # modifiable by cmdline
computer_json = None # modifiable by cmdline
log_file = None # modifiable by cmdline
output_definition_file = None # modifiable by cmdline
dry_run = None # modifiable by cmdline
key_file = None
cert_file = None
def __init__(self, logger): def __init__(self, logger):
self.logger = logger self.logger = logger
...@@ -1414,30 +1430,20 @@ class FormatConfig(object): ...@@ -1414,30 +1430,20 @@ class FormatConfig(object):
def mergeConfig(self, args, configp): def mergeConfig(self, args, configp):
""" """
Set options given by parameters. Set options given by config files and arguments.
Must be executed before setting up the logger. Must be executed before setting up the logger.
""" """
self.key_file = None # First, the configuration file options erase the default class options
self.cert_file = None
# Set argument parameters
for key, value in args.__dict__.items():
setattr(self, key, value)
# Merges the arguments and configuration
for section in ("slapformat", "slapos"): for section in ("slapformat", "slapos"):
configuration_dict = dict(configp.items(section)) configuration_dict = dict(configp.items(section))
for key in configuration_dict: for key in configuration_dict:
if not getattr(self, key, None): setattr(self, key, configuration_dict[key])
setattr(self, key, configuration_dict[key])
def setConfig(self): # Second, the command line arguments erase the configuration file options
# setup some nones for key, value in args.__dict__.items():
for parameter in ['interface_name', 'partition_base_name', 'user_base_name', setattr(self, key, value)
'tap_base_name', 'ipv4_local_network', 'ipv6_interface']:
if getattr(self, parameter, None) is None:
setattr(self, parameter, None)
def setConfig(self):
# deprecated options raise an error # deprecated options raise an error
for option in ['bridge_name', 'no_bridge']: for option in ['bridge_name', 'no_bridge']:
if getattr(self, option, None): if getattr(self, option, None):
......
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