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

« back to all changes in this revision

Viewing changes to cloudinit/distros/parsers/hostname.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
# Parser that knows how to work with /etc/hostname format
 
25
class HostnameConf(object):
 
26
    def __init__(self, text):
 
27
        self._text = text
 
28
        self._contents = None
 
29
 
 
30
    def parse(self):
 
31
        if self._contents is None:
 
32
            self._contents = self._parse(self._text)
 
33
 
 
34
    def __str__(self):
 
35
        self.parse()
 
36
        contents = StringIO()
 
37
        for (line_type, components) in self._contents:
 
38
            if line_type == 'blank':
 
39
                contents.write("%s\n" % (components[0]))
 
40
            elif line_type == 'all_comment':
 
41
                contents.write("%s\n" % (components[0]))
 
42
            elif line_type == 'hostname':
 
43
                (hostname, tail) = components
 
44
                contents.write("%s%s\n" % (hostname, tail))
 
45
        # Ensure trailing newline
 
46
        contents = contents.getvalue()
 
47
        if not contents.endswith("\n"):
 
48
            contents += "\n"
 
49
        return contents
 
50
 
 
51
    @property
 
52
    def hostname(self):
 
53
        self.parse()
 
54
        for (line_type, components) in self._contents:
 
55
            if line_type == 'hostname':
 
56
                return components[0]
 
57
        return None
 
58
 
 
59
    def set_hostname(self, your_hostname):
 
60
        your_hostname = your_hostname.strip()
 
61
        if not your_hostname:
 
62
            return
 
63
        self.parse()
 
64
        replaced = False
 
65
        for (line_type, components) in self._contents:
 
66
            if line_type == 'hostname':
 
67
                components[0] = str(your_hostname)
 
68
                replaced = True
 
69
        if not replaced:
 
70
            self._contents.append(('hostname', [str(your_hostname), '']))
 
71
 
 
72
    def _parse(self, contents):
 
73
        entries = []
 
74
        hostnames_found = set()
 
75
        for line in contents.splitlines():
 
76
            if not len(line.strip()):
 
77
                entries.append(('blank', [line]))
 
78
                continue
 
79
            (head, tail) = chop_comment(line.strip(), '#')
 
80
            if not len(head):
 
81
                entries.append(('all_comment', [line]))
 
82
                continue
 
83
            entries.append(('hostname', [head, tail]))
 
84
            hostnames_found.add(head)
 
85
        if len(hostnames_found) > 1:
 
86
            raise IOError("Multiple hostnames (%s) found!"
 
87
                           % (hostnames_found))
 
88
        return entries