-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubrs.py
95 lines (86 loc) · 3.32 KB
/
ubrs.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
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
#
# Etienne Glossi - Avril 2012 - [email protected]
# Interface web de supervision et gestion des relays de broadcast UDP pour les jeux en LANs.
# Permet de propager le broadcast entre plusieurs VLANs.
import tornado.ioloop
import sys
import os, os.path
import handler
import config
from os import environ
from daemon import Daemon
from relay import Relay
from tornado.web import URLSpec, Application
# UDP-Brodcast-Relay Supervisor Daemon
# Lancement de l'interface de gestion web basée sur Tornado
class UBRSWebDaemon(Daemon):
def __init__(self):
Daemon.__init__(self, 'ubrs.pid')
self.ip = config.HTTP_IP
self.port = config.HTTP_PORT
self.relay = Relay(os.path.join(os.getcwd(), config.UBR_PATH))
self.app = UBRSApplication(self.relay)
# methode principale, appele par start()
# lance l'interface web tornado
def run(self):
self.app.listen(self.port)
tornado.ioloop.IOLoop.instance().start()
# on arrete proprement le serveur
def stop(self):
tornado.ioloop.IOLoop.instance().stop()
self.relay.stop_and_clean()
Daemon.stop(self)
# UDP-Broadcast-Relay Application
# Definit les différentes pages de l'interface et y associe leur handler
class UBRSApplication(Application):
def __init__(self, relay):
handlers = [
(r"/", handler.MainPage, dict(relay=relay)),
(r"/if/settrunk", handler.SetTrunkIfHandler, dict(relay=relay)),
(r"/if/setoam", handler.SetOamIfHandler, dict(relay=relay)),
(r"/port/add", handler.AddPortHandler, dict(relay=relay)),
(r"/port/remove", handler.RemovePortHandler, dict(relay=relay)),
(r"/port/start", handler.StartPortHandler, dict(relay=relay)),
(r"/port/stop", handler.StopPortHandler, dict(relay=relay)),
#(r"/port/status", handler.StatusPortHandler, dict(relay=relay)),
(r"/if/add", handler.AddIfHandler, dict(relay=relay)),
(r"/if/remove", handler.RemoveIfHandler, dict(relay=relay)),
]
settings = dict(
template_path=os.path.join(os.getcwd(), "templates"),
static_path=os.path.join(os.getcwd(), "static"),
autoescape="xhtml_escape",
debug=config.DEBUG,
)
Application.__init__(self, handlers, **settings)
#### Main ####
if __name__ == "__main__":
if environ['USER'] != "root" and environ['UID'] != "0":
print("Uniquement root peut lancer le serveur !")
exit(1)
ubr = UBRSWebDaemon()
# Background start
action = ""
if len(sys.argv) == 2:
action = sys.argv[1]
if action == "restart":
ubr.restart()
elif action == "stop":
ubr.stop()
elif action == "start":
ubr.start()
elif action != "":
print("Commande inconnue %s ! Uniquement 'start', 'restart' et 'stop' sont acceptés." % action)
elif action == "":
# Foreground start
if ubr.isrunning():
print("Demarrage en mode normal...")
try:
ubr.run()
except KeyboardInterrupt:
ubr.stop()
print("stopping !")
else:
print("Déjà en cours d'execution (pid: %d)..." % ubr.isrunning())