"""write guniconf configuration""" import os import sys import subprocess from pathlib import Path import filing from nitpo import Nitpo from deploy import Deploy class Guniconf(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.socket_fufi = self.conf['files']['socket'] if not self.has_conf('files', 'guniconf'): print("I need a guniconf file configure") quit() self.out_fufi = self.conf['files']['guniconf'] if not self.out_fufi.endswith('.py'): print(f"Your gunicorn conf {self.out_fufi} must end in '.py'") quit() self.text = None self.deploy = Deploy() unit_gunicorn_fufi = self.deploy.systemd_guniconf_fufi() if unit_gunicorn_fufi != self.out_fufi: s_fufi = self.deploy.socket_outfufi print("Your systemd configuration and your ", end='') print("nitpo configuration don't match.") print(f"{s_fufi} uses {unit_gunicorn_fufi}.") print(f"This script builds a different file {self.out_fufi}.") print("This will be of no use. ", end='') print("Redeploy nitpo or change the nitpo configuration.") quit() self.verbose = do_verbose def write(self, do_verbose): text = self.must_have() text += self.get_options_from_conf() if(do_verbose): print(text) filing.srite(self.out_fufi, text, do_verbose=do_verbose) def must_have(self): text = '' text += '## This must be there.\n' text += 'import server\n' text += 'on_starting = server.on_starting\n\n' text += '## This may not be needed.\n' text += f"bind = 'unix:{self.socket_fufi}'\n" text += f"\n## options from Ф[gunicorn]\n" self.text = text return text def get_log(self): """some log option Thomas Krichel uses, now in Ф[gunicorn]""" text = '' if not self.has_conf('folders', 'web_log'): print("guniconf: I need Ф['folders']['web_log'] to add logs", file=sys.stderr) return log_dir = self.conf['folders', 'web_log'] if self.is_dev(): text += "--capture-output" text += f" --log-file {log_dir}/out" text += f" --error-logfile {log_dir}/err" text += f" --access-logfile {log_dir}/log" self.text += text return text def get_options_from_conf(self): out = '' if 'gunicorn' not in self.conf: return out for item in self.conf['gunicorn']: val = self.conf['gunicorn'][item] if val == '': out += f"{item} = True\n" continue if val == 'True': out += f"{item} = True\n" continue #if len(item) == 1: # out += ' -' + item + ' ' + val #else: # out += ' --' + item + ' ' + val out += f"{item} = '{val}'\n" return out def log_fufi(self): """for incremental logs, not used""" if not self.has_conf('folders', 'web_log'): print("guniconf: I need Ф[folders][web_log].") 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): """for incremental logs, not used""" import glob 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): """for incremental logs, not used""" import tinnus 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