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 |
| 13 | |
| 14 | def ps_id_to_pl_id(x): |
| 15 | # Since we don't want the XOS object IDs to conflict with existing |
| 16 | # PlanetLab object IDs in the CMI, just add 100000 to the XOS object |
| 17 | # IDs. |
| 18 | return 100000 + x |
| 19 | |
| 20 | def pl_id_to_ps_id(x): |
| 21 | return x - 100000 |
| 22 | |
| 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]) |
| 28 | else: |
| 29 | return ps_id_to_pl_id(slice.id) |
| 30 | |
| 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 |
| 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 | |
| 44 | def GetSlices(filter={}, slice_remap={}): |
| 45 | #ps_slices = Slice.objects.filter(**filter) |
| 46 | ps_slices = Slice.objects.all() |
| 47 | slices = [] |
| 48 | for ps_slice in ps_slices: |
| 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 | |
| 54 | node_ids=[] |
| 55 | for ps_instance in ps_slice.instances.all(): |
| 56 | node_ids.append(ps_id_to_pl_id(ps_instance.node.id)) |
| 57 | |
| 58 | slice = {"instantiation": "plc-instantiated", |
| 59 | "description": "XOS slice", |
| 60 | "slice_id": pl_slice_id(ps_slice, slice_remap), |
| 61 | "node_ids": node_ids, |
| 62 | "url": "xos", |
| 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), |
| 68 | "name": pl_slicename(ps_slice, slice_remap), |
| 69 | "planetstack_name": ps_slice.name} # keeping planetstack_name for now, to match the modified config.py |
| 70 | |
| 71 | # creator_person_id, person_ids, expires, created |
| 72 | |
| 73 | slices.append(slice) |
| 74 | return slices |
| 75 | |
| 76 | def GetNodes(node_ids=None, fields=None, slice_remap={}): |
| 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_instance in ps_node.instances.all(): |
| 85 | slice_ids.append(pl_slice_id(ps_instance.slice, slice_remap)) |
| 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, |
| 91 | "hostname": ps_node.name.lower(), |
| 92 | "conf_file_ids": [], |
| 93 | "slice_ids": slice_ids, |
| 94 | "model": "xos", |
| 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 | |
| 112 | def GetSites(slice_remap={}): |
| 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(): |
| 118 | slice_ids.append(pl_slice_id(ps_slice, slice_remap)) |
| 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 | |
| 124 | if ps_site.location: |
| 125 | longitude = ps_site.location.longitude |
| 126 | latitude = ps_site.location.latitude |
| 127 | else: |
| 128 | longitude = 0 |
| 129 | latitude = 0 |
| 130 | |
| 131 | site = {"site_id": ps_id_to_pl_id(ps_site.id), |
| 132 | "node_ids": node_ids, |
| 133 | "pcu_ids": [], |
| 134 | "max_slices": 100, |
| 135 | "max_instances": 1000, |
| 136 | "is_public": False, |
| 137 | "peer_site_id": None, |
| 138 | "abbrebiated_name": ps_site.abbreviated_name, |
| 139 | "address_ids": [], |
| 140 | "name": ps_site.name, |
| 141 | "url": None, |
| 142 | "site_tag_ids": [], |
| 143 | "enabled": True, |
| 144 | "longitude": float(longitude), |
| 145 | "latitude": float(latitude), |
| 146 | "slice_ids": slice_ids, |
| 147 | "login_base": ps_site.login_base, |
| 148 | "peer_id": None} |
| 149 | |
| 150 | # last_updated, ext_consortium_id, person_ids, date_created |
| 151 | |
| 152 | sites.append(site) |
| 153 | |
| 154 | return sites |
| 155 | |
| 156 | def GetInterfaces(slicename, node_ids, return_nat=False, return_private=False): |
| 157 | interfaces = [] |
| 158 | ps_slices = Slice.objects.filter(name=slicename) |
| 159 | for ps_slice in ps_slices: |
| 160 | for ps_instance in ps_slice.instances.all(): |
| 161 | node_id = ps_id_to_pl_id(ps_instance.node_id) |
| 162 | if node_id in node_ids: |
| 163 | ps_node = ps_instance.node |
| 164 | |
| 165 | ip = socket.gethostbyname(ps_node.name.strip()) |
| 166 | |
| 167 | # If the slice has a network that's labeled for hpc_client, then |
| 168 | # return that network. |
| 169 | found_labeled_network = False |
| 170 | for port in ps_instance.ports.all(): |
| 171 | if (not port.ip): |
| 172 | continue |
| 173 | if (port.network.owner != ps_slice): |
| 174 | continue |
| 175 | if port.network.labels and ("hpc_client" in port.network.labels): |
| 176 | ip=port.ip |
| 177 | found_labeled_network = True |
| 178 | |
| 179 | if not found_labeled_network: |
| 180 | # search for a dedicated public IP address |
| 181 | for port in ps_instance.ports.all(): |
| 182 | if (not port.ip): |
| 183 | continue |
| 184 | template = port.network.template |
| 185 | if (template.visibility=="public") and (template.translation=="none"): |
| 186 | ip=port.ip |
| 187 | |
| 188 | if return_nat: |
| 189 | ip = None |
| 190 | for port in ps_instance.ports.all(): |
| 191 | if (not port.ip): |
| 192 | continue |
| 193 | template = port.network.template |
| 194 | if (template.visibility=="private") and (template.translation=="NAT"): |
| 195 | ip=port.ip |
| 196 | if not ip: |
| 197 | continue |
| 198 | |
| 199 | if return_private: |
| 200 | ip = None |
| 201 | for port in ps_instance.ports.all(): |
| 202 | if (not port.ip): |
| 203 | continue |
| 204 | template = port.network.template |
| 205 | if (template.visibility=="private") and (template.translation=="none"): |
| 206 | ip=port.ip |
| 207 | if not ip: |
| 208 | continue |
| 209 | |
| 210 | interface = {"node_id": node_id, |
| 211 | "ip": ip, |
| 212 | "broadcast": None, |
| 213 | "mac": "11:22:33:44:55:66", |
| 214 | "bwlimit": None, |
| 215 | "network": None, |
| 216 | "is_primary": True, |
| 217 | "dns1": None, |
| 218 | "hostname": None, |
| 219 | "netmask": None, |
| 220 | "interface_tag_ids": [], |
| 221 | "interface_id": node_id, # assume each node has only one interface |
| 222 | "gateway": None, |
| 223 | "dns2": None, |
| 224 | "type": "ipv4", |
| 225 | "method": "dhcp"} |
| 226 | interfaces.append(interface) |
| 227 | return interfaces |
| 228 | |
| 229 | def GetConfiguration(name, slice_remap={}): |
| 230 | slicename = name["name"] |
| 231 | if "node_id" in name: |
| 232 | node_id = name["node_id"] |
| 233 | else: |
| 234 | node_id = 0 |
| 235 | |
| 236 | node_instance_tags = GetTags(slicename, node_id) |
| 237 | |
| 238 | slices = GetSlices({"name": slicename}, slice_remap=slice_remap) |
| 239 | perhost = {} |
| 240 | allinterfaces = {} |
| 241 | hostprivmap = {} |
| 242 | hostipmap = {} |
| 243 | hostnatmap = {} |
| 244 | nodes = [] |
| 245 | if len(slices)==1: |
| 246 | slice = slices[0] |
| 247 | node_ids = slice['node_ids'] |
| 248 | nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id'], slice_remap=slice_remap) |
| 249 | nodemap = {} |
| 250 | for node in nodes: |
| 251 | nodemap[node['node_id']]=node['hostname'] |
| 252 | |
| 253 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids) |
| 254 | hostipmap = {} |
| 255 | for interface in interfaces: |
| 256 | if nodemap[interface['node_id']] not in allinterfaces: |
| 257 | allinterfaces[nodemap[interface['node_id']]] = [] |
| 258 | interface['interface_tags'] = [] |
| 259 | allinterfaces[nodemap[interface['node_id']]].append(interface) |
| 260 | if interface['is_primary']: |
| 261 | hostipmap[nodemap[interface['node_id']]] = interface['ip'] |
| 262 | |
| 263 | hostnatmap = {} |
| 264 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids, return_nat=True) |
| 265 | for interface in interfaces: |
| 266 | interface['interface_tags'] = [] |
| 267 | hostnatmap[nodemap[interface['node_id']]] = interface['ip'] |
| 268 | |
| 269 | hostprivmap = {} |
| 270 | interfaces = GetInterfaces(slice["planetstack_name"], node_ids, return_private=True) |
| 271 | for interface in interfaces: |
| 272 | interface['interface_tags'] = [] |
| 273 | hostprivmap[nodemap[interface['node_id']]] = interface['ip'] |
| 274 | |
| 275 | for nid in node_ids: |
| 276 | instance_tags = GetTags(slicename,nid) |
| 277 | perhost[nodemap[nid]] = instance_tags |
| 278 | |
| 279 | instances = GetSlices(slice_remap=slice_remap) |
| 280 | if node_id != 0: |
| 281 | instances = [slice for slice in instances if (node_id in slice.node_ids)] |
| 282 | |
| 283 | sites = GetSites(slice_remap=slice_remap) |
| 284 | for site in sites: |
| 285 | site["site_tags"] = [] |
| 286 | |
| 287 | timestamp = int(time.time()) |
| 288 | return {'version': 3, |
| 289 | 'timestamp': timestamp, |
| 290 | 'configuration': node_instance_tags, |
| 291 | 'allconfigurations':perhost, |
| 292 | 'hostipmap':hostipmap, |
| 293 | 'hostnatmap':hostnatmap, |
| 294 | 'hostprivmap':hostprivmap, |
| 295 | 'slivers': instances, |
| 296 | 'interfaces': allinterfaces, |
| 297 | 'sites': sites, |
| 298 | 'nodes': nodes} |
| 299 | |
| 300 | DEFAULT_REMAP = {"princeton_vcoblitz2": ["princeton_vcoblitz", 70]} |
| 301 | |
| 302 | def HandleGetConfiguration1(): |
| 303 | configs={} |
| 304 | for slicename in ["princeton_vcoblitz"]: |
| 305 | configs[slicename] = GetConfiguration({"name": slicename}, DEFAULT_REMAP) |
| 306 | return configs |
| 307 | |
| 308 | def HandleGetNodes1(): |
| 309 | return GetNodes(slice_remap=DEFAULT_REMAP) |
| 310 | |
| 311 | def HandleGetSlices1(): |
| 312 | return GetSlices(slice_remap=DEFAULT_REMAP) |
| 313 | |
| 314 | def HandleGetConfiguration2(name, slice_remap): |
| 315 | return GetConfiguration(name, slice_remap=slice_remap) |
| 316 | |
| 317 | def HandleGetNodes2(slice_remap): |
| 318 | return GetNodes(slice_remap=slice_remap) |
| 319 | |
| 320 | def HandleGetSlices2(slice_remap): |
| 321 | return GetSlices(slice_remap=slice_remap) |
| 322 | |
| 323 | FUNCS = {"GetConfiguration": HandleGetConfiguration1, |
| 324 | "GetNodes": HandleGetNodes1, |
| 325 | "GetSlices": HandleGetSlices1, |
| 326 | "GetConfiguration2": HandleGetConfiguration2, |
| 327 | "GetNodes2": HandleGetNodes2, |
| 328 | "GetSlices2": HandleGetSlices2} |
| 329 | |
| 330 | @csrf_exempt |
| 331 | def LegacyXMLRPC(request): |
| 332 | if request.method == "POST": |
| 333 | try: |
| 334 | (args, method) = xmlrpclib.loads(request.body) |
| 335 | result = None |
| 336 | if method in FUNCS: |
| 337 | result = FUNCS[method](*args) |
| 338 | return HttpResponse(xmlrpclib.dumps((result,), methodresponse=True, allow_none=1)) |
| 339 | except: |
| 340 | traceback.print_exc() |
| 341 | return HttpResponseServerError() |
| 342 | else: |
| 343 | return HttpResponse("Not Implemented") |
| 344 | |
| 345 | if __name__ == '__main__': |
| 346 | slices = GetSlices(slice_remap = DEFAULT_REMAP) |
| 347 | nodes = GetNodes(slice_remap = DEFAULT_REMAP) |
| 348 | |
| 349 | config = GetConfiguration({"name": "princeton_vcoblitz"}, slice_remap = DEFAULT_REMAP) |
| 350 | print config |
| 351 | print slices |
| 352 | print nodes |
| 353 | |