2 Update /etc/hosts in slivers to contain the contents of the sliver_hostmap tag.
11 from hashlib import sha1 as sha
19 try: return sha(f.read()).digest()
25 logger.log("interfaces: plugin starting up...")
27 PREFIX = "# ----- This section added by nodemanager hostmap module. Do not edit. -----"
28 SUFFIX = "# ----- End -----"
30 def GetSlivers(data, config=None, plc=None):
32 if 'slivers' not in data:
33 logger.log_missing_data("hostmap.GetSlivers",'slivers')
36 if 'hostname' not in data:
37 logger.log_missing_data("hostmap.GetSlivers", 'hostname')
39 hostname = data['hostname']
41 hostname_filter = ".".join(hostname.split(".")[1:])
43 for sliver in data['slivers']:
44 slicename = sliver['name']
45 for tag in sliver['attributes']:
46 if tag['tagname'] == 'slice_hostmap':
47 fn = "/vservers/%s/etc/hosts" % slicename
48 if not os.path.exists(fn):
51 contents = file(fn,"r").read()
54 for index, entry in enumerate(tag["value"].split("\n")):
55 parts = entry.split(" ")
57 line = "%s pvt.%s private%d" % (parts[0], parts[1], index)
59 if (parts[0].startswith("10.")) and (hostname_filter not in parts[1]):
63 line = line + " headnode"
65 if parts[1] == hostname:
66 line = line + " pvt.self"
70 hostmap = "\n".join(hostmap)
71 hostmap = PREFIX + "\n" + hostmap + "\n" + SUFFIX + "\n"
73 if (hostmap in contents):
77 # remove anything between PREFIX and SUFFIX from contents
79 pattern = PREFIX + ".*" + SUFFIX + "\n"
80 regex = re.compile(pattern, re.DOTALL)
81 if regex.search(contents) != None:
82 contents = regex.sub(hostmap, contents)
84 contents = contents + hostmap
87 file(fn, "w").write(contents)
89 logger.log_exc("hostmap (%s): failed to write %s" % (slicename, fn))