This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
forked from sillyfrog/OctoPrint-Klipper-mjpg-Dockerfile
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstart.py
executable file
·57 lines (46 loc) · 1.54 KB
/
start.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
#!/usr/bin/env python
import subprocess
import time
import os
import pwd
MJPG = [
"/usr/local/bin/mjpg_streamer",
"-i",
"%(input)s",
"-o",
"output_http.so -p %(port)s -w /usr/local/share/mjpg-streamer/www/",
]
MJPG_INPUT_DEFAULT = "input_uvc.so -r HD"
OCTOPRINT = ["/opt/octoprint/venv/bin/octoprint", "serve"]
def main():
mjpg_processes = []
mjpg_ports = [5000] # Reserve the OctoPrint port
for k, v in os.environ.iteritems():
if k.startswith("MJPG") and not k.startswith("MJPG_"):
v = v.strip()
port_env = "MJPG_PORT" + k[4:]
port = int(os.environ.get(port_env, 8080))
if port in mjpg_ports:
raise ValueError("Port %s from key %s already in use" % (port, port_env))
if not v:
v = MJPG_INPUT_DEFAULT
subs = {'input': v, 'port': port}
cmd = []
for part in MJPG:
cmd.append(part % subs)
print("Starting: %s" % (cmd,))
mjpg_processes.append(subprocess.Popen(cmd))
# Start klipper
klipper = subprocess.Popen(['sudo', '-u', 'octoprint', '/runklipper.py'])
os.setgid(
1000
) # Drop privileges, https://stackoverflow.com/questions/2699907/dropping-root-permissions-in-python#2699996
os.setuid(1000)
os.environ['HOME'] = '/home/octoprint'
# subprocess.Popen('env', shell=True).wait()
while 1:
Poctoprint = subprocess.Popen(OCTOPRINT)
Poctoprint.wait()
time.sleep(1)
if __name__ == '__main__':
main()