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
8 # kml reference can be found at
9 # http://code.google.com/apis/kml/documentation/kmlreference.html
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"
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"
28 def __init__ (self,outputname,options):
29 self.outputname=outputname
33 self.output = open(self.outputname,"w")
40 def write(self,string):
41 self.output.write(string.encode("UTF-8"))
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
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">
55 <name> %(title)s </name>
57 <longitude>9.180821112577378</longitude>
58 <latitude>44.43275321178062</latitude>
59 <altitude>0</altitude>
60 <range>5782133.196489797</range>
62 <heading>-7.767386340832667</heading>
64 <description> %(detailed)s. </description>
67 def write_footer (self):
68 self.write("""</Document></kml>
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,)
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"
81 def site_compare (s1,s2):
83 if s1['peer_id']: p1=s1['peer_id']
84 if s2['peer_id']: p2=s2['peer_id']
89 if self.options.nodegroup:
90 self.refresh_nodegroup()
92 self.refresh_all_sites()
94 def refresh_all_sites(self):
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)
106 def refresh_nodegroup(self):
108 nodegroup=GetNodeGroups({'groupname':self.options.nodegroup})[0]
110 print("No such nodegroup %s - ignored"%self.options.nodegroup)
112 nodegroup_node_ids=nodegroup['node_ids']
113 if len(nodegroup_node_ids)==0:
114 print("Empty nodegroup %s - ignored"%self.options.nodegroup)
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
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)
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']:
139 # discard sites with no nodes
140 if len(site['node_ids']) == 0:
143 site_id=site['site_id']
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']
154 # the size for google icons
155 if not self.options.use_custom_icons:
158 iconfile=default_local_builtin
159 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
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
168 iconfile=self.options.local_icon
170 iconfile=self.options.foreign_icon
171 iconurl="%(baseurl)s/%(iconfile)s"%locals()
174 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
177 # can't seem to get classes to get through to the google maps API
178 # so have to use hard-wired settings
180 description += "<table style='border: 1px solid black; padding: 3px; margin-top:5px;' width='300px'>"
181 description += "<thead></thead><tbody>"
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>"
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>"
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>"
214 if not nodegroup_id: title="Usage"
216 description += "<tr>"
217 description += "<td style='font-weight: bold; margin-bottom:2px;'>%(title)s</td>"%locals()
219 # encapsulate usage in a table of its own
220 description += "<td>"
221 description += "<table style=''>"
222 description += "<thead></thead><tbody>"
225 # regular all-sites mode
227 description += "<tr><td align='center'>"
228 description += "<img src='%(apiurl)s/googlemap/node.png'/>"%locals()
229 description += "</td><td>"
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()
233 description += "<i>No node</i>"
234 description += "</td></tr>"
235 # nodegroup mode : show all nodes
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>"
248 description += "<tr><td align='center'>"
249 description += "<img src='%(apiurl)s/googlemap/slice.png'/>"%locals()
250 description += "</td><td>"
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()
254 description += "<span style='font-style:italic;'>No slice</span>"
255 description += "</td></tr>"
258 description += "</tbody></table>"
259 description += "</td></tr>"
262 description += "</tbody></table>"
264 if not self.options.labels:
268 # set the camera 50km high
269 template="""<Placemark>
270 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
271 <name><![CDATA[%(name)s]]></name>
273 <latitude>%(latitude)f</latitude>
274 <longitude>%(longitude)f</longitude>
275 <altitude>0</altitude>
276 <altitudeMode>relativeToGround</altitudeMode>
277 <range>50000.</range>
279 <description><![CDATA[%(description)s]]></description>
280 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
283 self.write(template%locals())
286 from optparse import OptionParser
287 usage = "Usage %prog [plcsh-options] [ -- options ]"
288 parser = OptionParser (usage=usage)
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",
295 help="outputs only geographic positions, no labels")
297 parser.add_option("-g","--nodegroup",action='store',dest='nodegroup',default=None,
298 help="outputs a kml file for a given nodegroup only")
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",
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)
311 (options, args) = parser.parse_args()
315 KmlMap(options.output,options).refresh()
318 if __name__ == "__main__":