blob: 31471ad2cf5271361a8c25442416f22369211885 [file] [log] [blame]
smbakere36a7de2013-10-29 17:25:25 -07001import os
2import json
3import socket
4import sys
5import time
6import traceback
7import xmlrpclib
Scott Baker9cc9bf32013-12-13 17:26:07 -08008
9# XXX for testing
10#sys.path.append("/opt/planetstack")
11#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
12
smbakere36a7de2013-10-29 17:25:25 -070013from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice
14
15from django.http import HttpResponse
16from django.views.decorators.csrf import csrf_exempt
17
smbakere36a7de2013-10-29 17:25:25 -070018def ps_id_to_pl_id(x):
19 # Since we don't want the PlanetStack object IDs to conflict with existing
20 # PlanetLab object IDs in the CMI, just add 100000 to the PlanetStack object
21 # IDs.
22 return 100000 + x
23
24def pl_id_to_ps_id(x):
25 return x - 100000
26
Scott Baker9cc9bf32013-12-13 17:26:07 -080027# slice_remap is a dict of ps_slice_name -> (pl_slice_name, pl_slice_id)
28
29def pl_slice_id(slice, slice_remap={}):
30 if slice.name in slice_remap:
31 return int(slice_remap[slice.name][1])
smbakere36a7de2013-10-29 17:25:25 -070032 else:
33 return ps_id_to_pl_id(slice.id)
34
Scott Baker9cc9bf32013-12-13 17:26:07 -080035def pl_slicename(slice, slice_remap={}):
36 if slice.name in slice_remap:
37 return slice_remap[slice.name][0]
38 else:
39 return slice.name
smbakere36a7de2013-10-29 17:25:25 -070040
41def filter_fields(src, fields):
42 dest = {}
43 for (key,value) in src.items():
44 if (not fields) or (key in fields):
45 dest[key] = value
46 return dest
47
Scott Baker9cc9bf32013-12-13 17:26:07 -080048def GetSlices(filter={}, slice_remap={}):
49 #ps_slices = Slice.objects.filter(**filter)
50 ps_slices = Slice.objects.all()
smbakere36a7de2013-10-29 17:25:25 -070051 slices = []
52 for ps_slice in ps_slices:
Scott Baker9cc9bf32013-12-13 17:26:07 -080053 if (filter) and ("name" in filter):
54 remapped_name = slice_remap.get(ps_slice.name, (ps_slice.name,))[0]
55 if (remapped_name != filter["name"]):
56 continue
57
smbakere36a7de2013-10-29 17:25:25 -070058 node_ids=[]
59 for ps_sliver in ps_slice.slivers.all():
60 node_ids.append(ps_id_to_pl_id(ps_sliver.node.id))
61
62 slice = {"instantiation": "plc-instantiated",
63 "description": "planetstack slice",
Scott Baker9cc9bf32013-12-13 17:26:07 -080064 "slice_id": pl_slice_id(ps_slice, slice_remap),
smbakere36a7de2013-10-29 17:25:25 -070065 "node_ids": node_ids,
66 "url": "planetstack",
67 "max_nodes": 1000,
68 "peer_slice_id": None,
69 "slice_tag_ids": [],
70 "peer_id": None,
71 "site_id": ps_id_to_pl_id(ps_slice.site_id),
Scott Baker9cc9bf32013-12-13 17:26:07 -080072 "name": pl_slicename(ps_slice, slice_remap),
73 "planetstack_name": ps_slice.name}
smbakere36a7de2013-10-29 17:25:25 -070074
75 # creator_person_id, person_ids, expires, created
76
77 slices.append(slice)
78 return slices
79
Scott Baker9cc9bf32013-12-13 17:26:07 -080080def GetNodes(node_ids=None, fields=None, slice_remap={}):
smbakere36a7de2013-10-29 17:25:25 -070081 if node_ids:
82 ps_nodes = Node.objects.filter(id__in=[pl_id_to_ps_id(nid) for nid in node_ids])
83 else:
84 ps_nodes = Node.objects.all()
85 nodes = []
86 for ps_node in ps_nodes:
87 slice_ids=[]
88 for ps_sliver in ps_node.slivers.all():
Scott Baker9cc9bf32013-12-13 17:26:07 -080089 slice_ids.append(pl_slice_id(ps_sliver.slice, slice_remap))
smbakere36a7de2013-10-29 17:25:25 -070090
91 node = {"node_id": ps_id_to_pl_id(ps_node.id),
92 "site_id": ps_id_to_pl_id(ps_node.site_id),
93 "node_type": "regular",
94 "peer_node_id": None,
Scott Baker60b72a42014-10-09 11:10:05 -070095 "hostname": ps_node.name.lower(),
smbakere36a7de2013-10-29 17:25:25 -070096 "conf_file_ids": [],
97 "slice_ids": slice_ids,
98 "model": "planetstack",
99 "peer_id": None,
100 "node_tag_ids": []}
101
102 # last_updated, key, boot_state, pcu_ids, node_type, session, last_boot,
103 # interface_ids, slice_ids_whitelist, run_level, ssh_rsa_key, last_pcu_reboot,
104 # nodegroup_ids, verified, last_contact, boot_nonce, version,
105 # last_pcu_configuration, last_download, date_created, ports
106
107 nodes.append(node)
108
109 nodes = [filter_fields(node, fields) for node in nodes]
110
111 return nodes
112
113def GetTags(slicename,node_id):
114 return {}
115
Scott Baker9cc9bf32013-12-13 17:26:07 -0800116def GetSites(slice_remap={}):
smbakere36a7de2013-10-29 17:25:25 -0700117 ps_sites = Site.objects.all()
118 sites = []
119 for ps_site in ps_sites:
120 slice_ids=[]
121 for ps_slice in ps_site.slices.all():
Scott Baker9cc9bf32013-12-13 17:26:07 -0800122 slice_ids.append(pl_slice_id(ps_slice, slice_remap))
smbakere36a7de2013-10-29 17:25:25 -0700123
124 node_ids=[]
125 for ps_node in ps_site.nodes.all():
126 node_ids.append(ps_id_to_pl_id(ps_node.id))
127
smbakere36a7de2013-10-29 17:25:25 -0700128 site = {"site_id": ps_id_to_pl_id(ps_site.id),
129 "node_ids": node_ids,
130 "pcu_ids": [],
131 "max_slices": 100,
132 "max_slivers": 1000,
133 "is_public": False,
134 "peer_site_id": None,
135 "abbrebiated_name": ps_site.abbreviated_name,
136 "address_ids": [],
137 "name": ps_site.name,
138 "url": None,
139 "site_tag_ids": [],
140 "enabled": True,
141 "longitude": float(ps_site.location.longitude),
142 "latitude": float(ps_site.location.latitude),
143 "slice_ids": slice_ids,
144 "login_base": ps_site.login_base,
145 "peer_id": None}
146
147 # last_updated, ext_consortium_id, person_ids, date_created
148
149 sites.append(site)
150
151 return sites
152
153def GetInterfaces(slicename, node_ids):
154 interfaces = []
155 ps_slices = Slice.objects.filter(name=slicename)
156 for ps_slice in ps_slices:
157 for ps_sliver in ps_slice.slivers.all():
158 node_id = ps_id_to_pl_id(ps_sliver.node_id)
159 if node_id in node_ids:
160 ps_node = ps_sliver.node
Scott Bakerc8d96532014-09-09 14:54:48 -0700161
162 ip = socket.gethostbyname(ps_node.name)
163
164 # search for a dedicated public IP address
165 for networkSliver in ps_sliver.networksliver_set.all():
166 if (not networkSliver.ip):
167 continue
168 template = networkSliver.network.template
169 if (template.visibility=="public") and (template.translation=="none"):
170 ip=networkSliver.ip
171
smbakere36a7de2013-10-29 17:25:25 -0700172 interface = {"node_id": node_id,
Scott Bakerc8d96532014-09-09 14:54:48 -0700173 "ip": ip,
smbakere36a7de2013-10-29 17:25:25 -0700174 "broadcast": None,
175 "mac": "11:22:33:44:55:66",
176 "bwlimit": None,
177 "network": None,
178 "is_primary": True,
179 "dns1": None,
180 "hostname": None,
181 "netmask": None,
182 "interface_tag_ids": [],
183 "interface_id": node_id, # assume each node has only one interface
184 "gateway": None,
185 "dns2": None,
186 "type": "ipv4",
187 "method": "dhcp"}
188 interfaces.append(interface)
189 return interfaces
190
Scott Baker9cc9bf32013-12-13 17:26:07 -0800191def GetConfiguration(name, slice_remap={}):
smbakere36a7de2013-10-29 17:25:25 -0700192 slicename = name["name"]
193 if "node_id" in name:
194 node_id = name["node_id"]
195 else:
196 node_id = 0
197
smbakere36a7de2013-10-29 17:25:25 -0700198 node_sliver_tags = GetTags(slicename, node_id)
199
Scott Baker9cc9bf32013-12-13 17:26:07 -0800200 slices = GetSlices({"name": slicename}, slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700201 perhost = {}
202 allinterfaces = {}
203 hostipmap = {}
204 nodes = []
205 if len(slices)==1:
206 slice = slices[0]
207 node_ids = slice['node_ids']
Scott Baker9cc9bf32013-12-13 17:26:07 -0800208 nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id'], slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700209 nodemap = {}
210 for node in nodes:
211 nodemap[node['node_id']]=node['hostname']
212
Scott Baker9cc9bf32013-12-13 17:26:07 -0800213 interfaces = GetInterfaces(slice["planetstack_name"], node_ids)
214 print "XXX", interfaces
smbakere36a7de2013-10-29 17:25:25 -0700215 hostipmap = {}
216 for interface in interfaces:
217 if nodemap[interface['node_id']] not in allinterfaces:
218 allinterfaces[nodemap[interface['node_id']]] = []
219 interface['interface_tags'] = []
220 allinterfaces[nodemap[interface['node_id']]].append(interface)
221 if interface['is_primary']:
222 hostipmap[nodemap[interface['node_id']]] = interface['ip']
223
224 for nid in node_ids:
225 sliver_tags = GetTags(slicename,nid)
226 perhost[nodemap[nid]] = sliver_tags
227
Scott Baker9cc9bf32013-12-13 17:26:07 -0800228 slivers = GetSlices(slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700229 if node_id != 0:
230 slivers = [slice for slice in slivers if (node_id in slice.node_ids)]
231
Scott Baker9cc9bf32013-12-13 17:26:07 -0800232 sites = GetSites(slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700233 for site in sites:
234 site["site_tags"] = []
235
236 timestamp = int(time.time())
237 return {'version': 3,
238 'timestamp': timestamp,
239 'configuration': node_sliver_tags,
240 'allconfigurations':perhost,
241 'hostipmap':hostipmap,
242 'slivers': slivers,
243 'interfaces': allinterfaces,
244 'sites': sites,
245 'nodes': nodes}
246
Scott Baker9cc9bf32013-12-13 17:26:07 -0800247DEFAULT_REMAP = {"princeton_vcoblitz2": ["princeton_vcoblitz", 70]}
248
249def HandleGetConfiguration1():
smbakere36a7de2013-10-29 17:25:25 -0700250 configs={}
251 for slicename in ["princeton_vcoblitz"]:
Scott Baker9cc9bf32013-12-13 17:26:07 -0800252 configs[slicename] = GetConfiguration({"name": slicename}, DEFAULT_REMAP)
smbakerf33829c2013-10-29 17:46:32 -0700253 return configs
smbakere36a7de2013-10-29 17:25:25 -0700254
Scott Baker9cc9bf32013-12-13 17:26:07 -0800255def HandleGetNodes1():
256 return GetNodes(slice_remap=DEFAULT_REMAP)
smbakere36a7de2013-10-29 17:25:25 -0700257
Scott Baker9cc9bf32013-12-13 17:26:07 -0800258def HandleGetSlices1():
259 return GetSlices(slice_remap=DEFAULT_REMAP)
smbakerf33829c2013-10-29 17:46:32 -0700260
Scott Baker9cc9bf32013-12-13 17:26:07 -0800261def HandleGetConfiguration2(name, slice_remap):
262 return GetConfiguration(name, slice_remap=slice_remap)
smbakerf33829c2013-10-29 17:46:32 -0700263
Scott Baker9cc9bf32013-12-13 17:26:07 -0800264def HandleGetNodes2(slice_remap):
265 return GetNodes(slice_remap=slice_remap)
266
267def HandleGetSlices2(slice_remap):
268 return GetSlices(slice_remap=slice_remap)
269
270FUNCS = {"GetConfiguration": HandleGetConfiguration1,
271 "GetNodes": HandleGetNodes1,
272 "GetSlices": HandleGetSlices1,
273 "GetConfiguration2": HandleGetConfiguration2,
274 "GetNodes2": HandleGetNodes2,
275 "GetSlices2": HandleGetSlices2}
smbakere36a7de2013-10-29 17:25:25 -0700276
277@csrf_exempt
278def LegacyXMLRPC(request):
279 if request.method == "POST":
280 try:
281 (args, method) = xmlrpclib.loads(request.body)
282 result = None
smbakerf33829c2013-10-29 17:46:32 -0700283 if method in FUNCS:
Scott Baker9cc9bf32013-12-13 17:26:07 -0800284 result = FUNCS[method](*args)
smbakere36a7de2013-10-29 17:25:25 -0700285 return HttpResponse(xmlrpclib.dumps((result,), methodresponse=True, allow_none=1))
286 except:
287 traceback.print_exc()
288 return HttpResponseServerError()
289 else:
290 return HttpResponse("Not Implemented")
291
292if __name__ == '__main__':
Scott Baker9cc9bf32013-12-13 17:26:07 -0800293 slices = GetSlices(slice_remap = DEFAULT_REMAP)
294 nodes = GetNodes(slice_remap = DEFAULT_REMAP)
smbakere36a7de2013-10-29 17:25:25 -0700295
296 if ("-d" in sys.argv):
Scott Baker9cc9bf32013-12-13 17:26:07 -0800297 config = GetConfiguration({"name": "princeton_vcoblitz"}, slice_remap = DEFAULT_REMAP)
smbakere36a7de2013-10-29 17:25:25 -0700298 print config
299 print slices
300 print nodes
301 else:
302 configs={}
Scott Baker9cc9bf32013-12-13 17:26:07 -0800303 configs[slicename] = GetConfiguration({"name": "princeton_vcoblitz"})
smbakere36a7de2013-10-29 17:25:25 -0700304
305 file("planetstack_config","w").write(json.dumps(configs))
306 file("planetstack_slices","w").write(json.dumps(slices))
307 file("planetstack_nodes","w").write(json.dumps(nodes))