diff options
| author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-11-13 08:54:35 -0500 |
|---|---|---|
| committer | Scott Moser <smoser@ubuntu.com> | 2012-11-13 08:54:35 -0500 |
| commit | c26b0674aa2ef31c7c3f7a0392044382cf6a452f (patch) | |
| tree | b049de27577d6fe2400ef52649e6066c700f8c1c /cloudinit/distros/parsers/hosts.py | |
| parent | 8d5e1e108b0cdd3af872383da7654bec91355b5f (diff) | |
| parent | cd8fbfffdf7ba9ba79d39ee7b4a223d1bd7863b4 (diff) | |
| download | vyos-cloud-init-c26b0674aa2ef31c7c3f7a0392044382cf6a452f.tar.gz vyos-cloud-init-c26b0674aa2ef31c7c3f7a0392044382cf6a452f.zip | |
Use a set of helper/parsing classes to perform system configuration
Previously file modification of system configuration was done
in a functional and hard to test manner. Now instead this patch
allows for a manner that provides a nice object oriented
interface to those objects as well as makes it possible to test
those parsing entities without having to invoke distro class code.
- Created parsers for:
- /etc/sysconfig
- /etc/hostname
- resolv.conf
- /etc/hosts
Moved duplicated functionality into the root level distro class including:
- apply_hostname
- set_hostname
- *various shared configuration file names/paths*
Diffstat (limited to 'cloudinit/distros/parsers/hosts.py')
| -rw-r--r-- | cloudinit/distros/parsers/hosts.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/cloudinit/distros/parsers/hosts.py b/cloudinit/distros/parsers/hosts.py new file mode 100644 index 00000000..94c97051 --- /dev/null +++ b/cloudinit/distros/parsers/hosts.py @@ -0,0 +1,92 @@ +# vi: ts=4 expandtab +# +# Copyright (C) 2012 Yahoo! Inc. +# +# Author: Joshua Harlow <harlowja@yahoo-inc.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from StringIO import StringIO + +from cloudinit.distros.parsers import chop_comment + + +# See: man hosts +# or http://unixhelp.ed.ac.uk/CGI/man-cgi?hosts +# or http://tinyurl.com/6lmox3 +class HostsConf(object): + def __init__(self, text): + self._text = text + self._contents = None + + def parse(self): + if self._contents is None: + self._contents = self._parse(self._text) + + def get_entry(self, ip): + self.parse() + options = [] + for (line_type, components) in self._contents: + if line_type == 'option': + (pieces, _tail) = components + if len(pieces) and pieces[0] == ip: + options.append(pieces[1:]) + return options + + def del_entries(self, ip): + self.parse() + n_entries = [] + for (line_type, components) in self._contents: + if line_type != 'option': + n_entries.append((line_type, components)) + continue + else: + (pieces, _tail) = components + if len(pieces) and pieces[0] == ip: + pass + elif len(pieces): + n_entries.append((line_type, list(components))) + self._contents = n_entries + + def add_entry(self, ip, canonical_hostname, *aliases): + self.parse() + self._contents.append(('option', + ([ip, canonical_hostname] + list(aliases), ''))) + + def _parse(self, contents): + entries = [] + for line in contents.splitlines(): + if not len(line.strip()): + entries.append(('blank', [line])) + continue + (head, tail) = chop_comment(line.strip(), '#') + if not len(head): + entries.append(('all_comment', [line])) + continue + entries.append(('option', [head.split(None), tail])) + return entries + + def __str__(self): + self.parse() + contents = StringIO() + for (line_type, components) in self._contents: + if line_type == 'blank': + contents.write("%s\n" % (components[0])) + elif line_type == 'all_comment': + contents.write("%s\n" % (components[0])) + elif line_type == 'option': + (pieces, tail) = components + pieces = [str(p) for p in pieces] + pieces = "\t".join(pieces) + contents.write("%s%s\n" % (pieces, tail)) + return contents.getvalue() |
