#!/usr/bin/env python3 """One SSH call; encode the helper sources and quote every command argument.""" import base64 from pathlib import Path import shlex import subprocess import sys def command(target, action, arguments): if action not in {'start', 'stop', 'restart', 'status', 'enable', 'disable', 'log', 'setup', 'info'}: raise ValueError('Unknown service action') if arguments and action != 'log': raise ValueError('Only log accepts additional arguments') root = Path(__file__).parent helper = base64.b64encode((root / 'deploy-update-remote.py').read_bytes()).decode('ascii') controls = base64.b64encode((root / 'service-control-remote.py').read_bytes()).decode('ascii') template = base64.b64encode((root / 'werkjournal-backend.service.in').read_bytes()).decode('ascii') program = ("import base64; scope={'__name__':'service_helpers'}; " "exec(compile(base64.b64decode('" + helper + "'),'deployment','exec'),scope); " "scope['SERVICE_UNIT_TEMPLATE']=base64.b64decode('" + template + "').decode(); " "scope['__name__']='__main__'; " "exec(compile(base64.b64decode('" + controls + "'),'service-control','exec'),scope)") return ['ssh', '-o', 'BatchMode=yes', target, shlex.join(['python3', '-c', program, action, *arguments])] if __name__ == '__main__': raise SystemExit(subprocess.call(command(sys.argv[1], sys.argv[2], sys.argv[3:])))