Remove whitespace around equal signs in zookeeper.cfg

The merge_configs action plugin is updated with an option to control
whether whitespace should be used around equal signs. The default
remains to use whitespace: only zookeeper.cfg doesn't use them.

This is to avoid issues with the zkCleanup.sh script which expects no
whitespace. The ZooKeeper documentation also uses no whitespace in
configuration file examples.

Change-Id: Ia082a1c002cc4e8b04f7696fdee827b747c6d13f
Closes-Bug: #1917490
This commit is contained in:
Pierre Riteau 2021-03-05 15:11:55 +01:00
parent cef8c8b3de
commit 2b723ead05
4 changed files with 60 additions and 5 deletions

View File

@ -46,6 +46,12 @@ options:
default: None default: None
required: True required: True
type: str type: str
whitespace:
description:
- Whether whitespace characters should be used around equal signs
default: True
required: False
type: bool
author: Sam Yaple author: Sam Yaple
''' '''
@ -67,10 +73,11 @@ Merge multiple configs:
class OverrideConfigParser(iniparser.BaseParser): class OverrideConfigParser(iniparser.BaseParser):
def __init__(self): def __init__(self, whitespace=True):
self._cur_sections = collections.OrderedDict() self._cur_sections = collections.OrderedDict()
self._sections = collections.OrderedDict() self._sections = collections.OrderedDict()
self._cur_section = None self._cur_section = None
self._whitespace = ' ' if whitespace else ''
def assignment(self, key, value): def assignment(self, key, value):
if self._cur_section is None: if self._cur_section is None:
@ -107,12 +114,20 @@ class OverrideConfigParser(iniparser.BaseParser):
def write_key_value(key, values): def write_key_value(key, values):
for v in values: for v in values:
if not v: if not v:
fp.write('{} =\n'.format(key)) fp.write('{key}{ws}=\n'.format(
key=key, ws=self._whitespace))
for index, value in enumerate(v): for index, value in enumerate(v):
if index == 0: if index == 0:
fp.write('{} = {}\n'.format(key, value)) fp.write('{key}{ws}={ws}{value}\n'.format(
key=key,
ws=self._whitespace,
value=value))
else: else:
fp.write('{} {}\n'.format(len(key) * ' ', value)) indent_size = len(key) + len(self._whitespace) * 2 + 1
ws_indent = ' ' * indent_size
fp.write('{ws_indent}{value}\n'.format(
ws_indent=ws_indent,
value=value))
def write_section(section): def write_section(section):
for key, values in section.items(): for key, values in section.items():
@ -158,7 +173,8 @@ class ActionModule(action.ActionBase):
if not isinstance(sources, list): if not isinstance(sources, list):
sources = [sources] sources = [sources]
config = OverrideConfigParser() config = OverrideConfigParser(
whitespace=self._task.args.get('whitespace', True))
for source in sources: for source in sources:
self.read_config(source, config) self.read_config(source, config)
@ -179,6 +195,7 @@ class ActionModule(action.ActionBase):
new_task = self._task.copy() new_task = self._task.copy()
new_task.args.pop('sources', None) new_task.args.pop('sources', None)
new_task.args.pop('whitespace', None)
new_task.args.update( new_task.args.update(
dict( dict(

View File

@ -31,6 +31,7 @@
- "{{ role_path }}/templates/{{ item.key }}.cfg.j2" - "{{ role_path }}/templates/{{ item.key }}.cfg.j2"
- "{{ node_custom_config }}/{{ item.key }}.cfg" - "{{ node_custom_config }}/{{ item.key }}.cfg"
- "{{ node_custom_config }}/{{ item.key }}/{{ inventory_hostname }}/{{ item.key }}.cfg" - "{{ node_custom_config }}/{{ item.key }}/{{ inventory_hostname }}/{{ item.key }}.cfg"
whitespace: False
dest: "{{ node_config_directory }}/{{ item.key }}/{{ item.key }}.cfg" dest: "{{ node_config_directory }}/{{ item.key }}/{{ item.key }}.cfg"
mode: "0660" mode: "0660"
become: true become: true

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Removes whitespace around equal signs in ``zookeeper.cfg`` which were
preventing the ``zkCleanup.sh`` script from running correctly.

View File

@ -132,6 +132,29 @@ key2 = not_b
''' '''
# TESTC_NO_WHITESPACE is TESTA + TESTB without whitespace around equal signs
TESTC_NO_WHITESPACE = '''[DEFAULT]
key1=b
c
key2=v3
v4
v5
key3=v3
key3=v4
key4=v4
key4=
[b]
b_key1=1
b_key2=2
[c]
c_key1=
c_key2=1 2 3
4 5 6
'''
class OverrideConfigParserTest(base.BaseTestCase): class OverrideConfigParserTest(base.BaseTestCase):
@ -178,3 +201,12 @@ class OverrideConfigParserTest(base.BaseTestCase):
parser.write(output) parser.write(output)
self.assertEqual(TESTC_NO_DEFAULT_SECTION, output.getvalue()) self.assertEqual(TESTC_NO_DEFAULT_SECTION, output.getvalue())
output.close() output.close()
def test_merge_no_whitespace(self):
parser = merge_configs.OverrideConfigParser(whitespace=False)
parser.parse(StringIO(TESTA))
parser.parse(StringIO(TESTB))
output = StringIO()
parser.write(output)
self.assertEqual(TESTC_NO_WHITESPACE, output.getvalue())
output.close()