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