blob: 5aa08874466e57a2b4b05b7f03469101168a5a6b [file] [log] [blame]
Scott Baker0e8a2d72013-08-30 19:00:11 -07001import os
2import json
3import sys
4import time
5
6sys.path.append("/opt/planetstack")
7
8os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
9from openstack.manager import OpenStackManager
10from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice
11
12def ps_id_to_pl_id(x):
Scott Baker6ebb5d72013-09-01 15:13:40 -070013 # 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
18def pl_id_to_ps_id(x):
19 return x - 100000
Scott Baker0e8a2d72013-08-30 19:00:11 -070020
21def 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
28def 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 Baker6ebb5d72013-09-01 15:13:40 -070045 "site_id": ps_id_to_pl_id(ps_slice.site_id),
Scott Baker0e8a2d72013-08-30 19:00:11 -070046 "name": ps_slice.name}
47
48 # creator_person_id, person_ids, expires, created
49
50 slices.append(slice)
51 return slices
52
Scott Baker6ebb5d72013-09-01 15:13:40 -070053def 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 Baker0e8a2d72013-08-30 19:00:11 -070055 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 Baker6ebb5d72013-09-01 15:13:40 -070065 "hostname": ps_node.name,
Scott Baker0e8a2d72013-08-30 19:00:11 -070066 "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
83def GetTags(slicename,node_id):
Scott Baker6ebb5d72013-09-01 15:13:40 -070084 return {}
Scott Baker0e8a2d72013-08-30 19:00:11 -070085
86def 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 Baker6ebb5d72013-09-01 15:13:40 -0700125def GetConfiguration(name):
Scott Baker0e8a2d72013-08-30 19:00:11 -0700126 slicename = name["name"]
127 if "node_id" in name:
128 node_id = name["node_id"]
129 else:
130 node_id = 0
131
Scott Baker6ebb5d72013-09-01 15:13:40 -0700132 node_sliver_tags = GetTags(slicename, node_id)
133
Scott Baker0e8a2d72013-08-30 19:00:11 -0700134 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 Baker6ebb5d72013-09-01 15:13:40 -0700172print GetConfiguration({"name": "smbaker-coblitz"})
173
Scott Baker0e8a2d72013-08-30 19:00:11 -0700174
175