use the FQDN for PLC_WWW_HOST rather than localhost to get cron.php
[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 default_local_icon   = "sites/google-local.png"
12 default_foreign_icon = "sites/google-foreign.png"
13
14 class KmlMap:
15
16     def __init__ (self,outputname,options):
17         self.outputname=outputname
18         self.options=options
19
20     def open (self):
21         self.output = open(self.outputname,"w")
22
23     def close (self):
24         if self.output:
25             self.output.close()
26         self.output = None
27
28     def write(self,string):
29         self.output.write(string.encode("UTF-8"))
30
31     def refresh (self):
32         self.open()
33         self.write_header()
34         # cache peers 
35         peers = GetPeers({},['peer_id','peername'])
36         for site in GetSites({'enabled':True,'is_public':True}):
37             self.write_site(site,peers)
38         self.write_footer()
39         self.close()
40
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">
44 <Document>
45 <name> PlanetLab Sites </name>
46 <description> All the sites known to the PlanetLab testbed. </description>
47 """)
48
49     def write_footer (self):
50         self.write("""</Document></kml>
51 """)
52
53     def peer_name (self,site, peers):
54         if not site['peer_id']:
55             return "local"
56         for peer in peers:
57             if peer['peer_id'] == site['peer_id']:
58                 return peer['peername']
59
60     def write_site (self, site, peers):
61         # discard sites with missing lat or lon
62         if not site['latitude'] or not site['longitude']:
63             return
64         # discard sites with no nodes 
65         if len(site['node_ids']) == 0:
66             return
67
68         site_id=site['site_id']
69         name=site['name']
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']
77
78         # open description
79         description='<ul>'
80         # Name and URL
81         description += '<li>'
82         description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
83         if site['url']:
84             site_url=site['url']
85             description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
86         description += '</li>'
87         # NODES
88         if nb_nodes:
89             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>'
93         else:
94             description += '<li>No node</li>'
95         #SLICES
96         if nb_slices:
97             description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
98         else:
99             description += '<li>No slice</li>'
100         # PEER
101         if peer_id:
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>'
106         # close description
107         description +='</ul>'
108
109         # STYLE
110         if self.options.use_google_icons:
111             if not peer_id:
112                 # local sites
113                 iconfile="palette-4.png"
114                 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
115             else:
116                 # remote
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()
120         else:
121             if not peer_id:
122                 iconfile=self.options.local_icon
123             else:
124                 iconfile=self.options.foreign_icon
125             iconurl="%(baseurl)s/%(iconfile)s"%locals()
126             xyspec=""
127
128         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
129
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>
135 </Placemark>
136 """
137         self.write(template%locals())
138
139 def main () :
140     from optparse import OptionParser
141     usage = "Usage %prog [plcsh-options] [ -- options ]"
142     parser = OptionParser (usage=usage)
143
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",
148                       default=True,
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()
157     if len(args) != 0:
158         parser.print_help()
159         sys.exit(1)
160     KmlMap(options.output,options).refresh()
161
162 ####################
163 if __name__ == "__main__":
164     main()