diff --git a/src/allmydata/node.py b/src/allmydata/node.py index c058fce4f..083cbef2c 100644 --- a/src/allmydata/node.py +++ b/src/allmydata/node.py @@ -321,6 +321,21 @@ class _Config(object): ) return default + def set_config(self, section, option, value): + """ + Set a config options in a section and re-write the tahoe.cfg file + """ + if option.endswith(".furl") and self._contains_unescaped_hash(value): + raise UnescapedHashError(section, option, item) + + try: + self.config.add_section(section) + except ConfigParser.DuplicateSectionError: + pass + self.config.set(section, option, value) + with open(self._config_fname, "w") as f: + self.config.write(f) + def get_config_from_file(self, name, required=False): """Get the (string) contents of a config file, or None if the file did not exist. If required=True, raise an exception rather than diff --git a/src/allmydata/scripts/admin.py b/src/allmydata/scripts/admin.py index ef1059a31..60d31cfb8 100644 --- a/src/allmydata/scripts/admin.py +++ b/src/allmydata/scripts/admin.py @@ -124,12 +124,10 @@ def add_grid_manager_cert(options): print("Already have file '{}'".format(cert_path), file=options.parent.parent.stderr) return 1 - cfg = config.config # why aren't methods we call on cfg in _Config itself? - gm_certs = config.get_config("storage", "grid_manager_certificate_files", "").split() if cert_fname not in gm_certs: gm_certs.append(cert_fname) - cfg.set("storage", "grid_manager_certificate_files", " ".join(gm_certs)) + config.set_config("storage", "grid_manager_certificate_files", " ".join(gm_certs)) # print("grid_manager_certificate_files in {}: {}".format(config_path, len(gm_certs))) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 096cd4f8f..a7ed6d528 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -316,6 +316,12 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): yield client.create_client(basedir) self.failUnless(ns.called) + def test_set_config_new_section(self): + basedir = "test_node/test_set_config_new_section" + config = config_from_string(basedir, "", "") + config.set_config("foo", "bar", "value1") + config.set_config("foo", "bar", "value2") + class TestMissingPorts(unittest.TestCase): """