Fix invalid /etc/hosts edit

When the hostname was written to /etc/hosts, it resulted in an invalid
/etc/hosts file due to 127.0.0.1 being specified twice on different lines.

That issue is now corrected such that the hostnames will be added to the
existing line for 127.0.0.1, which results in valid syntax for /etc/hosts.

Change-Id: I8cd262b26708bd282e1b8623317e9e48f37e527d
Closes-Bug: #1709460
(cherry picked from commit 04ac069f4b)
This commit is contained in:
James Slagle 2017-08-08 15:52:31 -04:00
parent daf2d83154
commit 0c364056dd
3 changed files with 18 additions and 6 deletions

View File

@ -203,7 +203,8 @@ class TestCheckHostname(BaseTestCase):
undercloud._check_hostname()
mock_run_command.assert_called_with([
'sudo', '/bin/bash', '-c',
'echo 127.0.0.1 test.hostname test >> /etc/hosts'],
'sed -i "s/127.0.0.1\(\s*\)/127.0.0.1\\1test.hostname test /" '
'/etc/hosts'],
name='hostname-to-etc-hosts')
@mock.patch('instack_undercloud.undercloud._run_command')
@ -219,7 +220,8 @@ class TestCheckHostname(BaseTestCase):
undercloud._check_hostname()
mock_run_command.assert_called_with([
'sudo', '/bin/bash', '-c',
'echo 127.0.0.1 test.hostname test >> /etc/hosts'],
'sed -i "s/127.0.0.1\(\s*\)/127.0.0.1\\1test.hostname test /" '
'/etc/hosts'],
name='hostname-to-etc-hosts')
@mock.patch('instack_undercloud.undercloud._run_command')
@ -237,7 +239,9 @@ class TestCheckHostname(BaseTestCase):
undercloud._check_hostname()
mock_run_command.assert_called_with([
'sudo', '/bin/bash', '-c',
'echo 127.0.0.1 test-hostname.domain test-hostname >> /etc/hosts'],
'sed -i "s/127.0.0.1\(\s*\)/'
'127.0.0.1\\1test-hostname.domain test-hostname /" '
'/etc/hosts'],
name='hostname-to-etc-hosts')
@mock.patch('instack_undercloud.undercloud._run_command')

View File

@ -636,9 +636,10 @@ def _check_hostname():
if short_hostname == detected_static_hostname:
raise RuntimeError('Configured hostname is not fully '
'qualified.')
echo_cmd = ('echo 127.0.0.1 %s %s >> /etc/hosts' %
(detected_static_hostname, short_hostname))
args = ['sudo', '/bin/bash', '-c', echo_cmd]
sed_cmd = ('sed -i "s/127.0.0.1\(\s*\)/127.0.0.1\\1%s %s /" '
'/etc/hosts' %
(detected_static_hostname, short_hostname))
args = ['sudo', '/bin/bash', '-c', sed_cmd]
_run_command(args, name='hostname-to-etc-hosts')
LOG.info('Added hostname %s to /etc/hosts',
detected_static_hostname)

View File

@ -0,0 +1,7 @@
---
fixes:
- When the hostname was written to /etc/hosts, it resulted in an invalid
/etc/hosts file due to 127.0.0.1 being specified twice on different lines.
That issue is now corrected such that the hostnames will be added to the
existing line for 127.0.0.1, which results in valid syntax for /etc/hosts.
See https://bugs.launchpad.net/tripleo/+bug/1709460