-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
145 lines (113 loc) · 3.65 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from collections import defaultdict
import web
import config
from marshmallow_jsonapi import Schema, fields
from pyroute2 import IPRoute
from subprocess import call
import json
VERSION = "0.0.1"
INTERFACE = "ppp0"
urls = (
r'/', 'Index',
r'/get_status', 'Status',
r'/get_log', 'Log',
r'/set_connect', 'Connect',
r'/set_disconnect', 'Disconnect',
)
app = web.application(urls, globals())
render = web.template.render('templates/',
base='base',
cache=config.cache)
# t_globals = web.template.Template.globals
# t_globals['datestr'] = web.datestr
# t_globals['app_version'] = lambda: VERSION + ' - ' + config.env
# t_globals['render'] = lambda t, *args: render._template(t)(*args)
class Index:
def GET(self):
return render.index(INTERFACE)
class Status:
def GET(self):
st = StatusSchema()
web.header('Content-Type', 'application/json')
data, errs = st.dump(st)
return json.dumps(data)
class Log:
def GET(self):
log = LogSchema()
web.header('Content-Type', 'application/json')
data, errs = log.dump(log)
return json.dumps(data)
class Connect:
def GET(self):
cr = CommandResult()
cr.exitCode = call(["sudo", "pon", "dsl-provider"])
web.header('Content-Type', 'application/json')
data, errs = cr.dump(cr)
return json.dumps(data)
class Disconnect:
def GET(self):
cr = CommandResult()
cr.exitCode = call(["sudo", "poff", "dsl-provider"])
web.header('Content-Type', 'application/json')
data, errs = cr.dump(cr)
return json.dumps(data)
# Set a custom internal error message
def internalerror():
msg = """
An internal server error occurred. Please try your request again by
hitting back on your web browser. You can also <a href="/"> go back
to the main page.</a>
"""
return web.internalerror(msg)
class StatusSchema(Schema):
id = fields.Str(dump_only=True)
connect = fields.Boolean()
details = fields.Str()
def __init__(self):
ip = IPRoute()
pppLinksIds = ip.link_lookup(ifname=INTERFACE)
if len(pppLinksIds) > 0:
state = ip.get_links(pppLinksIds)[0].get_attr('IFLA_OPERSTATE')
if state == 'UP':
self.connect = True
self.details = INTERFACE + " interface UP"
else:
self.connect = False
self.details = INTERFACE + " exist but no UP"
ip.close()
else:
self.connect = False
self.details = INTERFACE + " interface no exist"
super(StatusSchema, self).__init__()
class Meta:
type_ = 'pppoe_status'
strict = True
class LogSchema(Schema):
id = fields.Str(dump_only=True)
rawLog = fields.Str()
def __init__(self):
self.rawLog = self.getPppoeLog()
super(LogSchema, self).__init__()
def getPppoeLog(self, fileName="/var/log/syslog"):
result = ""
with open(fileName) as infile:
for line in infile:
if "ppp" in line:
result += line
return result
class Meta:
type_ = 'pppoe_log'
strict = True
class CommandResult(Schema):
id = fields.Str(dump_only=True)
exitCode = fields.Int()
stdout = fields.Str()
class Meta:
type_ = 'pppoe_command_status'
strict = True
# Setup the application's error handler
app.internalerror = web.debugerror if web.config.debug else internalerror
# Adds a wsgi callable for uwsgi
application = app.wsgifunc()
if __name__ == "__main__":
app.run()