source: ogClient-Git/src/linux/ogOperations.py @ fa0e48a

Last change on this file since fa0e48a was ab7abf9, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

#1065 linux: add shell run operation

  • Executed script runs with same privilege as ogClient process.
  • Uses subprocess.run instead of subprocess.Popen, it's a bit simpler. We can't specify executable, though. Shouldn't need so in Linux mode.
  • Uses shell=True, keep in mind security considerations listed at: https://docs.python.org/3/library/subprocess.html#security-considerations (shlex.quote can be used for unix shells)
  • Property mode set to 100644
File size: 3.1 KB
Line 
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
9import os
10import subprocess
11from subprocess import CalledProcessError
12import multiprocessing as mp
13from multiprocessing import Process
14
15from PIL import Image, ImageDraw
16from pystray import Icon, Menu, MenuItem
17
18from src.ogRest import ThreadState
19
20
21def _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
42def 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
51def 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
59systray_p = Process(target=create_systray)
60
61
62class 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'}
Note: See TracBrowser for help on using the repository browser.