blind and brutal 2to3
[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 xmlrpc.client
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                 with open(fn) as f:
52                     contents = f.read()
53
54                 hostmap = []
55                 for index, entry in enumerate(tag["value"].split("\n")):
56                     parts = entry.split(" ")
57                     if len(parts)==2:
58                        line = "%s pvt.%s private%d" % (parts[0], parts[1], index)
59
60                        if (parts[0].startswith("10.")) and (hostname_filter not in parts[1]):
61                            continue
62
63                        if (index==0):
64                            line = line + " headnode"
65
66                        if parts[1] == hostname:
67                            line = line + " pvt.self"
68
69                        hostmap.append(line)
70
71                 hostmap = "\n".join(hostmap)
72                 hostmap = PREFIX + "\n" + hostmap + "\n" + SUFFIX + "\n"
73
74                 if (hostmap in contents):
75                     # it's already there
76                     continue
77
78                 # remove anything between PREFIX and SUFFIX from contents
79
80                 pattern = PREFIX + ".*" + SUFFIX + "\n"
81                 regex = re.compile(pattern, re.DOTALL)
82                 if regex.search(contents) != None:
83                     contents = regex.sub(hostmap, contents)
84                 else:
85                     contents = contents + hostmap
86
87                 try:
88                     with open(fn, "w") as f:
89                         f.write(contents)
90                 except:
91                     logger.log_exc("hostmap (%s): failed to write %s" % (slicename, fn))
92
93