blob: ed5a836683def8ac7e0a7d4d42dfdfa44048cbab [file] [log] [blame]
Scott Baker2290b052014-05-08 10:28:22 -07001import os
2import base64
3import string
4import sys
5import socket
6from sets import Set
7if __name__ == '__main__':
Scott Bakereaeabad2015-02-09 11:18:46 -08008 sys.path.append("/opt/xos")
Scott Baker2290b052014-05-08 10:28:22 -07009 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
10
11from planetstack.config import Config
12from core.models import Service
13from requestrouter.models import RequestRouterService, ServiceMap
14from util.logger import Logger, logging
15import rrlib_config
16
17logger = Logger(level=logging.INFO)
18
19'''
20Conventions:
211) All dnsredir backend will listen at port 9000+ servicemap.pk ( where pk is the primary key generated in django model)
22'''
23
24class RequestRouterLibrary:
25
26 def __init__(self):
27 pass
28
29 def gen_slice_info(self, service=None):
30 """generates sliver information from slice of request router
31 """
32
33 if (service is None ):
34 service = RequestRouterService.objects.get()
35
36 mapping = {}
37 #static mapping for demo purpose
38 #mapping["node47.princeton.vicci.org"] = "128.112.171.112"
39 mapping["node48.princeton.vicci.org"] = "128.112.171.114"
40
41 '''for slice in service.service.all():
42 name = slice.name
43 for sliver in slice.slivers.all():
44 mapping[sliver.name] = str(sliver.ip)
45 '''
46 return mapping
47
48 def gen_servicemap_slice_info(self, servicemap):
49 """generates sliver information from slice of servicemap
50 """
51
52 wzone = Set(['arizona', 'stanford', 'on.lab', 'housten']) # zone=1 in cooden.conf
53 ezone = Set(['princeton', 'atlanta', 'new york', 'georgia tech']) # zone=2 in coodeen.conf
54
55 mapping_zone = {}
56 mapping_ip = {}
57 slice = servicemap.slice
58 name = slice.name
59 for sliver in slice.slivers.all():
60 mapping_ip[sliver.node.name] = socket.gethostbyname(sliver.node.name)
61 #print "sliver name "+sliver.name+str(sliver.ip)+"\n"
62 site = sliver.node.site.name
63 if(site.lower() in wzone):
64 mapping_zone[sliver.node.name] = str(1)
65 else:
66 mapping_zone[sliver.node.name] = str(2)
67
68 return mapping_ip, mapping_zone
69
70
71
72 def gen_slice_file(self, service):
73 """ generates host file for the slice information
74 to be used by ansible to push configuration files
75 """
76
77 mapping = self.gen_slice_info(service)
78
79 fn = "/etc/ansible/requestrouter/dnsredir/hosts"
80 f = open(fn, "w")
81 for (k,v) in mapping.items():
82 f.write("%s\n" % k)
83
84 fn = "/etc/ansible/requestrouter/dnsdemux/hosts"
85 f = open(fn, "w")
86 for (k,v) in mapping.items():
87 f.write("%s\n" % k)
88
89
90 def get_servicemap_uid(self, servicemap):
91 seq = ("service_", str(servicemap.pk));
92 return "".join(seq)
93
94 def get_service_port(self, servicemap):
95 return str(9000+servicemap.pk)
96
97 def gen_dnsredir_serviceconf(self, servicemap):
98 objname = self.get_servicemap_uid(servicemap)
99
100 rr_mapping = self.gen_slice_info(None)
101
102 #generate dnsredir.conf file parameters to be used in static file.
103 mapping = {}
104 mapping["port_listen"] = self.get_service_port(servicemap)
105 mapping["configdir"] = rrlib_config.DNSREDIR_CONFIGDIR_PREFIX+objname+".d/"
106 mapping["logdir"] = rrlib_config.DNSREDIR_LOGDIR_PREFIX+objname+".d"
107 mapping["pidfile"] = rrlib_config.DNSREDIR_PIDFILE_PREFIX+objname+".pid"
108 mapping["domain_name"] = servicemap.prefix
109 mapping["heartbeat_port"] = rrlib_config.HEARTBEAT_PORT
110
111 #generate dnsredir.conf file
112
113 fn = "./temp_config/dnsredir/"+objname+".conf"
114 f = open(fn, "w")
115 for (k,v) in rr_mapping.items():
116 f.write(mapping["domain_name"]+". NS "+k+". "+v+" 3600 \n" % mapping)
117
118
119 f.write("""
120Default_TTL 30
121
122Port %(port_listen)s
123
124ConfigDir %(configdir)s
125
126MapsDir maps.d
127
128HTTPPort %(heartbeat_port)d
129
130PidFile %(pidfile)s
131
132HttpRequestPort 8081
133
134""" % mapping)
135
136 #generate configdirectory
137
138 os.mkdir("./temp_config/dnsredir/"+objname+".d")
139
140 #geenrate codeen_nodes.conf
141 mapping_ip, mapping_zone = self.gen_servicemap_slice_info(servicemap)
142
143 codeen_name = "./temp_config/dnsredir/"+objname+".d/codeen_nodes.conf"
144 f = open(codeen_name, "w")
145 for (k,v) in mapping_zone.items():
146 f.write(k+" zone="+v+" \n")
147
148 iptxt = "./temp_config/dnsredir/"+objname+".d/node-to-ip.txt"
149 f = open(iptxt, "w")
150 for (k,v) in mapping_ip.items():
151 f.write(k+" "+v+" \n")
152
153 #generate maps directory
154 os.mkdir("./temp_config/dnsredir/"+objname+".d/maps.d")
155
156 # redirection map
157 map = "./temp_config/dnsredir/"+objname+".d/maps.d/map.conf"
158 f = open(map, "w")
159 #hardcoded probable public IP masks from arizona and princeton region respectively
160 f.write("prefix "+servicemap.prefix+" \n")
161 f.write("map 150.135.211.252/32 zone 1 || zone 2 \n")
162 f.write("map 128.112.171.112/24 zone 2 || zone 1 \n")
163 f.write("map 0.0.0.0/0 zone 1 || zone 2 \n")
164
165
166 def gen_dnsdemux_serviceconf(self, servicemap):
167 '''
168 generates frontend service*.conf file for each of the service
169 It assumes that there is a dnsdemux frontend running on the RR istallation and will
170 just add a conf file for each service in /etc/dnsdemux/default
171 '''
172 objname = self.get_servicemap_uid(servicemap)
173 #generate dnsdemux.conf file parameters to be used in static file.
174
175 port_listen = self.get_service_port(servicemap)
176 domain_name = servicemap.prefix
177 #generate service specific .conf file
178
179 rr_mapping = self.gen_slice_info(None)
180
181 fn = "./temp_config/dnsdemux/"+objname+".conf"
182 f = open(fn, "w")
183
184 for (k,v) in rr_mapping.items():
185 f.write("Forward "+v+" "+port_listen+" 8081 "+domain_name+".\n")
186
187
188 def teardown_temp_configfiles(self, objname):
189 if os.path.exists("./temp_config/dnsdemux/"+objname+".conf"):
190 os.remove("./temp_config/dnsdemux/"+objname+".conf")
191 if os.path.exists("./temp_config/dnsredir/"+objname+".d/maps.d/map.conf"):
192 os.remove("./temp_config/dnsredir/"+objname+".d/maps.d/map.conf")
193 if os.path.exists("./temp_config/dnsredir/"+objname+".d/maps.d"):
194 os.rmdir("./temp_config/dnsredir/"+objname+".d/maps.d")
195 if os.path.exists("./temp_config/dnsredir/"+objname+".d/node-to-ip.txt"):
196 os.remove("./temp_config/dnsredir/"+objname+".d/node-to-ip.txt")
197 if os.path.exists("./temp_config/dnsredir/"+objname+".d/codeen_nodes.conf"):
198 os.remove("./temp_config/dnsredir/"+objname+".d/codeen_nodes.conf")
199 if os.path.exists("./temp_config/dnsredir/"+objname+".d"):
200 os.rmdir("./temp_config/dnsredir/"+objname+".d")
201 if os.path.exists("./temp_config/dnsredir/"+objname+".conf"):
202 os.remove("./temp_config/dnsredir/"+objname+".conf")
203