blob: b3193f48095add6f3a770cca9fd86a8a22ad97c9 [file] [log] [blame]
Scott Baker0e8a2d72013-08-30 19:00:11 -07001import os
2import json
Scott Baker4c051ed2013-09-01 15:36:46 -07003import socket
Scott Baker0e8a2d72013-08-30 19:00:11 -07004import sys
5import time
6
7sys.path.append("/opt/planetstack")
8
9os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
10from openstack.manager import OpenStackManager
11from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice
12
13def ps_id_to_pl_id(x):
Scott Baker6ebb5d72013-09-01 15:13:40 -070014 # 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
19def pl_id_to_ps_id(x):
20 return x - 100000
Scott Baker0e8a2d72013-08-30 19:00:11 -070021
Scott Baker60ceb032013-09-01 16:42:33 -070022def 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 Baker0e8a2d72013-08-30 19:00:11 -070029def 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
36def 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 Baker60ceb032013-09-01 16:42:33 -070046 "slice_id": pl_slice_id(ps_slice),
Scott Baker0e8a2d72013-08-30 19:00:11 -070047 "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 Baker6ebb5d72013-09-01 15:13:40 -070053 "site_id": ps_id_to_pl_id(ps_slice.site_id),
Scott Baker0e8a2d72013-08-30 19:00:11 -070054 "name": ps_slice.name}
55
56 # creator_person_id, person_ids, expires, created
57
58 slices.append(slice)
59 return slices
60
Scott Baker60ceb032013-09-01 16:42:33 -070061def 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 Baker0e8a2d72013-08-30 19:00:11 -070066 nodes = []
67 for ps_node in ps_nodes:
68 slice_ids=[]
69 for ps_sliver in ps_node.slivers.all():
Scott Baker60ceb032013-09-01 16:42:33 -070070 slice_ids.append(pl_slice_id(ps_sliver.slice))
Scott Baker0e8a2d72013-08-30 19:00:11 -070071
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 Baker6ebb5d72013-09-01 15:13:40 -070076 "hostname": ps_node.name,
Scott Baker0e8a2d72013-08-30 19:00:11 -070077 "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
94def GetTags(slicename,node_id):
Scott Baker6ebb5d72013-09-01 15:13:40 -070095 return {}
Scott Baker0e8a2d72013-08-30 19:00:11 -070096
97def 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 Baker60ceb032013-09-01 16:42:33 -0700103 slice_ids.append(pl_slice_id(ps_slice))
Scott Baker0e8a2d72013-08-30 19:00:11 -0700104
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 Baker4c051ed2013-09-01 15:36:46 -0700135def 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 Baker0e8a2d72013-08-30 19:00:11 -0700161
Scott Baker6ebb5d72013-09-01 15:13:40 -0700162def GetConfiguration(name):
Scott Baker0e8a2d72013-08-30 19:00:11 -0700163 slicename = name["name"]
164 if "node_id" in name:
165 node_id = name["node_id"]
166 else:
167 node_id = 0
168
Scott Baker6ebb5d72013-09-01 15:13:40 -0700169 node_sliver_tags = GetTags(slicename, node_id)
170
Scott Baker0e8a2d72013-08-30 19:00:11 -0700171 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 Baker4c051ed2013-09-01 15:36:46 -0700184 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 Baker0e8a2d72013-08-30 19:00:11 -0700193
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 Bakercde8eaa2013-09-01 15:14:34 -0700217if __name__ == '__main__':
Scott Baker60ceb032013-09-01 16:42:33 -0700218 slices = GetSlices()
219 nodes = GetNodes()
Scott Baker6ebb5d72013-09-01 15:13:40 -0700220
Scott Baker60ceb032013-09-01 16:42:33 -0700221 if ("-d" in sys.argv):
Scott Baker847eb562013-09-01 22:29:36 -0700222 config = GetConfiguration({"name": "princeton_coblitz"})
Scott Baker60ceb032013-09-01 16:42:33 -0700223 print config
224 print slices
225 print nodes
226 else:
Scott Baker847eb562013-09-01 22:29:36 -0700227 configs={}
228 for slicename in ["princeton_vcoblitz"]:
229 configs[slicename] = GetConfiguration({"name": slicename})
230
231 file("planetstack_config","w").write(json.dumps(configs))
Scott Baker60ceb032013-09-01 16:42:33 -0700232 file("planetstack_slices","w").write(json.dumps(slices))
233 file("planetstack_nodes","w").write(json.dumps(nodes))