From Yaniv Bronhaim <ybronhei(a)redhat.com>:
Yaniv Bronhaim has uploaded a new change for review.
Change subject: configfile: remove old configuration when attribute is set
......................................................................
configfile: remove old configuration when attribute is set
When vdsm configures a key that already exists in conf file, the new key
will be created in vdsm-config-section and won't be copied from the
original file. oldentries is a variable that tried to do the opposite
and leave keys instead of overriding with vdsm conf - the intention
should be to use vdsm configuration after calling vdsm-tool configure
Change-Id: Id1dba75449b712432e0c2bc5b128cc996ede0153
Signed-off-by: Yaniv Bronhaim <ybronhei(a)redhat.com>
---
M lib/vdsm/tool/configfile.py
M tests/tool_test.py
2 files changed, 30 insertions(+), 14 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/31/74331/1
diff --git a/lib/vdsm/tool/configfile.py b/lib/vdsm/tool/configfile.py
index bad0759..1c2076f 100644
--- a/lib/vdsm/tool/configfile.py
+++ b/lib/vdsm/tool/configfile.py
@@ -21,7 +21,6 @@
import functools
import os
import tempfile
-import re
import selinux
from six.moves import configparser
import io
@@ -112,9 +111,7 @@
return self
def _getOldContent(self):
- confpat = re.compile(r'^\s*(?P<key>[^=\s#]*)\s*=')
oldlines = []
- oldentries = set()
with io.open(self._filename, 'r', encoding='utf8') as f:
for line in f:
if self._remove:
@@ -135,13 +132,10 @@
line = line[len(self._prefix):]
if self._prefixAdd:
line = self._prefix + line
- m = confpat.match(line.rstrip())
- if m:
- oldentries.add(m.group('key'))
# remove this if at 4.0. see 'Backward compatibility'
if not self._remove or self._lineComment not in line:
oldlines.append(line)
- return oldlines, oldentries
+ return oldlines
def _start(self):
return u"%s-%s\n" % (self._sectionStart, self._version)
@@ -154,26 +148,28 @@
f.write(self._section)
f.write(self._end())
- def _writeEntries(self, f, oldentries):
+ def _writeEntries(self, f):
f.write(self._start())
for key, val in sorted(self._entries.items()):
- if key not in oldentries:
- f.write(u"{k}={v}\n".format(k=key, v=val))
+ f.write(u"{k}={v}\n".format(k=key, v=val))
f.write(self._end())
def __exit__(self, exec_ty, exec_val, tb):
-
self._context = False
if exec_ty is None:
fd, tname = tempfile.mkstemp(dir=os.path.dirname(self._filename))
try:
- oldlines, oldentries = self._getOldContent()
+ oldlines = self._getOldContent()
with io.open(fd, 'w', encoding='utf8') as f:
if self._section:
self._writeSection(f)
- f.writelines(oldlines)
+ # if oldlines includes something that we have in
+ # self._entries we need to write only the new value!
+ for line in oldlines:
+ if line.split("=")[0] not in self._entries:
+ f.write(line)
if self._entries:
- self._writeEntries(f, oldentries)
+ self._writeEntries(f)
os.rename(tname, self._filename)
diff --git a/tests/tool_test.py b/tests/tool_test.py
index 828f5ca..1051398 100644
--- a/tests/tool_test.py
+++ b/tests/tool_test.py
@@ -446,6 +446,25 @@
def testAddExistingConf(self):
self._writeConf("key1=val1\n"
+ "key2=val2\n"
+ "#key3=val4")
+ with ConfigFile(self.tname,
+ version='3.4.4',
+ sectionStart="# start conf",
+ sectionEnd="# end conf") as conf:
+ conf.addEntry("key3", "val3")
+ conf.addEntry("key2", "val3")
+
+ with open(self.tname, 'r') as f:
+ self.assertEqual(f.read(), "key1=val1\n"
+ "#key3=val4"
+ "# start conf-3.4.4\n"
+ "key2=val3\n"
+ "key3=val3\n"
+ "# end conf-3.4.4\n")
+
+ def testAddExistingConfWithWhitespaces(self):
+ self._writeConf("key1=val1\n"
" key2 =val2\n"
"#key3=val4")
with ConfigFile(self.tname,
@@ -460,6 +479,7 @@
" key2 =val2\n"
"#key3=val4"
"# start conf-3.4.4\n"
+ "key2=val3\n"
"key3=val3\n"
"# end conf-3.4.4\n")
--
To view, visit
https://gerrit.ovirt.org/74331
To unsubscribe, visit
https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id1dba75449b712432e0c2bc5b128cc996ede0153
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Yaniv Bronhaim <ybronhei(a)redhat.com>