From 47f9f64a03c90ea08b6e363f786dfee921345682 Mon Sep 17 00:00:00 2001 From: harlowja Date: Sun, 17 Jun 2012 13:48:53 -0700 Subject: 1. Adjust the ubuntu network hostname writing to go through a standard write function 2. Add comment as to why we search for "#" when reading --- cloudinit/distros/ubuntu.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/ubuntu.py b/cloudinit/distros/ubuntu.py index ec4d6b5b..786974ad 100644 --- a/cloudinit/distros/ubuntu.py +++ b/cloudinit/distros/ubuntu.py @@ -49,11 +49,14 @@ class Distro(distros.Distro): util.write_file("/etc/network/interfaces", settings) def set_hostname(self, hostname): - contents = "%s\n" % (hostname) - util.write_file("/etc/hostname", contents, 0644) + self._write_hostname(hostname, "/etc/hostname") LOG.debug("Setting hostname to %s", hostname) util.subp(['hostname', hostname]) + def _write_hostname(self, hostname, out_fn): + contents = "%s\n" % (hostname) + util.write_file(out_fn, contents, 0644) + def update_hostname(self, hostname, prev_file): hostname_prev = self._read_hostname(prev_file) hostname_in_etc = self._read_hostname("/etc/hostname") @@ -65,8 +68,7 @@ class Distro(distros.Distro): update_files.append("/etc/hostname") for fn in update_files: try: - contents = "%s\n" % (hostname) - util.write_file(fn, contents, 0644) + self._write_hostname(hostname, fn) except: util.logexc(LOG, "Failed to write hostname %s to %s", hostname, fn) @@ -82,11 +84,12 @@ class Distro(distros.Distro): contents = util.load_file(filename, quiet=True) for line in contents.splitlines(): hpos = line.find("#") + # Handle inline comments if hpos != -1: line = line[0:hpos] - line = line.rstrip() - if line: - return line + line_c = line.strip() + if line_c: + return line_c return default def _get_localhost_ip(self): -- cgit v1.2.3