Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 1 | import os |
| 2 | import json |
| 3 | import sys |
| 4 | import time |
| 5 | |
| 6 | sys.path.append("/opt/planetstack") |
| 7 | |
| 8 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 9 | from openstack.manager import OpenStackManager |
| 10 | from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice |
| 11 | |
| 12 | def ps_id_to_pl_id(x): |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 13 | # Since we don't want the PlanetStack object IDs to conflict with existing |
| 14 | # PlanetLab object IDs in the CMI, just add 100000 to the PlanetStack object |
| 15 | # IDs. |
| 16 | return 100000 + x |
| 17 | |
| 18 | def pl_id_to_ps_id(x): |
| 19 | return x - 100000 |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 20 | |
| 21 | def filter_fields(src, fields): |
| 22 | dest = {} |
| 23 | for (key,value) in src.items(): |
| 24 | if (not fields) or (key in fields): |
| 25 | dest[key] = value |
| 26 | return dest |
| 27 | |
| 28 | def GetSlices(filter={}): |
| 29 | ps_slices = Slice.objects.filter(**filter) |
| 30 | slices = [] |
| 31 | for ps_slice in ps_slices: |
| 32 | node_ids=[] |
| 33 | for ps_sliver in ps_slice.slivers.all(): |
| 34 | node_ids.append(ps_id_to_pl_id(ps_sliver.node.id)) |
| 35 | |
| 36 | slice = {"instantiation": "plc-instantiated", |
| 37 | "description": "planetstack slice", |
| 38 | "slice_id": ps_id_to_pl_id(ps_slice.id), |
| 39 | "node_ids": node_ids, |
| 40 | "url": "planetstack", |
| 41 | "max_nodes": 1000, |
| 42 | "peer_slice_id": None, |
| 43 | "slice_tag_ids": [], |
| 44 | "peer_id": None, |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 45 | "site_id": ps_id_to_pl_id(ps_slice.site_id), |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 46 | "name": ps_slice.name} |
| 47 | |
| 48 | # creator_person_id, person_ids, expires, created |
| 49 | |
| 50 | slices.append(slice) |
| 51 | return slices |
| 52 | |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 53 | def GetNodes(node_ids, fields=None): |
| 54 | ps_nodes = Node.objects.filter(id__in=[pl_id_to_ps_id(nid) for nid in node_ids]) |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 55 | nodes = [] |
| 56 | for ps_node in ps_nodes: |
| 57 | slice_ids=[] |
| 58 | for ps_sliver in ps_node.slivers.all(): |
| 59 | slice_ids.append(ps_id_to_pl_id(ps_sliver.slice.id)) |
| 60 | |
| 61 | node = {"node_id": ps_id_to_pl_id(ps_node.id), |
| 62 | "site_id": ps_id_to_pl_id(ps_node.site_id), |
| 63 | "node_type": "regular", |
| 64 | "peer_node_id": None, |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 65 | "hostname": ps_node.name, |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 66 | "conf_file_ids": [], |
| 67 | "slice_ids": slice_ids, |
| 68 | "model": "planetstack", |
| 69 | "peer_id": None, |
| 70 | "node_tag_ids": []} |
| 71 | |
| 72 | # last_updated, key, boot_state, pcu_ids, node_type, session, last_boot, |
| 73 | # interface_ids, slice_ids_whitelist, run_level, ssh_rsa_key, last_pcu_reboot, |
| 74 | # nodegroup_ids, verified, last_contact, boot_nonce, version, |
| 75 | # last_pcu_configuration, last_download, date_created, ports |
| 76 | |
| 77 | nodes.append(node) |
| 78 | |
| 79 | nodes = [filter_fields(node, fields) for node in nodes] |
| 80 | |
| 81 | return nodes |
| 82 | |
| 83 | def GetTags(slicename,node_id): |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 84 | return {} |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 85 | |
| 86 | def GetSites(): |
| 87 | ps_sites = Site.objects.all() |
| 88 | sites = [] |
| 89 | for ps_site in ps_sites: |
| 90 | slice_ids=[] |
| 91 | for ps_slice in ps_site.slices.all(): |
| 92 | slice_ids.append(ps_id_to_pl_id(ps_slice.id)) |
| 93 | |
| 94 | node_ids=[] |
| 95 | for ps_node in ps_site.nodes.all(): |
| 96 | node_ids.append(ps_id_to_pl_id(ps_node.id)) |
| 97 | |
| 98 | |
| 99 | site = {"site_id": ps_id_to_pl_id(ps_site.id), |
| 100 | "node_ids": node_ids, |
| 101 | "pcu_ids": [], |
| 102 | "max_slices": 100, |
| 103 | "max_slivers": 1000, |
| 104 | "is_public": False, |
| 105 | "peer_site_id": None, |
| 106 | "abbrebiated_name": ps_site.abbreviated_name, |
| 107 | "address_ids": [], |
| 108 | "name": ps_site.name, |
| 109 | "url": None, |
| 110 | "site_tag_ids": [], |
| 111 | "enabled": True, |
| 112 | "longitude": float(ps_site.location.longitude), |
| 113 | "latitude": float(ps_site.location.latitude), |
| 114 | "slice_ids": slice_ids, |
| 115 | "login_base": ps_site.login_base, |
| 116 | "peer_id": None} |
| 117 | |
| 118 | # last_updated, ext_consortium_id, person_ids, date_created |
| 119 | |
| 120 | sites.append(site) |
| 121 | |
| 122 | return sites |
| 123 | |
| 124 | |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 125 | def GetConfiguration(name): |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 126 | slicename = name["name"] |
| 127 | if "node_id" in name: |
| 128 | node_id = name["node_id"] |
| 129 | else: |
| 130 | node_id = 0 |
| 131 | |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 132 | node_sliver_tags = GetTags(slicename, node_id) |
| 133 | |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 134 | slices = GetSlices({"name": slicename}) |
| 135 | perhost = {} |
| 136 | allinterfaces = {} |
| 137 | hostipmap = {} |
| 138 | nodes = [] |
| 139 | if len(slices)==1: |
| 140 | slice = slices[0] |
| 141 | node_ids = slice['node_ids'] |
| 142 | nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id']) |
| 143 | nodemap = {} |
| 144 | for node in nodes: |
| 145 | nodemap[node['node_id']]=node['hostname'] |
| 146 | |
| 147 | # interfaces |
| 148 | |
| 149 | for nid in node_ids: |
| 150 | sliver_tags = GetTags(slicename,nid) |
| 151 | perhost[nodemap[nid]] = sliver_tags |
| 152 | |
| 153 | slivers = GetSlices() |
| 154 | if node_id != 0: |
| 155 | slivers = [slice for slice in slivers if (node_id in slice.node_ids)] |
| 156 | |
| 157 | sites = GetSites() |
| 158 | for site in sites: |
| 159 | site["site_tags"] = [] |
| 160 | |
| 161 | timestamp = int(time.time()) |
| 162 | return {'version': 3, |
| 163 | 'timestamp': timestamp, |
| 164 | 'configuration': node_sliver_tags, |
| 165 | 'allconfigurations':perhost, |
| 166 | 'hostipmap':hostipmap, |
| 167 | 'slivers': slivers, |
| 168 | 'interfaces': allinterfaces, |
| 169 | 'sites': sites, |
| 170 | 'nodes': nodes} |
| 171 | |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame^] | 172 | print GetConfiguration({"name": "smbaker-coblitz"}) |
| 173 | |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 174 | |
| 175 | |