From 80eb53deb9f80694c5fd0e0190197de1ff496716 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Wed, 10 Oct 2012 16:21:22 -0700 Subject: System config niceness! 1. Move out the old helpers that provided oop access/reading/writing to various standard conf files and place those in parsers instead. 2. Unify the 'update_hostname' which varied very little between distros and make it generic so that subclasses can only provide a couple of functions to obtain the hostname updating functionality 3. Implement that new set of functions in rhel/debian 4. Use the new parsers chop_comment function for similar use cases as well as add a new utils make header function that can be used for configuration files that are newly generated to use (less duplication here of this same thing being done in multiple places. 5. Add in a distro '_apply_hostname' which calls out to the 'hostname' program to set the system hostname (more duplication elimination). 6. Make the 'constant' filenames being written to for configuration by the various distros be instance members instead of string constants 'sprinkled' throughout the code --- cloudinit/distros/parsers/__init__.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 cloudinit/distros/parsers/__init__.py (limited to 'cloudinit/distros/parsers/__init__.py') diff --git a/cloudinit/distros/parsers/__init__.py b/cloudinit/distros/parsers/__init__.py new file mode 100644 index 00000000..8334a5e6 --- /dev/null +++ b/cloudinit/distros/parsers/__init__.py @@ -0,0 +1,27 @@ +# vi: ts=4 expandtab +# +# Copyright (C) 2012 Yahoo! Inc. +# +# Author: Joshua Harlow +# +# 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 . + +def chop_comment(text, comment_chars): + comment_locations = [text.find(c) for c in comment_chars] + comment_locations = [c for c in comment_locations if c != -1] + if not comment_locations: + return (text, '') + min_comment = min(comment_locations) + before_comment = text[0:min_comment] + comment = text[min_comment:] + return (before_comment, comment) -- cgit v1.2.3 From 82e90789f11b5371b352a477b75cad0c5d1457ec Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 12 Nov 2012 14:30:08 -0800 Subject: Cleanup of /etc/hosts ordering and pep8/pylint adjustments. Fix how the comparison of a fqdn and its aliases was done via sorting instead of existence checking which is the better way to check if a alias already exists as well as cleanup the new files pep8/pylint issues. LP: #1078097 --- cloudinit/distros/__init__.py | 19 ++++++++++++------- cloudinit/distros/parsers/__init__.py | 1 + cloudinit/distros/parsers/hostname.py | 2 -- cloudinit/distros/parsers/resolv_conf.py | 4 +--- cloudinit/distros/parsers/sys_conf.py | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) (limited to 'cloudinit/distros/parsers/__init__.py') diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index fa7cc1ca..4bde2393 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -189,15 +189,20 @@ class Distro(object): else: need_change = True for entry in prev_info: - if sorted(entry) == sorted([fqdn, hostname]): - # Exists already, leave it be - need_change = False - break + entry_fqdn = None + entry_aliases = [] + if len(entry) >= 1: + entry_fqdn = entry[0] + if len(entry) >= 2: + entry_aliases = entry[1:] + if entry_fqdn is not None and entry_fqdn == fqdn: + if hostname in entry_aliases: + # Exists already, leave it be + need_change = False if need_change: - # Doesn't exist, change the first - # entry to be this entry + # Doesn't exist, add that entry in... new_entries = list(prev_info) - new_entries[0] = [fqdn, hostname] + new_entries.append([fqdn, hostname]) eh.del_entries(local_ip) for entry in new_entries: if len(entry) == 1: diff --git a/cloudinit/distros/parsers/__init__.py b/cloudinit/distros/parsers/__init__.py index 8334a5e6..1c413eaa 100644 --- a/cloudinit/distros/parsers/__init__.py +++ b/cloudinit/distros/parsers/__init__.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . + def chop_comment(text, comment_chars): comment_locations = [text.find(c) for c in comment_chars] comment_locations = [c for c in comment_locations if c != -1] diff --git a/cloudinit/distros/parsers/hostname.py b/cloudinit/distros/parsers/hostname.py index 7e19f017..617b3c36 100644 --- a/cloudinit/distros/parsers/hostname.py +++ b/cloudinit/distros/parsers/hostname.py @@ -86,5 +86,3 @@ class HostnameConf(object): raise IOError("Multiple hostnames (%s) found!" % (hostnames_found)) return entries - - diff --git a/cloudinit/distros/parsers/resolv_conf.py b/cloudinit/distros/parsers/resolv_conf.py index 377ada6b..5733c25a 100644 --- a/cloudinit/distros/parsers/resolv_conf.py +++ b/cloudinit/distros/parsers/resolv_conf.py @@ -127,7 +127,7 @@ class ResolvConf(object): # Hard restriction on only 6 search domains raise ValueError(("Adding %r would go beyond the " "'6' maximum search domains") % (search_domain)) - s_list = " ".join(new_sds) + s_list = " ".join(new_sds) if len(s_list) > 256: # Some hard limit on 256 chars total raise ValueError(("Adding %r would go beyond the " @@ -167,5 +167,3 @@ class ResolvConf(object): raise IOError("Unexpected resolv.conf option %s" % (cfg_opt)) entries.append(("option", [cfg_opt, cfg_values, tail])) return entries - - diff --git a/cloudinit/distros/parsers/sys_conf.py b/cloudinit/distros/parsers/sys_conf.py index 5cd765fc..20ca1871 100644 --- a/cloudinit/distros/parsers/sys_conf.py +++ b/cloudinit/distros/parsers/sys_conf.py @@ -40,7 +40,7 @@ SHELL_VAR_REGEXES = [ # Things like $?, $0, $-, $@ re.compile(r"\$[0-9#\?\-@\*]"), # Things like ${blah:1} - but this one - # gets very complex so just try the + # gets very complex so just try the # simple path re.compile(r"\$\{.+\}"), ] -- cgit v1.2.3