"""run gunicorn""" import os import subprocess import glob import tinnus # # only imported to get lib directory name # # tinnus shows the ernad path import docing from nitpo import Nitpo class Runic(Nitpo): def __init__(self, do_verbose=False): super().__init__() ns = self.const['ns'] 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.bin_fudi = os.path.dirname(self.lib_fudi) + '/bin' self.home_dir = 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() self.log_dir = self.conf['folders']['web_log'] self.systemd_fudi = self + '/.config/systemd/user' self.verbose = do_verbose def options(self): if not self.has_conf('folders', 'web_log'): print("server needs a web_log folder") quit() if not self.has_conf('files', 'socket'): print("runic needs a socket file in nitpo.ini.") quit() socket_fufi = self.conf['files']['socket'] if not self.has_conf('files', 'pid'): print("runic needs a pid file in nitpo.ini.") quit() pid_fufi = self.conf['files']['pid'] # # just a sample lib to get the nitpo python directory lib_dir = os.path.dirname(os.path.abspath(docing.__file__)) conf_fufi = lib_dir + '/guniconf.py' log = self.log_fufi() string = '' if self.is_dev(): string += ' --capture-output' string += f" --log-file {log}.out" string += f" --error-logfile {log}.err" string += f" --access-logfile {log}.log" # string += f" -D -b unix:{socket_fufi} -p {pid_fufi} " string += f" -p {pid_fufi} " string += f" -c {conf_fufi} " string += ' server:app > /dev/null' print(string) return string def stop(self): """stop the server""" if not self.has_conf('files', 'pid'): print("runic needs a pid file in nitpo.ini.") quit() # # not under systemd pid_fufi = self.conf['files']['pid'] if not os.path.isfile(pid_fufi): kill = "/usr/bin/killall gunicorn" out = subprocess.run(kill, check=False, shell=True) return out kill = f"/bin/kill `/bin/cat {pid_fufi}` 2> /dev/null" out = subprocess.run(kill, shell=True) return out return None def start(self): options = self.options() run = f"/usr/bin/gunicorn {options}" print(run) subprocess.run(run, check=True, shell=True) def log_fufi(self): if not self.has_conf('folders', 'web_log'): print("runic needs a pid file in nitpo.ini.") quit() log_fudi = self.conf['folders']['web_log'] if not os.path.isdir(log_fudi): os.makedirs(log_fudi) fufis = self.list_logs_fufis(log_fudi) bana = self.next_log_number(fufis) fufi = log_fudi + '/' + bana return fufi def list_logs_fufis(self, folder, with_gz=True): fufis = [] base_glob = folder + '/*' for fufi in sorted(glob.glob(base_glob)): fufis.append(fufi) if not with_gz: return fufis comp_glob = base_glob + '.gz' for fufi in glob.glob(comp_glob): fufis.append(fufi) sorted_fufis = sorted(fufis) return sorted_fufis def next_log_number(self, fufis): if len(fufis) == 0: return str(0) maxi = 0 for fufi in fufis: bana = os.path.basename(fufi) if bana.endswith('.gz'): bana = bana[:-3] # # remove .log bana = bana[:-4] parts = bana.partition('.') number = tinnus.ekam(parts[0]) if number is None: continue if number > maxi: maxi = number number = tinnus.make(maxi + 1) return number ### recent part def get_socket_text(self): socket_text = '' socket_text += "[Unit]\nDescription=nitpo socket\n\n[Socket]\n" socket_text += f"ListenStream={self.socket_fufi}\n" socket_text += "SocketMode=0777\n" socket_text += "SocketUser=ernad\n\n" socket_text += "[Install]\nWantedBy=sockets.target\n\n" if not os.path.isdir(systemd_fudi): Path(systemd_fudi).mkdir(parents=True, exist_ok=True) systemd_fufi = systemd_fudi + '/nitpo.socket' systemd_file = open(systemd_fufi, 'w') systemd_file.write(socket_text) systemd_file.close() system_text = '' system_text += "[Unit]\nDescription=nitpo service\n" system_text += "Requires=nitpo.socket\nAfter=network.target\n\n" system_text += f'[Service]\nEnvironment="PYTHONPATH={lib_fudi}"\n' system_text += "Type=notify\n" # system_text += f"User={user}\nGroup={user}\n" system_text += "ExecStart=/usr/bin/gunicorn --capture-output" system_text += f" --log-file {log_dir}/out" system_text += f" --error-logfile {log_dir}/err" system_text += f" --access-logfile {log_dir}/log" system_text += f" -c {lib_fudi}/gunicorn.py server:app\n" system_text += 'ExecReload=/bin/kill -s HUP $MAINPID\n' system_text += "KillMode=mixed\nTimeoutStopSec=5\nPrivateTmp=true\n\n" system_text += "[Install]\nWantedBy=sockets.target\n" #print(socket_text)# # #quit() #print(system_text) #quit() systemd_fufi = systemd_fudi + '/nitpo.service' systemd_file = open(systemd_fufi, 'w') systemd_file.write(system_text) systemd_file.close() #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) quit() print(getpass.getuser())