mostr-zulip-bot/zulip/integrations/zephyr/process_ccache

46 lines
1.6 KiB
Text
Raw Normal View History

2020-04-02 09:59:28 -04:00
#!/usr/bin/env python3
2021-05-28 17:00:04 +08:00
import base64
import subprocess
2021-05-28 17:00:04 +08:00
import sys
from pathlib import Path
short_user = sys.argv[1]
api_key = sys.argv[2]
ccache_data_encoded = sys.argv[3]
# Update the Kerberos ticket cache file
program_name = f"zmirror-{short_user}"
with open(f"/home/zulip/ccache/{program_name}", "wb") as f:
f.write(base64.b64decode(ccache_data_encoded))
# Setup API key
api_key_path = Path(f"/home/zulip/api-keys/{program_name}")
api_key_path.write_text(api_key + "\n")
# Setup supervisord configuration
supervisor_path = Path(f"/etc/supervisor/conf.d/zmirror/{program_name}.conf")
template_path = Path(__file__).parent / "zmirror_private.conf.template"
template_data = template_path.read_text()
session_path = f"/home/zulip/zephyr_sessions/{program_name}"
# Preserve mail zephyrs forwarding setting across rewriting the config file
try:
if "--forward-mail-zephyrs" in supervisor_path.read_text():
template_data = template_data.replace(
"--use-sessions", "--use-sessions --forward-mail-zephyrs"
)
except FileNotFoundError:
pass
supervisor_path.write_text(template_data.replace("USERNAME", short_user))
# Delete your session
subprocess.check_call(["rm", "-f", session_path])
# Update your supervisor config, which may restart your mirror
subprocess.check_call(["supervisorctl", "reread"])
subprocess.check_call(["supervisorctl", "update"])
# Restart your mirror, in case it wasn't restarted by the previous
# (Otherwise if the mirror lost subs, this would do nothing)
# TODO: check whether we JUST restarted it first
subprocess.check_call(["supervisorctl", "restart", program_name])