smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 1 | import os |
| 2 | import json |
| 3 | import socket |
| 4 | import sys |
| 5 | import time |
| 6 | import traceback |
| 7 | import xmlrpclib |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 8 | |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 9 | from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice |
| 10 | |
| 11 | from django.http import HttpResponse |
| 12 | from django.views.decorators.csrf import csrf_exempt |
| 13 | |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 14 | def 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 | |
| 20 | def pl_id_to_ps_id(x): |
| 21 | return x - 100000 |
| 22 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 23 | # slice_remap is a dict of ps_slice_name -> (pl_slice_name, pl_slice_id) |
| 24 | |
| 25 | def pl_slice_id(slice, slice_remap={}): |
| 26 | if slice.name in slice_remap: |
| 27 | return int(slice_remap[slice.name][1]) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 28 | else: |
| 29 | return ps_id_to_pl_id(slice.id) |
| 30 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 31 | def 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 |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 36 | |
| 37 | def 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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 44 | def GetSlices(filter={}, slice_remap={}): |
| 45 | #ps_slices = Slice.objects.filter(**filter) |
| 46 | ps_slices = Slice.objects.all() |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 47 | slices = [] |
| 48 | for ps_slice in ps_slices: |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 49 | 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 | |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 54 | 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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 60 | "slice_id": pl_slice_id(ps_slice, slice_remap), |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 61 | "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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 68 | "name": pl_slicename(ps_slice, slice_remap), |
| 69 | "planetstack_name": ps_slice.name} |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 70 | |
| 71 | # creator_person_id, person_ids, expires, created |
| 72 | |
| 73 | slices.append(slice) |
| 74 | return slices |
| 75 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 76 | def GetNodes(node_ids=None, fields=None, slice_remap={}): |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 77 | 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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 85 | slice_ids.append(pl_slice_id(ps_sliver.slice, slice_remap)) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 86 | |
| 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 Baker | cb23625 | 2014-10-09 11:10:05 -0700 | [diff] [blame] | 91 | "hostname": ps_node.name.lower(), |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 92 | "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 | |
| 109 | def GetTags(slicename,node_id): |
| 110 | return {} |
| 111 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 112 | def GetSites(slice_remap={}): |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 113 | 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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 118 | slice_ids.append(pl_slice_id(ps_slice, slice_remap)) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 119 | |
| 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 | |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 124 | 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 | |
| 149 | def 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 Baker | 2b9f520 | 2014-09-09 14:54:48 -0700 | [diff] [blame] | 157 | |
| 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 | |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 168 | interface = {"node_id": node_id, |
Scott Baker | 2b9f520 | 2014-09-09 14:54:48 -0700 | [diff] [blame] | 169 | "ip": ip, |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 170 | "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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 187 | def GetConfiguration(name, slice_remap={}): |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 188 | slicename = name["name"] |
| 189 | if "node_id" in name: |
| 190 | node_id = name["node_id"] |
| 191 | else: |
| 192 | node_id = 0 |
| 193 | |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 194 | node_sliver_tags = GetTags(slicename, node_id) |
| 195 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 196 | slices = GetSlices({"name": slicename}, slice_remap=slice_remap) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 197 | perhost = {} |
| 198 | allinterfaces = {} |
| 199 | hostipmap = {} |
| 200 | nodes = [] |
| 201 | if len(slices)==1: |
| 202 | slice = slices[0] |
| 203 | node_ids = slice['node_ids'] |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 204 | nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id'], slice_remap=slice_remap) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 205 | nodemap = {} |
| 206 | for node in nodes: |
| 207 | nodemap[node['node_id']]=node['hostname'] |
| 208 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 209 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids) |
| 210 | print "XXX", interfaces |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 211 | 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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 224 | slivers = GetSlices(slice_remap=slice_remap) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 225 | if node_id != 0: |
| 226 | slivers = [slice for slice in slivers if (node_id in slice.node_ids)] |
| 227 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 228 | sites = GetSites(slice_remap=slice_remap) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 229 | 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 Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 243 | DEFAULT_REMAP = {"princeton_vcoblitz2": ["princeton_vcoblitz", 70]} |
| 244 | |
| 245 | def HandleGetConfiguration1(): |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 246 | configs={} |
| 247 | for slicename in ["princeton_vcoblitz"]: |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 248 | configs[slicename] = GetConfiguration({"name": slicename}, DEFAULT_REMAP) |
smbaker | f33829c | 2013-10-29 17:46:32 -0700 | [diff] [blame] | 249 | return configs |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 250 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 251 | def HandleGetNodes1(): |
| 252 | return GetNodes(slice_remap=DEFAULT_REMAP) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 253 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 254 | def HandleGetSlices1(): |
| 255 | return GetSlices(slice_remap=DEFAULT_REMAP) |
smbaker | f33829c | 2013-10-29 17:46:32 -0700 | [diff] [blame] | 256 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 257 | def HandleGetConfiguration2(name, slice_remap): |
| 258 | return GetConfiguration(name, slice_remap=slice_remap) |
smbaker | f33829c | 2013-10-29 17:46:32 -0700 | [diff] [blame] | 259 | |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 260 | def HandleGetNodes2(slice_remap): |
| 261 | return GetNodes(slice_remap=slice_remap) |
| 262 | |
| 263 | def HandleGetSlices2(slice_remap): |
| 264 | return GetSlices(slice_remap=slice_remap) |
| 265 | |
| 266 | FUNCS = {"GetConfiguration": HandleGetConfiguration1, |
| 267 | "GetNodes": HandleGetNodes1, |
| 268 | "GetSlices": HandleGetSlices1, |
| 269 | "GetConfiguration2": HandleGetConfiguration2, |
| 270 | "GetNodes2": HandleGetNodes2, |
| 271 | "GetSlices2": HandleGetSlices2} |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 272 | |
| 273 | @csrf_exempt |
| 274 | def LegacyXMLRPC(request): |
| 275 | if request.method == "POST": |
| 276 | try: |
| 277 | (args, method) = xmlrpclib.loads(request.body) |
| 278 | result = None |
smbaker | f33829c | 2013-10-29 17:46:32 -0700 | [diff] [blame] | 279 | if method in FUNCS: |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 280 | result = FUNCS[method](*args) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 281 | 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 | |
| 288 | if __name__ == '__main__': |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 289 | slices = GetSlices(slice_remap = DEFAULT_REMAP) |
| 290 | nodes = GetNodes(slice_remap = DEFAULT_REMAP) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 291 | |
| 292 | if ("-d" in sys.argv): |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 293 | config = GetConfiguration({"name": "princeton_vcoblitz"}, slice_remap = DEFAULT_REMAP) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 294 | print config |
| 295 | print slices |
| 296 | print nodes |
| 297 | else: |
| 298 | configs={} |
Scott Baker | 9cc9bf3 | 2013-12-13 17:26:07 -0800 | [diff] [blame] | 299 | configs[slicename] = GetConfiguration({"name": "princeton_vcoblitz"}) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 300 | |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 301 | file("xos.config","w").write(json.dumps(configs)) |
smbaker | e36a7de | 2013-10-29 17:25:25 -0700 | [diff] [blame] | 302 | file("planetstack_slices","w").write(json.dumps(slices)) |
| 303 | file("planetstack_nodes","w").write(json.dumps(nodes)) |