460c0d9eba82a21a9eececa23d74b729d53389f8
[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     # mention local last 
38     @staticmethod
39     def site_compare (s1,s2):
40         p1 = p2 = 0
41         if s1['peer_id']: p1=s1['peer_id']
42         if s2['peer_id']: p2=s2['peer_id']
43         return p2-p1
44
45     def refresh (self):
46         self.open()
47         self.write_header()
48         # cache peers 
49         peers = GetPeers({},['peer_id','peername'])
50         all_sites = GetSites({'enabled':True,'is_public':True})
51         all_sites.sort(KmlMap.site_compare)
52         for site in all_sites:
53             self.write_site(site,peers)
54         self.write_footer()
55         self.close()
56
57 # initial placement is for europe - dunno how to tune that yet
58     def write_header (self):
59         self.write("""<?xml version="1.0" encoding="UTF-8"?>
60 <kml xmlns="http://earth.google.com/kml/2.2">
61 <Document>
62 <name> PlanetLab Sites </name>
63 <LookAt>
64 <longitude>9.180821112577378</longitude>
65 <latitude>44.43275321178062</latitude>
66 <altitude>0</altitude>
67 <range>5782133.196489797</range>
68 <tilt>0</tilt>
69 <heading>-7.767386340832667</heading>
70 </LookAt>
71 <description> All the sites known to the PlanetLab testbed. </description>
72 """)
73
74     def write_footer (self):
75         self.write("""</Document></kml>
76 """)
77
78     def peer_name (self,site, peers):
79         if not site['peer_id']:
80             return "local"
81         for peer in peers:
82             if peer['peer_id'] == site['peer_id']:
83                 return peer['peername']
84
85     def write_site (self, site, peers):
86         # discard sites with missing lat or lon
87         if not site['latitude'] or not site['longitude']:
88             return
89         # discard sites with no nodes 
90         if len(site['node_ids']) == 0:
91             return
92
93         site_id=site['site_id']
94         name=site['name']
95         nb_nodes=len(site['node_ids'])
96         nb_slices=len(site['slice_ids'])
97         latitude=site['latitude']
98         longitude=site['longitude']
99         apiurl='https://%s:443'%api.config.PLC_WWW_HOST
100         baseurl='http://%s'%api.config.PLC_WWW_HOST
101         peer_id=site['peer_id']
102
103         # open description
104         description  = "<br/><table style='border: 1px solid black; padding: 5px;' width='500px'>"
105
106         # TESTBED
107         description += "<tr><td></td><td>&nbsp;</td><td></td></tr>"
108         description += "<tr><td align='center'>"
109         description += "<b>Testbed</b>"
110         description += "</td><td>"
111         if peer_id:
112             peername = 'PlanetLab Central'
113             peerurl = 'http://www.planet-lab.org'
114             #self.peer_name(site,peers)
115         else:
116             peername = 'PlanetLab Europe'
117             peerurl = 'http://www.planet-lab.eu'
118         description += "<a style='text-decoration: none;' href='%(peerurl)s'> %(peername)s </a>"%locals()
119         #description += "<a style='text-decoration: none;' href='%(apiurl)s/db/peers/index.php?id=%(peer_id)d'>[description]</a>"%locals()
120         description += "</td><td>"
121         if peer_id:
122             description += "<img src='%(apiurl)s/sites/plc24-32.png'/>"%locals()
123         else:
124             description += "<img src='%(apiurl)s/sites/ple24-32.png'/>"%locals()
125         description += "</td></tr>"
126
127         # URL
128         if site['url']:
129             site_url=site['url']
130             description += "<tr><td align='center'>"
131             description += "<b>Website</b>"
132             description += "</td><td>"
133             description += "<a style='text-decoration: none;' href='%(site_url)s'> %(site_url)s </a>"%locals()
134             description += "</td><td></td></tr>"
135
136         description += "<tr><td></td><td>&nbsp;</td><td></td></tr>"
137         description += "<tr><td align='center'>Usage details:</td><td></td><td></td></tr>"
138         description += "<tr><td></td><td>&nbsp;</td><td></td></tr>"
139
140         # NODES
141         description += "<tr><td align='center'>"
142         description += "<img src='%(apiurl)s/sites/node.png'/>"%locals()
143         description += "</td><td>"
144         if nb_nodes:
145             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()
146             #description += "<a style='text-decoration: none;' href='%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d'> (in Comon)</a>"%locals()
147         else:
148             description += "<i>No node</i>"
149         description += "</td><td></td></tr>"
150
151         #SLICES
152         description += "<tr><td align='center'>"
153         description += "<img src='%(apiurl)s/sites/slice.png'/>"%locals()
154         description += "</td><td>"
155         if nb_slices:
156             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()
157         else:
158             description += "<i>No slice</i>"
159         description += "</td><td></td></tr>"
160
161         description += "<tr><td></td><td>&nbsp;</td><td></td></tr>"
162
163         # close description
164         description += "</table>"
165
166         if not self.options.labels:
167             name=""
168             description=""
169
170         # STYLE
171         # the size for google icons
172         if not self.options.use_custom_icons:
173             if not peer_id:
174                 # local sites
175                 iconfile=default_local_builtin
176                 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
177             else:
178                 # remote
179                 iconfile=default_foreign_builtin
180                 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
181             iconurl="root://icons/%(iconfile)s"%locals()
182         # the size for our own brew of icons
183         else:
184             if not peer_id:
185                 iconfile=self.options.local_icon
186             else:
187                 iconfile=self.options.foreign_icon
188             iconurl="%(baseurl)s/%(iconfile)s"%locals()
189             xyspec=""
190
191         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
192
193         # set the camera 50km high
194         template="""<Placemark>
195 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
196 <name><![CDATA[%(name)s]]></name>
197 <LookAt>
198   <latitude>%(latitude)f</latitude>
199   <longitude>%(longitude)f</longitude>
200   <altitude>0</altitude>
201   <altitudeMode>relativeToGround</altitudeMode>              
202   <range>50000.</range> 
203 </LookAt>
204 <description><![CDATA[%(description)s]]></description>
205 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
206 </Placemark>
207 """
208         self.write(template%locals())
209
210 def main () :
211     from optparse import OptionParser
212     usage = "Usage %prog [plcsh-options] [ -- options ]"
213     parser = OptionParser (usage=usage)
214
215     parser.add_option("-o","--output",action="store",dest="output",
216                       default=default_output,
217                       help="output file - default is %s"%default_output)
218     parser.add_option("-n","--no-label",action="store_false",dest="labels",
219                       default=True,
220                       help="outputs only geographic positions, no labels")
221     # default - for private depls. - is to use google-provided icons like palette-3
222     parser.add_option("-c","--custom",action="store_true",dest="use_custom_icons",
223                       default=False,
224                       help="use locally customized icons rather than the %s and %s defaults"%(default_local_builtin,default_foreign_builtin))
225     parser.add_option("-l","--local",action="store",dest="local_icon",
226                       default=default_local_icon,
227                       help="set icon url to use for local sites marker -- requires -c -- default is %s"%default_local_icon)
228     parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
229                       default=default_foreign_icon,
230                       help="set icon url to use for foreign sites marker -- requires -c -- default is %s"%default_foreign_icon)
231
232     (options, args) = parser.parse_args()
233     if len(args) != 0:
234         parser.print_help()
235         sys.exit(1)
236     KmlMap(options.output,options).refresh()
237
238 ####################
239 if __name__ == "__main__":
240     main()