88
100
# Make the intermediate format as the rhel format...
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')
98
net_cfg['BOOTPROTO'] = boot_proto
99
net_mask = info.get('netmask')
101
net_cfg["NETMASK"] = net_mask
102
addr = info.get('address')
104
net_cfg["IPADDR"] = addr
106
net_cfg['ONBOOT'] = 'yes'
108
net_cfg['ONBOOT'] = 'no'
109
gtway = info.get('gateway')
111
net_cfg["GATEWAY"] = gtway
112
bcast = info.get('broadcast')
114
net_cfg["BROADCAST"] = bcast
115
mac_addr = info.get('hwaddress')
117
net_cfg["MACADDR"] = mac_addr
118
lines = net_cfg.write()
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')),
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'])
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)
125
'NETWORKING': _make_sysconfig_bool(True),
127
self._update_sysconfig_file("/etc/sysconfig/network", net_cfg)
130
def _update_sysconfig_file(self, fn, adjustments, allow_empty=False):
133
(exists, contents) = self._read_conf(fn)
135
for (k, v) in adjustments.items():
139
if len(v) == 0 and not allow_empty:
144
lines = contents.write()
146
lines.insert(0, _make_header())
147
util.write_file(fn, "\n".join(lines), 0644)
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])
139
154
def apply_locale(self, locale, out_fn=None):
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'
160
self._update_sysconfig_file(out_fn, locale_cfg)
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)
164
'HOSTNAME': hostname,
166
self._update_sysconfig_file(out_fn, host_cfg)
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:
168
180
self._write_hostname(hostname, fn)
195
207
return (exists, QuotingConfigObj(contents))
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)
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)
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")
212
228
def package_command(self, command, args=None):