From 9945a732f71d0363369fe4c5d318b2cf1b6db8f4 Mon Sep 17 00:00:00 2001 From: Johann Schmitz Date: Sat, 23 Aug 2014 09:00:28 +0200 Subject: [PATCH] Hostname strategy implementation for OpenRC based systems (e.g. Gentoo) --- library/system/hostname | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/library/system/hostname b/library/system/hostname index f077cc877c..50eaec12ff 100755 --- a/library/system/hostname +++ b/library/system/hostname @@ -288,6 +288,44 @@ class FedoraStrategy(GenericStrategy): # =========================================== +class OpenRCStrategy(GenericStrategy): + """ + This is a Gentoo (OpenRC) Hostname manipulation strategy class - it edits + the /etc/conf.d/hostname file. + """ + + HOSTNAME_FILE = '/etc/conf.d/hostname' + + def get_permanent_hostname(self): + try: + with open(self.HOSTNAME_FILE, 'r') as f: + for line in f: + line = line.strip() + if line.startswith('hostname='): + return line[10:].strip('"') + return None + except Exception, err: + self.module.fail_json(msg="failed to read hostname: %s" % + str(err)) + + def set_permanent_hostname(self, name): + try: + with open(self.HOSTNAME_FILE, 'r') as f: + lines = [x.strip() for x in f] + + for i, line in enumerate(lines): + if line.startswith('hostname='): + lines[i] = 'hostname="%s"' % name + break + + with open(self.HOSTNAME_FILE, 'w') as f: + f.write('\n'.join(lines) + '\n') + except Exception, err: + self.module.fail_json(msg="failed to update hostname: %s" % + str(err)) + +# =========================================== + class FedoraHostname(Hostname): platform = 'Linux' distribution = 'Fedora' @@ -366,6 +404,11 @@ class LinaroHostname(Hostname): distribution = 'Linaro' strategy_class = DebianStrategy +class GentooHostname(Hostname): + platform = 'Linux' + distribution = 'Gentoo base system' + strategy_class = OpenRCStrategy + # =========================================== def main():