~ubuntu-branches/ubuntu/quantal/cloud-init/quantal

« back to all changes in this revision

Viewing changes to cloudinit/distros/rhel.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2012-09-30 14:29:04 UTC
  • Revision ID: package-import@ubuntu.com-20120930142904-nq8fkve62i0xytqz
* add CloudStack to DataSources listed by dpkg-reconfigure (LP: #1002155)
* New upstream snapshot.
  * 0440 permissions on /etc/sudoers.d files rather than 0644
  * get host ssh keys to the console (LP: #1055688)
  * MAAS DataSource adjust timestamp in oauth header to one based on the
    timestamp in the response of a 403.  This accounts for a bad local
    clock. (LP: #978127)
  * re-start the salt daemon rather than start to ensure config changes
    are taken.
  * allow for python unicode types in yaml that is loaded.
  * cleanup in how config modules get at users and groups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from cloudinit import helpers
27
27
from cloudinit import log as logging
28
28
from cloudinit import util
 
29
from cloudinit import version
29
30
 
30
31
from cloudinit.settings import PER_INSTANCE
31
32
 
56
57
}
57
58
 
58
59
 
 
60
def _make_sysconfig_bool(val):
 
61
    if val:
 
62
        return 'yes'
 
63
    else:
 
64
        return 'no'
 
65
 
 
66
 
 
67
def _make_header():
 
68
    ci_ver = version.version_string()
 
69
    return '# Created by cloud-init v. %s' % (ci_ver)
 
70
 
 
71
 
59
72
class Distro(distros.Distro):
60
73
 
61
74
    def __init__(self, name, cfg, paths):
76
89
        if search_servers:
77
90
            contents.append("search %s" % (" ".join(search_servers)))
78
91
        if contents:
79
 
            resolve_rw_fn = self._paths.join(False, "/etc/resolv.conf")
80
 
            contents.insert(0, '# Created by cloud-init')
81
 
            util.write_file(resolve_rw_fn, "\n".join(contents), 0644)
 
92
            contents.insert(0, _make_header())
 
93
            util.write_file("/etc/resolv.conf", "\n".join(contents), 0644)
82
94
 
83
95
    def _write_network(self, settings):
84
96
        # TODO(harlowja) fix this... since this is the ubuntu format
88
100
        # Make the intermediate format as the rhel format...
89
101
        nameservers = []
90
102
        searchservers = []
 
103
        dev_names = entries.keys()
91
104
        for (dev, info) in entries.iteritems():
92
105
            net_fn = NETWORK_FN_TPL % (dev)
93
 
            net_ro_fn = self._paths.join(True, net_fn)
94
 
            (prev_exist, net_cfg) = self._read_conf(net_ro_fn)
95
 
            net_cfg['DEVICE'] = dev
96
 
            boot_proto = info.get('bootproto')
97
 
            if boot_proto:
98
 
                net_cfg['BOOTPROTO'] = boot_proto
99
 
            net_mask = info.get('netmask')
100
 
            if net_mask:
101
 
                net_cfg["NETMASK"] = net_mask
102
 
            addr = info.get('address')
103
 
            if addr:
104
 
                net_cfg["IPADDR"] = addr
105
 
            if info.get('auto'):
106
 
                net_cfg['ONBOOT'] = 'yes'
107
 
            else:
108
 
                net_cfg['ONBOOT'] = 'no'
109
 
            gtway = info.get('gateway')
110
 
            if gtway:
111
 
                net_cfg["GATEWAY"] = gtway
112
 
            bcast = info.get('broadcast')
113
 
            if bcast:
114
 
                net_cfg["BROADCAST"] = bcast
115
 
            mac_addr = info.get('hwaddress')
116
 
            if mac_addr:
117
 
                net_cfg["MACADDR"] = mac_addr
118
 
            lines = net_cfg.write()
 
106
            net_cfg = {
 
107
                'DEVICE': dev,
 
108
                'NETMASK': info.get('netmask'),
 
109
                'IPADDR': info.get('address'),
 
110
                'BOOTPROTO': info.get('bootproto'),
 
111
                'GATEWAY': info.get('gateway'),
 
112
                'BROADCAST': info.get('broadcast'),
 
113
                'MACADDR': info.get('hwaddress'),
 
114
                'ONBOOT': _make_sysconfig_bool(info.get('auto')),
 
115
            }
 
116
            self._update_sysconfig_file(net_fn, net_cfg)
119
117
            if 'dns-nameservers' in info:
120
118
                nameservers.extend(info['dns-nameservers'])
121
119
            if 'dns-search' in info:
122
120
                searchservers.extend(info['dns-search'])
123
 
            if not prev_exist:
124
 
                lines.insert(0, '# Created by cloud-init')
125
 
            w_contents = "\n".join(lines)
126
 
            net_rw_fn = self._paths.join(False, net_fn)
127
 
            util.write_file(net_rw_fn, w_contents, 0644)
128
121
        if nameservers or searchservers:
129
122
            self._write_resolve(nameservers, searchservers)
 
123
        if dev_names:
 
124
            net_cfg = {
 
125
                'NETWORKING': _make_sysconfig_bool(True),
 
126
            }
 
127
            self._update_sysconfig_file("/etc/sysconfig/network", net_cfg)
 
128
        return dev_names
 
129
 
 
130
    def _update_sysconfig_file(self, fn, adjustments, allow_empty=False):
 
131
        if not adjustments:
 
132
            return
 
133
        (exists, contents) = self._read_conf(fn)
 
134
        updated_am = 0
 
135
        for (k, v) in adjustments.items():
 
136
            if v is None:
 
137
                continue
 
138
            v = str(v)
 
139
            if len(v) == 0 and not allow_empty:
 
140
                continue
 
141
            contents[k] = v
 
142
            updated_am += 1
 
143
        if updated_am:
 
144
            lines = contents.write()
 
145
            if not exists:
 
146
                lines.insert(0, _make_header())
 
147
            util.write_file(fn, "\n".join(lines), 0644)
130
148
 
131
149
    def set_hostname(self, hostname):
132
 
        out_fn = self._paths.join(False, '/etc/sysconfig/network')
133
 
        self._write_hostname(hostname, out_fn)
134
 
        if out_fn == '/etc/sysconfig/network':
135
 
            # Only do this if we are running in non-adjusted root mode
136
 
            LOG.debug("Setting hostname to %s", hostname)
137
 
            util.subp(['hostname', hostname])
 
150
        self._write_hostname(hostname, '/etc/sysconfig/network')
 
151
        LOG.debug("Setting hostname to %s", hostname)
 
152
        util.subp(['hostname', hostname])
138
153
 
139
154
    def apply_locale(self, locale, out_fn=None):
140
155
        if not out_fn:
141
 
            out_fn = self._paths.join(False, '/etc/sysconfig/i18n')
142
 
        ro_fn = self._paths.join(True, '/etc/sysconfig/i18n')
143
 
        (_exists, contents) = self._read_conf(ro_fn)
144
 
        contents['LANG'] = locale
145
 
        w_contents = "\n".join(contents.write())
146
 
        util.write_file(out_fn, w_contents, 0644)
 
156
            out_fn = '/etc/sysconfig/i18n'
 
157
        locale_cfg = {
 
158
            'LANG': locale,
 
159
        }
 
160
        self._update_sysconfig_file(out_fn, locale_cfg)
147
161
 
148
162
    def _write_hostname(self, hostname, out_fn):
149
 
        (_exists, contents) = self._read_conf(out_fn)
150
 
        contents['HOSTNAME'] = hostname
151
 
        w_contents = "\n".join(contents.write())
152
 
        util.write_file(out_fn, w_contents, 0644)
 
163
        host_cfg = {
 
164
            'HOSTNAME': hostname,
 
165
        }
 
166
        self._update_sysconfig_file(out_fn, host_cfg)
153
167
 
154
168
    def update_hostname(self, hostname, prev_file):
155
169
        hostname_prev = self._read_hostname(prev_file)
156
 
        read_fn = self._paths.join(True, "/etc/sysconfig/network")
157
 
        hostname_in_sys = self._read_hostname(read_fn)
 
170
        hostname_in_sys = self._read_hostname("/etc/sysconfig/network")
158
171
        update_files = []
159
172
        if not hostname_prev or hostname_prev != hostname:
160
173
            update_files.append(prev_file)
161
174
        if (not hostname_in_sys or
162
175
            (hostname_in_sys == hostname_prev
163
176
             and hostname_in_sys != hostname)):
164
 
            write_fn = self._paths.join(False, "/etc/sysconfig/network")
165
 
            update_files.append(write_fn)
 
177
            update_files.append("/etc/sysconfig/network")
166
178
        for fn in update_files:
167
179
            try:
168
180
                self._write_hostname(hostname, fn)
194
206
            contents = []
195
207
        return (exists, QuotingConfigObj(contents))
196
208
 
 
209
    def _bring_up_interfaces(self, device_names):
 
210
        if device_names and 'all' in device_names:
 
211
            raise RuntimeError(('Distro %s can not translate '
 
212
                                'the device name "all"') % (self.name))
 
213
        return distros.Distro._bring_up_interfaces(self, device_names)
 
214
 
197
215
    def set_timezone(self, tz):
198
216
        tz_file = os.path.join("/usr/share/zoneinfo", tz)
199
217
        if not os.path.isfile(tz_file):
200
218
            raise RuntimeError(("Invalid timezone %s,"
201
219
                                " no file found at %s") % (tz, tz_file))
202
220
        # Adjust the sysconfig clock zone setting
203
 
        read_fn = self._paths.join(True, "/etc/sysconfig/clock")
204
 
        (_exists, contents) = self._read_conf(read_fn)
205
 
        contents['ZONE'] = tz
206
 
        tz_contents = "\n".join(contents.write())
207
 
        write_fn = self._paths.join(False, "/etc/sysconfig/clock")
208
 
        util.write_file(write_fn, tz_contents)
 
221
        clock_cfg = {
 
222
            'ZONE': tz,
 
223
        }
 
224
        self._update_sysconfig_file("/etc/sysconfig/clock", clock_cfg)
209
225
        # This ensures that the correct tz will be used for the system
210
 
        util.copy(tz_file, self._paths.join(False, "/etc/localtime"))
 
226
        util.copy(tz_file, "/etc/localtime")
211
227
 
212
228
    def package_command(self, command, args=None):
213
229
        cmd = ['yum']