]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/boot.py
5 import tornado
.httpserver
11 from webapp
import backend
13 BASEDIR
= os
.path
.dirname(__file__
)
16 tornado
.options
.enable_pretty_logging()
17 tornado
.options
.parse_command_line()
19 def word_wrap(s
, width
=45):
20 paragraphs
= s
.split('\n')
22 for paragraph
in paragraphs
:
23 while len(paragraph
) > width
:
24 pos
= paragraph
.rfind(' ', 0, width
)
27 lines
.append(paragraph
[:pos
])
28 paragraph
= paragraph
[pos
:]
29 lines
.append(paragraph
.lstrip())
30 return '\n'.join(lines
)
32 class BaseHandler(tornado
.web
.RequestHandler
):
35 return backend
.NetBoot()
38 class MenuGPXEHandler(BaseHandler
):
43 # XXX Check if version of the bootloader is allright
46 self
.set_header("Content-Type", "text/plain")
47 self
.write("#!gpxe\n")
48 self
.write("chain menu.c32 premenu.cfg\n")
51 class MenuCfgHandler(BaseHandler
):
52 def _menu_string(self
, menu
, level
=0):
56 s
+= self
._menu
_entry
(entry
, level
=level
)
60 def _menu_entry(self
, entry
, level
=0):
65 if entry
.type == "seperator":
66 lines
.append(ident
+ "menu separator")
68 elif entry
.type == "header":
69 lines
.append(ident
+ "menu begin %d" % entry
.id)
70 lines
.append(ident
+ "\tmenu title %s" % entry
.title
)
73 lines
.append(ident
+ "\tlabel %d.back" % entry
.id)
74 lines
.append(ident
+ "\t\tmenu label Back...")
75 lines
.append(ident
+ "\t\tmenu exit")
76 lines
.append(ident
+ "\tmenu separator")
78 lines
.append("%s" % self
._menu
_string
(entry
.submenu
, level
=level
+1))
79 lines
.append(ident
+ "menu end")
81 elif entry
.type == "config":
82 lines
.append(ident
+ "label %d" % entry
.id)
83 lines
.append(ident
+ "\tmenu label %s" % entry
.title
)
85 lines
.append(ident
+ "\ttext help")
86 lines
.append(word_wrap(entry
.description
))
87 lines
.append(ident
+ "\tendtext")
88 lines
.append(ident
+ "\tkernel /config/%s/boot.gpxe" % entry
.item
)
90 return "\n".join(lines
+ [""])
93 self
.set_header("Content-Type", "text/plain")
95 menu
= self
._menu
_string
(self
.netboot
.get_menu(1))
97 self
.render("menu.cfg", menu
=menu
)
100 class BootGPXEHandler(BaseHandler
):
102 config
= self
.netboot
.get_config(id)
104 raise tornado
.web
.HTTPError(404, "Configuration with ID '%s' was not found" % id)
106 lines
= ["#!gpxe", "imgfree",]
108 line
= "kernel -n img %s" % config
.image1
109 if line
.endswith(".iso"):
114 lines
.append("initrd -n img %s" % config
.image2
)
116 lines
.append("boot img")
119 self
.write("%s\n" % line
)
122 class Application(tornado
.web
.Application
):
127 static_path
= os
.path
.join(BASEDIR
, "static/netboot"),
128 template_path
= os
.path
.join(BASEDIR
, "templates/netboot"),
131 tornado
.web
.Application
.__init
__(self
, **settings
)
133 self
.add_handlers(r
"boot.ipfire.org", [
135 (r
"/files/menu.gpxe", MenuGPXEHandler
),
136 (r
"/files/menu.cfg", MenuCfgHandler
),
137 (r
"/config/([0-9]+)/boot.gpxe", BootGPXEHandler
),
140 (r
"/files/(boot.png|custom.gpxe|premenu.cfg|vesamenu.c32|menu.c32)",
141 tornado
.web
.StaticFileHandler
, { "path" : self
.settings
["static_path"] }),
146 return tornado
.ioloop
.IOLoop
.instance()
148 def shutdown(self
, *args
):
149 logging
.debug("Caught shutdown signal")
152 def run(self
, port
=8002):
153 logging
.debug("Going to background")
155 # All requests should be done after 30 seconds or they will be killed.
156 self
.ioloop
.set_blocking_log_threshold(30)
158 http_server
= tornado
.httpserver
.HTTPServer(self
, xheaders
=True)
160 # If we are not running in debug mode, we can actually run multiple
161 # frontends to get best performance out of our service.
162 if not self
.settings
["debug"]:
163 http_server
.bind(port
)
164 http_server
.start(num_processes
=4)
166 http_server
.listen(port
)
170 if __name__
== "__main__":