grasp/server/setup

60 lines
1.4 KiB
Plaintext
Raw Normal View History

2019-01-05 06:44:36 -08:00
#!/usr/bin/env python3
2019-01-07 14:53:08 -08:00
import argparse
2019-01-05 06:44:36 -08:00
from pathlib import Path
from subprocess import check_call
from grasp_server import setup_parser
2019-01-05 06:44:36 -08:00
2019-01-07 14:53:08 -08:00
SYSTEMD_CONFIG = """
2019-01-05 06:44:36 -08:00
[Unit]
Description=Grasp extension server conterpart
[Install]
WantedBy=default.target
[Service]
2019-01-07 14:53:08 -08:00
ExecStart={server} {extra_args}
2019-01-05 06:44:36 -08:00
Type=simple
Restart=always
"""
def systemd(*args):
check_call([
'systemctl', '--user', *args,
])
2019-01-07 14:53:08 -08:00
def setup(args):
unit_name = args.unit_name
out = Path(f'~/.config/systemd/user/{unit_name}').expanduser()
print(f"Writing systemd config to {out}:")
server_bin = Path(__file__).parent.joinpath('grasp_server.py').absolute()
2019-01-07 14:53:08 -08:00
extra_args = f'--port {args.port} --path {args.path}'
with out.open('w') as fo:
fo.write(SYSTEMD_CONFIG.format(
server=server_bin,
extra_args=extra_args,
))
try:
systemd('daemon-reload')
systemd('enable', unit_name)
systemd('start' , unit_name)
systemd('status', unit_name)
except Exception as e:
print(f"Something has gone wrong... you might want to use 'journalctl --user -u {unit_name}' to debug")
raise e
def main():
p = argparse.ArgumentParser('grasp server setup', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument('--unit-name', type=str, default='grasp.service', help='Systemd unit name')
setup_parser(p)
args = p.parse_args()
setup(args)
if __name__ == '__main__':
main()