#!/usr/bin/python3 import os import sys import codecs if len(sys.argv) < 3: print(f'Usage:\n{sys.argv[0]} venvname') sys.exit(1) user = os.getlogin() home = os.environ.get('HOME') try: os.mkdir(f'{home}/ve') except: pass venv = sys.argv[1] port = int(sys.argv[2]) if not os.path.exists(f'{home}/{venv}'): os.system(f'python3 -m venv ~/{venv}') pwd = os.getcwd() name = os.path.basename(pwd) if os.path.exists(f'./app/{name}.py'): print('env exists') sys.exit(1) dirs = f'./app ./conf ./files ./wwwroot ./script ./logs'.split(' ') for d in dirs: try: os.mkdir(d) except: pass files=f'app/{name}.py conf/config.json files/README.md wwwroot/index.dspy'.split(' ') for f in files: os.system(f'touch {f}') service = f"""[Unit] Description={name} service Wants=systemd-networkd.service Requires=nginx.service [Service] Type=forking ExecStart=su - {user} -c "{pwd}/script/{name}.sh" ExecStop=su - ymq "{home}/bin/killname {name}.py" [Install] WantedBy=multi-user.target """ with codecs.open(f'./script/{name}.service', 'w', 'utf-8') as f: f.write(service) with codecs.open(f'./script/{name}.sh', 'w', 'utf-8') as f: f.write(f"""#!/usr/bin/bash killname {pwd}/app/{name}.py {home}/{venv}/bin/python {pwd}/app/{name}.py -w {pwd} > {pwd}/logs/stderr.log 2>&1 & exit 0 """) with codecs.open(f'./script/install.sh', 'w', 'utf-8') as f: f.write(f"""#!/usr/bin/bash sudo cp {name}.service /etc/systemd/system sudo systemctl enable {name}.service sudo systemctl start {name} """) if not os.path.exists(f'{home}/bin'): os.mkdir(f'{home}/bin') if not os.path.exists(f'{home}/bin/killname'): with codecs.open(f'{home}/bin/killname', 'w', 'utf-8') as f: f.write("""#!/usr/bin/bash ps -ef|grep "$1"|grep -v grep|awk '{print("kill -9", $2)}'|sh """) os.system(f'chmod +x {pwd}/bin/*') os.system(f'{pwd}/script/install.sh')