blob: 978fd218fc1a78c5e3a8fa0bd3957d5b65a3081d [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
smbakere36a7de2013-10-29 17:25:25 -07009from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice
10
11from django.http import HttpResponse
12from django.views.decorators.csrf import csrf_exempt
13
smbakere36a7de2013-10-29 17:25:25 -070014def ps_id_to_pl_id(x):
15 # Since we don't want the PlanetStack object IDs to conflict with existing
16 # PlanetLab object IDs in the CMI, just add 100000 to the PlanetStack object
17 # IDs.
18 return 100000 + x
19
20def pl_id_to_ps_id(x):
21 return x - 100000
22
Scott Baker9cc9bf32013-12-13 17:26:07 -080023# slice_remap is a dict of ps_slice_name -> (pl_slice_name, pl_slice_id)
24
25def pl_slice_id(slice, slice_remap={}):
26 if slice.name in slice_remap:
27 return int(slice_remap[slice.name][1])
smbakere36a7de2013-10-29 17:25:25 -070028 else:
29 return ps_id_to_pl_id(slice.id)
30
Scott Baker9cc9bf32013-12-13 17:26:07 -080031def pl_slicename(slice, slice_remap={}):
32 if slice.name in slice_remap:
33 return slice_remap[slice.name][0]
34 else:
35 return slice.name
smbakere36a7de2013-10-29 17:25:25 -070036
37def filter_fields(src, fields):
38 dest = {}
39 for (key,value) in src.items():
40 if (not fields) or (key in fields):
41 dest[key] = value
42 return dest
43
Scott Baker9cc9bf32013-12-13 17:26:07 -080044def GetSlices(filter={}, slice_remap={}):
45 #ps_slices = Slice.objects.filter(**filter)
46 ps_slices = Slice.objects.all()
smbakere36a7de2013-10-29 17:25:25 -070047 slices = []
48 for ps_slice in ps_slices:
Scott Baker9cc9bf32013-12-13 17:26:07 -080049 if (filter) and ("name" in filter):
50 remapped_name = slice_remap.get(ps_slice.name, (ps_slice.name,))[0]
51 if (remapped_name != filter["name"]):
52 continue
53
smbakere36a7de2013-10-29 17:25:25 -070054 node_ids=[]
55 for ps_sliver in ps_slice.slivers.all():
56 node_ids.append(ps_id_to_pl_id(ps_sliver.node.id))
57
58 slice = {"instantiation": "plc-instantiated",
59 "description": "planetstack slice",
Scott Baker9cc9bf32013-12-13 17:26:07 -080060 "slice_id": pl_slice_id(ps_slice, slice_remap),
smbakere36a7de2013-10-29 17:25:25 -070061 "node_ids": node_ids,
62 "url": "planetstack",
63 "max_nodes": 1000,
64 "peer_slice_id": None,
65 "slice_tag_ids": [],
66 "peer_id": None,
67 "site_id": ps_id_to_pl_id(ps_slice.site_id),
Scott Baker9cc9bf32013-12-13 17:26:07 -080068 "name": pl_slicename(ps_slice, slice_remap),
69 "planetstack_name": ps_slice.name}
smbakere36a7de2013-10-29 17:25:25 -070070
71 # creator_person_id, person_ids, expires, created
72
73 slices.append(slice)
74 return slices
75
Scott Baker9cc9bf32013-12-13 17:26:07 -080076def GetNodes(node_ids=None, fields=None, slice_remap={}):
smbakere36a7de2013-10-29 17:25:25 -070077 if node_ids:
78 ps_nodes = Node.objects.filter(id__in=[pl_id_to_ps_id(nid) for nid in node_ids])
79 else:
80 ps_nodes = Node.objects.all()
81 nodes = []
82 for ps_node in ps_nodes:
83 slice_ids=[]
84 for ps_sliver in ps_node.slivers.all():
Scott Baker9cc9bf32013-12-13 17:26:07 -080085 slice_ids.append(pl_slice_id(ps_sliver.slice, slice_remap))
smbakere36a7de2013-10-29 17:25:25 -070086
87 node = {"node_id": ps_id_to_pl_id(ps_node.id),
88 "site_id": ps_id_to_pl_id(ps_node.site_id),
89 "node_type": "regular",
90 "peer_node_id": None,
Scott Bakercb236252014-10-09 11:10:05 -070091 "hostname": ps_node.name.lower(),
smbakere36a7de2013-10-29 17:25:25 -070092 "conf_file_ids": [],
93 "slice_ids": slice_ids,
94 "model": "planetstack",
95 "peer_id": None,
96 "node_tag_ids": []}
97
98 # last_updated, key, boot_state, pcu_ids, node_type, session, last_boot,
99 # interface_ids, slice_ids_whitelist, run_level, ssh_rsa_key, last_pcu_reboot,
100 # nodegroup_ids, verified, last_contact, boot_nonce, version,
101 # last_pcu_configuration, last_download, date_created, ports
102
103 nodes.append(node)
104
105 nodes = [filter_fields(node, fields) for node in nodes]
106
107 return nodes
108
109def GetTags(slicename,node_id):
110 return {}
111
Scott Baker9cc9bf32013-12-13 17:26:07 -0800112def GetSites(slice_remap={}):
smbakere36a7de2013-10-29 17:25:25 -0700113 ps_sites = Site.objects.all()
114 sites = []
115 for ps_site in ps_sites:
116 slice_ids=[]
117 for ps_slice in ps_site.slices.all():
Scott Baker9cc9bf32013-12-13 17:26:07 -0800118 slice_ids.append(pl_slice_id(ps_slice, slice_remap))
smbakere36a7de2013-10-29 17:25:25 -0700119
120 node_ids=[]
121 for ps_node in ps_site.nodes.all():
122 node_ids.append(ps_id_to_pl_id(ps_node.id))
123
smbakere36a7de2013-10-29 17:25:25 -0700124 site = {"site_id": ps_id_to_pl_id(ps_site.id),
125 "node_ids": node_ids,
126 "pcu_ids": [],
127 "max_slices": 100,
128 "max_slivers": 1000,
129 "is_public": False,
130 "peer_site_id": None,
131 "abbrebiated_name": ps_site.abbreviated_name,
132 "address_ids": [],
133 "name": ps_site.name,
134 "url": None,
135 "site_tag_ids": [],
136 "enabled": True,
137 "longitude": float(ps_site.location.longitude),
138 "latitude": float(ps_site.location.latitude),
139 "slice_ids": slice_ids,
140 "login_base": ps_site.login_base,
141 "peer_id": None}
142
143 # last_updated, ext_consortium_id, person_ids, date_created
144
145 sites.append(site)
146
147 return sites
148
149def GetInterfaces(slicename, node_ids):
150 interfaces = []
151 ps_slices = Slice.objects.filter(name=slicename)
152 for ps_slice in ps_slices:
153 for ps_sliver in ps_slice.slivers.all():
154 node_id = ps_id_to_pl_id(ps_sliver.node_id)
155 if node_id in node_ids:
156 ps_node = ps_sliver.node
Scott Baker2b9f5202014-09-09 14:54:48 -0700157
158 ip = socket.gethostbyname(ps_node.name)
159
160 # search for a dedicated public IP address
161 for networkSliver in ps_sliver.networksliver_set.all():
162 if (not networkSliver.ip):
163 continue
164 template = networkSliver.network.template
165 if (template.visibility=="public") and (template.translation=="none"):
166 ip=networkSliver.ip
167
smbakere36a7de2013-10-29 17:25:25 -0700168 interface = {"node_id": node_id,
Scott Baker2b9f5202014-09-09 14:54:48 -0700169 "ip": ip,
smbakere36a7de2013-10-29 17:25:25 -0700170 "broadcast": None,
171 "mac": "11:22:33:44:55:66",
172 "bwlimit": None,
173 "network": None,
174 "is_primary": True,
175 "dns1": None,
176 "hostname": None,
177 "netmask": None,
178 "interface_tag_ids": [],
179 "interface_id": node_id, # assume each node has only one interface
180 "gateway": None,
181 "dns2": None,
182 "type": "ipv4",
183 "method": "dhcp"}
184 interfaces.append(interface)
185 return interfaces
186
Scott Baker9cc9bf32013-12-13 17:26:07 -0800187def GetConfiguration(name, slice_remap={}):
smbakere36a7de2013-10-29 17:25:25 -0700188 slicename = name["name"]
189 if "node_id" in name:
190 node_id = name["node_id"]
191 else:
192 node_id = 0
193
smbakere36a7de2013-10-29 17:25:25 -0700194 node_sliver_tags = GetTags(slicename, node_id)
195
Scott Baker9cc9bf32013-12-13 17:26:07 -0800196 slices = GetSlices({"name": slicename}, slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700197 perhost = {}
198 allinterfaces = {}
199 hostipmap = {}
200 nodes = []
201 if len(slices)==1:
202 slice = slices[0]
203 node_ids = slice['node_ids']
Scott Baker9cc9bf32013-12-13 17:26:07 -0800204 nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id'], slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700205 nodemap = {}
206 for node in nodes:
207 nodemap[node['node_id']]=node['hostname']
208
Scott Baker9cc9bf32013-12-13 17:26:07 -0800209 interfaces = GetInterfaces(slice["planetstack_name"], node_ids)
210 print "XXX", interfaces
smbakere36a7de2013-10-29 17:25:25 -0700211 hostipmap = {}
212 for interface in interfaces:
213 if nodemap[interface['node_id']] not in allinterfaces:
214 allinterfaces[nodemap[interface['node_id']]] = []
215 interface['interface_tags'] = []
216 allinterfaces[nodemap[interface['node_id']]].append(interface)
217 if interface['is_primary']:
218 hostipmap[nodemap[interface['node_id']]] = interface['ip']
219
220 for nid in node_ids:
221 sliver_tags = GetTags(slicename,nid)
222 perhost[nodemap[nid]] = sliver_tags
223
Scott Baker9cc9bf32013-12-13 17:26:07 -0800224 slivers = GetSlices(slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700225 if node_id != 0:
226 slivers = [slice for slice in slivers if (node_id in slice.node_ids)]
227
Scott Baker9cc9bf32013-12-13 17:26:07 -0800228 sites = GetSites(slice_remap=slice_remap)
smbakere36a7de2013-10-29 17:25:25 -0700229 for site in sites:
230 site["site_tags"] = []
231
232 timestamp = int(time.time())
233 return {'version': 3,
234 'timestamp': timestamp,
235 'configuration': node_sliver_tags,
236 'allconfigurations':perhost,
237 'hostipmap':hostipmap,
238 'slivers': slivers,
239 'interfaces': allinterfaces,
240 'sites': sites,
241 'nodes': nodes}
242
Scott Baker9cc9bf32013-12-13 17:26:07 -0800243DEFAULT_REMAP = {"princeton_vcoblitz2": ["princeton_vcoblitz", 70]}
244
245def HandleGetConfiguration1():
smbakere36a7de2013-10-29 17:25:25 -0700246 configs={}
247 for slicename in ["princeton_vcoblitz"]:
Scott Baker9cc9bf32013-12-13 17:26:07 -0800248 configs[slicename] = GetConfiguration({"name": slicename}, DEFAULT_REMAP)
smbakerf33829c2013-10-29 17:46:32 -0700249 return configs
smbakere36a7de2013-10-29 17:25:25 -0700250
Scott Baker9cc9bf32013-12-13 17:26:07 -0800251def HandleGetNodes1():
252 return GetNodes(slice_remap=DEFAULT_REMAP)
smbakere36a7de2013-10-29 17:25:25 -0700253
Scott Baker9cc9bf32013-12-13 17:26:07 -0800254def HandleGetSlices1():
255 return GetSlices(slice_remap=DEFAULT_REMAP)
smbakerf33829c2013-10-29 17:46:32 -0700256
Scott Baker9cc9bf32013-12-13 17:26:07 -0800257def HandleGetConfiguration2(name, slice_remap):
258 return GetConfiguration(name, slice_remap=slice_remap)
smbakerf33829c2013-10-29 17:46:32 -0700259
Scott Baker9cc9bf32013-12-13 17:26:07 -0800260def HandleGetNodes2(slice_remap):
261 return GetNodes(slice_remap=slice_remap)
262
263def HandleGetSlices2(slice_remap):
264 return GetSlices(slice_remap=slice_remap)
265
266FUNCS = {"GetConfiguration": HandleGetConfiguration1,
267 "GetNodes": HandleGetNodes1,
268 "GetSlices": HandleGetSlices1,
269 "GetConfiguration2": HandleGetConfiguration2,
270 "GetNodes2": HandleGetNodes2,
271 "GetSlices2": HandleGetSlices2}
smbakere36a7de2013-10-29 17:25:25 -0700272
273@csrf_exempt
274def LegacyXMLRPC(request):
275 if request.method == "POST":
276 try:
277 (args, method) = xmlrpclib.loads(request.body)
278 result = None
smbakerf33829c2013-10-29 17:46:32 -0700279 if method in FUNCS:
Scott Baker9cc9bf32013-12-13 17:26:07 -0800280 result = FUNCS[method](*args)
smbakere36a7de2013-10-29 17:25:25 -0700281 return HttpResponse(xmlrpclib.dumps((result,), methodresponse=True, allow_none=1))
282 except:
283 traceback.print_exc()
284 return HttpResponseServerError()
285 else:
286 return HttpResponse("Not Implemented")
287
288if __name__ == '__main__':
Scott Baker9cc9bf32013-12-13 17:26:07 -0800289 slices = GetSlices(slice_remap = DEFAULT_REMAP)
290 nodes = GetNodes(slice_remap = DEFAULT_REMAP)
smbakere36a7de2013-10-29 17:25:25 -0700291
292 if ("-d" in sys.argv):
Scott Baker9cc9bf32013-12-13 17:26:07 -0800293 config = GetConfiguration({"name": "princeton_vcoblitz"}, slice_remap = DEFAULT_REMAP)
smbakere36a7de2013-10-29 17:25:25 -0700294 print config
295 print slices
296 print nodes
297 else:
298 configs={}
Scott Baker9cc9bf32013-12-13 17:26:07 -0800299 configs[slicename] = GetConfiguration({"name": "princeton_vcoblitz"})
smbakere36a7de2013-10-29 17:25:25 -0700300
301 file("planetstack_config","w").write(json.dumps(configs))
302 file("planetstack_slices","w").write(json.dumps(slices))
303 file("planetstack_nodes","w").write(json.dumps(nodes))