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"
11 default_local_icon = "sites/google-local.png"
12 default_foreign_icon = "sites/google-foreign.png"
16 def __init__ (self,outputname,options):
17 self.outputname=outputname
21 self.output = open(self.outputname,"w")
28 def write(self,string):
29 self.output.write(string.encode("UTF-8"))
35 peers = GetPeers({},['peer_id','peername'])
36 for site in GetSites({'enabled':True,'is_public':True}):
37 self.write_site(site,peers)
41 def write_header (self):
42 self.write("""<?xml version="1.0" encoding="UTF-8"?>
43 <kml xmlns="http://earth.google.com/kml/2.2">
45 <name> PlanetLab Sites </name>
46 <description> This map shows all sites knows to the PlanetLab testbed. </description>
49 def write_footer (self):
50 self.write("""</Document></kml>
53 def peer_name (self,site, peers):
54 if not site['peer_id']:
57 if peer['peer_id'] == site['peer_id']:
58 return peer['peername']
60 def write_site (self, site, peers):
61 # discard sites with missing lat or lon
62 if not site['latitude'] or not site['longitude']:
64 # discard sites with no nodes
65 if len(site['node_ids']) == 0:
68 site_id=site['site_id']
70 nb_nodes=len(site['node_ids'])
71 nb_slices=len(site['slice_ids'])
72 latitude=site['latitude']
73 longitude=site['longitude']
74 apiurl='https://%s:443'%api.config.PLC_WWW_HOST
75 baseurl='http://%s'%api.config.PLC_WWW_HOST
76 peer_id=site['peer_id']
82 description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
85 description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
86 description += '</li>'
90 description += '<a href="%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d node(s)</a>'%locals()
91 description += '<a href="%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
92 description += '</li>'
94 description += '<li>No node</li>'
97 description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
99 description += '<li>No slice</li>'
102 peername = self.peer_name(site,peers)
103 description += '<li>'
104 description += '<a href="%(apiurl)s/db/peers/index.php?id=%(peer_id)d">At peer %(peername)s</a>'%locals()
105 description += '</li>'
107 description +='</ul>'
110 if self.options.use_google_icons:
113 iconfile="palette-4.png"
114 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
117 iconfile="palette-3.png"
118 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
119 iconurl="root://icons/%(iconfile)s"%locals()
122 iconfile=self.options.local_icon
124 iconfile=self.options.foreign_icon
125 iconurl="%(baseurl)s/%(iconfile)s"%locals()
128 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
130 template="""<Placemark>
131 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
132 <name><![CDATA[%(name)s]]></name>
133 <description><![CDATA[%(description)s]]></description>
134 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
137 self.write(template%locals())
140 from optparse import OptionParser
141 usage = "Usage %prog [plcsh-options] [ -- options ]"
142 parser = OptionParser (usage=usage)
144 parser.add_option("-o","--output",action="store",dest="output",
145 default=default_output,
146 help="output file - default is %s"%default_output)
147 parser.add_option("-c","--custom",action="store_false",dest="use_google_icons",
149 help="use locally customized icons rather than the google-provided defaults")
150 parser.add_option("-l","--local",action="store",dest="local_icon",
151 default=default_local_icon,
152 help="set icon url to use for local sites marker -- default is %s"%default_local_icon)
153 parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
154 default=default_foreign_icon,
155 help="set icon url to use for foreign sites marker -- default is %s"%default_foreign_icon)
156 (options, args) = parser.parse_args()
160 KmlMap(options.output,options).refresh()
163 if __name__ == "__main__":