← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/fix-hostname-rh into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/fix-hostname-rh into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)
Related bugs:
  Bug #1076759 in cloud-init: "sysconfig fqdn network"
  https://bugs.launchpad.net/cloud-init/+bug/1076759

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/fix-hostname-rh/+merge/133607
-- 
https://code.launchpad.net/~harlowja/cloud-init/fix-hostname-rh/+merge/133607
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/fix-hostname-rh into lp:cloud-init.
=== modified file 'cloudinit/config/cc_set_hostname.py'
--- cloudinit/config/cc_set_hostname.py	2012-06-21 16:12:16 +0000
+++ cloudinit/config/cc_set_hostname.py	2012-11-09 01:40:24 +0000
@@ -27,9 +27,11 @@
                     " not setting the hostname in module %s"), name)
         return
 
-    (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
+    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
     try:
-        log.debug("Setting hostname to %s", hostname)
-        cloud.distro.set_hostname(hostname)
+        log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
+        cloud.distro.set_hostname(hostname, fqdn)
     except Exception:
-        util.logexc(log, "Failed to set hostname to %s", hostname)
+        util.logexc(log, "Failed to set the hostname to %s (%s)",
+                    fqdn, hostname)
+        raise

=== modified file 'cloudinit/config/cc_update_hostname.py'
--- cloudinit/config/cc_update_hostname.py	2012-08-22 18:12:32 +0000
+++ cloudinit/config/cc_update_hostname.py	2012-11-09 01:40:24 +0000
@@ -32,10 +32,12 @@
                     " not updating the hostname in module %s"), name)
         return
 
-    (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
+    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
     try:
         prev_fn = os.path.join(cloud.get_cpath('data'), "previous-hostname")
-        cloud.distro.update_hostname(hostname, prev_fn)
+        log.debug("Updating hostname to %s (%s)", fqdn, hostname)
+        cloud.distro.update_hostname(hostname, fqdn, prev_fn)
     except Exception:
-        util.logexc(log, "Failed to set the hostname to %s", hostname)
+        util.logexc(log, "Failed to update the hostname to %s (%s)",
+                    fqdn, hostname)
         raise

=== modified file 'cloudinit/distros/__init__.py'
--- cloudinit/distros/__init__.py	2012-11-07 15:42:54 +0000
+++ cloudinit/distros/__init__.py	2012-11-09 01:40:24 +0000
@@ -58,11 +58,11 @@
         return self._cfg.get(opt_name, default)
 
     @abc.abstractmethod
-    def set_hostname(self, hostname):
+    def set_hostname(self, hostname, fqdn=None):
         raise NotImplementedError()
 
     @abc.abstractmethod
-    def update_hostname(self, hostname, prev_hostname_fn):
+    def update_hostname(self, hostname, fqdn, prev_hostname_fn):
         raise NotImplementedError()
 
     @abc.abstractmethod

=== modified file 'cloudinit/distros/debian.py'
--- cloudinit/distros/debian.py	2012-10-28 02:25:48 +0000
+++ cloudinit/distros/debian.py	2012-11-09 01:40:24 +0000
@@ -67,7 +67,7 @@
         else:
             return distros.Distro._bring_up_interfaces(self, device_names)
 
-    def set_hostname(self, hostname):
+    def set_hostname(self, hostname, fqdn=None):
         self._write_hostname(hostname, "/etc/hostname")
         LOG.debug("Setting hostname to %s", hostname)
         util.subp(['hostname', hostname])
@@ -76,7 +76,7 @@
         # "" gives trailing newline.
         util.write_file(out_fn, "%s\n" % str(hostname), 0644)
 
-    def update_hostname(self, hostname, prev_fn):
+    def update_hostname(self, hostname, fqdn, prev_fn):
         hostname_prev = self._read_hostname(prev_fn)
         hostname_in_etc = self._read_hostname("/etc/hostname")
         update_files = []

=== modified file 'cloudinit/distros/rhel.py'
--- cloudinit/distros/rhel.py	2012-09-25 20:44:16 +0000
+++ cloudinit/distros/rhel.py	2012-11-09 01:40:24 +0000
@@ -146,8 +146,13 @@
                 lines.insert(0, _make_header())
             util.write_file(fn, "\n".join(lines), 0644)
 
-    def set_hostname(self, hostname):
-        self._write_hostname(hostname, '/etc/sysconfig/network')
+    def set_hostname(self, hostname, fqdn=None):
+        # See: http://bit.ly/TwitgL
+        # Should be fqdn if we can use it
+        sysconfig_hostname = fqdn
+        if not sysconfig_hostname:
+            sysconfig_hostname = hostname
+        self._write_hostname(sysconfig_hostname, '/etc/sysconfig/network')
         LOG.debug("Setting hostname to %s", hostname)
         util.subp(['hostname', hostname])
 
@@ -165,28 +170,32 @@
         }
         self._update_sysconfig_file(out_fn, host_cfg)
 
-    def update_hostname(self, hostname, prev_file):
+    def update_hostname(self, hostname, fqdn, prev_file):
+        # See: http://bit.ly/TwitgL
+        # Should be fqdn if we can use it
+        sysconfig_hostname = fqdn
+        if not sysconfig_hostname:
+            sysconfig_hostname = hostname
         hostname_prev = self._read_hostname(prev_file)
         hostname_in_sys = self._read_hostname("/etc/sysconfig/network")
         update_files = []
-        if not hostname_prev or hostname_prev != hostname:
+        if not hostname_prev or hostname_prev != sysconfig_hostname:
             update_files.append(prev_file)
         if (not hostname_in_sys or
             (hostname_in_sys == hostname_prev
-             and hostname_in_sys != hostname)):
+             and hostname_in_sys != sysconfig_hostname)):
             update_files.append("/etc/sysconfig/network")
         for fn in update_files:
             try:
-                self._write_hostname(hostname, fn)
+                self._write_hostname(sysconfig_hostname, fn)
             except:
                 util.logexc(LOG, "Failed to write hostname %s to %s",
-                            hostname, fn)
+                            sysconfig_hostname, fn)
         if (hostname_in_sys and hostname_prev and
             hostname_in_sys != hostname_prev):
             LOG.debug(("%s differs from /etc/sysconfig/network."
                         " Assuming user maintained hostname."), prev_file)
         if "/etc/sysconfig/network" in update_files:
-            # Only do this if we are running in non-adjusted root mode
             LOG.debug("Setting hostname to %s", hostname)
             util.subp(['hostname', hostname])
 


Follow ups