add functions to set items in local.conf
This commit is contained in:
parent
a0f574ab97
commit
925b1bc682
|
@ -173,3 +173,41 @@ class LocalConf(object):
|
||||||
with open(target, "a+") as f:
|
with open(target, "a+") as f:
|
||||||
for line in self._section("local", "localrc"):
|
for line in self._section("local", "localrc"):
|
||||||
f.write(line)
|
f.write(line)
|
||||||
|
|
||||||
|
def _at_insert_point_local(self, name, func):
|
||||||
|
temp = tempfile.NamedTemporaryFile(mode='r')
|
||||||
|
shutil.copyfile(self.fname, temp.name)
|
||||||
|
in_meta = False
|
||||||
|
done = False
|
||||||
|
with open(self.fname, "w+") as writer:
|
||||||
|
with open(temp.name) as reader:
|
||||||
|
for line in reader.readlines():
|
||||||
|
if done:
|
||||||
|
writer.write(line)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if re.match(re.escape("[[local|localrc]]"), line):
|
||||||
|
in_meta = True
|
||||||
|
writer.write(line)
|
||||||
|
elif re.match("\[\[.*\|.*\]\]", line):
|
||||||
|
# if we're not done yet, we
|
||||||
|
if in_meta:
|
||||||
|
func(writer, None)
|
||||||
|
writer.write(line)
|
||||||
|
done = True
|
||||||
|
in_meta = False
|
||||||
|
continue
|
||||||
|
elif re.match("\s*%s\s*\=" % re.escape(name), line):
|
||||||
|
# we found our key, pass to the writer func
|
||||||
|
func(writer, line)
|
||||||
|
done = True
|
||||||
|
else:
|
||||||
|
# write out whatever we find
|
||||||
|
writer.write(line)
|
||||||
|
if not done:
|
||||||
|
func(writer, None)
|
||||||
|
|
||||||
|
def set_local(self, name, value):
|
||||||
|
def _do_set(writer, line):
|
||||||
|
writer.write("%s = %s\n" % (name, value))
|
||||||
|
self._at_insert_point_local(name, _do_set)
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
# Copyright 2017 IBM
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
# Implementation of ini add / remove for devstack. We don't use the
|
||||||
|
# python ConfigFile parser because that ends up rewriting the entire
|
||||||
|
# file and doesn't ensure comments remain.
|
||||||
|
|
||||||
|
import fixtures
|
||||||
|
import os.path
|
||||||
|
import testtools
|
||||||
|
|
||||||
|
from devstack import dsconf
|
||||||
|
|
||||||
|
|
||||||
|
BASIC = """
|
||||||
|
[[local|localrc]]
|
||||||
|
a = b
|
||||||
|
c = d
|
||||||
|
f = 1
|
||||||
|
[[post-config|$NEUTRON_CONF]]
|
||||||
|
[DEFAULT]
|
||||||
|
global_physnet_mtu=1450
|
||||||
|
[[post-config|$NOVA_CONF]]
|
||||||
|
[upgrade_levels]
|
||||||
|
compute = auto
|
||||||
|
"""
|
||||||
|
|
||||||
|
RESULT1 = """
|
||||||
|
[[local|localrc]]
|
||||||
|
a = b
|
||||||
|
c = d
|
||||||
|
f = 1
|
||||||
|
g = 2
|
||||||
|
[[post-config|$NEUTRON_CONF]]
|
||||||
|
[DEFAULT]
|
||||||
|
global_physnet_mtu=1450
|
||||||
|
[[post-config|$NOVA_CONF]]
|
||||||
|
[upgrade_levels]
|
||||||
|
compute = auto
|
||||||
|
"""
|
||||||
|
|
||||||
|
RESULT2 = """
|
||||||
|
[[local|localrc]]
|
||||||
|
a = 2
|
||||||
|
c = d
|
||||||
|
f = 1
|
||||||
|
[[post-config|$NEUTRON_CONF]]
|
||||||
|
[DEFAULT]
|
||||||
|
global_physnet_mtu=1450
|
||||||
|
[[post-config|$NOVA_CONF]]
|
||||||
|
[upgrade_levels]
|
||||||
|
compute = auto
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class TestLcSet(testtools.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestLcSet, self).setUp()
|
||||||
|
self._path = self.useFixture(fixtures.TempDir()).path
|
||||||
|
self._path += "/local.conf"
|
||||||
|
with open(self._path, "w") as f:
|
||||||
|
f.write(BASIC)
|
||||||
|
|
||||||
|
def test_set_new(self):
|
||||||
|
conf = dsconf.LocalConf(self._path)
|
||||||
|
conf.set_local("g", "2")
|
||||||
|
with open(self._path) as f:
|
||||||
|
content = f.read()
|
||||||
|
self.assertEqual(content, RESULT1)
|
||||||
|
|
||||||
|
def test_set_existing(self):
|
||||||
|
conf = dsconf.LocalConf(self._path)
|
||||||
|
conf.set_local("a", "2")
|
||||||
|
with open(self._path) as f:
|
||||||
|
content = f.read()
|
||||||
|
self.assertEqual(content, RESULT2)
|
Loading…
Reference in New Issue