← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~rjschwei/cloud-init:hostsTemplate into cloud-init:master

 

Robert Schweikert has proposed merging ~rjschwei/cloud-init:hostsTemplate into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~rjschwei/cloud-init/+git/cloud-init/+merge/333418
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~rjschwei/cloud-init:hostsTemplate into cloud-init:master.
diff --git a/cloudinit/config/cc_update_etc_hosts.py b/cloudinit/config/cc_update_etc_hosts.py
index b394784..c96eede 100644
--- a/cloudinit/config/cc_update_etc_hosts.py
+++ b/cloudinit/config/cc_update_etc_hosts.py
@@ -23,8 +23,8 @@ using the template located in ``/etc/cloud/templates/hosts.tmpl``. In the
 
 If ``manage_etc_hosts`` is set to ``localhost``, then cloud-init will not
 rewrite ``/etc/hosts`` entirely, but rather will ensure that a entry for the
-fqdn with ip ``127.0.1.1`` is present in ``/etc/hosts`` (i.e.
-``ping <hostname>`` will ping ``127.0.1.1``).
+fqdn with a distribution dependent ip is present in ``/etc/hosts`` (i.e.
+``ping <hostname>`` will ping ``127.0.0.1`` or ``127.0.1.1`` or other ip).
 
 .. note::
     if ``manage_etc_hosts`` is set ``true`` or ``template``, the contents
diff --git a/templates/hosts.opensuse.tmpl b/templates/hosts.opensuse.tmpl
deleted file mode 100644
index 655da3f..0000000
--- a/templates/hosts.opensuse.tmpl
+++ /dev/null
@@ -1,26 +0,0 @@
-*
-    This file /etc/cloud/templates/hosts.opensuse.tmpl is only utilized
-    if enabled in cloud-config.  Specifically, in order to enable it
-    you need to add the following to config:
-      manage_etc_hosts: True
-*#
-# Your system has configured 'manage_etc_hosts' as True.
-# As a result, if you wish for changes to this file to persist
-# then you will need to either
-# a.) make changes to the master file in
-#     /etc/cloud/templates/hosts.opensuse.tmpl
-# b.) change or remove the value of 'manage_etc_hosts' in
-#     /etc/cloud/cloud.cfg or cloud-config from user-data
-#
-# The following lines are desirable for IPv4 capable hosts
-127.0.0.1 localhost
-
-# The following lines are desirable for IPv6 capable hosts
-::1 localhost ipv6-localhost ipv6-loopback
-fe00::0 ipv6-localnet
-
-ff00::0 ipv6-mcastprefix
-ff02::1 ipv6-allnodes
-ff02::2 ipv6-allrouters
-ff02::3 ipv6-allhosts
-
diff --git a/templates/hosts.suse.tmpl b/templates/hosts.suse.tmpl
index b608269..8e664db 100644
--- a/templates/hosts.suse.tmpl
+++ b/templates/hosts.suse.tmpl
@@ -13,12 +13,18 @@ you need to add the following to config:
 #     /etc/cloud/cloud.cfg or cloud-config from user-data
 #
 # The following lines are desirable for IPv4 capable hosts
-127.0.0.1 localhost
+127.0.0.1 {{fqdn}} {{hostname}}
+127.0.0.1 localhost.localdomain localhost
+127.0.0.1 localhost4.localdomain4 localhost4
 
 # The following lines are desirable for IPv6 capable hosts
+::1 {{fqdn}} {{hostname}}
+::1 localhost.localdomain localhost
+::1 localhost6.localdomain6 localhost6
 ::1 localhost ipv6-localhost ipv6-loopback
-fe00::0 ipv6-localnet
 
+
+fe00::0 ipv6-localnet
 ff00::0 ipv6-mcastprefix
 ff02::1 ipv6-allnodes
 ff02::2 ipv6-allrouters
diff --git a/tests/unittests/test_handler/test_handler_etc_hosts.py b/tests/unittests/test_handler/test_handler_etc_hosts.py
new file mode 100644
index 0000000..ced05a8
--- /dev/null
+++ b/tests/unittests/test_handler/test_handler_etc_hosts.py
@@ -0,0 +1,69 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
+from cloudinit.config import cc_update_etc_hosts
+
+from cloudinit import cloud
+from cloudinit import distros
+from cloudinit import helpers
+from cloudinit import util
+
+from cloudinit.tests import helpers as t_help
+
+import logging
+import os
+import shutil
+
+LOG = logging.getLogger(__name__)
+
+
+class TestHostsFile(t_help.FilesystemMockingTestCase):
+    def setUp(self):
+        super(TestHostsFile, self).setUp()
+        self.tmp = self.tmp_dir()
+
+    def _fetch_distro(self, kind):
+        cls = distros.fetch(kind)
+        paths = helpers.Paths({})
+        return cls(kind, {}, paths)
+
+    def test_write_etc_hosts_suse_localhost(self):
+        cfg = {
+            'manage_etc_hosts': 'localhost',
+            'hostname': 'cloud-init.test.us'
+        }
+        os.makedirs('%s/etc/' % self.tmp)
+        hosts_content = '192.168.1.1 blah.blah.us blah\n'
+        fout = open('%s/etc/hosts' % self.tmp, 'w')
+        fout.write(hosts_content)
+        fout.close()
+        distro = self._fetch_distro('sles')
+        distro.hosts_fn = '%s/etc/hosts' % self.tmp
+        paths = helpers.Paths({})
+        ds = None
+        cc = cloud.Cloud(ds, paths, {}, distro, None)
+        self.patchUtils(self.tmp)
+        cc_update_etc_hosts.handle('test', cfg, cc, LOG, [])
+        contents = util.load_file('%s/etc/hosts' % self.tmp)
+        if '127.0.0.1\tcloud-init.test.us\tcloud-init' not in contents:
+            self.assertIsNone('No entry for 127.0.0.1 in etc/hosts')
+        if '192.168.1.1\tblah.blah.us\tblah' not in contents:
+            self.assertIsNone('Default etc/hosts content modified')
+
+    def test_write_etc_hosts_suse_template(self):
+        cfg = {
+            'manage_etc_hosts': 'template',
+            'hostname': 'cloud-init.test.us'
+        }
+        shutil.copytree('templates', '%s/etc/cloud/templates' % self.tmp)
+        distro = self._fetch_distro('sles')
+        paths = helpers.Paths({})
+        paths.template_tpl = '%s' % self.tmp + '/etc/cloud/templates/%s.tmpl'
+        ds = None
+        cc = cloud.Cloud(ds, paths, {}, distro, None)
+        self.patchUtils(self.tmp)
+        cc_update_etc_hosts.handle('test', cfg, cc, LOG, [])
+        contents = util.load_file('%s/etc/hosts' % self.tmp)
+        if '127.0.0.1 cloud-init.test.us cloud-init' not in contents:
+            self.assertIsNone('No entry for 127.0.0.1 in etc/hosts')
+        if '::1 cloud-init.test.us cloud-init' not in contents:
+            self.assertIsNone('No entry for 127.0.0.1 in etc/hosts')

Follow ups