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
10 default_output="/var/www/html/sites/sites.kml"
14 def __init__ (self,outputname):
15 self.outputname=outputname
18 self.output = open(self.outputname,"w")
29 peers = GetPeers({},['peer_id','peername'])
30 for site in GetSites({'enabled':True,'is_public':True}):
31 self.write_site(site,peers)
35 def write(self,string):
36 self.output.write(string.encode("UTF-8"))
38 def write_header (self):
39 self.write("""<?xml version="1.0" encoding="UTF-8"?>
40 <kml xmlns="http://earth.google.com/kml/2.2">
42 <name> PlanetLab Sites </name>
43 <description> This map shows all sites knows to the PlanetLab testbed. </description>
46 def write_footer (self):
47 self.write("""</Document></kml>
50 def peer_name (self,site, peers):
51 if not site['peer_id']:
54 if peer['peer_id'] == site['peer_id']:
55 return peer['peername']
57 def write_site (self, site, peers):
58 # discard sites with missing lat or lon
59 if not site['latitude'] or not site['longitude']:
61 # discard sites with no nodes
62 if len(site['node_ids']) == 0:
65 site_id=site['site_id']
66 site_name=site['name']
67 nb_nodes=len(site['node_ids'])
68 nb_slices=len(site['slice_ids'])
69 latitude=site['latitude']
70 longitude=site['longitude']
71 baseurl='https://%s:443'%api.config.PLC_WWW_HOST
73 name = '<h3> <a href="%(baseurl)s/db/sites/index.php?id=%(site_id)d"> %(site_name)s </a></h3>'%locals()
78 description += '<li>URL:<a href="%(site_url)s"> %(site_url)s </a></li>'%locals()
82 description += '<a href="%(baseurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d nodes</a>'%locals()
83 description += '<a href="%(baseurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
84 description += '</li>'
86 description += '<li>No node</li>'
89 description += '<li><a href="%(baseurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slices</a></li>'%locals()
91 description += '<li>No slice</li>'
96 # iconfile="google-local.png"
98 # iconfile="google-foreign.png"
99 # iconurl="http://%(base)s/misc/%(iconfile)s"%locals()
102 if not site['peer_id']:
104 iconurl="root://icons/palette-3.png"
105 xyspec="<x>0</x><y>0</y><w>32</w><h>32</h>"
108 iconurl="root://icons/palette-3.png"
109 xyspec="<x>32</x><y>0</y><w>32</w><h>32</h>"
111 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
113 template="""<Placemark>
114 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
115 <name><![CDATA[%(name)s]]></name>
116 <description><![CDATA[%(description)s]]></description>
117 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
120 self.write(template%locals())
124 # print 'description',description
126 # print template%locals()
128 if __name__ == "__main__":
129 if len(sys.argv) == 1:
131 elif len(sys.argv) == 2:
134 print "Usage: %s [output]"%sys.argv[0]
135 print "default output is %s"%default_output
137 KmlMap(out).refresh()