112
115
def _get_localhost_ip(self):
113
116
return "127.0.0.1"
119
def _read_hostname(self, filename, default=None):
120
raise NotImplementedError()
123
def _write_hostname(self, hostname, filename):
124
raise NotImplementedError()
127
def _read_system_hostname(self):
128
raise NotImplementedError()
130
def _apply_hostname(self, hostname):
131
# This really only sets the hostname
132
# temporarily (until reboot so it should
133
# not be depended on). Use the write
134
# hostname functions for 'permanent' adjustments.
135
LOG.debug("Non-persistently setting the system hostname to %s",
138
util.subp(['hostname', hostname])
139
except util.ProcessExecutionError:
140
util.logexc(LOG, ("Failed to non-persistently adjust"
141
" the system hostname to %s"), hostname)
144
def _select_hostname(self, hostname, fqdn):
145
raise NotImplementedError()
147
def update_hostname(self, hostname, fqdn, prev_hostname_fn):
148
applying_hostname = hostname
149
hostname = self._select_hostname(hostname, fqdn)
150
prev_hostname = self._read_hostname(prev_hostname_fn)
151
(sys_fn, sys_hostname) = self._read_system_hostname()
153
if not prev_hostname or prev_hostname != hostname:
154
update_files.append(prev_hostname_fn)
156
if (not sys_hostname) or (sys_hostname == prev_hostname
157
and sys_hostname != hostname):
158
update_files.append(sys_fn)
160
update_files = set([f for f in update_files if f])
161
LOG.debug("Attempting to update hostname to %s in %s files",
162
hostname, len(update_files))
164
for fn in update_files:
166
self._write_hostname(hostname, fn)
168
util.logexc(LOG, "Failed to write hostname %s to %s",
171
if (sys_hostname and prev_hostname and
172
sys_hostname != prev_hostname):
173
LOG.debug("%s differs from %s, assuming user maintained hostname.",
174
prev_hostname_fn, sys_fn)
176
if sys_fn in update_files:
177
self._apply_hostname(applying_hostname)
115
179
def update_etc_hosts(self, hostname, fqdn):
117
# http://unixhelp.ed.ac.uk/CGI/man-cgi?hosts
118
header = "# Added by cloud-init"
119
real_header = "%s on %s" % (header, util.time_rfc2822())
181
if os.path.exists(self.hosts_fn):
182
eh = hosts.HostsConf(util.load_file(self.hosts_fn))
184
eh = hosts.HostsConf('')
185
header = util.make_header(base="added")
120
186
local_ip = self._get_localhost_ip()
121
hosts_line = "%s\t%s %s" % (local_ip, fqdn, hostname)
122
new_etchosts = StringIO()
125
for line in util.load_file("/etc/hosts").splitlines():
126
if line.strip().startswith(header):
128
if not line.strip() or line.strip().startswith("#"):
129
new_etchosts.write("%s\n" % (line))
131
split_line = [s.strip() for s in line.split()]
132
if len(split_line) < 2:
133
new_etchosts.write("%s\n" % (line))
135
(ip, hosts) = split_line[0], split_line[1:]
137
if sorted([hostname, fqdn]) == sorted(hosts):
140
line = "%s\n%s" % (real_header, hosts_line)
143
new_etchosts.write("%s\n" % (line))
187
prev_info = eh.get_entry(local_ip)
190
eh.add_entry(local_ip, fqdn, hostname)
194
for entry in prev_info:
198
entry_fqdn = entry[0]
200
entry_aliases = entry[1:]
201
if entry_fqdn is not None and entry_fqdn == fqdn:
202
if hostname in entry_aliases:
203
# Exists already, leave it be
206
# Doesn't exist, add that entry in...
207
new_entries = list(prev_info)
208
new_entries.append([fqdn, hostname])
209
eh.del_entries(local_ip)
210
for entry in new_entries:
212
eh.add_entry(local_ip, entry[0])
213
elif len(entry) >= 2:
214
eh.add_entry(local_ip, *entry)
145
new_etchosts.write("%s\n%s\n" % (real_header, hosts_line))
148
contents = new_etchosts.getvalue()
149
util.write_file("/etc/hosts", contents, mode=0644)
216
contents = StringIO()
218
contents.write("%s\n" % (header))
219
contents.write("%s\n" % (eh))
220
util.write_file(self.hosts_fn, contents.getvalue(), mode=0644)
151
222
def _bring_up_interface(self, device_name):
152
223
cmd = ['ifup', device_name]
323
394
def write_sudo_rules(self, user, rules, sudo_file=None):
324
395
if not sudo_file:
325
sudo_file = "/etc/sudoers.d/90-cloud-init-users"
327
content_header = "# user rules for %s" % user
328
content = "%s\n%s %s\n\n" % (content_header, user, rules)
330
if isinstance(rules, list):
331
content = "%s\n" % content_header
396
sudo_file = self.ci_sudoers_fn
400
"# User rules for %s" % user,
402
if isinstance(rules, collections.Iterable):
332
403
for rule in rules:
333
content += "%s %s\n" % (user, rule)
404
lines.append("%s %s" % (user, rule))
406
lines.append("%s %s" % (user, rules))
407
content = "\n".join(lines)
336
409
self.ensure_sudo_dir(os.path.dirname(sudo_file))
338
410
if not os.path.exists(sudo_file):
339
util.write_file(sudo_file, content, 0440)
416
util.write_file(sudo_file, "\n".join(contents), 0440)
418
util.logexc(LOG, "Failed to write sudoers file %s", sudo_file)
342
422
util.append_file(sudo_file, content)
343
423
except IOError as e:
344
util.logexc(LOG, "Failed to write %s" % sudo_file, e)
424
util.logexc(LOG, "Failed to append sudoers file %s", sudo_file)
347
427
def create_group(self, name, members):