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"
22 def __init__ (self,outputname,options):
23 self.outputname=outputname
27 self.output = open(self.outputname,"w")
34 def write(self,string):
35 self.output.write(string.encode("UTF-8"))
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
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">
48 <name> %(title)s </name>
50 <longitude>9.180821112577378</longitude>
51 <latitude>44.43275321178062</latitude>
52 <altitude>0</altitude>
53 <range>5782133.196489797</range>
55 <heading>-7.767386340832667</heading>
57 <description> %(detailed)s. </description>
60 def write_footer (self):
61 self.write("""</Document></kml>
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,)
68 if peer['peer_id'] == site['peer_id']:
69 return (peer['peername'],peer['peer_url'].replace("PLCAPI/",""),)
70 return "Unknown peer_name"
74 def site_compare (s1,s2):
76 if s1['peer_id']: p1=s1['peer_id']
77 if s2['peer_id']: p2=s2['peer_id']
82 if self.options.nodegroup:
83 self.refresh_nodegroup()
85 self.refresh_all_sites()
87 def refresh_all_sites(self):
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)
99 def refresh_nodegroup(self):
101 nodegroup=GetNodeGroups({'groupname':self.options.nodegroup})[0]
103 print "No such nodegroup %s - ignored"%self.options.nodegroup
105 nodegroup_node_ids=nodegroup['node_ids']
106 if len(nodegroup_node_ids)==0:
107 print "Empty nodegroup %s - ignored"%self.options.nodegroup
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
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)
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']:
132 # discard sites with no nodes
133 if len(site['node_ids']) == 0:
136 site_id=site['site_id']
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']
147 # the size for google icons
148 if not self.options.use_custom_icons:
151 iconfile=default_local_builtin
152 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
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
161 iconfile=self.options.local_icon
163 iconfile=self.options.foreign_icon
164 iconurl="%(baseurl)s/%(iconfile)s"%locals()
167 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
170 # can't seem to get classes to get through to the google maps API
171 # so have to use hard-wired settings
173 description += "<table style='border: 1px solid black; padding: 3px; margin-top:5px;' width='300px'>"
174 description += "<thead></thead><tbody>"
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>"
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>"
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>"
207 description += "<tr>"
208 description += "<td style='font-weight: bold; margin-bottom:2px;'>Usage</td>"
210 # encapsulate usage in a table of its own
211 description += "<td>"
212 description += "<table style=''>"
213 description += "<thead></thead><tbody>"
216 # regular all-sites mode
218 description += "<tr><td align='center'>"
219 description += "<img src='%(apiurl)s/googlemap/node.png'/>"%locals()
220 description += "</td><td>"
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()
224 description += "<i>No node</i>"
225 description += "</td></tr>"
226 # nodegroup mode : show all nodes
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>"
239 description += "<tr><td align='center'>"
240 description += "<img src='%(apiurl)s/googlemap/slice.png'/>"%locals()
241 description += "</td><td>"
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()
245 description += "<span style='font-style:italic;'>No slice</span>"
246 description += "</td></tr>"
249 description += "</tbody></table>"
250 description += "</td></tr>"
253 description += "</tbody></table>"
255 if not self.options.labels:
259 # set the camera 50km high
260 template="""<Placemark>
261 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
262 <name><![CDATA[%(name)s]]></name>
264 <latitude>%(latitude)f</latitude>
265 <longitude>%(longitude)f</longitude>
266 <altitude>0</altitude>
267 <altitudeMode>relativeToGround</altitudeMode>
268 <range>50000.</range>
270 <description><![CDATA[%(description)s]]></description>
271 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
274 self.write(template%locals())
277 from optparse import OptionParser
278 usage = "Usage %prog [plcsh-options] [ -- options ]"
279 parser = OptionParser (usage=usage)
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",
286 help="outputs only geographic positions, no labels")
288 parser.add_option("-g","--nodegroup",action='store',dest='nodegroup',default=None,
289 help="outputs a kml file for a given nodegroup only")
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",
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)
302 (options, args) = parser.parse_args()
306 KmlMap(options.output,options).refresh()
309 if __name__ == "__main__":