From 04ac069f4be1317bf13b0c5d31f8b000f2d67bbb Mon Sep 17 00:00:00 2001 From: James Slagle Date: Tue, 8 Aug 2017 15:52:31 -0400 Subject: [PATCH] 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 --- instack_undercloud/tests/test_undercloud.py | 10 +++++++--- instack_undercloud/undercloud.py | 7 ++++--- .../notes/set-valid-hosts-file-49d6aa96365908a7.yaml | 7 +++++++ 3 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml diff --git a/instack_undercloud/tests/test_undercloud.py b/instack_undercloud/tests/test_undercloud.py index 24f6656c4..021cbcd97 100644 --- a/instack_undercloud/tests/test_undercloud.py +++ b/instack_undercloud/tests/test_undercloud.py @@ -196,7 +196,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') @@ -212,7 +213,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') @@ -230,7 +232,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') diff --git a/instack_undercloud/undercloud.py b/instack_undercloud/undercloud.py index 89020f68d..d5bfcdff6 100644 --- a/instack_undercloud/undercloud.py +++ b/instack_undercloud/undercloud.py @@ -631,9 +631,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) diff --git a/releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml b/releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml new file mode 100644 index 000000000..edea8c785 --- /dev/null +++ b/releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml @@ -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