summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/distros/__init__.py6
-rw-r--r--cloudinit/distros/rhel.py32
-rw-r--r--cloudinit/distros/ubuntu.py35
3 files changed, 42 insertions, 31 deletions
diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py
index 483642f3..6a98fdb1 100644
--- a/cloudinit/distros/__init__.py
+++ b/cloudinit/distros/__init__.py
@@ -100,7 +100,8 @@ class Distro(object):
new_etchosts = StringIO()
need_write = False
need_change = True
- for line in util.load_file("/etc/hosts").splitlines():
+ hosts_ro_fn = self._paths.join(True, "/etc/hosts")
+ for line in util.load_file(hosts_ro_fn).splitlines():
if line.strip().startswith(header):
continue
if not line.strip() or line.strip().startswith("#"):
@@ -124,7 +125,8 @@ class Distro(object):
need_write = True
if need_write:
contents = new_etchosts.getvalue()
- util.write_file("/etc/hosts", contents, mode=0644)
+ util.write_file(self._paths.join(False, "/etc/hosts"),
+ contents, mode=0644)
def _interface_action(self, action):
if action not in IFACE_ACTIONS:
diff --git a/cloudinit/distros/rhel.py b/cloudinit/distros/rhel.py
index aef7f6f3..e9f3f5d9 100644
--- a/cloudinit/distros/rhel.py
+++ b/cloudinit/distros/rhel.py
@@ -73,13 +73,15 @@ class Distro(distros.Distro):
lines.insert(0, '# Created by cloud-init')
contents = "\n".join(lines)
net_fn = NETWORK_FN_TPL % (dev)
- net_fn = self._paths.join(False, net_fn)
- util.write_file(net_fn, contents, 0644)
+ util.write_file(self._paths.join(False, net_fn), contents, 0644)
def set_hostname(self, hostname):
- self._write_hostname(hostname, "/etc/sysconfig/network")
- LOG.debug("Setting hostname to %s", hostname)
- util.subp(['hostname', hostname])
+ out_fn = self._paths.join(False, '/etc/sysconfig/network')
+ self._write_hostname(hostname, out_fn)
+ if out_fn == '/etc/sysconfig/network':
+ # Only do this if we are running in non-adjusted root mode
+ LOG.debug("Setting hostname to %s", hostname)
+ util.subp(['hostname', hostname])
def _write_hostname(self, hostname, out_fn):
old_contents = []
@@ -105,18 +107,19 @@ class Distro(distros.Distro):
new_contents.append("# Added by cloud-init")
new_contents.append("HOSTNAME=%s" % (hostname))
contents = "\n".join(new_contents)
- out_fn = self._paths.join(False, out_fn)
util.write_file(out_fn, contents, 0644)
def update_hostname(self, hostname, prev_file):
hostname_prev = self._read_hostname(prev_file)
- hostname_in_sys = self._read_hostname("/etc/sysconfig/network")
+ read_fn = self._paths.join(True, "/etc/sysconfig/network")
+ hostname_in_sys = self._read_hostname(read_fn)
update_files = []
if not hostname_prev or hostname_prev != hostname:
update_files.append(prev_file)
if (not hostname_in_sys or
- (hostname_in_sys == hostname_prev and hostname_in_sys != hostname)):
- update_files.append("/etc/sysconfig/network")
+ (hostname_in_sys == hostname_prev and hostname_in_sys != hostname)):
+ write_fn = self._paths.join(False, "/etc/sysconfig/network")
+ update_files.append(write_fn)
for fn in update_files:
try:
self._write_hostname(hostname, fn)
@@ -128,6 +131,7 @@ class Distro(distros.Distro):
LOG.debug(("%s differs from /etc/sysconfig/network."
" Assuming user maintained hostname."), prev_file)
if "/etc/sysconfig/network" in update_files:
+ # Only do this if we are running in non-adjusted root mode
LOG.debug("Setting hostname to %s", hostname)
util.subp(['hostname', hostname])
@@ -145,7 +149,6 @@ class Distro(distros.Distro):
return default
def _read_conf(self, filename):
- filename = self._paths.join(True, filename)
contents = util.load_file(filename, quiet=True)
conf_lines = []
for line in contents.splitlines():
@@ -176,7 +179,8 @@ class Distro(distros.Distro):
raise Exception(("Invalid timezone %s,"
" no file found at %s") % (tz, tz_file))
# Adjust the sysconfig clock zone setting
- old_contents = self._read_conf("/etc/sysconfig/clock")
+ read_fn = self._paths.join(True, "/etc/sysconfig/clock")
+ old_contents = self._read_conf(read_fn)
new_contents = []
zone_added = False
# Update the 'ZONE' if it exists instead of appending
@@ -197,10 +201,10 @@ class Distro(distros.Distro):
new_contents.append("# Added by cloud-init")
new_contents.append('ZONE="%s"' % (tz))
tz_contents = "\n".join(new_contents)
- tz_fn = self._paths.join(False, "/etc/sysconfig/clock")
- util.write_file(tz_fn, tz_contents)
+ write_fn = self._paths.join(False, "/etc/sysconfig/clock")
+ util.write_file(write_fn, tz_contents)
# This ensures that the correct tz will be used for the system
- util.copy(tz_file, "/etc/localtime")
+ util.copy(tz_file, self._paths.join(False, "/etc/localtime"))
def package_command(self, command, args=None):
cmd = ['yum']
diff --git a/cloudinit/distros/ubuntu.py b/cloudinit/distros/ubuntu.py
index eeda2921..515b59c8 100644
--- a/cloudinit/distros/ubuntu.py
+++ b/cloudinit/distros/ubuntu.py
@@ -46,30 +46,35 @@ class Distro(distros.Distro):
self.package_command('install', pkglist)
def _write_network(self, settings):
- n_fn = self._paths.join(False, "/etc/network/interfaces")
- util.write_file(n_fn, settings)
+ net_fn = self._paths.join(False, "/etc/network/interfaces")
+ util.write_file(net_fn, settings)
def set_hostname(self, hostname):
- self._write_hostname(hostname, "/etc/hostname")
- LOG.debug("Setting hostname to %s", hostname)
- util.subp(['hostname', hostname])
+ out_fn = self._paths.join(False, "/etc/hostname")
+ self._write_hostname(hostname, out_fn)
+ if out_fn == '/etc/hostname':
+ # Only do this if we are running in non-adjusted root mode
+ LOG.debug("Setting hostname to %s", hostname)
+ util.subp(['hostname', hostname])
def _write_hostname(self, hostname, out_fn):
lines = []
lines.append("# Created by cloud-init")
lines.append(str(hostname))
contents = "\n".join(lines)
- util.write_file(self._paths.join(False, out_fn), contents, 0644)
+ util.write_file(out_fn, contents, 0644)
- def update_hostname(self, hostname, prev_file):
- hostname_prev = self._read_hostname(prev_file)
- hostname_in_etc = self._read_hostname("/etc/hostname")
+ def update_hostname(self, hostname, prev_fn):
+ hostname_prev = self._read_hostname(prev_fn)
+ read_fn = self._paths.join(True, "/etc/hostname")
+ hostname_in_etc = self._read_hostname(read_fn)
update_files = []
if not hostname_prev or hostname_prev != hostname:
- update_files.append(prev_file)
+ update_files.append(prev_fn)
if (not hostname_in_etc or
- (hostname_in_etc == hostname_prev and hostname_in_etc != hostname)):
- update_files.append("/etc/hostname")
+ (hostname_in_etc == hostname_prev and hostname_in_etc != hostname)):
+ write_fn = self._paths.join(False, "/etc/hostname")
+ update_files.append(write_fn)
for fn in update_files:
try:
self._write_hostname(hostname, fn)
@@ -79,13 +84,13 @@ class Distro(distros.Distro):
if (hostname_in_etc and hostname_prev and
hostname_in_etc != hostname_prev):
LOG.debug(("%s differs from /etc/hostname."
- " Assuming user maintained hostname."), prev_file)
+ " Assuming user maintained hostname."), prev_fn)
if "/etc/hostname" in update_files:
+ # Only do this if we are running in non-adjusted root mode
LOG.debug("Setting hostname to %s", hostname)
util.subp(['hostname', hostname])
def _read_hostname(self, filename, default=None):
- filename = self._paths.join(True, filename)
contents = util.load_file(filename, quiet=True)
for line in contents.splitlines():
c_pos = line.find("#")
@@ -109,7 +114,7 @@ class Distro(distros.Distro):
tz_contents = "%s\n" % tz
tz_fn = self._paths.join(False, "/etc/timezone")
util.write_file(tz_fn, tz_contents)
- util.copy(tz_file, "/etc/localtime")
+ util.copy(tz_file, self._paths.join(False, "/etc/localtime"))
def package_command(self, command, args=None):
e = os.environ.copy()