Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 1 | import os |
| 2 | import json |
Scott Baker | 4c051ed | 2013-09-01 15:36:46 -0700 | [diff] [blame] | 3 | import socket |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 4 | import sys |
| 5 | import time |
| 6 | |
| 7 | sys.path.append("/opt/planetstack") |
| 8 | |
| 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 10 | from openstack.manager import OpenStackManager |
| 11 | from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice |
| 12 | |
| 13 | def ps_id_to_pl_id(x): |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 14 | # Since we don't want the PlanetStack object IDs to conflict with existing |
| 15 | # PlanetLab object IDs in the CMI, just add 100000 to the PlanetStack object |
| 16 | # IDs. |
| 17 | return 100000 + x |
| 18 | |
| 19 | def pl_id_to_ps_id(x): |
| 20 | return x - 100000 |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 21 | |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 22 | def pl_slice_id(slice): |
| 23 | if slice.name == "princeton_vcoblitz": |
| 24 | # 70 is the slice id of princeton_vcoblitz on vicci |
| 25 | return 70 |
| 26 | else: |
| 27 | return ps_id_to_pl_id(slice.id) |
| 28 | |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 29 | def filter_fields(src, fields): |
| 30 | dest = {} |
| 31 | for (key,value) in src.items(): |
| 32 | if (not fields) or (key in fields): |
| 33 | dest[key] = value |
| 34 | return dest |
| 35 | |
| 36 | def GetSlices(filter={}): |
| 37 | ps_slices = Slice.objects.filter(**filter) |
| 38 | slices = [] |
| 39 | for ps_slice in ps_slices: |
| 40 | node_ids=[] |
| 41 | for ps_sliver in ps_slice.slivers.all(): |
| 42 | node_ids.append(ps_id_to_pl_id(ps_sliver.node.id)) |
| 43 | |
| 44 | slice = {"instantiation": "plc-instantiated", |
| 45 | "description": "planetstack slice", |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 46 | "slice_id": pl_slice_id(ps_slice), |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 47 | "node_ids": node_ids, |
| 48 | "url": "planetstack", |
| 49 | "max_nodes": 1000, |
| 50 | "peer_slice_id": None, |
| 51 | "slice_tag_ids": [], |
| 52 | "peer_id": None, |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 53 | "site_id": ps_id_to_pl_id(ps_slice.site_id), |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 54 | "name": ps_slice.name} |
| 55 | |
| 56 | # creator_person_id, person_ids, expires, created |
| 57 | |
| 58 | slices.append(slice) |
| 59 | return slices |
| 60 | |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 61 | def GetNodes(node_ids=None, fields=None): |
| 62 | if node_ids: |
| 63 | ps_nodes = Node.objects.filter(id__in=[pl_id_to_ps_id(nid) for nid in node_ids]) |
| 64 | else: |
| 65 | ps_nodes = Node.objects.all() |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 66 | nodes = [] |
| 67 | for ps_node in ps_nodes: |
| 68 | slice_ids=[] |
| 69 | for ps_sliver in ps_node.slivers.all(): |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 70 | slice_ids.append(pl_slice_id(ps_sliver.slice)) |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 71 | |
| 72 | node = {"node_id": ps_id_to_pl_id(ps_node.id), |
| 73 | "site_id": ps_id_to_pl_id(ps_node.site_id), |
| 74 | "node_type": "regular", |
| 75 | "peer_node_id": None, |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 76 | "hostname": ps_node.name, |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 77 | "conf_file_ids": [], |
| 78 | "slice_ids": slice_ids, |
| 79 | "model": "planetstack", |
| 80 | "peer_id": None, |
| 81 | "node_tag_ids": []} |
| 82 | |
| 83 | # last_updated, key, boot_state, pcu_ids, node_type, session, last_boot, |
| 84 | # interface_ids, slice_ids_whitelist, run_level, ssh_rsa_key, last_pcu_reboot, |
| 85 | # nodegroup_ids, verified, last_contact, boot_nonce, version, |
| 86 | # last_pcu_configuration, last_download, date_created, ports |
| 87 | |
| 88 | nodes.append(node) |
| 89 | |
| 90 | nodes = [filter_fields(node, fields) for node in nodes] |
| 91 | |
| 92 | return nodes |
| 93 | |
| 94 | def GetTags(slicename,node_id): |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 95 | return {} |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 96 | |
| 97 | def GetSites(): |
| 98 | ps_sites = Site.objects.all() |
| 99 | sites = [] |
| 100 | for ps_site in ps_sites: |
| 101 | slice_ids=[] |
| 102 | for ps_slice in ps_site.slices.all(): |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 103 | slice_ids.append(pl_slice_id(ps_slice)) |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 104 | |
| 105 | node_ids=[] |
| 106 | for ps_node in ps_site.nodes.all(): |
| 107 | node_ids.append(ps_id_to_pl_id(ps_node.id)) |
| 108 | |
| 109 | |
| 110 | site = {"site_id": ps_id_to_pl_id(ps_site.id), |
| 111 | "node_ids": node_ids, |
| 112 | "pcu_ids": [], |
| 113 | "max_slices": 100, |
| 114 | "max_slivers": 1000, |
| 115 | "is_public": False, |
| 116 | "peer_site_id": None, |
| 117 | "abbrebiated_name": ps_site.abbreviated_name, |
| 118 | "address_ids": [], |
| 119 | "name": ps_site.name, |
| 120 | "url": None, |
| 121 | "site_tag_ids": [], |
| 122 | "enabled": True, |
| 123 | "longitude": float(ps_site.location.longitude), |
| 124 | "latitude": float(ps_site.location.latitude), |
| 125 | "slice_ids": slice_ids, |
| 126 | "login_base": ps_site.login_base, |
| 127 | "peer_id": None} |
| 128 | |
| 129 | # last_updated, ext_consortium_id, person_ids, date_created |
| 130 | |
| 131 | sites.append(site) |
| 132 | |
| 133 | return sites |
| 134 | |
Scott Baker | 4c051ed | 2013-09-01 15:36:46 -0700 | [diff] [blame] | 135 | def GetInterfaces(slicename, node_ids): |
| 136 | interfaces = [] |
| 137 | ps_slices = Slice.objects.filter(name=slicename) |
| 138 | for ps_slice in ps_slices: |
| 139 | for ps_sliver in ps_slice.slivers.all(): |
| 140 | node_id = ps_id_to_pl_id(ps_sliver.node_id) |
| 141 | if node_id in node_ids: |
| 142 | ps_node = ps_sliver.node |
| 143 | interface = {"node_id": node_id, |
| 144 | "ip": socket.gethostbyname(ps_node.name), |
| 145 | "broadcast": None, |
| 146 | "mac": "11:22:33:44:55:66", |
| 147 | "bwlimit": None, |
| 148 | "network": None, |
| 149 | "is_primary": True, |
| 150 | "dns1": None, |
| 151 | "hostname": None, |
| 152 | "netmask": None, |
| 153 | "interface_tag_ids": [], |
| 154 | "interface_id": node_id, # assume each node has only one interface |
| 155 | "gateway": None, |
| 156 | "dns2": None, |
| 157 | "type": "ipv4", |
| 158 | "method": "dhcp"} |
| 159 | interfaces.append(interface) |
| 160 | return interfaces |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 161 | |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 162 | def GetConfiguration(name): |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 163 | slicename = name["name"] |
| 164 | if "node_id" in name: |
| 165 | node_id = name["node_id"] |
| 166 | else: |
| 167 | node_id = 0 |
| 168 | |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 169 | node_sliver_tags = GetTags(slicename, node_id) |
| 170 | |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 171 | slices = GetSlices({"name": slicename}) |
| 172 | perhost = {} |
| 173 | allinterfaces = {} |
| 174 | hostipmap = {} |
| 175 | nodes = [] |
| 176 | if len(slices)==1: |
| 177 | slice = slices[0] |
| 178 | node_ids = slice['node_ids'] |
| 179 | nodes = GetNodes(node_ids, ['hostname', 'node_id', 'site_id']) |
| 180 | nodemap = {} |
| 181 | for node in nodes: |
| 182 | nodemap[node['node_id']]=node['hostname'] |
| 183 | |
Scott Baker | 4c051ed | 2013-09-01 15:36:46 -0700 | [diff] [blame] | 184 | interfaces = GetInterfaces(slicename, node_ids) |
| 185 | hostipmap = {} |
| 186 | for interface in interfaces: |
| 187 | if nodemap[interface['node_id']] not in allinterfaces: |
| 188 | allinterfaces[nodemap[interface['node_id']]] = [] |
| 189 | interface['interface_tags'] = [] |
| 190 | allinterfaces[nodemap[interface['node_id']]].append(interface) |
| 191 | if interface['is_primary']: |
| 192 | hostipmap[nodemap[interface['node_id']]] = interface['ip'] |
Scott Baker | 0e8a2d7 | 2013-08-30 19:00:11 -0700 | [diff] [blame] | 193 | |
| 194 | for nid in node_ids: |
| 195 | sliver_tags = GetTags(slicename,nid) |
| 196 | perhost[nodemap[nid]] = sliver_tags |
| 197 | |
| 198 | slivers = GetSlices() |
| 199 | if node_id != 0: |
| 200 | slivers = [slice for slice in slivers if (node_id in slice.node_ids)] |
| 201 | |
| 202 | sites = GetSites() |
| 203 | for site in sites: |
| 204 | site["site_tags"] = [] |
| 205 | |
| 206 | timestamp = int(time.time()) |
| 207 | return {'version': 3, |
| 208 | 'timestamp': timestamp, |
| 209 | 'configuration': node_sliver_tags, |
| 210 | 'allconfigurations':perhost, |
| 211 | 'hostipmap':hostipmap, |
| 212 | 'slivers': slivers, |
| 213 | 'interfaces': allinterfaces, |
| 214 | 'sites': sites, |
| 215 | 'nodes': nodes} |
| 216 | |
Scott Baker | cde8eaa | 2013-09-01 15:14:34 -0700 | [diff] [blame] | 217 | if __name__ == '__main__': |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 218 | slices = GetSlices() |
| 219 | nodes = GetNodes() |
Scott Baker | 6ebb5d7 | 2013-09-01 15:13:40 -0700 | [diff] [blame] | 220 | |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 221 | if ("-d" in sys.argv): |
Scott Baker | 847eb56 | 2013-09-01 22:29:36 -0700 | [diff] [blame] | 222 | config = GetConfiguration({"name": "princeton_coblitz"}) |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 223 | print config |
| 224 | print slices |
| 225 | print nodes |
| 226 | else: |
Scott Baker | 847eb56 | 2013-09-01 22:29:36 -0700 | [diff] [blame] | 227 | configs={} |
| 228 | for slicename in ["princeton_vcoblitz"]: |
| 229 | configs[slicename] = GetConfiguration({"name": slicename}) |
| 230 | |
| 231 | file("planetstack_config","w").write(json.dumps(configs)) |
Scott Baker | 60ceb03 | 2013-09-01 16:42:33 -0700 | [diff] [blame] | 232 | file("planetstack_slices","w").write(json.dumps(slices)) |
| 233 | file("planetstack_nodes","w").write(json.dumps(nodes)) |