Browse Source

add functions to set items in local.conf

Sean Dague 2 years ago
parent
commit
925b1bc682
2 changed files with 126 additions and 0 deletions
  1. 38
    0
      devstack/dsconf.py
  2. 88
    0
      devstack/tests/test_localconf_set_local.py

+ 38
- 0
devstack/dsconf.py View File

@@ -173,3 +173,41 @@ class LocalConf(object):
173 173
         with open(target, "a+") as f:
174 174
             for line in self._section("local", "localrc"):
175 175
                 f.write(line)
176
+
177
+    def _at_insert_point_local(self, name, func):
178
+        temp = tempfile.NamedTemporaryFile(mode='r')
179
+        shutil.copyfile(self.fname, temp.name)
180
+        in_meta = False
181
+        done = False
182
+        with open(self.fname, "w+") as writer:
183
+            with open(temp.name) as reader:
184
+                for line in reader.readlines():
185
+                    if done:
186
+                        writer.write(line)
187
+                        continue
188
+
189
+                    if re.match(re.escape("[[local|localrc]]"), line):
190
+                        in_meta = True
191
+                        writer.write(line)
192
+                    elif re.match("\[\[.*\|.*\]\]", line):
193
+                        # if we're not done yet, we
194
+                        if in_meta:
195
+                            func(writer, None)
196
+                            writer.write(line)
197
+                            done = True
198
+                        in_meta = False
199
+                        continue
200
+                    elif re.match("\s*%s\s*\=" % re.escape(name), line):
201
+                        # we found our key, pass to the writer func
202
+                        func(writer, line)
203
+                        done = True
204
+                    else:
205
+                        # write out whatever we find
206
+                        writer.write(line)
207
+            if not done:
208
+                func(writer, None)
209
+
210
+    def set_local(self, name, value):
211
+        def _do_set(writer, line):
212
+            writer.write("%s = %s\n" % (name, value))
213
+        self._at_insert_point_local(name, _do_set)

+ 88
- 0
devstack/tests/test_localconf_set_local.py View File

@@ -0,0 +1,88 @@
1
+# Copyright 2017 IBM
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+# not use this file except in compliance with the License. You may obtain
5
+# a copy of the License at
6
+#
7
+#    http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+# Implementation of ini add / remove for devstack. We don't use the
16
+# python ConfigFile parser because that ends up rewriting the entire
17
+# file and doesn't ensure comments remain.
18
+
19
+import fixtures
20
+import os.path
21
+import testtools
22
+
23
+from devstack import dsconf
24
+
25
+
26
+BASIC = """
27
+[[local|localrc]]
28
+a = b
29
+c = d
30
+f = 1
31
+[[post-config|$NEUTRON_CONF]]
32
+[DEFAULT]
33
+global_physnet_mtu=1450
34
+[[post-config|$NOVA_CONF]]
35
+[upgrade_levels]
36
+compute = auto
37
+"""
38
+
39
+RESULT1 = """
40
+[[local|localrc]]
41
+a = b
42
+c = d
43
+f = 1
44
+g = 2
45
+[[post-config|$NEUTRON_CONF]]
46
+[DEFAULT]
47
+global_physnet_mtu=1450
48
+[[post-config|$NOVA_CONF]]
49
+[upgrade_levels]
50
+compute = auto
51
+"""
52
+
53
+RESULT2 = """
54
+[[local|localrc]]
55
+a = 2
56
+c = d
57
+f = 1
58
+[[post-config|$NEUTRON_CONF]]
59
+[DEFAULT]
60
+global_physnet_mtu=1450
61
+[[post-config|$NOVA_CONF]]
62
+[upgrade_levels]
63
+compute = auto
64
+"""
65
+
66
+
67
+class TestLcSet(testtools.TestCase):
68
+
69
+    def setUp(self):
70
+        super(TestLcSet, self).setUp()
71
+        self._path = self.useFixture(fixtures.TempDir()).path
72
+        self._path += "/local.conf"
73
+        with open(self._path, "w") as f:
74
+            f.write(BASIC)
75
+
76
+    def test_set_new(self):
77
+        conf = dsconf.LocalConf(self._path)
78
+        conf.set_local("g", "2")
79
+        with open(self._path) as f:
80
+            content = f.read()
81
+            self.assertEqual(content, RESULT1)
82
+
83
+    def test_set_existing(self):
84
+        conf = dsconf.LocalConf(self._path)
85
+        conf.set_local("a", "2")
86
+        with open(self._path) as f:
87
+            content = f.read()
88
+            self.assertEqual(content, RESULT2)

Loading…
Cancel
Save