-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·66 lines (52 loc) · 2.18 KB
/
install.py
File metadata and controls
executable file
·66 lines (52 loc) · 2.18 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
import os
import subprocess
import sys
import typing as tp
def generate_systemd_service(
filename: str,
path_placeholder: str = "PROJECT_DIRECTORY",
user_placeholder: str = "PROJECT_USER",
) -> None:
"""replaces placeholder in the service file to match the actual data"""
actual_path: str = os.path.dirname(os.path.realpath(__file__))
actual_user: str = os.getlogin()
new_lines: tp.List[str] = []
with open(filename, "r") as file:
for line in file.readlines():
if path_placeholder in line:
line = line.replace(path_placeholder, actual_path)
if user_placeholder in line:
line = line.replace(user_placeholder, actual_user)
new_lines.append(line)
with open(filename, "w") as file:
file.writelines(new_lines)
if __name__ == "__main__":
# check if running with sudo
if os.getuid() != 0:
print("This script must be executed with Root permissions.")
sys.exit()
# get own repository name
cwd: str = os.path.dirname(os.path.realpath(__file__))
project_name: str = cwd.split("/")[-1]
print(f"Starting installation of {project_name}.")
# find the systemd service file
systemd_service: str = [file for file in os.listdir() if file.endswith(".service")][0]
if not systemd_service:
print(f"Systemd service file not found in {cwd}. Exiting.")
sys.exit()
else:
# generate a valid systemd service for the daemon
generate_systemd_service(systemd_service)
# commands to execute
commands: tp.List[str] = [
f"sudo cp {systemd_service} /etc/systemd/system/",
"sudo systemctl daemon-reload",
f"sudo systemctl enable {systemd_service}",
f"sudo systemctl start {systemd_service}",
f"sleep 5 && sudo systemctl status {systemd_service}",
]
for command, i in zip(commands, range(len(commands))):
print(f"\nExecuting command {i + 1}/{len(commands)}: {command}")
subprocess.run(command, shell=True)
print(f"\nDaemon for the project {project_name} has been installed on this machine.")
print(f"To uninstall {project_name} run 'sudo python3 uninstall.py'.")