]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - boot/boot.py
5 import tornado
.httpserver
13 BASEDIR
= os
.path
.dirname(__file__
)
15 def word_wrap(s
, width
=45):
16 paragraphs
= s
.split('\n')
18 for paragraph
in paragraphs
:
19 while len(paragraph
) > width
:
20 pos
= paragraph
.rfind(' ', 0, width
)
23 lines
.append(paragraph
[:pos
])
24 paragraph
= paragraph
[pos
:]
25 lines
.append(paragraph
.lstrip())
26 return '\n'.join(lines
)
28 class BaseHandler(tornado
.web
.RequestHandler
):
31 return backend
.NetBoot()
34 class MenuGPXEHandler(BaseHandler
):
39 # XXX Check if version of the bootloader is allright
42 self
.set_header("Content-Type", "text/plain")
43 self
.write("#!gpxe\n")
44 self
.write("chain menu.c32 premenu.cfg\n")
47 class MenuCfgHandler(BaseHandler
):
48 def _menu_string(self
, menu
, level
=0):
52 s
+= self
._menu
_entry
(entry
, level
=level
)
56 def _menu_entry(self
, entry
, level
=0):
61 if entry
.type == "seperator":
62 lines
.append(ident
+ "menu separator")
64 elif entry
.type == "header":
65 lines
.append(ident
+ "menu begin %d" % entry
.id)
66 lines
.append(ident
+ "\tmenu title %s" % entry
.title
)
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")
74 lines
.append("%s" % self
._menu
_string
(entry
.submenu
, level
=level
+1))
75 lines
.append(ident
+ "menu end")
77 elif entry
.type == "config":
78 lines
.append(ident
+ "label %d" % entry
.id)
79 lines
.append(ident
+ "\tmenu label %s" % entry
.title
)
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
)
86 return "\n".join(lines
+ [""])
89 self
.set_header("Content-Type", "text/plain")
91 menu
= self
._menu
_string
(self
.netboot
.get_menu(1))
93 self
.render("menu.cfg", menu
=menu
)
96 class BootGPXEHandler(BaseHandler
):
98 config
= self
.netboot
.get_config(id)
100 raise tornado
.web
.HTTPError(404, "Configuration with ID '%s' was not found" % id)
102 lines
= ["#!gpxe", "imgfree",]
104 line
= "kernel -n img %s" % config
.image1
105 if line
.endswith(".iso"):
108 line
+= " %s" % config
.args
112 lines
.append("initrd -n img %s" % config
.image2
)
114 lines
.append("boot img")
117 self
.write("%s\n" % line
)
120 class Application(tornado
.web
.Application
):
125 static_path
= os
.path
.join(BASEDIR
, "static"),
126 template_path
= os
.path
.join(BASEDIR
, "templates"),
129 tornado
.web
.Application
.__init
__(self
, **settings
)
131 self
.add_handlers(r
"boot.ipfire.org", [
133 (r
"/menu.gpxe", MenuGPXEHandler
),
134 (r
"/menu.cfg", MenuCfgHandler
),
135 (r
"/config/([0-9]+)/boot.gpxe", BootGPXEHandler
),
138 (r
"/(boot.png|custom.gpxe|premenu.cfg|vesamenu.c32|menu.c32)",
139 tornado
.web
.StaticFileHandler
, { "path" : self
.settings
["static_path"] }),
144 return tornado
.ioloop
.IOLoop
.instance()
146 def shutdown(self
, *args
):
147 logging
.debug("Caught shutdown signal")
150 def run(self
, port
=8002):
151 logging
.debug("Going to background")
153 # All requests should be done after 30 seconds or they will be killed.
154 self
.ioloop
.set_blocking_log_threshold(30)
156 http_server
= tornado
.httpserver
.HTTPServer(self
, xheaders
=True)
158 # If we are not running in debug mode, we can actually run multiple
159 # frontends to get best performance out of our service.
160 if not self
.settings
["debug"]:
161 http_server
.bind(port
)
162 http_server
.start(num_processes
=4)
164 http_server
.listen(port
)
168 if __name__
== "__main__":