mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge pull request #39590 from raazvvann/dev/rheghedu/system-comments
system: multi line support in computer description
This commit is contained in:
commit
87aa4a5dc0
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user