-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.py
82 lines (67 loc) · 2.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
__version__ = "0.2.2"
__author__ = "M. Heuwes <[email protected]>"
import os
import json
import logging
from tornado.web import authenticated
from tornado import web, ioloop
from nojava_ipmi_kvm.config import config, DEFAULT_CONFIG_FILEPATH
from nojava_ipmi_kvm import utils
from login_handler import OAuth2LoginHandler
from basehandler import BaseHandler, authorized
from kvm_handler import KVMHandler
WEBAPP_PORT = int(os.environ["WEBAPP_PORT"])
WEBAPP_BASE = os.environ["WEBAPP_BASE"]
CONFIG_PATH = os.environ.get("KVM_CONFIG_PATH", DEFAULT_CONFIG_FILEPATH)
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
config.read_config(CONFIG_PATH)
class MainHandler(BaseHandler):
@authenticated
@authorized
def get(self):
self.render(
"index.tpl",
title="Remote KVM",
user=self.get_current_user(),
servers=config.get_servers(),
base_uri=WEBAPP_BASE,
websocket_uri="ws" + WEBAPP_BASE[4:],
)
@authenticated
@authorized
def post(self):
self.render(
"index_instant.tpl",
title="Remote KVM",
user=self.get_current_user(),
servers=config.get_servers(),
base_uri=WEBAPP_BASE,
websocket_uri="ws" + WEBAPP_BASE[4:],
server_name=json.dumps(self.get_body_argument("server_name")),
password=json.dumps(self.get_body_argument("password")),
resolution=json.dumps(self.get_body_argument("resolution")),
)
def make_app():
"""
returns a tornado.web.Application
"""
settings = {
"template_path": "templates",
"static_path": "static",
"debug": True,
"cookie_secret": utils.generate_temp_password(32),
"login_url": "/oauth/login",
"xsrf_cookies": True,
"default_handler_class": MainHandler,
"websocket_ping_interval": 10,
}
return web.Application(
[web.url(r"/oauth/login", OAuth2LoginHandler), web.url(r"/", MainHandler), web.url(r"/kvm", KVMHandler)],
**settings
)
if __name__ == "__main__":
APP = make_app()
APP.listen(WEBAPP_PORT)
print("Started app on port {}".format(WEBAPP_PORT))
ioloop.IOLoop.current().start()