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