15
15
import os
16
16
17
17
# Search for template path
18
- template_paths = [ os .path .join (os .path .abspath (os .path .dirname (__file__ )), 'templates' ),
19
- '/usr/share/sysdweb/templates' ]
18
+ template_paths = [os .path .join (os .path .abspath (os .path .dirname (__file__ )), 'templates' ),
19
+ '/usr/share/sysdweb/templates' ]
20
20
template_path = [path for path in template_paths if os .access (path , os .R_OK )]
21
21
if template_path == []:
22
22
raise SystemExit ('Templates are missing.' )
23
23
TEMPLATE_PATH .insert (0 , os .path .join (template_path [0 ], 'views' ))
24
24
static_path = os .path .join (template_path [0 ], 'static' )
25
25
26
26
# Define auth function
27
+
28
+
27
29
def login (user , password ):
28
30
users = config .get ('DEFAULT' , 'users' , fallback = None )
29
31
if users and not user in users .split (',' ):
@@ -32,11 +34,13 @@ def login(user, password):
32
34
# Validate user with password
33
35
return pam ().authenticate (user , password )
34
36
37
+
35
38
@route ('/api/v1/<service>/<action>' )
36
39
@auth_basic (login )
37
40
def get_service_action (service , action ):
38
41
if service in config .sections ():
39
- sdbus = systemdBus (True ) if config .get ('DEFAULT' , 'scope' , fallback = 'system' ) == 'user' else systemdBus ()
42
+ sdbus = systemdBus (True ) if config .get (
43
+ 'DEFAULT' , 'scope' , fallback = 'system' ) == 'user' else systemdBus ()
40
44
unit = config .get (service , 'unit' )
41
45
if action == 'start' :
42
46
return {action : 'OK' } if sdbus .start_unit (unit ) else {action : 'Fail' }
@@ -62,6 +66,7 @@ def get_service_action(service, action):
62
66
response .status = 400
63
67
return {'msg' : 'Sorry, but \' {}\' is not defined in config.' .format (service )}
64
68
69
+
65
70
@route ('/api/v1/<service>/journal/<lines>' )
66
71
@auth_basic (login )
67
72
def get_service_journal (service , lines ):
@@ -80,6 +85,7 @@ def get_service_journal(service, lines):
80
85
response .status = 400
81
86
return {'msg' : 'Sorry, but \' {}\' is not defined in config.' .format (service )}
82
87
88
+
83
89
@route ('/' )
84
90
@auth_basic (login )
85
91
def get_main ():
@@ -98,19 +104,21 @@ def get_main():
98
104
disabled_stop = True if cls == 'active' or cls == 'danger' else False
99
105
disabled_restart = True if cls == 'active' or cls == 'danger' else False
100
106
services .append ({'class' : cls ,
101
- 'disabled_start' : disabled_start ,
102
- 'disabled_stop' : disabled_stop ,
103
- 'disabled_restart' : disabled_restart ,
104
- 'title' : config .get (service , 'title' ),
105
- 'service' : service })
107
+ 'disabled_start' : disabled_start ,
108
+ 'disabled_stop' : disabled_stop ,
109
+ 'disabled_restart' : disabled_restart ,
110
+ 'title' : config .get (service , 'title' ),
111
+ 'service' : service })
106
112
return template ('index' , hostname = gethostname (), services = services )
107
113
114
+
108
115
@route ('/journal/<service>' )
109
116
@auth_basic (login )
110
117
def get_service_journal_page (service ):
111
118
if service in config .sections ():
112
119
if get_service_action (service , 'status' )['status' ] == 'not-found' :
113
- abort (400 ,'Sorry, but service \' {}\' unit not found in system.' .format (config .get (service , 'title' )))
120
+ abort (400 , 'Sorry, but service \' {}\' unit not found in system.' .format (
121
+ config .get (service , 'title' )))
114
122
journal_lines = get_service_journal (service , 100 )
115
123
return template ('journal' , hostname = gethostname (), service = config .get (service , 'title' ), journal = journal_lines ['journal' ])
116
124
else :
@@ -122,33 +130,40 @@ def get_service_journal_page(service):
122
130
def get_favicon ():
123
131
return static_file ('favicon.ico' , root = os .path .join (static_path , 'img' ))
124
132
133
+
125
134
@route ('/css/<file>' )
126
135
@auth_basic (login )
127
136
def get_css (file ):
128
137
return static_file (file , root = os .path .join (static_path , 'css' ))
129
138
139
+
130
140
@route ('/fonts/<file>' )
131
141
@auth_basic (login )
132
142
def get_fonts (file ):
133
143
return static_file (file , root = os .path .join (static_path , 'fonts' ))
134
144
145
+
135
146
@route ('/img/<file>' )
136
147
@auth_basic (login )
137
148
def get_img (file ):
138
149
return static_file (file , root = os .path .join (static_path , 'img' ))
139
150
151
+
140
152
@route ('/js/<file>' )
141
153
@auth_basic (login )
142
154
def get_js (file ):
143
155
return static_file (file , root = os .path .join (static_path , 'js' ))
144
156
157
+
145
158
def start (config_file , host , port ):
146
159
# Check config
147
160
global config
148
161
config = checkConfig (config_file )
149
162
150
- if host == None : host = config .get ('DEFAULT' , 'host' , fallback = '127.0.0.1' )
151
- if port == None : port = config .get ('DEFAULT' , 'port' , fallback = '10080' )
163
+ if host == None :
164
+ host = config .get ('DEFAULT' , 'host' , fallback = '127.0.0.1' )
165
+ if port == None :
166
+ port = config .get ('DEFAULT' , 'port' , fallback = '10080' )
152
167
153
168
# Run webserver
154
169
run (host = host , port = port )
0 commit comments