Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 1 | import os |
| 2 | import json |
| 3 | import socket |
| 4 | import sys |
| 5 | import time |
| 6 | import traceback |
| 7 | import xmlrpclib |
| 8 | |
| 9 | from core.models import Slice, Instance, 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 |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 13 | from rest_framework.views import APIView |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 14 | |
| 15 | def ps_id_to_pl_id(x): |
| 16 | # Since we don't want the XOS object IDs to conflict with existing |
| 17 | # PlanetLab object IDs in the CMI, just add 100000 to the XOS object |
| 18 | # IDs. |
| 19 | return 100000 + x |
| 20 | |
| 21 | def pl_id_to_ps_id(x): |
| 22 | return x - 100000 |
| 23 | |
| 24 | # slice_remap is a dict of ps_slice_name -> (pl_slice_name, pl_slice_id) |
| 25 | |
| 26 | def pl_slice_id(slice, slice_remap={}): |
| 27 | if slice.name in slice_remap: |
| 28 | return int(slice_remap[slice.name][1]) |
| 29 | else: |
| 30 | return ps_id_to_pl_id(slice.id) |
| 31 | |
| 32 | def pl_slicename(slice, slice_remap={}): |
| 33 | if slice.name in slice_remap: |
| 34 | return slice_remap[slice.name][0] |
| 35 | else: |
| 36 | return slice.name |
| 37 | |
| 38 | def filter_fields(src, fields): |
| 39 | dest = {} |
| 40 | for (key,value) in src.items(): |
| 41 | if (not fields) or (key in fields): |
| 42 | dest[key] = value |
| 43 | return dest |
| 44 | |
| 45 | def GetSlices(filter={}, slice_remap={}): |
| 46 | #ps_slices = Slice.objects.filter(**filter) |
| 47 | ps_slices = Slice.objects.all() |
| 48 | slices = [] |
| 49 | for ps_slice in ps_slices: |
| 50 | if (filter) and ("name" in filter): |
| 51 | remapped_name = slice_remap.get(ps_slice.name, (ps_slice.name,))[0] |
| 52 | if (remapped_name != filter["name"]): |
| 53 | continue |
| 54 | |
| 55 | node_ids=[] |
| 56 | for ps_instance in ps_slice.instances.all(): |
| 57 | node_ids.append(ps_id_to_pl_id(ps_instance.node.id)) |
| 58 | |
| 59 | slice = {"instantiation": "plc-instantiated", |
| 60 | "description": "XOS slice", |
| 61 | "slice_id": pl_slice_id(ps_slice, slice_remap), |
| 62 | "node_ids": node_ids, |
| 63 | "url": "xos", |
| 64 | "max_nodes": 1000, |
| 65 | "peer_slice_id": None, |
| 66 | "slice_tag_ids": [], |
| 67 | "peer_id": None, |
| 68 | "site_id": ps_id_to_pl_id(ps_slice.site_id), |
| 69 | "name": pl_slicename(ps_slice, slice_remap), |
| 70 | "planetstack_name": ps_slice.name} # keeping planetstack_name for now, to match the modified config.py |
| 71 | |
| 72 | # creator_person_id, person_ids, expires, created |
| 73 | |
| 74 | slices.append(slice) |
| 75 | return slices |
| 76 | |
| 77 | def GetNodes(node_ids=None, fields=None, slice_remap={}): |
| 78 | if node_ids: |
| 79 | ps_nodes = Node.objects.filter(id__in=[pl_id_to_ps_id(nid) for nid in node_ids]) |
| 80 | else: |
| 81 | ps_nodes = Node.objects.all() |
| 82 | nodes = [] |
| 83 | for ps_node in ps_nodes: |
| 84 | slice_ids=[] |
| 85 | for ps_instance in ps_node.instances.all(): |
| 86 | slice_ids.append(pl_slice_id(ps_instance.slice, slice_remap)) |
| 87 | |
| 88 | node = {"node_id": ps_id_to_pl_id(ps_node.id), |
| 89 | "site_id": ps_id_to_pl_id(ps_node.site_id), |
| 90 | "node_type": "regular", |
| 91 | "peer_node_id": None, |
| 92 | "hostname": ps_node.name.lower(), |
| 93 | "conf_file_ids": [], |
| 94 | "slice_ids": slice_ids, |
| 95 | "model": "xos", |
| 96 | "peer_id": None, |
| 97 | "node_tag_ids": []} |
| 98 | |
| 99 | # last_updated, key, boot_state, pcu_ids, node_type, session, last_boot, |
| 100 | # interface_ids, slice_ids_whitelist, run_level, ssh_rsa_key, last_pcu_reboot, |
| 101 | # nodegroup_ids, verified, last_contact, boot_nonce, version, |
| 102 | # last_pcu_configuration, last_download, date_created, ports |
| 103 | |
| 104 | nodes.append(node) |
| 105 | |
| 106 | nodes = [filter_fields(node, fields) for node in nodes] |
| 107 | |
| 108 | return nodes |
| 109 | |
| 110 | def GetTags(slicename,node_id): |
| 111 | return {} |
| 112 | |
| 113 | def GetSites(slice_remap={}): |
| 114 | ps_sites = Site.objects.all() |
| 115 | sites = [] |
| 116 | for ps_site in ps_sites: |
| 117 | slice_ids=[] |
| 118 | for ps_slice in ps_site.slices.all(): |
| 119 | slice_ids.append(pl_slice_id(ps_slice, slice_remap)) |
| 120 | |
| 121 | node_ids=[] |
| 122 | for ps_node in ps_site.nodes.all(): |
| 123 | node_ids.append(ps_id_to_pl_id(ps_node.id)) |
| 124 | |
| 125 | if ps_site.location: |
| 126 | longitude = ps_site.location.longitude |
| 127 | latitude = ps_site.location.latitude |
| 128 | else: |
| 129 | longitude = 0 |
| 130 | latitude = 0 |
| 131 | |
| 132 | site = {"site_id": ps_id_to_pl_id(ps_site.id), |
| 133 | "node_ids": node_ids, |
| 134 | "pcu_ids": [], |
| 135 | "max_slices": 100, |
| 136 | "max_instances": 1000, |
| 137 | "is_public": False, |
| 138 | "peer_site_id": None, |
| 139 | "abbrebiated_name": ps_site.abbreviated_name, |
| 140 | "address_ids": [], |
| 141 | "name": ps_site.name, |
| 142 | "url": None, |
| 143 | "site_tag_ids": [], |
| 144 | "enabled": True, |
| 145 | "longitude": float(longitude), |
| 146 | "latitude": float(latitude), |
| 147 | "slice_ids": slice_ids, |
| 148 | "login_base": ps_site.login_base, |
| 149 | "peer_id": None} |
| 150 | |
| 151 | # last_updated, ext_consortium_id, person_ids, date_created |
| 152 | |
| 153 | sites.append(site) |
| 154 | |
| 155 | return sites |
| 156 | |
| 157 | def GetInterfaces(slicename, node_ids, return_nat=False, return_private=False): |
| 158 | interfaces = [] |
| 159 | ps_slices = Slice.objects.filter(name=slicename) |
| 160 | for ps_slice in ps_slices: |
| 161 | for ps_instance in ps_slice.instances.all(): |
| 162 | node_id = ps_id_to_pl_id(ps_instance.node_id) |
| 163 | if node_id in node_ids: |
| 164 | ps_node = ps_instance.node |
| 165 | |
| 166 | ip = socket.gethostbyname(ps_node.name.strip()) |
| 167 | |
| 168 | # If the slice has a network that's labeled for hpc_client, then |
| 169 | # return that network. |
| 170 | found_labeled_network = False |
| 171 | for port in ps_instance.ports.all(): |
| 172 | if (not port.ip): |
| 173 | continue |
| 174 | if (port.network.owner != ps_slice): |
| 175 | continue |
| 176 | if port.network.labels and ("hpc_client" in port.network.labels): |
| 177 | ip=port.ip |
| 178 | found_labeled_network = True |
| 179 | |
| 180 | if not found_labeled_network: |
| 181 | # search for a dedicated public IP address |
| 182 | for port in ps_instance.ports.all(): |
| 183 | if (not port.ip): |
| 184 | continue |
| 185 | template = port.network.template |
| 186 | if (template.visibility=="public") and (template.translation=="none"): |
| 187 | ip=port.ip |
| 188 | |
| 189 | if return_nat: |
| 190 | ip = None |
| 191 | for port in ps_instance.ports.all(): |
| 192 | if (not port.ip): |
| 193 | continue |
| 194 | template = port.network.template |
| 195 | if (template.visibility=="private") and (template.translation=="NAT"): |
| 196 | ip=port.ip |
| 197 | if not ip: |
| 198 | continue |
| 199 | |
| 200 | if return_private: |
| 201 | ip = None |
| 202 | for port in ps_instance.ports.all(): |
| 203 | if (not port.ip): |
| 204 | continue |
| 205 | template = port.network.template |
| 206 | if (template.visibility=="private") and (template.translation=="none"): |
| 207 | ip=port.ip |
| 208 | if not ip: |
| 209 | continue |
| 210 | |
| 211 | interface = {"node_id": node_id, |
| 212 | "ip": ip, |
| 213 | "broadcast": None, |
| 214 | "mac": "11:22:33:44:55:66", |
| 215 | "bwlimit": None, |
| 216 | "network": None, |
| 217 | "is_primary": True, |
| 218 | "dns1": None, |
| 219 | "hostname": None, |
| 220 | "netmask": None, |
| 221 | "interface_tag_ids": [], |
| 222 | "interface_id": node_id, # assume each node has only one interface |
| 223 | "gateway": None, |
| 224 | "dns2": None, |
| 225 | "type": "ipv4", |
| 226 | "method": "dhcp"} |
| 227 | interfaces.append(interface) |
| 228 | return interfaces |
| 229 | |
| 230 | def GetConfiguration(name, slice_remap={}): |
| 231 | slicename = name["name"] |
| 232 | if "node_id" in name: |
| 233 | node_id = name["node_id"] |
| 234 | else: |
| 235 | node_id = 0 |
| 236 | |
| 237 | node_instance_tags = GetTags(slicename, node_id) |
| 238 | |
| 239 | slices = GetSlices({"name": slicename}, slice_remap=slice_remap) |
| 240 | perhost = {} |
| 241 | allinterfaces = {} |
| 242 | hostprivmap = {} |
| 243 | hostipmap = {} |
| 244 | hostnatmap = {} |
| 245 | nodes = [] |
| 246 | if len(slices)==1: |
| 247 | slice = slices[0] |
| 248 | node_ids = slice['node_ids'] |
| 249 | nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id'], slice_remap=slice_remap) |
| 250 | nodemap = {} |
| 251 | for node in nodes: |
| 252 | nodemap[node['node_id']]=node['hostname'] |
| 253 | |
| 254 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids) |
| 255 | hostipmap = {} |
| 256 | for interface in interfaces: |
| 257 | if nodemap[interface['node_id']] not in allinterfaces: |
| 258 | allinterfaces[nodemap[interface['node_id']]] = [] |
| 259 | interface['interface_tags'] = [] |
| 260 | allinterfaces[nodemap[interface['node_id']]].append(interface) |
| 261 | if interface['is_primary']: |
| 262 | hostipmap[nodemap[interface['node_id']]] = interface['ip'] |
| 263 | |
| 264 | hostnatmap = {} |
| 265 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids, return_nat=True) |
| 266 | for interface in interfaces: |
| 267 | interface['interface_tags'] = [] |
| 268 | hostnatmap[nodemap[interface['node_id']]] = interface['ip'] |
| 269 | |
| 270 | hostprivmap = {} |
| 271 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids, return_private=True) |
| 272 | for interface in interfaces: |
| 273 | interface['interface_tags'] = [] |
| 274 | hostprivmap[nodemap[interface['node_id']]] = interface['ip'] |
| 275 | |
| 276 | for nid in node_ids: |
| 277 | instance_tags = GetTags(slicename,nid) |
| 278 | perhost[nodemap[nid]] = instance_tags |
| 279 | |
| 280 | instances = GetSlices(slice_remap=slice_remap) |
| 281 | if node_id != 0: |
| 282 | instances = [slice for slice in instances if (node_id in slice.node_ids)] |
| 283 | |
| 284 | sites = GetSites(slice_remap=slice_remap) |
| 285 | for site in sites: |
| 286 | site["site_tags"] = [] |
| 287 | |
| 288 | timestamp = int(time.time()) |
| 289 | return {'version': 3, |
| 290 | 'timestamp': timestamp, |
| 291 | 'configuration': node_instance_tags, |
| 292 | 'allconfigurations':perhost, |
| 293 | 'hostipmap':hostipmap, |
| 294 | 'hostnatmap':hostnatmap, |
| 295 | 'hostprivmap':hostprivmap, |
| 296 | 'slivers': instances, |
| 297 | 'interfaces': allinterfaces, |
| 298 | 'sites': sites, |
| 299 | 'nodes': nodes} |
| 300 | |
| 301 | DEFAULT_REMAP = {"princeton_vcoblitz2": ["princeton_vcoblitz", 70]} |
| 302 | |
| 303 | def HandleGetConfiguration1(): |
| 304 | configs={} |
| 305 | for slicename in ["princeton_vcoblitz"]: |
| 306 | configs[slicename] = GetConfiguration({"name": slicename}, DEFAULT_REMAP) |
| 307 | return configs |
| 308 | |
| 309 | def HandleGetNodes1(): |
| 310 | return GetNodes(slice_remap=DEFAULT_REMAP) |
| 311 | |
| 312 | def HandleGetSlices1(): |
| 313 | return GetSlices(slice_remap=DEFAULT_REMAP) |
| 314 | |
| 315 | def HandleGetConfiguration2(name, slice_remap): |
| 316 | return GetConfiguration(name, slice_remap=slice_remap) |
| 317 | |
| 318 | def HandleGetNodes2(slice_remap): |
| 319 | return GetNodes(slice_remap=slice_remap) |
| 320 | |
| 321 | def HandleGetSlices2(slice_remap): |
| 322 | return GetSlices(slice_remap=slice_remap) |
| 323 | |
| 324 | FUNCS = {"GetConfiguration": HandleGetConfiguration1, |
| 325 | "GetNodes": HandleGetNodes1, |
| 326 | "GetSlices": HandleGetSlices1, |
| 327 | "GetConfiguration2": HandleGetConfiguration2, |
| 328 | "GetNodes2": HandleGetNodes2, |
| 329 | "GetSlices2": HandleGetSlices2} |
| 330 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 331 | #@csrf_exempt |
| 332 | class LegacyApi(APIView): |
| 333 | method_kind = "list" |
| 334 | method_name = "legacyapi" |
| 335 | |
| 336 | def post(self, request, format=None): |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 337 | try: |
| 338 | (args, method) = xmlrpclib.loads(request.body) |
| 339 | result = None |
| 340 | if method in FUNCS: |
| 341 | result = FUNCS[method](*args) |
| 342 | return HttpResponse(xmlrpclib.dumps((result,), methodresponse=True, allow_none=1)) |
| 343 | except: |
| 344 | traceback.print_exc() |
| 345 | return HttpResponseServerError() |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 346 | |
| 347 | if __name__ == '__main__': |
| 348 | slices = GetSlices(slice_remap = DEFAULT_REMAP) |
| 349 | nodes = GetNodes(slice_remap = DEFAULT_REMAP) |
| 350 | |
| 351 | config = GetConfiguration({"name": "princeton_vcoblitz"}, slice_remap = DEFAULT_REMAP) |
| 352 | print config |
| 353 | print slices |
| 354 | print nodes |
| 355 | |