-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtooloop-settings-server.py
More file actions
422 lines (335 loc) · 13.1 KB
/
Copy pathtooloop-settings-server.py
File metadata and controls
422 lines (335 loc) · 13.1 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# -*- coding: utf-8 -*-
"""
Tooloop Settings Server
~~~~~~~~~~~~~~~~~~~~~~~
System adminisation tool for a Tooloop OS box.
:copyright: (c) 2017 by Daniel Stock.
:license: Beerware, see LICENSE for more details.
"""
from flask import Flask, jsonify, render_template, request, after_this_request, abort, send_from_directory
from jinja2 import ChoiceLoader, FileSystemLoader
from controllers.system_controller import System
from controllers.presentation_controller import Presentation
from controllers.appcenter_controller import AppCenter
from controllers.services_controller import Services
from controllers.screenshot_controller import Screenshots
from utils.time_utils import *
import augeas
import time
from pprint import pprint
from subprocess import call
# ------------------------------------------------------------------------------
# INIT
# ------------------------------------------------------------------------------
app = Flask(__name__)
app.config.from_pyfile('data/config.cfg')
augtool = augeas.Augeas()
system = System(augtool)
presentation = Presentation()
appcenter = AppCenter(presentation, app)
services = Services(app)
screenshots = Screenshots()
# let jinja also look in the installed_app folder
template_loader = ChoiceLoader([
app.jinja_loader,
FileSystemLoader([app.root_path+'/templates', app.root_path+'/installed_app']),
])
app.jinja_loader = template_loader
# ------------------------------------------------------------------------------
# PAGE ROUTING
# ------------------------------------------------------------------------------
@app.route("/")
@app.route("/dashboard")
def render_dashboard():
return render_template('dashboard.html',
page = 'dashboard',
installed_app = appcenter.get_installed_app(),
app_controller = appcenter.get_installed_app_controller(),
hostname = system.get_hostname(),
display_state = system.get_display_state(),
audio_volume = system.get_audio_volume(),
uptime = time_to_ISO_string(system.get_uptime()),
screenshot_service_running = services.is_screenshot_service_running()
)
@app.route("/network")
def render_network():
return render_template('network.html',
page='network',
installed_app = appcenter.get_installed_app(),
interfaces = [{
'ip': 'x.x.x.x',
'subnet_mask': '255.255.255.0',
'gateway': 'x.x.x.x'
}]
)
@app.route("/appcenter")
def render_appcenter():
appcenter.check_available_apps()
return render_template('appcenter.html',
page='appcenter',
installed_app = appcenter.get_installed_app(),
available_apps = appcenter.get_availeble_apps(),
time_stamp = time.time(),
)
@app.route("/services")
def render_services():
return render_template('services.html',
page='services',
installed_app = appcenter.get_installed_app(),
services = services.get_status(),
)
@app.route("/system")
def render_system():
return render_template('system.html',
page='system',
installed_app = appcenter.get_installed_app(),
hostname = system.get_hostname(),
ip_address = system.get_ip(),
)
# ------------------------------------------------------------------------------
# ADDITIONAL RESOURCE FOLDERS
# ------------------------------------------------------------------------------
@app.route('/screenshots/<path:filename>')
def serve_screenshot(filename):
return send_from_directory('/assets/screenshots/', filename)
@app.route('/app/<path:filename>')
def serve_installed_app(filename):
return send_from_directory('installed_app/', filename)
@app.route('/appcenter/<path:filename>')
def serve_available_apps(filename):
return send_from_directory('/assets/apps/', filename)
# ------------------------------------------------------------------------------
# RESTFUL API
# ------------------------------------------------------------------------------
# System
@app.route('/tooloop/api/v1.0/system', methods=['GET'])
def get_system():
return jsonify(system.to_dict())
@app.route('/tooloop/api/v1.0/system/hostname', methods=['GET'])
def get_hostname():
try:
return jsonify({'hostname':system.get_hostname()})
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/system/hostname', methods=['PUT'])
def set_hostname():
if not request.form or not 'hostname' in request.form:
abort(400)
try:
system.set_hostname(request.form['hostname'])
return jsonify({
'message': 'Hostname saved',
'hostname': system.get_hostname(),
'needsReboot': system.needs_reboot
})
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/system/usage', methods=['GET'])
def get_usage():
return jsonify({
'hd': system.get_hd(),
'cpu': system.get_cpu(),
'gpu': system.get_gpu(),
'memory': system.get_memory()
})
@app.route('/tooloop/api/v1.0/system/uptime', methods=['GET'])
def get_uptime():
return jsonify({'uptime': time_to_ISO_string(system.get_uptime())})
@app.route('/tooloop/api/v1.0/system/hd', methods=['GET'])
def get_hd():
return jsonify(system.get_hd())
@app.route('/tooloop/api/v1.0/system/cpu', methods=['GET'])
def get_cpu():
return jsonify(system.get_cpu())
@app.route('/tooloop/api/v1.0/system/gpu', methods=['GET'])
def get_gpu():
return jsonify(system.get_gpu())
@app.route('/tooloop/api/v1.0/system/memory', methods=['GET'])
def get_memory():
return jsonify(system.get_memory())
@app.route('/tooloop/api/v1.0/system/reboot', methods=['GET'])
def reboot():
try:
system.reboot()
return jsonify({ 'message' : 'Rebooting' })
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/system/poweroff', methods=['GET'])
def poweroff():
try:
system.poweroff()
return jsonify({ 'message' : 'Powering Off' })
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/system/password', methods=['PUT'])
def set_password():
if not request.form or not 'oldPassword' in request.form or not 'newPassword' in request.form:
abort(400)
try:
system.set_password(request.form['oldPassword'], request.form['newPassword'])
return jsonify({ 'message' : 'Password saved'})
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/system/audiovolume', methods=['GET'])
def get_audio_volume():
return jsonify(system.get_audio_volume())
@app.route('/tooloop/api/v1.0/system/audiovolume', methods=['PUT'])
def set_audio_volume():
if not request.form or not 'volume' in request.form:
abort(400)
try:
volume = int(float(request.form['volume']))
system.set_audio_volume(volume)
return jsonify({'message' : 'Volume set to ' + str(volume)})
except Exception as e:
raise e
@app.route('/tooloop/api/v1.0/system/audiomute', methods=['PUT'])
def set_audio_mute():
if not request.form or not 'mute' in request.form:
abort(400)
try:
mute = request.form['mute'].lower() == 'true' or request.form['mute'] == '1'
system.set_audio_mute(mute)
message = 'muted' if mute else 'unmuted'
return jsonify({'message' : 'Audio' + message})
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/system/displaystate', methods=['GET'])
def get_display_state():
try:
state = system.get_display_state()
return jsonify({ 'Display' : state })
except Exception as e:
abort(500,e)
@app.route('/tooloop/api/v1.0/system/displaystate', methods=['PUT'])
def set_display_state():
if not request.form or not 'state' in request.form:
abort(400)
try:
system.set_display_state(request.form['state'])
state = system.get_display_state()
return jsonify({ 'Display' : state })
except Exception as e:
abort(500, e)
# presentation
@app.route('/tooloop/api/v1.0/presentation/start', methods=['GET'])
def start_presentation():
# try:
return_code = presentation.start()
return jsonify({ 'message' : 'Called start script with return code '+str(return_code) })
# except Exception as e:
# abort(500, e)
@app.route('/tooloop/api/v1.0/presentation/stop', methods=['GET'])
def stop_presentation():
return_code = presentation.stop()
try:
return jsonify({ 'message' : 'Called stop script with return code '+str(return_code) })
except Exception as e:
abort(500, e)
@app.route('/tooloop/api/v1.0/presentation/reset', methods=['GET'])
def reset_presentation():
try:
return_code = presentation.reset()
return jsonify({ 'message' : 'Called reset script with return code '+str(return_code) })
except Exception as e:
abort(500, e)
# appcenter
@app.route('/tooloop/api/v1.0/appcenter/installed', methods=['GET'])
def get_installed_app():
return jsonify(appcenter.get_installed_app().to_dict())
@app.route('/tooloop/api/v1.0/appcenter/available', methods=['GET'])
def get_availeble_apps():
available = appcenter.get_availeble_apps()
available_as_dict = []
for app in available:
available_as_dict.append(app.to_dict())
return jsonify(available_as_dict)
@app.route('/tooloop/api/v1.0/appcenter/refresh', methods=['GET'])
def check_available_apps():
appcenter.check_available_apps()
return get_availeble_apps()
@app.route('/tooloop/api/v1.0/appcenter/install/<string:name>', methods=['GET'])
def install_app(name):
@after_this_request
def add_header(response):
response.headers['X-Foo'] = 'Parachute'
return response
appcenter.install(name)
# call(['systemctl','restart','tooloop-settings-server'])
try:
return jsonify(appcenter.get_installed_app().to_dict())
except Exception as e:
abort(500, e)
# services
@app.route('/tooloop/api/v1.0/services', methods=['GET'])
def get_services_status():
return jsonify(services.get_status())
@app.route('/tooloop/api/v1.0/services/vnc', methods=['GET'])
def vnc_status():
return jsonify({'vnc':services.is_vnc_running()})
@app.route('/tooloop/api/v1.0/services/vnc/enable', methods=['GET'])
def enable_vnc():
services.enable_vnc()
return jsonify({ 'message' : 'VNC enabled' })
@app.route('/tooloop/api/v1.0/services/vnc/disable', methods=['GET'])
def disable_vnc():
services.disable_vnc()
return jsonify({ 'message' : 'VNC disabled' })
@app.route('/tooloop/api/v1.0/services/ssh', methods=['GET'])
def ssh_status():
return jsonify({'ssh':services.is_ssh_running()})
@app.route('/tooloop/api/v1.0/services/ssh/enable', methods=['GET'])
def enable_ssh():
services.enable_ssh()
return jsonify({ 'message' : 'SSH enabled' })
@app.route('/tooloop/api/v1.0/services/ssh/disable', methods=['GET'])
def disable_ssh():
services.disable_ssh()
return jsonify({ 'message' : 'SSH disabled' })
@app.route('/tooloop/api/v1.0/services/remoteconfiguration', methods=['GET'])
def remote_configuration_status():
return jsonify({'remote_configuration':services.is_remote_configuration_running()})
@app.route('/tooloop/api/v1.0/services/remoteconfiguration/enable', methods=['GET'])
def enable_remote_configuration():
services.enable_remote_configuration()
return jsonify({ 'message' : 'Remote configuration enabled' })
@app.route('/tooloop/api/v1.0/services/remoteconfiguration/disable', methods=['GET'])
def disable_remote_configuration():
services.disable_remote_configuration()
return jsonify({ 'message' : 'Remote configuration disabled' })
@app.route('/tooloop/api/v1.0/services/screenshots', methods=['GET'])
def screenshot_service_status():
return jsonify({'screenshot_service':services.is_screenshot_service_running()})
@app.route('/tooloop/api/v1.0/services/screenshots/enable', methods=['GET'])
def enable_screenshot_service():
services.enable_screenshot_service()
return jsonify({ 'message' : 'Screenshot service enabled' })
@app.route('/tooloop/api/v1.0/services/screenshots/disable', methods=['GET'])
def disable_screenshot_service():
services.disable_screenshot_service()
return jsonify({ 'message' : 'Screenshot service disabled' })
# screenshots
@app.route('/tooloop/api/v1.0/screenshot/latest', methods=['GET'])
def get_latest_screenshot():
return jsonify(screenshots.get_latest_screenshot())
@app.route('/tooloop/api/v1.0/screenshot/<int:index>', methods=['GET'])
def get_screenshot(index):
return jsonify(screenshots.get_screenshot(index))
@app.route('/tooloop/api/v1.0/screenshot/date/<string:date>', methods=['GET'])
def get_screenshot_at_date(date):
return jsonify(screenshots.get_screenshot_at_date(date))
@app.route('/tooloop/api/v1.0/screenshot/grab', methods=['GET'])
def grab_screenshot():
try:
return jsonify(screenshots.grab_screenshot())
except Exception as e:
abort(500, e)
# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------
if __name__ == "__main__":
app.run(
debug=True,
host=app.config['HOST'],
port=80
)