netboot: Allow booting multiple architectures
[people/shoehn/ipfire.org.git] / webapp / __init__.py
CommitLineData
81675874 1#/usr/bin/python
2
feb02477 3import logging
e6d36e0a 4import multiprocessing
81675874 5import os.path
8ccd2ff0 6import simplejson
feb02477 7import tornado.httpserver
81675874 8import tornado.locale
81675874 9import tornado.web
10
18e60079
MT
11from tornado.options import options
12
940227cb 13import backend
feb02477 14
8ccd2ff0
MT
15from handlers import *
16from ui_modules import *
81675874 17
18BASEDIR = os.path.join(os.path.dirname(__file__), "..")
19
60024cc8
MT
20# Load translations.
21tornado.locale.load_gettext_translations(os.path.join(BASEDIR, "translations"), "webapp")
81675874 22
23class Application(tornado.web.Application):
9068dba1 24 def __init__(self, **kwargs):
a6dc0bad
MT
25 self.__backend = None
26
81675874 27 settings = dict(
28 cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==",
18e60079 29 debug = options.debug,
81675874 30 gzip = True,
d0d074e0 31 login_url = "/login",
81675874 32 template_path = os.path.join(BASEDIR, "templates"),
cc3b928d
MT
33 ui_methods = {
34 "format_month_name" : self.format_month_name,
35 },
81675874 36 ui_modules = {
9068dba1
MT
37 "Advertisement" : AdvertisementModule,
38 "DonationBox" : DonationBoxModule,
e00c06b9 39 "DonationButton" : DonationButtonModule,
60b0917c 40 "DonationInputBox" : DonationInputBoxModule,
9068dba1 41 "DownloadButton" : DownloadButtonModule,
66862195 42 "LanguageName" : LanguageNameModule,
9068dba1
MT
43 "Map" : MapModule,
44 "Menu" : MenuModule,
45 "MirrorItem" : MirrorItemModule,
46 "MirrorsTable" : MirrorsTableModule,
47 "NetBootMenuConfig" : NetBootMenuConfigModule,
48 "NetBootMenuHeader" : NetBootMenuHeaderModule,
49 "NetBootMenuSeparator" : NetBootMenuSeparatorModule,
50 "NewsItem" : NewsItemModule,
51 "NewsLine" : NewsLineModule,
52 "NewsTable" : NewsTableModule,
53 "NewsYearNavigation" : NewsYearNavigationModule,
d88b8f41 54 "PlanetAuthorBox" : PlanetAuthorBoxModule,
9068dba1 55 "PlanetEntry" : PlanetEntryModule,
fa7e1a0a 56 "PlanetSearchBox" : PlanetSearchBoxModule,
66862195 57 "ProgressBar" : ProgressBarModule,
9068dba1
MT
58 "ReleaseItem" : ReleaseItemModule,
59 "SidebarBanner" : SidebarBannerModule,
60 "SidebarRelease" : SidebarReleaseModule,
66862195
MT
61 "FireinfoDeviceTable" : FireinfoDeviceTableModule,
62 "FireinfoDeviceAndGroupsTable" : FireinfoDeviceAndGroupsTableModule,
63 "FireinfoGeoTable" : FireinfoGeoTableModule,
64 "TalkCallLog" : TalkCallLogModule,
77431b9c 65 "TalkLines" : TalkLinesModule,
66862195 66 "TalkOngoingCalls" : TalkOngoingCallsModule,
9068dba1
MT
67 "TrackerPeerList" : TrackerPeerListModule,
68 "Wish" : WishModule,
69 "Wishlist" : WishlistModule,
e64ce07e 70 "WishlistItems" : WishlistItemsModule,
81675874 71 },
72 xsrf_cookies = True,
73 )
9068dba1 74 settings.update(kwargs)
5cf160e0 75
ae0228e1
MT
76 tornado.web.Application.__init__(self, **settings)
77
78 self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static")
79 static_handlers = [
80 (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)),
81 (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)),
82 (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)),
83 ]
84
66862195
MT
85 authentication_handlers = [
86 (r"/login", LoginHandler),
87 (r"/logout", LogoutHandler),
88 ]
89
d4dc517c 90 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
91 # Entry site that lead the user to index
92 (r"/", IndexHandler),
93 (r"/index\.?(s?html?)?", RootHandler),
94
95 # Handle news items
940227cb 96 (r"/news", NewsIndexHandler),
7771acea 97 (r"/news/year/([0-9]*)", NewsYearHandler),
940227cb 98 (r"/news/(.*)", NewsItemHandler),
940227cb 99
81675874 100 # Download sites
60b0917c
MT
101 (r"/download", DownloadHandler),
102 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
940227cb 103
8d48f4ef
MT
104 # Handle old pages that have moved elsewhere
105 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
60024cc8 106 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
c801ee56 107 (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }),
60b0917c 108 (r"/getinvolved", tornado.web.RedirectHandler, { "url" : "/get-involved" }),
60024cc8 109
e64ce07e
MT
110 # Donate
111 (r"/donate", DonateHandler),
112 (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }),
8d48f4ef 113
de683d7c 114 # RSS feed
bcc3ed4d 115 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
116
117 # Redirection for bookmarks, etc.
940227cb
MT
118 (r"/(de|en)/(.*)", LangCompatHandler)
119
120 ] + static_handlers + [
81675874 121 # Always the last rule
940227cb
MT
122 (r"/(.*)", StaticHandler),
123 ])
124
940227cb 125 # downloads.ipfire.org
80594ae3 126 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
940227cb
MT
127 (r"/", DownloadsIndexHandler),
128 (r"/latest", DownloadsLatestHandler),
9068dba1
MT
129 (r"/release/(\d)", DownloadsReleaseHandler),
130 (r"/release/([\w\.\-]*)", DownloadsReleaseHandler),
940227cb
MT
131 (r"/older", DownloadsOlderHandler),
132 (r"/development", DownloadsDevelopmentHandler),
133 (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }),
134 (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }),
60024cc8 135 (r"/download-splash", DownloadSplashHandler),
54af860e 136 ] + static_handlers + [
edd297c4 137 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
138 (r"/(.*)", DownloadFileHandler),
139 ])
940227cb
MT
140
141 # mirrors.ipfire.org
8fceca0a 142 self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
940227cb 143 (r"/", MirrorIndexHandler),
9068dba1 144 (r"/mirror/(.*)", MirrorItemHandler),
0a003782 145 (r"/lists/pakfire2", MirrorListPakfire2Handler),
940227cb
MT
146 ] + static_handlers)
147
d0d074e0 148 # planet.ipfire.org
7cad1818 149 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
940227cb 150 (r"/", PlanetMainHandler),
d88b8f41 151 (r"/hottest", PlanetHotEntriesHandler),
d0d074e0 152 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 153 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 154 (r"/search", PlanetSearchHandler),
cc3b928d 155 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d
MT
156
157 # RSS
158 (r"/rss", RSSPlanetAllHandler),
159 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
20ccd136 160 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
d0d074e0
MT
161 ] + static_handlers)
162
66862195 163 # fireinfo.ipfire.org
8fceca0a 164 self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
66862195
MT
165 (r"/", FireinfoIndexHandler),
166 (r"/device/driver/(.*)", FireinfoDeviceDriverDetail),
167 (r"/device/vendors", FireinfoDeviceVendorsHandler),
168 (r"/device/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorHandler),
169 (r"/device/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelHandler),
170
171 # Show profiles
172 (r"/profile/random", FireinfoRandomProfileHandler),
173 (r"/profile/([a-z0-9]{40})", FireinfoProfileDetailHandler),
91a446f0 174
30852a9e 175 # Send profiles.
66862195
MT
176 (r"/send/([a-z0-9]+)", FireinfoProfileSendHandler),
177
178 # Stats handlers
179 (r"/statistics", FireinfoStatsHandler),
180 (r"/statistics/processors", FireinfoStatsProcessorsHandler),
181 (r"/statistics/processors/(arm|x86)", FireinfoStatsProcessorDetailHandler),
182 (r"/statistics/geo-locations", FireinfoStatsGeoHandler),
183 (r"/statistics/languages", FireinfoStatsLanguagesHandler),
184 (r"/statistics/memory", FireinfoStatsMemoryHandler),
185 (r"/statistics/networking", FireinfoStatsNetworkingHandler),
186 (r"/statistics/releases", FireinfoStatsReleasesHandler),
187 (r"/statistics/virtualization", FireinfoStatsVirtualHandler),
188
189 # Compat handlers
190 (r"/stats", tornado.web.RedirectHandler, { "url" : "/statistics" }),
191 (r"/stats/cpus", tornado.web.RedirectHandler, { "url" : "/statistics/processors" }),
192 (r"/stats/geo", tornado.web.RedirectHandler, { "url" : "/statistics/geo-locations" }),
193 (r"/stats/network", tornado.web.RedirectHandler, { "url" : "/statistics/networking" }),
194 (r"/stats/oses", tornado.web.RedirectHandler, { "url" : "/statistics/releases" }),
195 (r"/stats/virtual", tornado.web.RedirectHandler, { "url" : "/statistics/virtualization" }),
196 (r"/vendor/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorCompatHandler),
197 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelCompatHandler),
ae0228e1 198 ] + static_handlers)
5cf160e0 199
c37ec602 200 # i-use.ipfire.org
8fceca0a 201 self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
c37ec602
MT
202 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
203 ])
204
5cf160e0 205 # tracker.ipfire.org
8fceca0a 206 self.add_handlers(r"(torrent|tracker)(\.dev)?\.ipfire\.org", [
940227cb 207 (r"/", TrackerIndexHandler),
344a3d62 208 (r"/announce.*", TrackerAnnounceHandler),
e2afbd6a 209 (r"/scrape", TrackerScrapeHandler),
940227cb 210 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
fadcfd00
MT
211 (r"/([0-9a-f]{40})", TrackerDetailHandler),
212 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
ae0228e1
MT
213 ] + static_handlers)
214
8e2e1261
MT
215 # boot.ipfire.org
216 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
8fceca0a 217 self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [
8e2e1261
MT
218 (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }),
219
220 # Configurations
37b5c0cf 221 (r"/premenu.cfg", PremenuCfgHandler),
8e2e1261
MT
222 (r"/menu.gpxe", MenuGPXEHandler),
223 (r"/menu.cfg", MenuCfgHandler),
8e2e1261
MT
224
225 # Static files
37b5c0cf 226 (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
227 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
228 ])
229
60024cc8 230 # nopaste.ipfire.org
8fceca0a 231 self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
66862195
MT
232 (r"/", NopasteCreateHandler),
233 (r"/raw/(.*)", NopasteRawHandler),
234 (r"/view/(.*)", NopasteViewHandler),
235 ] + authentication_handlers + static_handlers)
60024cc8 236
7771acea 237 # wishlist.ipfire.org
e64ce07e 238 self.add_handlers(r"wishlist(\.dev)?\.ipfire\.org", [
7771acea 239 (r"/", WishlistIndexHandler),
1bf8d482 240 (r"/closed", WishlistClosedHandler),
7771acea
MT
241 (r"/wish/(.*)/donate", WishDonateHandler),
242 (r"/wish/(.*)", WishHandler),
243 (r"/terms", WishlistTermsHandler),
244 ] + static_handlers)
245
9068dba1 246 # geoip.ipfire.org
8fceca0a 247 self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
9068dba1
MT
248 (r"/", GeoIPHandler),
249 ] + static_handlers)
250
66862195
MT
251 # talk.ipfire.org
252 self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [
253 (r"/", TalkIndexHandler),
254 (r"/call/(\d+)", TalkInitiateCallHandler),
255 (r"/conferences", TalkConferencesHandler),
256 (r"/conferences/(\d+)/invite/(\d+)", TalkConferencesInviteHandler),
257 (r"/diagnosis", TalkDiagnosisHandler),
258 (r"/phonebook/(\w+)", TalkPhonebookAccountHandler),
259 (r"/phonebook", TalkPhonebookHandler),
260 (r"/profile", TalkProfileHandler),
66862195
MT
261 ] + authentication_handlers + static_handlers)
262
2cd9af74
MT
263 # accounts.ipfire.org
264 self.add_handlers(r"accounts(\.dev)?\.ipfire\.org", [
265 (r"/avatar/(\w+)\.jpg", AccountsAvatarHandler),
266 ] + static_handlers)
267
d0d074e0 268 # admin.ipfire.org
7cad1818 269 self.add_handlers(r"admin(\.dev)?\.ipfire\.org", [
d0d074e0 270 (r"/", AdminIndexHandler),
d0d074e0
MT
271 # Accounts
272 (r"/accounts", AdminAccountsHandler),
940227cb
MT
273 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
274 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
275 # Planet
276 (r"/planet", AdminPlanetHandler),
277 (r"/planet/compose", AdminPlanetComposeHandler),
67ab72b8
MT
278 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
279 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
940227cb
MT
280 # Mirrors
281 (r"/mirrors", AdminMirrorsHandler),
282 (r"/mirrors/create", AdminMirrorsCreateHandler),
283 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
284 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
285 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
286 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7 287 # Fireinfo
66862195 288 (r"/fireinfo", AdminFireinfoHandler),
60024cc8
MT
289 # Downloads
290 (r"/downloads", AdminDownloadsHandler),
291 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0
MT
292 # API
293 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
66862195 294 ] + authentication_handlers + static_handlers)
d0d074e0 295
ae0228e1 296 # ipfire.org
3add293a 297 self.add_handlers(r".*", [
ae0228e1 298 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 299 ])
3add293a 300
feb02477
MT
301 logging.info("Successfully initialied application")
302
303 self.__running = True
304
3add293a 305 def __del__(self):
feb02477
MT
306 logging.info("Shutting down application")
307
a6dc0bad
MT
308 @property
309 def backend(self):
310 if self.__backend is None:
9068dba1
MT
311 configfile = self.settings.get("configfile", None)
312 if not configfile:
313 raise RuntimeException("Could not find configuration file")
314
2cd9af74
MT
315 self.__backend = backend.Backend(configfile=configfile,
316 debug=self.settings.get("debug", False))
a6dc0bad
MT
317
318 return self.__backend
319
feb02477
MT
320 @property
321 def ioloop(self):
322 return tornado.ioloop.IOLoop.instance()
323
940227cb 324 def shutdown(self, *args):
feb02477
MT
325 logging.debug("Caught shutdown signal")
326 self.ioloop.stop()
327
328 self.__running = False
329
330 def run(self, port=8001):
331 logging.debug("Going to background")
332
333 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 334
cef37a4a
MT
335 num_processes = multiprocessing.cpu_count() / 2
336
940227cb
MT
337 # If we are not running in debug mode, we can actually run multiple
338 # frontends to get best performance out of our service.
339 if not self.settings["debug"]:
340 http_server.bind(port)
cef37a4a 341 http_server.start(num_processes=num_processes)
940227cb
MT
342 else:
343 http_server.listen(port)
de683d7c
MT
344
345 # All requests should be done after 30 seconds or they will be killed.
346 self.ioloop.set_blocking_log_threshold(30)
feb02477 347
940227cb 348 self.ioloop.start()
feb02477
MT
349
350 def reload(self):
351 logging.debug("Caught reload signal")
cc3b928d
MT
352
353 def format_month_name(self, handler, month):
354 _ = handler.locale.translate
355
356 if month == 1:
357 return _("January")
358 elif month == 2:
359 return _("February")
360 elif month == 3:
361 return _("March")
362 elif month == 4:
363 return _("April")
364 elif month == 5:
365 return _("May")
366 elif month == 6:
367 return _("June")
368 elif month == 7:
369 return _("July")
370 elif month == 8:
371 return _("August")
372 elif month == 9:
373 return _("September")
374 elif month == 10:
375 return _("October")
376 elif month == 11:
377 return _("November")
378 elif month == 12:
379 return _("December")
380
381 return month