Browse Source

[1443] add method to move config file

Jelte Jansen 13 years ago
parent
commit
e17fed2f2b

+ 15 - 0
src/lib/python/isc/config/cfgmgr.py

@@ -148,6 +148,21 @@ class ConfigManagerData:
             # Ok if we really can't delete it anymore, leave it
             pass
 
+    def rename_config_file(self, old_file_name=None, new_file_name=None):
+        """Renames the given configuration file to the given new file name,
+           if it exists. If it does not exist, nothing happens.
+           If old_file_name is None (default), the file used in
+           read_from_file is used. If new_file_name is None (default), the
+           file old_file_name appended with .bak is used.
+        """
+        if old_file_name is None:
+            old_file_name = self.db_filename
+        if new_file_name is None:
+            new_file_name = old_file_name + ".bak"
+        if os.path.exists(old_file_name):
+            logger.info(CFGMGR_RENAMED_CONFIG_FILE, old_file_name, new_file_name)
+            os.rename(old_file_name, new_file_name)
+
     def __eq__(self, other):
         """Returns True if the data contained is equal. data_path and
            db_filename may be different."""

+ 4 - 0
src/lib/python/isc/config/cfgmgr_messages.mes

@@ -55,3 +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
+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.

+ 33 - 1
src/lib/python/isc/config/tests/cfgmgr_test.py

@@ -74,6 +74,37 @@ class TestConfigManagerData(unittest.TestCase):
         self.assertEqual(self.config_manager_data, new_config)
         os.remove(output_file_name)
 
+    def test_rename_config_file(self):
+        output_file_name = "b10-config-rename-test"
+        renamed_file_name = "b10-config-rename-test.bak"
+        if os.path.exists(output_file_name):
+            os.remove(output_file_name)
+        if os.path.exists(renamed_file_name):
+            os.remove(renamed_file_name)
+
+        # The original does not exist, so the new one should not be created
+        self.config_manager_data.rename_config_file(output_file_name)
+        self.assertFalse(os.path.exists(output_file_name))
+        self.assertFalse(os.path.exists(renamed_file_name))
+
+        # now create a file to rename, and call rename again
+        self.config_manager_data.write_to_file(output_file_name)
+        self.config_manager_data.rename_config_file(output_file_name)
+        self.assertFalse(os.path.exists(output_file_name))
+        self.assertTrue(os.path.exists(renamed_file_name))
+
+        # Test with explicit renamed file argument
+        self.config_manager_data.rename_config_file(renamed_file_name,
+                                                    output_file_name)
+        self.assertTrue(os.path.exists(output_file_name))
+        self.assertFalse(os.path.exists(renamed_file_name))
+
+        # clean up again to be nice
+        if os.path.exists(output_file_name):
+            os.remove(output_file_name)
+        if os.path.exists(renamed_file_name):
+            os.remove(renamed_file_name)
+
     def test_equality(self):
         # tests the __eq__ function. Equality is only defined
         # by equality of the .data element. If data_path or db_filename
@@ -570,5 +601,6 @@ if __name__ == '__main__':
     if not 'CONFIG_TESTDATA_PATH' in os.environ or not 'CONFIG_WR_TESTDATA_PATH' in os.environ:
         print("You need to set the environment variable CONFIG_TESTDATA_PATH and CONFIG_WR_TESTDATA_PATH to point to the directory containing the test data files")
         exit(1)
+    isc.log.init("unittests")
+    isc.log.resetUnitTestRootLogger()
     unittest.main()
-