hack for how to render the public names PlanetLab and PlanetLabEurope
[myplc.git] / bin / plc-kml.py
1 #!/usr/bin/env plcsh
2 #
3 # this script generates a kml file, located under the default location below
4 # you should crontab this job from your myplc image
5 # you can then use the googlemap.js javascript for creating your applet
6 # more on this at http://svn.planet-lab.org/wiki/GooglemapSetup
7
8 # kml reference can be found at
9 # http://code.google.com/apis/kml/documentation/kmlreference.html
10 #
11
12 import sys
13
14 default_output           = "/var/www/html/sites/sites.kml"
15 default_local_icon       = "sites/google-local.png"
16 default_foreign_icon     = "sites/google-foreign.png"
17 default_local_builtin    = "palette-4.png"
18 default_foreign_builtin  = "palette-3.png"
19
20 # cosmetic - peername cannot be easily changed on the PLC-PLE link...
21 def render_public_name (peername):
22     if peername=='PlanetLab': return "PlanetLab Central"
23     elif peername == 'PlanetLabEurope': return "PlanetLab Europe"
24     else: return peername
25
26 class KmlMap:
27
28     def __init__ (self,outputname,options):
29         self.outputname=outputname
30         self.options=options
31
32     def open (self):
33         self.output = open(self.outputname,"w")
34
35     def close (self):
36         if self.output:
37             self.output.close()
38         self.output = None
39
40     def write(self,string):
41         self.output.write(string.encode("UTF-8"))
42
43 # initial placement is for europe - dunno how to tune that yet
44     def write_header (self):
45         if not self.options.nodegroup:
46             local_peername=render_public_name(api.config.PLC_NAME)
47             title="%s sites"%local_peername
48             detailed="All the sites known to the %s testbed"%local_peername
49         else:
50             title="Nodegroup %s"%self.options.nodegroup
51             detailed="All sites involved in nodegroup %s"%self.options.nodegroup
52         self.write("""<?xml version="1.0" encoding="UTF-8"?>
53 <kml xmlns="http://earth.google.com/kml/2.2">
54 <Document>
55 <name> %(title)s </name>
56 <LookAt>
57 <longitude>9.180821112577378</longitude>
58 <latitude>44.43275321178062</latitude>
59 <altitude>0</altitude>
60 <range>5782133.196489797</range>
61 <tilt>0</tilt>
62 <heading>-7.767386340832667</heading>
63 </LookAt>
64 <description> %(detailed)s. </description>
65 """%locals())
66
67     def write_footer (self):
68         self.write("""</Document></kml>
69 """)
70
71     def peer_info (self,site, peers):
72         if not site['peer_id']:
73             return (render_public_name(api.config.PLC_NAME), "http://%s/"%api.config.PLC_API_HOST,)
74         for peer in peers:
75             if peer['peer_id'] == site['peer_id']:
76                 return (render_public_name(peer['peername']),peer['peer_url'].replace("PLCAPI/",""),)
77         return "Unknown peer_name"
78
79     # mention local last 
80     @staticmethod
81     def site_compare (s1,s2):
82         p1 = p2 = 0
83         if s1['peer_id']: p1=s1['peer_id']
84         if s2['peer_id']: p2=s2['peer_id']
85         return p2-p1
86
87     ####################
88     def refresh (self):
89         if self.options.nodegroup:
90             self.refresh_nodegroup()
91         else:
92             self.refresh_all_sites()
93
94     def refresh_all_sites(self):
95         self.open()
96         self.write_header()
97         # cache peers 
98         peers = GetPeers()
99         all_sites = GetSites({'enabled':True,'is_public':True})
100         all_sites.sort(KmlMap.site_compare)
101         for site in all_sites:
102             self.write_site(site,peers)
103         self.write_footer()
104         self.close()
105
106     def refresh_nodegroup(self):
107         try:
108             nodegroup=GetNodeGroups({'groupname':self.options.nodegroup})[0]
109         except:
110             print "No such nodegroup %s - ignored"%self.options.nodegroup
111             return
112         nodegroup_node_ids=nodegroup['node_ids']
113         if len(nodegroup_node_ids)==0:
114             print "Empty nodegroup %s - ignored"%self.options.nodegroup
115             return
116         # let's go
117         self.open()
118         self.write_header()
119         # cache peers 
120         peers = GetPeers()
121         nodes=GetNodes(nodegroup_node_ids)
122         global_node_hash = dict ( [ (n['node_id'],n) for n in nodes ] )
123         site_ids = [ node['site_id'] for node in nodes]
124         # remove any duplicate
125         site_ids = list(set(site_ids))
126         sites = GetSites (site_ids)
127         # patch sites so that 'node_ids' only contains the nodes in the nodegroup
128         for site in sites:
129             site['node_ids'] = [ node_id for node_id in site['node_ids'] if node_id in nodegroup_node_ids ]
130             node_hash = dict ( [ (node_id, global_node_hash[node_id]) for node_id in site['node_ids'] ] )
131             self.write_site(site,peers,nodegroup_id=nodegroup['nodegroup_id'], node_hash=node_hash)
132         self.write_footer()
133         self.close()
134
135     def write_site (self, site, peers, nodegroup_id=False, node_hash={}):
136         # discard sites with missing lat or lon
137         if not site['latitude'] or not site['longitude']:
138             return
139         # discard sites with no nodes 
140         if len(site['node_ids']) == 0:
141             return
142
143         site_id=site['site_id']
144         name=site['name']
145         nb_nodes=len(site['node_ids'])
146         nb_slices=len(site['slice_ids'])
147         latitude=site['latitude']
148         longitude=site['longitude']
149         apiurl='https://%s:443'%api.config.PLC_WWW_HOST
150         baseurl='http://%s'%api.config.PLC_WWW_HOST
151         peer_id=site['peer_id']
152
153         # STYLE
154         # the size for google icons
155         if not self.options.use_custom_icons:
156             if not peer_id:
157                 # local sites
158                 iconfile=default_local_builtin
159                 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
160             else:
161                 # remote
162                 iconfile=default_foreign_builtin
163                 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
164             iconurl="root://icons/%(iconfile)s"%locals()
165         # the size for our own brew of icons
166         else:
167             if not peer_id:
168                 iconfile=self.options.local_icon
169             else:
170                 iconfile=self.options.foreign_icon
171             iconurl="%(baseurl)s/%(iconfile)s"%locals()
172             xyspec=""
173
174         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
175
176         # open description
177         # can't seem to get classes to get through to the google maps API
178         # so have to use hard-wired settings
179         description = ""
180         description += "<table style='border: 1px solid black; padding: 3px; margin-top:5px;' width='300px'>"
181         description += "<thead></thead><tbody>"
182
183         # TESTBED
184         description += "<tr>"
185         description += "<td style='font-weight: bold'>Testbed</td>"
186         (peername,peerurl) = self.peer_info (site,peers)
187         description += "<td style='vertical-align:middle;'>"
188         description += "<p><img src='%(iconurl)s' style='vertical-align:middle;'/>"%locals()
189         description += "<a href='%(peerurl)s' style='text-decoration:none;vertical-align:middle;'> %(peername)s </a>"%locals()
190         description += "</p></td></tr>"
191
192         # URL
193         if site['url']:
194             site_url=site['url']
195             description += "<tr>"
196             description += "<td style='font-weight: bold'>Website</td>"
197             description += "<td>"
198             description += "<a style='text-decoration: none;' href='%(site_url)s'> %(site_url)s </a>"%locals()
199             description += "</td>"
200             description += "</tr>"
201
202         # nodegroup direct link
203         if self.options.nodegroup:
204             nodegroup=self.options.nodegroup
205             description += "<tr>"
206             description += "<td style='font-weight: bold'>Nodegroup</td>"
207             description += "<td>"
208             description += "<a style='text-decoration: none;' href='/planetlab/tags/nodegroups.php?id=%(nodegroup_id)d'> %(nodegroup)s </a>"%locals()
209             description += "</td>"
210             description += "</tr>"
211
212
213         # Usage area
214         if not nodegroup_id: title="Usage"
215         else: title="Nodes"
216         description += "<tr>"
217         description += "<td style='font-weight: bold; margin-bottom:2px;'>%(title)s</td>"%locals()
218
219         # encapsulate usage in a table of its own
220         description += "<td>"
221         description += "<table style=''>"
222         description += "<thead></thead><tbody>"
223
224         # NODES
225         # regular all-sites mode
226         if not nodegroup_id:
227             description += "<tr><td align='center'>"
228             description += "<img src='%(apiurl)s/googlemap/node.png'/>"%locals()
229             description += "</td><td>"
230             if nb_nodes:
231                 description += "<a style='text-decoration: none;' href='%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d'>%(nb_nodes)d node(s)</a>"%locals()
232             else:
233                 description += "<i>No node</i>"
234             description += "</td></tr>"
235         # nodegroup mode : show all nodes
236         else:
237             for node_id in site['node_ids']:
238                 node=node_hash[node_id]
239                 hostname=node['hostname']
240                 description += "<tr><td align='center'>"
241                 description += "<img src='%(apiurl)s/googlemap/node.png'/>"%locals()
242                 description += "</td><td>"
243                 description += "<a style='text-decoration: none;' href='%(apiurl)s/db/nodes/index.php?id=%(node_id)d'>%(hostname)s </a>"%locals()
244                 description += "</td></tr>"
245
246         #SLICES
247         if not nodegroup_id:
248             description += "<tr><td align='center'>"
249             description += "<img src='%(apiurl)s/googlemap/slice.png'/>"%locals()
250             description += "</td><td>"
251             if nb_slices:
252                 description += "<a style='text-decoration: none;' href='%(apiurl)s/db/slices/index.php?site_id=%(site_id)d'>%(nb_slices)d slice(s)</a>"%locals()
253             else:
254                 description += "<span style='font-style:italic;'>No slice</span>"
255                 description += "</td></tr>"
256         
257         # close usage table
258         description += "</tbody></table>"
259         description += "</td></tr>"
260
261         # close description
262         description += "</tbody></table>"
263
264         if not self.options.labels:
265             name=""
266             description=""
267
268         # set the camera 50km high
269         template="""<Placemark>
270 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
271 <name><![CDATA[%(name)s]]></name>
272 <LookAt>
273   <latitude>%(latitude)f</latitude>
274   <longitude>%(longitude)f</longitude>
275   <altitude>0</altitude>
276   <altitudeMode>relativeToGround</altitudeMode>              
277   <range>50000.</range> 
278 </LookAt>
279 <description><![CDATA[%(description)s]]></description>
280 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
281 </Placemark>
282 """
283         self.write(template%locals())
284
285 def main () :
286     from optparse import OptionParser
287     usage = "Usage %prog [plcsh-options] [ -- options ]"
288     parser = OptionParser (usage=usage)
289
290     parser.add_option("-o","--output",action="store",dest="output",
291                       default=default_output,
292                       help="output file - default is %s"%default_output)
293     parser.add_option("-n","--no-label",action="store_false",dest="labels",
294                       default=True,
295                       help="outputs only geographic positions, no labels")
296
297     parser.add_option("-g","--nodegroup",action='store',dest='nodegroup',default=None,
298                       help="outputs a kml file for a given nodegroup only")
299
300     # default - for private depls. - is to use google-provided icons like palette-3
301     parser.add_option("-c","--custom",action="store_true",dest="use_custom_icons",
302                       default=False,
303                       help="use locally customized icons rather than the %s and %s defaults"%(default_local_builtin,default_foreign_builtin))
304     parser.add_option("-l","--local",action="store",dest="local_icon",
305                       default=default_local_icon,
306                       help="set icon url to use for local sites marker -- requires -c -- default is %s"%default_local_icon)
307     parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
308                       default=default_foreign_icon,
309                       help="set icon url to use for foreign sites marker -- requires -c -- default is %s"%default_foreign_icon)
310
311     (options, args) = parser.parse_args()
312     if len(args) != 0:
313         parser.print_help()
314         sys.exit(1)
315     KmlMap(options.output,options).refresh()
316
317 ####################
318 if __name__ == "__main__":
319     main()