6fed8dd7878600dbdfe612a85872ed6a9f8b98e9
[people/shoehn/ipfire.org.git] / webapp / handlers_boot.py
1 #!/usr/bin/python
2
3 import logging
4 import os
5 import tornado.httpserver
6 import tornado.ioloop
7 import tornado.locale
8 import tornado.options
9 import tornado.web
10
11 import backend
12
13 from handlers_base import BaseHandler
14
15 BASEDIR = os.path.dirname(__file__)
16
17 def word_wrap(s, width=45):
18         paragraphs = s.split('\n')
19         lines = []
20         for paragraph in paragraphs:
21                 while len(paragraph) > width:
22                         pos = paragraph.rfind(' ', 0, width)
23                         if not pos:
24                                 pos = width
25                         lines.append(paragraph[:pos])
26                         paragraph = paragraph[pos:]
27                 lines.append(paragraph.lstrip())
28         return '\n'.join(lines)
29
30 class BootBaseHandler(BaseHandler):
31         pass
32
33
34 class MenuGPXEHandler(BootBaseHandler):
35         """
36                 menu.gpxe
37         """
38         def get(self):
39                 # Check if version of the bootloader is recent enough.
40                 # Otherwise send the latest version of the PXE loader.
41                 user_agent = self.request.headers.get("User-Agent", None)
42                 if user_agent:
43                         try:
44                                 client, version = user_agent.split("/")
45                         except:
46                                 pass
47                         else:
48                                 # We replaced gPXE by iPXE.
49                                 if client == "gPXE":
50                                         return self.serve_update()
51
52                                 # Everything under version 1.0.0 should be
53                                 # updated.
54                                 if version < "1.0.0":
55                                         return self.serve_update()
56
57                 # Devliver content
58                 self.set_header("Content-Type", "text/plain")
59                 self.write("#!gpxe\n")
60
61                 self.write("set 209:string premenu.cfg\n")
62                 self.write("set 210:string http://boot.ipfire.org/\n")
63                 self.write("chain pxelinux.0\n")
64
65         def serve_update(self):
66                 self.set_header("Content-Type", "text/plain")
67                 self.write("#!gpxe\n")
68
69                 # Small warning
70                 self.write("echo\necho Your copy of gPXE/iPXE is too old. ")
71                 self.write("Upgrade to avoid seeing this every boot!\n")
72
73                 self.write("chain http://mirror0.ipfire.org/releases/ipfire-boot/latest/ipxe.kpxe\n")
74
75
76 class MenuCfgHandler(BootBaseHandler):
77         class NightlyBuildReleaseDummy(object):
78                 id = 100
79                 name = "Nightly Build"
80                 sname = "nightly-build"
81
82                 netboot_kernel = "http://nightly.ipfire.org/next/latest/i586/images/vmlinuz"
83                 netboot_initrd = "http://nightly.ipfire.org/next/latest/i586/images/instroot"
84
85                 def is_netboot_capable(self):
86                         return True
87
88         nightly_build = NightlyBuildReleaseDummy()
89
90         def get(self):
91                 self.set_header("Content-Type", "text/plain")
92
93                 latest_release = self.releases.get_latest()
94                 stable_releases = self.releases.get_stable()
95                 try:
96                         stable_releases.remove(latest_release)
97                 except ValueError:
98                         pass
99
100                 development_releases = self.releases.get_unstable()
101                 development_releases.insert(0, self.nightly_build)
102
103                 self.render("netboot/menu.cfg", latest_release=latest_release,
104                         stable_releases=stable_releases, development_releases=development_releases)