01522177137bbd359ebdec0881d20a95ba9f689d
[myplc.git] / 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 import sys
9
10 default_output="/var/www/html/sites/sites.kml"
11
12 class KmlMap:
13
14     def __init__ (self,outputname):
15         self.outputname=outputname
16
17     def open (self):
18         self.output = open(self.outputname,"w")
19
20     def close (self):
21         if self.output:
22             self.output.close()
23         self.output = None
24
25     def refresh (self):
26         self.open()
27         self.write_header()
28         # cache peers 
29         peers = GetPeers({},['peer_id','peername'])
30         for site in GetSites({'enabled':True,'is_public':True}):
31             self.write_site(site,peers)
32         self.write_footer()
33         self.close()
34
35     def write(self,string):
36         self.output.write(string.encode("UTF-8"))
37
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">
41 <Document>
42 <name> PlanetLab Sites </name>
43 <description> This map shows all sites knows to the PlanetLab testbed. </description>
44 """)
45
46     def write_footer (self):
47         self.write("""</Document></kml>
48 """)
49
50     def peer_name (self,site, peers):
51         if not site['peer_id']:
52             return "local"
53         for peer in peers:
54             if peer['peer_id'] == site['peer_id']:
55                 return peer['peername']
56
57     def write_site (self, site, peers):
58         # discard sites with missing lat or lon
59         if not site['latitude'] or not site['longitude']:
60             return
61         # discard sites with no nodes 
62         if len(site['node_ids']) == 0:
63             return
64
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
72         # name
73         name = '<h3> <a href="%(baseurl)s/db/sites/index.php?id=%(site_id)d"> %(site_name)s </a></h3>'%locals()
74         description='<ul>'
75         # URL
76         if site['url']:
77             site_url=site['url']
78             description += '<li>URL:<a href="%(site_url)s"> %(site_url)s </a></li>'%locals()
79         # NODES
80         if nb_nodes:
81             description += '<li>'
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>'
85         else:
86             description += '<li>No node</li>'
87         #SLICES
88         if nb_slices:
89             description += '<li><a href="%(baseurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slices</a></li>'%locals()
90         else:
91             description += '<li>No slice</li>'
92
93         description +='</ul>'
94
95
96 #        iconfile="google-local.png"
97 #        if site['peer_id']:
98 #            iconfile="google-foreign.png"
99 #        iconurl="http://%(base)s/misc/%(iconfile)s"%locals()
100 #        xyspec=""
101
102         if not site['peer_id']:
103             # local sites
104             iconurl="root://icons/palette-3.png"
105             xyspec="<x>0</x><y>0</y><w>32</w><h>32</h>"
106         else:
107             # remote
108             iconurl="root://icons/palette-3.png"
109             xyspec="<x>32</x><y>0</y><w>32</w><h>32</h>"
110             
111         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
112
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>
118 </Placemark>
119 """
120         self.write(template%locals())
121
122         
123 #        print 'name',name
124 #        print 'description',description
125 #        print template
126 #        print template%locals()
127
128 if __name__ == "__main__":
129     if len(sys.argv) == 1:
130         out=default_output
131     elif len(sys.argv) == 2:
132         out=sys.argv[1]
133     else:
134         print "Usage: %s [output]"%sys.argv[0]
135         print "default output is %s"%default_output
136         sys.exit(1)
137     KmlMap(out).refresh()