From 1a01c110452fc4ede472a5f7b3b1230e2c7f925e Mon Sep 17 00:00:00 2001 From: Heghedus Razvan Date: Tue, 21 Feb 2017 14:25:50 +0200 Subject: [PATCH] system: multi line support in computer description Special characters like \n \t are not escaped, so it's impossible to set a multi line computer description. To fix this I escaped all special characters. Signed-off-by: Heghedus Razvan --- salt/modules/system.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/salt/modules/system.py b/salt/modules/system.py index 1632d290c8..c2d1841991 100644 --- a/salt/modules/system.py +++ b/salt/modules/system.py @@ -11,6 +11,7 @@ import os.path # Import salt libs import salt.utils +import salt.ext.six as six from salt.exceptions import CommandExecutionError, SaltInvocationError @@ -504,10 +505,14 @@ def get_computer_desc(): match = pattern.match(line) if match: # get rid of whitespace then strip off quotes - desc = _strip_quotes(match.group(1).strip()).replace('\\"', '"') + desc = _strip_quotes(match.group(1).strip()) # no break so we get the last occurance except IOError: return False + if six.PY3: + desc = desc.replace('\\"', '"').decode('unicode_escape') + else: + desc = desc.replace('\\"', '"').decode('string_escape') return desc @@ -526,9 +531,13 @@ def set_computer_desc(desc): salt '*' system.set_computer_desc "Michael's laptop" ''' + if six.PY3: + desc = desc.encode('unicode_escape').replace('"', '\\"') + else: + desc = desc.encode('string_escape').replace('"', '\\"') hostname_cmd = salt.utils.which('hostnamectl') if hostname_cmd: - result = __salt__['cmd.retcode']('{0} set-hostname --pretty {1}'.format(hostname_cmd, desc)) + result = __salt__['cmd.retcode']('{0} set-hostname --pretty "{1}"'.format(hostname_cmd, desc)) return True if result == 0 else False if not os.path.isfile('/etc/machine-info'): @@ -537,7 +546,7 @@ def set_computer_desc(desc): is_pretty_hostname_found = False pattern = re.compile(r'^\s*PRETTY_HOSTNAME=(.*)$') - new_line = 'PRETTY_HOSTNAME="{0}"'.format(desc.replace('"', '\\"')) + new_line = 'PRETTY_HOSTNAME="{0}"'.format(desc) try: with salt.utils.fopen('/etc/machine-info', 'r+') as mach_info: lines = mach_info.readlines()