8bd1c089a7fdf0732b7a21ff25692539763a6b76
[nodemanager.git] / plugins / hostmap.py
1 """
2 Update /etc/hosts in slivers to contain the contents of the sliver_hostmap tag.
3 """
4
5 import logger
6 import os
7 import curlwrapper
8 import re
9 import xmlrpclib
10 try:
11     from hashlib import sha1 as sha
12 except ImportError:
13     from sha import sha
14 import subprocess
15
16 def checksum(path):
17     try:
18         f = open(path)
19         try: return sha(f.read()).digest()
20         finally: f.close()
21     except IOError: 
22         return None
23
24 def start():
25     logger.log("interfaces: plugin starting up...")
26
27 PREFIX = "# ----- This section added by nodemanager hostmap module. Do not edit. -----"
28 SUFFIX = "# ----- End -----"
29
30 def GetSlivers(data, config=None, plc=None):
31
32     if 'slivers' not in data:
33         logger.log_missing_data("hostmap.GetSlivers",'slivers')
34         return
35
36     if 'hostname' not in data:
37         logger.log_missing_data("hostmap.GetSlivers", 'hostname')
38
39     hostname = data['hostname']
40
41     hostname_filter = ".".join(hostname.split(".")[1:])
42
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):
49                     continue
50
51                 contents = file(fn,"r").read()
52
53                 hostmap = []
54                 for index, entry in enumerate(tag["value"].split("\n")):
55                     parts = entry.split(" ")
56                     if len(parts)==2:
57                        line = "%s pvt.%s private%d" % (parts[0], parts[1], index)
58
59                        if (parts[0].startswith("10.")) and (hostname_filter not in parts[1]):
60                            continue
61
62                        if (index==0):
63                            line = line + " headnode"
64
65                        if parts[1] == hostname:
66                            line = line + " pvt.self"
67
68                        hostmap.append(line)
69
70                 hostmap = "\n".join(hostmap)
71                 hostmap = PREFIX + "\n" + hostmap + "\n" + SUFFIX + "\n"
72
73                 if (hostmap in contents):
74                     # it's already there
75                     continue
76
77                 # remove anything between PREFIX and SUFFIX from contents
78
79                 pattern = PREFIX + ".*" + SUFFIX + "\n"
80                 regex = re.compile(pattern, re.DOTALL)
81                 if regex.search(contents) != None:
82                     contents = regex.sub(hostmap, contents)
83                 else:
84                     contents = contents + hostmap
85
86                 try:
87                     file(fn, "w").write(contents)
88                 except:
89                     logger.log_exc("hostmap (%s): failed to write %s" % (slicename, fn))
90
91