switch from php script to a static sysctl.conf file
[myplc.git] / plc-kml.py
1 #!/usr/bin/env plcsh
2 #
3 # $Id$
4
5 # this script generates a kml file, located under the default location below
6 # you should crontab this job from your myplc image
7 # you can then use the googlemap.js javascript for creating your applet
8 # more on this at http://svn.planet-lab.org/wiki/GooglemapSetup
9
10 # kml reference can be found at
11 # http://code.google.com/apis/kml/documentation/kmlreference.html
12 #
13
14 import sys
15
16 default_output       = "/var/www/html/sites/sites.kml"
17 default_local_icon   = "sites/google-local.png"
18 default_foreign_icon = "sites/google-foreign.png"
19
20 class KmlMap:
21
22     def __init__ (self,outputname,options):
23         self.outputname=outputname
24         self.options=options
25
26     def open (self):
27         self.output = open(self.outputname,"w")
28
29     def close (self):
30         if self.output:
31             self.output.close()
32         self.output = None
33
34     def write(self,string):
35         self.output.write(string.encode("UTF-8"))
36
37     @staticmethod
38     def site_compare (s1,s2):
39         n1=s1['name']
40         n2=s2['name']
41         if n1<n2:
42             return -1
43         elif n1>n2:
44             return 1
45         else:
46             return 0
47
48     def refresh (self):
49         self.open()
50         self.write_header()
51         # cache peers 
52         peers = GetPeers({},['peer_id','peername'])
53         all_sites = GetSites({'enabled':True,'is_public':True})
54         all_sites.sort(KmlMap.site_compare)
55         for site in all_sites:
56             self.write_site(site,peers)
57         self.write_footer()
58         self.close()
59
60 # initial placement is for europe - dunno how to tune that yet
61     def write_header (self):
62         self.write("""<?xml version="1.0" encoding="UTF-8"?>
63 <kml xmlns="http://earth.google.com/kml/2.2">
64 <Document>
65 <name> PlanetLab Sites </name>
66 <LookAt>
67 <longitude>9.180821112577378</longitude>
68 <latitude>44.43275321178062</latitude>
69 <altitude>0</altitude>
70 <range>5782133.196489797</range>
71 <tilt>0</tilt>
72 <heading>-7.767386340832667</heading>
73 </LookAt>
74 <description> All the sites known to the PlanetLab testbed. </description>
75 """)
76
77     def write_footer (self):
78         self.write("""</Document></kml>
79 """)
80
81     def peer_name (self,site, peers):
82         if not site['peer_id']:
83             return "local"
84         for peer in peers:
85             if peer['peer_id'] == site['peer_id']:
86                 return peer['peername']
87
88     def write_site (self, site, peers):
89         # discard sites with missing lat or lon
90         if not site['latitude'] or not site['longitude']:
91             return
92         # discard sites with no nodes 
93         if len(site['node_ids']) == 0:
94             return
95
96         site_id=site['site_id']
97         name=site['name']
98         nb_nodes=len(site['node_ids'])
99         nb_slices=len(site['slice_ids'])
100         latitude=site['latitude']
101         longitude=site['longitude']
102         apiurl='https://%s:443'%api.config.PLC_WWW_HOST
103         baseurl='http://%s'%api.config.PLC_WWW_HOST
104         peer_id=site['peer_id']
105
106         # open description
107         description='<ul>'
108         # Name and URL
109         description += '<li>'
110         description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
111         if site['url']:
112             site_url=site['url']
113             description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
114         description += '</li>'
115         # NODES
116         if nb_nodes:
117             description += '<li>'
118             description += '<a href="%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d node(s)</a>'%locals()
119             description += '<a href="%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
120             description += '</li>'
121         else:
122             description += '<li>No node</li>'
123         #SLICES
124         if nb_slices:
125             description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
126         else:
127             description += '<li>No slice</li>'
128         # PEER
129         if peer_id:
130             peername = self.peer_name(site,peers)
131             description += '<li>'
132             description += '<a href="%(apiurl)s/db/peers/index.php?id=%(peer_id)d">At peer %(peername)s</a>'%locals()
133             description += '</li>'
134         # close description
135         description +='</ul>'
136
137         if not self.options.labels:
138             name=""
139             description=""
140
141         # STYLE
142         if self.options.use_google_icons:
143             if not peer_id:
144                 # local sites
145                 iconfile="palette-4.png"
146                 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
147             else:
148                 # remote
149                 iconfile="palette-3.png"
150                 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
151             iconurl="root://icons/%(iconfile)s"%locals()
152         else:
153             if not peer_id:
154                 iconfile=self.options.local_icon
155             else:
156                 iconfile=self.options.foreign_icon
157             iconurl="%(baseurl)s/%(iconfile)s"%locals()
158             xyspec=""
159
160         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
161
162         # set the camera 50km high
163         template="""<Placemark>
164 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
165 <name><![CDATA[%(name)s]]></name>
166 <LookAt>
167   <latitude>%(latitude)f</latitude>
168   <longitude>%(longitude)f</longitude>
169   <altitude>0</altitude>
170   <altitudeMode>relativeToGround</altitudeMode>              
171   <range>50000.</range> 
172 </LookAt>
173 <description><![CDATA[%(description)s]]></description>
174 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
175 </Placemark>
176 """
177         self.write(template%locals())
178
179 def main () :
180     from optparse import OptionParser
181     usage = "Usage %prog [plcsh-options] [ -- options ]"
182     parser = OptionParser (usage=usage)
183
184     parser.add_option("-o","--output",action="store",dest="output",
185                       default=default_output,
186                       help="output file - default is %s"%default_output)
187     parser.add_option("-n","--no-label",action="store_false",dest="labels",
188                       default=True,
189                       help="outputs only geographic positions, no labels")
190     parser.add_option("-c","--custom",action="store_false",dest="use_google_icons",
191                       default=True,
192                       help="use locally customized icons rather than the google-provided defaults")
193     parser.add_option("-l","--local",action="store",dest="local_icon",
194                       default=default_local_icon,
195                       help="set icon url to use for local sites marker -- default is %s"%default_local_icon)
196     parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
197                       default=default_foreign_icon,
198                       help="set icon url to use for foreign sites marker -- default is %s"%default_foreign_icon)
199
200     (options, args) = parser.parse_args()
201     if len(args) != 0:
202         parser.print_help()
203         sys.exit(1)
204     KmlMap(options.output,options).refresh()
205
206 ####################
207 if __name__ == "__main__":
208     main()