diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-10-11 12:49:45 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-10-11 12:49:45 -0700 |
commit | 0f1a2cbe434cba243ce65ff43a88722c2bcf6f2c (patch) | |
tree | 4f78abcde4e023878558cb066fc939ae8a12a3da /cloudinit/distros/debian.py | |
parent | fe1ec4d4cbb682731e8f65be5dab60f4593ed9d6 (diff) | |
download | vyos-cloud-init-0f1a2cbe434cba243ce65ff43a88722c2bcf6f2c.tar.gz vyos-cloud-init-0f1a2cbe434cba243ce65ff43a88722c2bcf6f2c.zip |
More adjustments/cleanups for the system configuration
helper objects.
1. Add in a parser for the /etc/hostname file that can be shared
2. Adjust the sysconfig configobj parser to not always quote
fields that it does not need to quote + add in tests around
this to ensure that we don't go nuts with quoting again.
Diffstat (limited to 'cloudinit/distros/debian.py')
-rw-r--r-- | cloudinit/distros/debian.py | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index c8b13f95..0d5cbac7 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -27,7 +27,7 @@ from cloudinit import helpers from cloudinit import log as logging from cloudinit import util -from cloudinit.distros.parsers import chop_comment +from cloudinit.distros.parsers.hostname import HostnameConf from cloudinit.settings import PER_INSTANCE @@ -84,27 +84,38 @@ class Distro(distros.Distro): self._write_hostname(hostname, self.hostname_conf_fn) self._apply_hostname(hostname) - def _write_hostname(self, hostname, out_fn): - # "" gives trailing newline. - hostname_lines = [ - str(hostname), - "", - ] - util.write_file(out_fn, "\n".join(hostname_lines), 0644) + def _write_hostname(self, your_hostname, out_fn): + conf = self._read_hostname_conf(out_fn) + if not conf: + conf = HostnameConf('') + conf.parse() + conf.set_hostname(your_hostname) + util.write_file(out_fn, str(conf), 0644) def _read_system_hostname(self): - return (self.hostname_conf_fn, - self._read_hostname(self.hostname_conf_fn)) + conf = self._read_hostname_conf(self.hostname_conf_fn) + if conf: + sys_hostname = conf.hostname + else: + sys_hostname = None + return (self.hostname_conf_fn, sys_hostname) + + def _read_hostname_conf(self, filename): + try: + conf = HostnameConf(util.load_file(filename)) + conf.parse() + return conf + except IOError: + util.logexc(LOG, "Error reading hostname from %s", filename) + return None def _read_hostname(self, filename, default=None): - contents = util.load_file(filename, quiet=True) - for line in contents.splitlines(): - # Handle inline comments - (before_comment, _comment) = chop_comment(line, "#") - before_comment = before_comment.strip() - if len(before_comment): - return before_comment - return default + conf = self._read_hostname_conf(filename) + if not conf: + return default + if not conf.hostname: + return default + return conf.hostname def _get_localhost_ip(self): # Note: http://www.leonardoborda.com/blog/127-0-1-1-ubuntu-debian/ |