f5tts/install
2024-12-25 16:04:39 +08:00

63 lines
1.5 KiB
Python
Executable File

#!/usr/bin/python3
import os
import sys
import codecs
if len(sys.argv) < 2:
print(f'Usage:\n{sys.argv[0]} venvname')
sys.exit(1)
user = os.getlogin()
home = os.environ.get('HOME')
venv = sys.argv[1]
if not os.path.exists(f'{home}/{venv}'):
os.system(f'python3 -m venv ~/{venv}')
pwd = os.getcwd()
name = os.path.basename(pwd)
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 - {user} "{home}/bin/killname app/{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'chmod +x {pwd}/script/*.sh')
os.system(f'{pwd}/script/install.sh')