This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
159 lines (138 loc) · 3.94 KB
/
setup.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
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
import os
import sys
import subprocess
import platform
import argparse
from misc.functions import network
from configparser import ConfigParser
config = ConfigParser()
config.read("settings.ini")
if platform.system() != "Linux":
sys.exit("error: This can only run on Linux.")
parser = argparse.ArgumentParser()
parser.add_argument(
"-n",
"--service_name",
help="Name of the service you want to create.",
default="vision",
)
args = parser.parse_args()
if os.path.isfile(f"/lib/systemd/system/{args.service_name}"):
os.remove(f"/lib/systemd/system/{args.service_name}")
if not os.path.isfile("settings.ini"):
print("No settings.ini found. Creating one.")
while True:
subprocess.run(
["sudo", "cp", "settings.ini.template", "settings.ini"],
shell=False,
check=True,
)
break
sys.exit("You should edit settings.ini to your needs before running this script.")
try:
if network.is_connected():
while True:
subprocess.run(
[
"sudo",
"-H",
"python",
"-m",
"pip",
"install",
"--upgrade",
"-r",
"requirements.txt",
],
shell=False,
check=True,
)
break
while True:
subprocess.run(
["sudo", "chmod", "+x", "misc/bash/install_os_dependencies.sh"],
shell=False,
check=True,
)
break
while True:
subprocess.run(
["sudo", "./misc/bash/install_os_dependencies.sh"], shell=False
)
break
else:
print(
"Internet is not connected. Skipping dependency installations. \
This may cause problems."
)
while True:
subprocess.run(
["sudo", "addgroup", "--system", f"{args.service_name}"], shell=False
)
break
while True:
subprocess.run(
[
"sudo",
"adduser",
"--system",
"--no-create-home",
"--disabled-login",
"--disabled-password",
"--ingroup",
f"{args.service_name}",
f"{args.service_name}",
],
shell=False,
)
break
while True:
subprocess.run(
[
"sudo",
"usermod",
"-a",
"-G",
"sudo,video,www-data,netdev",
f"{args.service_name}",
],
shell=False,
)
break
while True:
subprocess.run(
["sudo", "touch", f"/lib/systemd/system/{args.service_name}.service"],
shell=False,
check=True,
)
break
service = f"""
[Unit]
Requires=network-online.target
After=network-online.target
Description="{args.service_name} Service"
[Service]
WorkingDirectory={config.get("system","WORKING_DIR")}
ExecStart=/usr/bin/python {config.get("system","WORKING_DIR")}vision.py
User={args.service_name}
[Install]
WantedBy=multi-user.target
"""
with open(f"/lib/systemd/system/{args.service_name}.service", "w") as f:
f.write(service)
while True:
subprocess.run(["sudo", "systemctl", "daemon-reload"], shell=False, check=True)
break
while True:
subprocess.run(
["sudo", "systemctl", "enable", f"{args.service_name}"],
shell=False,
check=True,
)
break
print(
f"vision-2022 is installed and enabled. It will start automatically on boot.\nCheck service status with 'systemctl status {args.service_name}'."
)
except Exception as e:
print("error: Please run as root. (sudo -H python setup.py)")
print(e)