|
@@ -27,7 +27,7 @@ import copy
|
|
|
import tempfile
|
|
|
import json
|
|
|
from isc.cc import data
|
|
|
-from isc.config import ccsession
|
|
|
+from isc.config import ccsession, config_data
|
|
|
|
|
|
class ConfigManagerDataReadError(Exception):
|
|
|
"""This exception is thrown when there is an error while reading
|
|
@@ -43,15 +43,13 @@ class ConfigManagerData:
|
|
|
"""This class hold the actual configuration information, and
|
|
|
reads it from and writes it to persistent storage"""
|
|
|
|
|
|
- CONFIG_VERSION = 1
|
|
|
-
|
|
|
def __init__(self, data_path, file_name = "b10-config.db"):
|
|
|
"""Initialize the data for the configuration manager, and
|
|
|
set the version and path for the data store. Initializing
|
|
|
this does not yet read the database, a call to
|
|
|
read_from_file is needed for that."""
|
|
|
self.data = {}
|
|
|
- self.data['version'] = ConfigManagerData.CONFIG_VERSION
|
|
|
+ self.data['version'] = config_data.BIND10_CONFIG_DATA_VERSION
|
|
|
self.data_path = data_path
|
|
|
self.db_filename = data_path + os.sep + file_name
|
|
|
|
|
@@ -65,21 +63,36 @@ class ConfigManagerData:
|
|
|
the second exception, the best way is probably to report the
|
|
|
error and stop loading the system."""
|
|
|
config = ConfigManagerData(data_path, file_name)
|
|
|
+ file = None
|
|
|
try:
|
|
|
file = open(config.db_filename, 'r')
|
|
|
file_config = json.loads(file.read())
|
|
|
- if 'version' in file_config and \
|
|
|
- file_config['version'] == ConfigManagerData.CONFIG_VERSION:
|
|
|
- config.data = file_config
|
|
|
+ # handle different versions here
|
|
|
+ # If possible, we automatically convert to the new
|
|
|
+ # scheme and update the configuration
|
|
|
+ # If not, we raise an exception
|
|
|
+ if 'version' in file_config:
|
|
|
+ if file_config['version'] == config_data.BIND10_CONFIG_DATA_VERSION:
|
|
|
+ config.data = file_config
|
|
|
+ elif file_config['version'] == 1:
|
|
|
+ # only format change, no other changes necessary
|
|
|
+ file_config['version'] = 2
|
|
|
+ print("[b10-cfgmgr] Updating configuration database version from 1 to 2")
|
|
|
+ config.data = file_config
|
|
|
+ else:
|
|
|
+ if config_data.BIND10_CONFIG_DATA_VERSION > file_config['version']:
|
|
|
+ raise ConfigManagerDataReadError("Cannot load configuration file: version %d no longer supported" % file_config['version'])
|
|
|
+ else:
|
|
|
+ raise ConfigManagerDataReadError("Cannot load configuration file: version %d not yet supported" % file_config['version'])
|
|
|
else:
|
|
|
- # We can put in a migration path here for old data
|
|
|
- raise ConfigManagerDataReadError("[b10-cfgmgr] Old version of data found")
|
|
|
- file.close()
|
|
|
+ raise ConfigManagerDataReadError("No version information in configuration file " + config.db_filename)
|
|
|
except IOError as ioe:
|
|
|
- raise ConfigManagerDataEmpty("No config file found")
|
|
|
- except:
|
|
|
- raise ConfigManagerDataReadError("Config file unreadable")
|
|
|
-
|
|
|
+ raise ConfigManagerDataEmpty("No configuration file found")
|
|
|
+ except ValueError:
|
|
|
+ raise ConfigManagerDataReadError("Configuration file out of date or corrupt, please update or remove " + config.db_filename)
|
|
|
+ finally:
|
|
|
+ if file:
|
|
|
+ file.close();
|
|
|
return config
|
|
|
|
|
|
def write_to_file(self, output_file_name = None):
|
|
@@ -102,11 +115,11 @@ class ConfigManagerData:
|
|
|
os.rename(filename, self.db_filename)
|
|
|
except IOError as ioe:
|
|
|
# TODO: log this (level critical)
|
|
|
- print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ioe))
|
|
|
+ print("[b10-cfgmgr] Unable to write configuration file; configuration not stored: " + str(ioe))
|
|
|
# TODO: debug option to keep file?
|
|
|
except OSError as ose:
|
|
|
# TODO: log this (level critical)
|
|
|
- print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ose))
|
|
|
+ print("[b10-cfgmgr] Unable to write configuration file; configuration not stored: " + str(ose))
|
|
|
try:
|
|
|
if filename and os.path.exists(filename):
|
|
|
os.remove(filename)
|
|
@@ -243,7 +256,7 @@ class ConfigManager:
|
|
|
except data.DataNotFoundError as dnfe:
|
|
|
# no data is ok, that means we have nothing that
|
|
|
# deviates from default values
|
|
|
- return ccsession.create_answer(0, { 'version': self.config.CONFIG_VERSION })
|
|
|
+ return ccsession.create_answer(0, { 'version': config_data.BIND10_CONFIG_DATA_VERSION })
|
|
|
else:
|
|
|
return ccsession.create_answer(1, "Bad module_name in get_config command")
|
|
|
|