Install logic in user_script
This commit is contained in:
parent
70a7401332
commit
d50a8b7623
|
@ -7,3 +7,7 @@ def get_lingering_users() -> List[str]:
|
|||
|
||||
def create_vhost(domain: str, user: str):
|
||||
pass
|
||||
|
||||
|
||||
def get_ip() -> str:
|
||||
pass
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
[Unit]
|
||||
Description={{ name }}
|
||||
After=syslog.target
|
||||
After=network.target
|
||||
StartLimitIntervalSec=300
|
||||
StartLimitBurst=5
|
||||
|
||||
[Service]
|
||||
RestartSec=5s
|
||||
Restart=always
|
||||
ExecStart={{ command }}
|
||||
WorkingDirectory={{ directory }}
|
||||
Environment={{ env }}
|
||||
PrivateTmp=yes
|
||||
ProtectSystem=full
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
|
@ -1,10 +1,38 @@
|
|||
import argparse
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import csc_api
|
||||
import jinja2
|
||||
|
||||
|
||||
def install(name, command, domain, port, ip, directory, env) -> int:
|
||||
template_loader = jinja2.FileSystemLoader(searchpath="./")
|
||||
template_env = jinja2.Environment(loader=template_loader, autoescape=True)
|
||||
template = template_env.get_template('systemd_template.j2')
|
||||
output_text = template.render(command=command, domain=domain, port=port, ip=ip, directory=directory, env=env)
|
||||
|
||||
sanitized_path = os.path.join('~/.config/systemd/user/', os.path.basename(name))
|
||||
if os.path.isfile(sanitized_path):
|
||||
raise FileExistsError(f"Try running `rm -f {sanitized_path}`")
|
||||
with open(sanitized_path, 'w') as f:
|
||||
f.write(output_text)
|
||||
return subprocess.run(['systemctl', '--user', 'enable', name, '--now']).returncode
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(prog='Deployment Helper')
|
||||
parser.add_argument('-v', '--verbose', action='store_true')
|
||||
subparsers = parser.add_subparsers(help='sub-command help')
|
||||
subparsers = parser.add_subparsers(help='command help', dest='command')
|
||||
|
||||
parser_install = subparsers.add_parser('install', help='a help')
|
||||
parser_install.add_argument('name')
|
||||
parser_install.add_argument('command')
|
||||
parser_install.add_argument('domain')
|
||||
parser_install.add_argument('port')
|
||||
parser_install.add_argument('ip', nargs='?', default=csc_api.get_ip())
|
||||
parser_install.add_argument('--directory')
|
||||
parser_install.add_argument('--env', nargs='*')
|
||||
|
||||
parser_uninstall = subparsers.add_parser('uninstall', help='a help')
|
||||
parser_start = subparsers.add_parser('start', help='a help')
|
||||
parser_stop = subparsers.add_parser('stop', help='a help')
|
||||
|
@ -22,4 +50,6 @@ if args.verbose:
|
|||
else:
|
||||
logging.basicConfig(format="%(levelname)s: %(message)s")
|
||||
|
||||
|
||||
if args.command == 'install': # can use switch case, but I think ceo might be using 3.9
|
||||
install(args.command.name, args.command.command, args.command.domain, args.command.port, args.command.ip,
|
||||
args.command.directory, args.command.env)
|
||||
|
|
Loading…
Reference in New Issue