summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Ipsum <richard.ipsum@codethink.co.uk>2015-03-14 17:31:12 +0000
committerRichard Ipsum <richard.ipsum@codethink.co.uk>2015-03-18 16:08:12 +0000
commitf682482396bcbdded7a3a342c68d792dd4bd791f (patch)
treec80be54185fc9cddb06b5eba90eeccd7de6c222d
parentc425d9678c39175b9dc65d34b02ee0397a384258 (diff)
downloadmorph-f682482396bcbdded7a3a342c68d792dd4bd791f.tar.gz
Add hosts.configure
This adds a new config extension to allow deployments to write to /etc/hosts by adding HOSTS_x: </etc/hosts line> to a cluster morph in a similar manner to the fstab.configure extension.
-rwxr-xr-xmorphlib/exts/hosts.configure48
1 files changed, 48 insertions, 0 deletions
diff --git a/morphlib/exts/hosts.configure b/morphlib/exts/hosts.configure
new file mode 100755
index 00000000..6b068d04
--- /dev/null
+++ b/morphlib/exts/hosts.configure
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright © 2015 Codethink Limited
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# =*= License: GPL-2 =*=
+
+
+import os
+import sys
+import socket
+
+import morphlib
+
+def validate(var, line):
+ xs = line.split()
+ if len(xs) == 0:
+ raise morphlib.Error("`%s: %s': line is empty" % (var, line))
+
+ ip = xs[0]
+ hostnames = xs[1:]
+
+ if len(hostnames) == 0:
+ raise morphlib.Error("`%s: %s': missing hostname" % (var, line))
+
+ family = socket.AF_INET6 if ':' in ip else socket.AF_INET
+
+ try:
+ socket.inet_pton(family, ip)
+ except socket.error:
+ raise morphlib.Error("`%s: %s' invalid ip" % (var, ip))
+
+envvars = {k: v for (k, v) in os.environ.iteritems() if k.startswith('HOSTS_')}
+
+conf_file = os.path.join(sys.argv[1], 'etc/hosts')
+morphlib.util.write_from_dict(conf_file, envvars, validate)