]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/handlers_boot.py
Move everything to the root of the repository.
[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 BASEDIR = os.path.dirname(__file__)
14
15 def word_wrap(s, width=45):
16 paragraphs = s.split('\n')
17 lines = []
18 for paragraph in paragraphs:
19 while len(paragraph) > width:
20 pos = paragraph.rfind(' ', 0, width)
21 if not pos:
22 pos = width
23 lines.append(paragraph[:pos])
24 paragraph = paragraph[pos:]
25 lines.append(paragraph.lstrip())
26 return '\n'.join(lines)
27
28 class BootBaseHandler(tornado.web.RequestHandler):
29 @property
30 def netboot(self):
31 return backend.NetBoot()
32
33
34 class MenuGPXEHandler(BootBaseHandler):
35 """
36 menu.gpxe
37 """
38 def get(self):
39 # XXX Check if version of the bootloader is allright
40
41 # Devliver content
42 self.set_header("Content-Type", "text/plain")
43 self.write("#!gpxe\n")
44 self.write("chain menu.c32 premenu.cfg\n")
45
46
47 class MenuCfgHandler(BootBaseHandler):
48 def _menu_string(self, menu, level=0):
49 s = ""
50
51 for entry in menu:
52 s += self._menu_entry(entry, level=level)
53
54 return s
55
56 def _menu_entry(self, entry, level=0):
57 lines = []
58
59 ident = "\t" * level
60
61 if entry.type == "seperator":
62 lines.append(ident + "menu separator")
63
64 elif entry.type == "header":
65 lines.append(ident + "menu begin %d" % entry.id)
66 lines.append(ident + "\tmenu title %s" % entry.title)
67
68 # Add "Back..." entry
69 lines.append(ident + "\tlabel %d.back" % entry.id)
70 lines.append(ident + "\t\tmenu label Back...")
71 lines.append(ident + "\t\tmenu exit")
72 lines.append(ident + "\tmenu separator")
73
74 lines.append("%s" % self._menu_string(entry.submenu, level=level+1))
75 lines.append(ident + "menu end")
76
77 elif entry.type == "config":
78 lines.append(ident + "label %d" % entry.id)
79 lines.append(ident + "\tmenu label %s" % entry.title)
80 if entry.description:
81 lines.append(ident + "\ttext help")
82 lines.append(word_wrap(entry.description))
83 lines.append(ident + "\tendtext")
84 lines.append(ident + "\tkernel /config/%s/boot.gpxe" % entry.item)
85
86 return "\n".join(lines + [""])
87
88 def get(self):
89 self.set_header("Content-Type", "text/plain")
90
91 menu = self._menu_string(self.netboot.get_menu(1))
92
93 self.render("menu.cfg", menu=menu)
94
95
96 class BootGPXEHandler(BootBaseHandler):
97 def get(self, id):
98 config = self.netboot.get_config(id)
99 if not config:
100 raise tornado.web.HTTPError(404, "Configuration with ID '%s' was not found" % id)
101
102 lines = ["#!gpxe", "imgfree",]
103
104 line = "kernel -n img %s" % config.image1
105 if line.endswith(".iso"):
106 line += " iso"
107 if config.args:
108 line += " %s" % config.args
109 lines.append(line)
110
111 if config.image2:
112 lines.append("initrd -n img %s" % config.image2)
113
114 lines.append("boot img")
115
116 for line in lines:
117 self.write("%s\n" % line)