~ubuntu-branches/ubuntu/saucy/cloud-init/saucy

« back to all changes in this revision

Viewing changes to cloudinit/distros/parsers/hosts.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2012-11-14 15:18:50 UTC
  • mto: (245.3.3 raring-proposed)
  • mto: This revision was merged to the branch mainline in revision 290.
  • Revision ID: package-import@ubuntu.com-20121114151850-8f1u0o17ta9dwkrt
* New upstream release.
  * landscape: install landscape-client package if not installed.
    only take action if cloud-config is present (LP: #1066115)
  * landscape: restart landscape after install or config (LP: #1070345)
  * multipart/archive: do not fail on unknown headers in multipart
    mime or cloud-archive config (LP: #1065116).
  * tools/Z99-cloud-locale-test.sh: avoid warning when user's shell is
    zsh (LP: #1073077)
  * fix stack trace when unknown user-data input had unicode (LP: #1075756)
  * split 'apt-update-upgrade' config module into 'apt-configure' and
    'package-update-upgrade-install'.  The 'package-update-upgrade-install'
    will be a cross distro module.
  * fix bug where cloud-config from user-data could not affect system_info
    settings (LP: #1076811)
  * add yum_add_repo configuration module for adding additional yum repos
  * fix public key importing with config-drive-v2 datasource (LP: #1077700)
  * handle renaming and fixing up of marker names (LP: #1075980)
    this relieves that burden from the distro/packaging.
  * group config: fix how group members weren't being translated correctly
    when the group: [member, member...] format was used (LP: #1077245)
  * work around an issue with boto > 0.6.0 that lazy loaded the return from 
    get_instance_metadata().  This resulted in failure for cloud-init to
    install ssh keys. (LP: #1068801)
  * add power_state_change config module for shutting down stystem after
    cloud-init finishes. (LP: #1064665)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vi: ts=4 expandtab
 
2
#
 
3
#    Copyright (C) 2012 Yahoo! Inc.
 
4
#
 
5
#    Author: Joshua Harlow <harlowja@yahoo-inc.com>
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License version 3, as
 
9
#    published by the Free Software Foundation.
 
10
#
 
11
#    This program is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the GNU General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
from StringIO import StringIO
 
20
 
 
21
from cloudinit.distros.parsers import chop_comment
 
22
 
 
23
 
 
24
# See: man hosts
 
25
# or http://unixhelp.ed.ac.uk/CGI/man-cgi?hosts
 
26
# or http://tinyurl.com/6lmox3
 
27
class HostsConf(object):
 
28
    def __init__(self, text):
 
29
        self._text = text
 
30
        self._contents = None
 
31
 
 
32
    def parse(self):
 
33
        if self._contents is None:
 
34
            self._contents = self._parse(self._text)
 
35
 
 
36
    def get_entry(self, ip):
 
37
        self.parse()
 
38
        options = []
 
39
        for (line_type, components) in self._contents:
 
40
            if line_type == 'option':
 
41
                (pieces, _tail) = components
 
42
                if len(pieces) and pieces[0] == ip:
 
43
                    options.append(pieces[1:])
 
44
        return options
 
45
 
 
46
    def del_entries(self, ip):
 
47
        self.parse()
 
48
        n_entries = []
 
49
        for (line_type, components) in self._contents:
 
50
            if line_type != 'option':
 
51
                n_entries.append((line_type, components))
 
52
                continue
 
53
            else:
 
54
                (pieces, _tail) = components
 
55
                if len(pieces) and pieces[0] == ip:
 
56
                    pass
 
57
                elif len(pieces):
 
58
                    n_entries.append((line_type, list(components)))
 
59
        self._contents = n_entries
 
60
 
 
61
    def add_entry(self, ip, canonical_hostname, *aliases):
 
62
        self.parse()
 
63
        self._contents.append(('option',
 
64
                              ([ip, canonical_hostname] + list(aliases), '')))
 
65
 
 
66
    def _parse(self, contents):
 
67
        entries = []
 
68
        for line in contents.splitlines():
 
69
            if not len(line.strip()):
 
70
                entries.append(('blank', [line]))
 
71
                continue
 
72
            (head, tail) = chop_comment(line.strip(), '#')
 
73
            if not len(head):
 
74
                entries.append(('all_comment', [line]))
 
75
                continue
 
76
            entries.append(('option', [head.split(None), tail]))
 
77
        return entries
 
78
 
 
79
    def __str__(self):
 
80
        self.parse()
 
81
        contents = StringIO()
 
82
        for (line_type, components) in self._contents:
 
83
            if line_type == 'blank':
 
84
                contents.write("%s\n" % (components[0]))
 
85
            elif line_type == 'all_comment':
 
86
                contents.write("%s\n" % (components[0]))
 
87
            elif line_type == 'option':
 
88
                (pieces, tail) = components
 
89
                pieces = [str(p) for p in pieces]
 
90
                pieces = "\t".join(pieces)
 
91
                contents.write("%s%s\n" % (pieces, tail))
 
92
        return contents.getvalue()