From 3c23b137553cc676568871aaff425456cda51fdd Mon Sep 17 00:00:00 2001 From: smbaker Date: Mon, 14 Jan 2013 12:59:14 -0800 Subject: [PATCH] generate /etc/hosts for private networks --- plugins/hostmap.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 plugins/hostmap.py diff --git a/plugins/hostmap.py b/plugins/hostmap.py new file mode 100644 index 0000000..3d030e6 --- /dev/null +++ b/plugins/hostmap.py @@ -0,0 +1,87 @@ +""" +Configure interfaces inside a container by pulling down files via URL. +""" + +import logger +import os +import curlwrapper +import re +import xmlrpclib +try: + from hashlib import sha1 as sha +except ImportError: + from sha import sha +import subprocess + +def checksum(path): + try: + f = open(path) + try: return sha(f.read()).digest() + finally: f.close() + except IOError: + return None + +def start(): + logger.log("interfaces: plugin starting up...") + +PREFIX = "# ----- This section added by nodemanager hostmap module. Do not edit. -----" +SUFFIX = "# ----- End -----" + +def GetSlivers(data, config=None, plc=None): + + if 'slivers' not in data: + logger.log_missing_data("hostmap.GetSlivers",'slivers') + return + + if 'hostname' not in data: + logger.log_missing_data("hostmap.GetSlivers", 'hostname') + + hostname = data['hostname'] + + for sliver in data['slivers']: + slicename = sliver['name'] + for tag in sliver['attributes']: + if tag['tagname'] == 'slice_hostmap': + fn = "/vservers/%s/etc/hosts" % slicename + if not os.path.exists(fn): + continue + + contents = file(fn,"r").read() + + hostmap = [] + for index, entry in enumerate(tag["value"].split("\n")): + parts = entry.split(" ") + if len(parts)==2: + if parts[1] == hostname: + line = "127.0.0.1 %s.%s private%d" % (slicename, parts[1], index) + else: + line = "%s %s.%s private%d" % (parts[0], slicename, parts[1], index) + + if (index==0): + line = line + " headnode" + + hostmap.append(line) + + hostmap = "\n".join(hostmap) + hostmap = PREFIX + "\n" + hostmap + "\n" + SUFFIX + "\n" + + if (hostmap in contents): + # it's already there + continue + + # remove anything between PREFIX and SUFFIX from contents + + pattern = PREFIX + ".*" + SUFFIX + regex = re.compile(pattern, re.DOTALL) + if regex.search(contents) != None: + contents = regex.sub(hostmap, contents) + else: + contents = contents + hostmap + + try: + file(fn, "w").write(contents) + except: + logger.log_exc("hostmap (%s): failed to write %s" % (slicename, fn)) + + + -- 2.43.0