Commit 24b2851d authored by Jelte Jansen's avatar Jelte Jansen
Browse files

[1443] add command-line arguments

also extend log message a bit
parent e17fed2f
......@@ -167,8 +167,8 @@ class BoB:
"""Boss of BIND class."""
def __init__(self, msgq_socket_file=None, data_path=None,
config_filename=None, nocache=False, verbose=False, setuid=None,
username=None, cmdctl_port=None, wait_time=10):
config_filename=None, clear_config=False, nocache=False, verbose=False,
setuid=None, username=None, cmdctl_port=None, wait_time=10):
"""
Initialize the Boss of BIND. This is a singleton (only one can run).
......@@ -210,6 +210,7 @@ class BoB:
self.verbose = verbose
self.data_path = data_path
self.config_filename = config_filename
self.clear_config = clear_config
self.cmdctl_port = cmdctl_port
self.wait_time = wait_time
self._component_configurator = isc.bind10.component.Configurator(self,
......@@ -465,6 +466,8 @@ class BoB:
args.append("--data-path=" + self.data_path)
if self.config_filename is not None:
args.append("--config-filename=" + self.config_filename)
if self.clear_config is not None:
args.append("--clear-config")
bind_cfgd = ProcessInfo("b10-cfgmgr", args,
self.c_channel_env)
bind_cfgd.spawn()
......@@ -1053,6 +1056,9 @@ def parse_args(args=sys.argv[1:], Parser=OptionParser):
parser.add_option("-c", "--config-file", action="store",
dest="config_file", default=None,
help="Configuration database filename")
parser.add_option("--clear-config", action="store_true",
dest="clear_config", default=False,
help="Back up the configuration file and start with a clean one")
parser.add_option("-p", "--data-path", dest="data_path",
help="Directory to search for configuration files",
default=None)
......@@ -1165,8 +1171,8 @@ def main():
try:
# Go bob!
boss_of_bind = BoB(options.msgq_socket_file, options.data_path,
options.config_file, options.nocache,
options.verbose, setuid, username,
options.config_file, options.clear_config,
options.nocache, options.verbose, setuid, username,
options.cmdctl_port, options.wait_time)
startup_result = boss_of_bind.startup()
if startup_result:
......
......@@ -1012,6 +1012,12 @@ class TestParseArgs(unittest.TestCase):
options = parse_args(['--config-file=config-file'], TestOptParser)
self.assertEqual('config-file', options.config_file)
def test_clear_config(self):
options = parse_args([], TestOptParser)
self.assertEqual(False, options.clear_config)
options = parse_args(['--clear-config'], TestOptParser)
self.assertEqual(True, options.clear_config)
def test_cmdctl_port(self):
"""
Test it can parse the command control port.
......
......@@ -49,6 +49,10 @@ def parse_options(args=sys.argv[1:], Parser=OptionParser):
help="Configuration database filename " +
"(default=" + DEFAULT_CONFIG_FILE + ")",
default=DEFAULT_CONFIG_FILE)
parser.add_option("--clear-config", action="store_true",
dest="clear_config", default=False,
help="Back up the configuration file and start with " +
"a clean one")
(options, args) = parser.parse_args(args)
if args:
parser.error("No non-option arguments allowed")
......@@ -85,7 +89,8 @@ def main():
options = parse_options()
global cm
try:
cm = ConfigManager(options.data_path, options.config_file)
cm = ConfigManager(options.data_path, options.config_file,
None, options.clear_config)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
cm.read_config()
......
......@@ -24,12 +24,13 @@ import bind10_config
from isc.testutils.parse_args import OptsError, TestOptParser
class MyConfigManager:
def __init__(self, path, filename):
def __init__(self, path, filename, session=None, rename_config_file=None):
self._path = path
self.read_config_called = False
self.notify_boss_called = False
self.run_called = False
self.write_config_called = False
self.rename_config_called = False
self.running = True
self.virtual_modules = []
......@@ -45,6 +46,9 @@ class MyConfigManager:
def write_config(self):
self.write_config_called = True
def rename_config_file(self, ofile, nfile):
self.rename_config_called = True
def set_virtual_module(self, spec, function):
self.virtual_modules.append((spec, function))
......@@ -90,6 +94,7 @@ class TestConfigManagerStartup(unittest.TestCase):
self.assertTrue(self.loaded_plugins)
# if there are no changes, config is not written
self.assertFalse(b.cm.write_config_called)
self.assertFalse(b.cm.rename_config_called)
self.assertTrue(b.cm.running)
b.signal_handler(None, None)
......@@ -187,6 +192,14 @@ class TestParseArgs(unittest.TestCase):
self.assertRaises(OptsError, b.parse_options, ['--config-filename'],
TestOptParser)
def test_clear_config(self):
b = __import__("b10-cfgmgr")
parsed = b.parse_options([], TestOptParser)
self.assertFalse(parsed.clear_config)
parsed = b.parse_options(['--clear-config'], TestOptParser)
self.assertTrue(parsed.clear_config)
if __name__ == '__main__':
unittest.main()
......@@ -178,14 +178,16 @@ class ConfigManager:
channel session. If not, a new session will be created.
The ability to specify a custom session is for testing purposes
and should not be needed for normal usage."""
def __init__(self, data_path, database_filename, session=None):
def __init__(self, data_path, database_filename, session=None,
clear_config=False):
"""Initialize the configuration manager. The data_path string
is the path to the directory where the configuration is
stored (in <data_path>/<database_filename> or in
<database_filename>, if it is absolute). The dabase_filename
is the config file to load. Session is an optional
cc-channel session. If this is not given, a new one is
created."""
created. If clear_config is True, the configuration file is
renamed and a new one is created."""
self.data_path = data_path
self.database_filename = database_filename
self.module_specs = {}
......@@ -194,6 +196,8 @@ class ConfigManager:
# of some other process
self.virtual_modules = {}
self.config = ConfigManagerData(data_path, database_filename)
if clear_config:
self.config.rename_config_file()
if session:
self.cc = session
else:
......
......@@ -55,7 +55,7 @@ configuration is not stored.
There was a keyboard interrupt signal to stop the cfgmgr daemon. The
daemon will now shut down.
% CFGMGR_RENAMED_CONFIG_FILE renamed configuration file %1 to %2
% CFGMGR_RENAMED_CONFIG_FILE renamed configuration file %1 to %2, will create new %1
BIND 10 has been started with the command to clear the configuration file.
The existing file is backed up to the given file name, so that data is not
immediately lost if this was done by accident.
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