-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
146 lines (122 loc) · 5.41 KB
/
install.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
import sys
from configparser import ConfigParser
from os.path import abspath, dirname
import subprocess
PATH = dirname(abspath(__file__))
class Installer:
def __init__(self, configFile, arg="all"):
self.importConf(configFile)
self.uniauth = "N"
self.name = self.config.get("server", "SERVICE_NAME").replace(" ", "_").upper()
if arg == "all" or arg == "a":
self.installAll()
elif arg == "nginx":
if len(sys.argv) > 2 and sys.argv[2] == "reset":
self.nukeNginx()
self.installNginx(link=False)
elif len(sys.argv) > 2 and sys.argv[2] == "mime":
self.addNginxMimeType()
else:
self.installNginx()
elif arg == "db":
self.installDB()
elif arg == "uniauth":
self.installUniauth()
elif arg == "sakura":
self.resetSakura()
elif arg == "reset":
self.resetDB()
elif arg == "service":
self.installService()
elif arg == "cron":
self.setupCrons()
def ex(self, command):
subprocess.run(command, shell=True, check=True)
def installDB(self):
print("----DB----")
self.ex("sudo -u postgres createdb " + self.config.get("DB", "DB_NAME"))
self.ex("sudo -u postgres createuser " + self.config.get("DB", "DB_USER") + " -s --pwprompt ")
def resetSakura(self):
self.ex("pip uninstall sakura")
self.ex("pip install git+https://gitlab.com/Louciole/sakura.git/")
def installUniauth(self):
print("----UNIAUTH----")
while True:
uniauth = input("Do you want to create a uniauth database? (y/n)")
if uniauth.upper() == 'Y' or uniauth.upper() == 'N':
self.uniauth = uniauth.upper()
break
if self.uniauth == 'Y':
self.ex("sudo -u postgres createdb " + self.config.get("UNIAUTH", "DB_NAME"))
def initDB(self):
self.ex("python3 ./db/initDB.py " + self.uniauth)
def resetDB(self):
self.ex("sudo -u postgres dropdb " + self.config.get("DB", "DB_NAME"))
self.initDB()
def installAll(self):
self.installNginx()
self.installDB()
self.installUniauth()
self.initDB()
def installNginx(self, link=True):
print("----NGINX----")
if self.config.getboolean("server", "DEBUG"):
self.editFile("misc/nginx_local", {"[PATH]": PATH, "[SERV-PORT]": self.config.get("server", "PORT")})
self.ex("sudo cp ./misc/nginx_local_filled /etc/nginx/sites-available/" + self.name)
else:
self.editFile("misc/nginx_prod", {"[PATH]": PATH, "[SERV-PORT]": self.config.get("server", "PORT"), "[WS-PORT]": self.config.get("NOTIFICATION", "PORT")})
self.ex("sudo cp ./misc/nginx_prod_filled /etc/nginx/sites-available/" + self.name)
if not link:
return
self.ex("sudo ln -s /etc/nginx/sites-available/" + self.name + " /etc/nginx/sites-enabled/")
def addNginxMimeType(self):
pattern = 'application/javascript'
new_line = 'application/javascript mjs;'
with open('/etc/nginx/mime.types', 'r+') as f:
lines = f.readlines()
found = False
for i, line in enumerate(lines):
if pattern in line:
found = True
lines.insert(i + 1, new_line + '\n')
break
if not found:
print(f"Pattern '{pattern}' not found in {filename}.")
else:
f.seek(0)
f.writelines(lines)
def setupCrons(self):
try:
self.ex("crontab -l > crontab")
except Exception:
pass
self.ex("echo '*/15 * * * * " + PATH + "/venv/bin/python3 " + PATH + "/crons/15mins.py' >> crontab")
self.ex("echo '0 * * * * " + PATH + "/venv/bin/python3 " + PATH + "/crons/1h.py' >> crontab")
self.ex("echo '0 0 * * * " + PATH + "/venv/bin/python3 " + PATH + "/crons/1day.py' >> crontab")
self.ex("crontab crontab")
def editFile(self, file, templates):
with open(file, "r+") as f:
data = f.read()
for key in templates:
data = data.replace(key, templates[key])
with open(file+"_filled", "w+") as f:
f.write(data)
def importConf(self, configFile):
self.config = ConfigParser()
try:
self.config.read(configFile)
print("config at " + configFile + " loaded")
except Exception:
print("please create a config file")
def nukeNginx(self):
self.ex("sudo rm /etc/nginx/sites-available/" + self.name)
def installService(self):
self.editFile("misc/sakura.service", {"[PATH]": PATH, "[SERV-PORT]": self.config.get("server", "PORT"), "[NAME]":self.config.get("server", "service_name")} )
self.ex("cp ./misc/sakura.service_filled /etc/systemd/system/" + self.name + ".service")
self.ex("sudo systemctl daemon-reload")
self.ex("sudo systemctl enable " + self.name + ".service")
self.ex("sudo systemctl start " + self.name + ".service")
if len(sys.argv) > 1:
Installer(PATH + "/server.ini", sys.argv[1])
else:
Installer(PATH + "/server.ini")