"""deploy gunicorn""" import os import subprocess # import glob # import tinnus from pathlib import Path # # only imported to get lib directory name # # tinnus shows the ernad path import docing from nitpo import Nitpo class Deploy(Nitpo): def __init__(self, do_verbose=False): super().__init__() if not self.has_conf('files', 'socket'): print("I need a socket file configure") quit() self.user = os.getlogin() self.lib_fudi = os.path.dirname(os.path.abspath(docing.__file__)) self.nitpo_fudi = os.path.dirname(self.lib_fudi) self.bin_fudi = self.nitpo_fudi + '/bin' self.js_fudi = self.nitpo_fudi + '/contrib/js' self.home_fudi = os.environ['HOME'] if not self.has_conf('files', 'socket'): print("I need a socket file defined") self.socket_fufi = self.conf['files']['socket'] if not self.has_conf('folders', 'web_log'): print("I need a socket file defined") quit() if not self.has_conf('web', 'pathstart'): print("I need a web path start ะค[web][pathstart]") quit() self.pathstart = self.conf['web']['pathstart'] self.log_dir = self.conf['folders']['web_log'] ## this is not used yet. self.systemd_fudi = self.home_fudi + '/.config/systemd/user' self.text = {} self.verbose = do_verbose def get_socket_text(self): text = '' text += "[Unit]\nDescription=nitpo socket\n\n[Socket]\n" text += f"ListenStream={self.socket_fufi}\n" text += "SocketMode=0777\n" text += "SocketUser=ernad\n\n" text += "[Install]\nWantedBy=sockets.target" self.socket_text = text return text def get_service_text(self, do_root=True): text = '' text += "[Unit]\nDescription=nitpo service\n" text += "Requires=nitpo.socket\nAfter=network.target\n\n" text += f'[Service]\nEnvironment="PYTHONPATH={self.lib_fudi}"\n' text += "Type=notify\n" if do_root: text += f"User={self.user}\nGroup={self.user}\n" text += "ExecStart=/usr/bin/gunicorn --capture-output" text += f" --log-file {self.log_dir}/out" text += f" --error-logfile {self.log_dir}/err" text += f" --access-logfile {self.log_dir}/log" text += f" -c {self.lib_fudi}/guniconf.py server:app\n" text += 'ExecReload=/bin/kill -s HUP $MAINPID\n' text += "KillMode=mixed\nTimeoutStopSec=5\nPrivateTmp=true\n\n" text += "[Install]\nWantedBy=sockets.target" self.service_text = text return text def user_write(self, kind): if not os.path.isdir(self.systemd_fudi): Path(self.systemd_fudi).mkdir(parents=True, exist_ok=True) if kind == 'service': text = self.get_service_text(do_root=False) elif kind == 'socket': text = self.get_socket_text() else: raise Exception("kind must be 'service' or 'socket'") systemd_fufi = self.systemd_fudi + '/nitpo.' + kind systemd_file = open(systemd_fufi, 'w') systemd_file.write(text) systemd_file.close() def say_apache(self): socket = f"unix:{self.socket_fufi}|http://127.0.1.1/" a = '\n## insert into the apache configuration: \n' a += f"Alias /js/nitpo.js {self.js_fudi}/web.js\n" a += f"ProxyPass /nitpo {socket}\n" a += f"ProxyPassReverse {self.pathstart} {socket}\n" a += '##' print(a) def say_systemd(self): t = '' t += '\n## create /usr/lib/systemd/system/nitpo.socket with\n' t += self.get_socket_text() t += '\n##\n' t += '\n## create /usr/lib/systemd/system/nitpo.service with\n' t += self.get_service_text() t += '\n##\n\n' t += '## Finally, run\n' t += 'systemctl daemon-reload ; systemctl enable --now nitpo.socket' t += '; systemctl start nitpo.socket' t += '\n##' print(t) # print(socket_text)# # # quit() # print(system_text) # quit() # subprocess.run("/bin/systemctl --user --now enable nitpo", check=True, shell=True) # subprocess.run("/bin/systemctl --user --now enable nitpo.socket", check=True, shell=True) # subprocess.run("/bin/systemctl --user start nitpo.socket", check=True, shell=True) # subprocess.run("/bin/systemctl --user start nitpo.service", check=True, shell=True)