1 | # |
---|
2 | # Copyright (C) 2021 Soleta Networks <info@soleta.eu> |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify it under |
---|
5 | # the terms of the GNU Affero General Public License as published by the |
---|
6 | # Free Software Foundation; either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | |
---|
9 | import os |
---|
10 | import subprocess |
---|
11 | from subprocess import CalledProcessError |
---|
12 | import multiprocessing as mp |
---|
13 | from multiprocessing import Process |
---|
14 | |
---|
15 | from PIL import Image, ImageDraw |
---|
16 | from pystray import Icon, Menu, MenuItem |
---|
17 | |
---|
18 | from src.ogRest import ThreadState |
---|
19 | |
---|
20 | |
---|
21 | def _create_default_image(): |
---|
22 | """ |
---|
23 | Creates a default image for the tray icon. Use in case |
---|
24 | no favicon.ico is found. |
---|
25 | """ |
---|
26 | width = height = 250 |
---|
27 | color1 = (255, 255, 255) |
---|
28 | color2 = (255, 0, 255) |
---|
29 | |
---|
30 | image = Image.new('RGB', (width, height), color1) |
---|
31 | dc = ImageDraw.Draw(image) |
---|
32 | dc.rectangle( |
---|
33 | (width // 2, 0, width, height // 2), |
---|
34 | fill=color2) |
---|
35 | dc.rectangle( |
---|
36 | (0, height // 2, width // 2, height), |
---|
37 | fill=color2) |
---|
38 | |
---|
39 | return image |
---|
40 | |
---|
41 | |
---|
42 | def create_image(): |
---|
43 | try: |
---|
44 | image = Image.open(r'./favicon.ico') |
---|
45 | image = Image.composite(image, Image.new('RGB', image.size, 'white'), image) |
---|
46 | except: |
---|
47 | image = _create_default_image() |
---|
48 | return image |
---|
49 | |
---|
50 | |
---|
51 | def create_systray(): |
---|
52 | menu = Menu(MenuItem('Powered by Soleta Networks!', |
---|
53 | lambda icon, item: 1)) |
---|
54 | icon = Icon('ogClient', create_image(), menu=menu) |
---|
55 | assert icon.icon |
---|
56 | icon.run() |
---|
57 | |
---|
58 | |
---|
59 | systray_p = Process(target=create_systray) |
---|
60 | |
---|
61 | |
---|
62 | class OgLinuxOperations: |
---|
63 | |
---|
64 | def __init__(self): |
---|
65 | mp.set_start_method('spawn') |
---|
66 | systray_p.start() |
---|
67 | |
---|
68 | def _restartBrowser(self, url): |
---|
69 | raise NotImplementedError |
---|
70 | |
---|
71 | def poweroff(self): |
---|
72 | systray_p.terminate() |
---|
73 | os.system('systemctl poweroff') |
---|
74 | |
---|
75 | def reboot(self): |
---|
76 | systray_p.terminate() |
---|
77 | os.system('systemctl reboot') |
---|
78 | |
---|
79 | def shellrun(self, request, ogRest): |
---|
80 | cmd = request.getrun() |
---|
81 | try: |
---|
82 | result = subprocess.run(cmd, |
---|
83 | shell=True, |
---|
84 | stdin=subprocess.DEVNULL, |
---|
85 | capture_output=True, |
---|
86 | text=True, |
---|
87 | check=True) |
---|
88 | except CalledProcessError as error: |
---|
89 | if error.stderr: |
---|
90 | return error.stderr |
---|
91 | if error.stdout: |
---|
92 | return error.stdout |
---|
93 | return "{Non zero exit code and empty output}" |
---|
94 | return result.stdout |
---|
95 | |
---|
96 | def session(self, request, ogRest): |
---|
97 | raise NotImplementedError |
---|
98 | |
---|
99 | def hardware(self, path, ogRest): |
---|
100 | raise NotImplementedError |
---|
101 | |
---|
102 | def setup(self, request, ogRest): |
---|
103 | raise NotImplementedError |
---|
104 | |
---|
105 | def image_restore(self, request, ogRest): |
---|
106 | raise NotImplementedError |
---|
107 | |
---|
108 | def image_create(self, path, request, ogRest): |
---|
109 | raise NotImplementedError |
---|
110 | |
---|
111 | def refresh(self, ogRest): |
---|
112 | return {"status": "LINUX"} |
---|
113 | |
---|
114 | def probe(self, ogRest): |
---|
115 | return {'status': 'LINUX' if ogRest.state != ThreadState.BUSY else 'BSY'} |
---|