Commit 9d72a29d 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 d564d6db
......@@ -29,6 +29,7 @@
import logging
import sys
import argparse
from slapos.cli.command import check_root_user
from slapos.cli.config import ConfigCommand
......@@ -45,41 +46,45 @@ class FormatCommand(ConfigCommand):
ap = super(FormatCommand, self).get_parser(prog_name)
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")
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)")
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 "
"declaration. Using definition file allows to disable "
"'discovery' of machine services and allows to define computer "
"configuration in fully controlled manner.")
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 "
"declaration.")
ap.add_argument('--alter_user',
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'
' (default: {})'.format(FormatConfig.alter_user))
ap.add_argument('--alter_network',
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'
' (default: {})'.format(FormatConfig.alter_network))
ap.add_argument('--now',
default=False,
default=False, # can have a default as it is not in .cfg
action="store_true",
help='Launch slapformat without delay'
' (default: %(default)s)')
ap.add_argument('-n', '--dry_run',
default=False,
default=False, # can have a default as it is not in .cfg
action="store_true",
help="Don't actually do anything"
" (default: %(default)s)")
......
......@@ -1376,22 +1376,38 @@ def do_format(conf):
class FormatConfig(object):
key_file = None
cert_file = None
alter_network = 'True'
alter_user = 'True'
"""This class represents the options for slapos node format
all the attributes of this class are options
all the attributes can be modified by config file (.cfg)
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_tun = False
computer_xml = None
computer_json = None
input_definition_file = None
log_file = None
output_definition_file = None
dry_run = None
software_user = 'slapsoft'
tap_base_name = None
ipv4_local_network = None
tap_gateway_interface = ''
use_unique_local_address_block = False
# User options
alter_user = 'True' # modifiable by cmdline
software_user = 'slapsoft'
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):
self.logger = logger
......@@ -1414,30 +1430,20 @@ class FormatConfig(object):
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.
"""
self.key_file = None
self.cert_file = None
# Set argument parameters
for key, value in args.__dict__.items():
setattr(self, key, value)
# Merges the arguments and configuration
# First, the configuration file options erase the default class options
for section in ("slapformat", "slapos"):
configuration_dict = dict(configp.items(section))
for key in configuration_dict:
if not getattr(self, key, None):
setattr(self, key, configuration_dict[key])
def setConfig(self):
# setup some nones
for parameter in ['interface_name', 'partition_base_name', 'user_base_name',
'tap_base_name', 'ipv4_local_network', 'ipv6_interface']:
if getattr(self, parameter, None) is None:
setattr(self, parameter, None)
# Second, the command line arguments erase the configuration file options
for key, value in args.__dict__.items():
setattr(self, key, value)
def setConfig(self):
# deprecated options raise an error
for option in ['bridge_name', 'no_bridge']:
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