summaryrefslogtreecommitdiff
path: root/cloudinit/distros/freebsd.py
diff options
context:
space:
mode:
authorHarm Weites <harm@weites.com>2013-12-14 18:58:31 +0000
committerHarm Weites <harm@weites.com>2013-12-14 18:58:31 +0000
commit45a3ef2157c21155a7d0a286849db330e767608d (patch)
tree919be64e6002631bcf45b63b9b23d08dcc1a9523 /cloudinit/distros/freebsd.py
parentddec564518c9aeb8bfaac713956ea0baf460a8ce (diff)
downloadvyos-cloud-init-45a3ef2157c21155a7d0a286849db330e767608d.tar.gz
vyos-cloud-init-45a3ef2157c21155a7d0a286849db330e767608d.zip
change: Separate functions to load, read and write /etc/rc.conf keys and
values. Use these right away to read and change the hostname.
Diffstat (limited to 'cloudinit/distros/freebsd.py')
-rw-r--r--cloudinit/distros/freebsd.py54
1 files changed, 37 insertions, 17 deletions
diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py
index 2d6cd924..55efbb7b 100644
--- a/cloudinit/distros/freebsd.py
+++ b/cloudinit/distros/freebsd.py
@@ -40,33 +40,53 @@ class Distro(distros.Distro):
self._runner = helpers.Runners(paths)
self.osfamily = 'freebsd'
+ # Updates a key in /etc/rc.conf.
def updatercconf(self, key, value):
LOG.debug("updatercconf: %s => %s" % (key, value))
- conf = {}
+ conf = self.loadrcconf()
configchanged = False
- with open("/etc/rc.conf") as file:
- for line in file:
- tok = line.split('=')
- # TODO: Handle keys with spaces, make this a bit more robust.
- if tok[0] == key:
- if tok[1] != value:
- conf[tok[0]] = value
- LOG.debug("[rc.conf]: Value %s for key %s needs to be changed" % (value, key))
- configchanged = True
- else:
- conf[tok[0]] = tok[1].rstrip()
+ for item in conf:
+ if item == key and conf[item] != value:
+ conf[item] = value
+ LOG.debug("[rc.conf]: Value %s for key %s needs to be changed" % (value, key))
+ configchanged = True
if configchanged:
LOG.debug("Writing new /etc/rc.conf file")
- with open ('/etc/rc.conf', 'w') as file:
+ with open('/etc/rc.conf', 'w') as file:
for keyval in conf.items():
file.write("%s=%s\n" % keyval)
- def _read_hostname():
- return
+ # Load the contents of /etc/rc.conf and store all keys in a dict.
+ def loadrcconf(self):
+ conf = {}
+ with open("/etc/rc.conf") as file:
+ for line in file:
+ tok = line.split('=')
+ conf[tok[0]] = tok[1].rstrip()
+ return conf
- def _read_system_hostname():
- return
+ def readrcconf(self, key):
+ conf = self.loadrcconf()
+ try:
+ val = conf[key]
+ except KeyError:
+ val = None
+ return val
+
+ def _read_system_hostname(self):
+ sys_hostname = self._read_hostname()
+ return ('rc.conf', sys_hostname)
+
+ def _read_hostname(self, default=None):
+ hostname = None
+ try:
+ hostname = self.readrcconf('hostname')
+ except IOError:
+ pass
+ if not hostname:
+ return default
+ return hostname
def _select_hostname(self, hostname, fqdn):
if not hostname: