2020-04-02 09:59:28 -04:00
|
|
|
#!/usr/bin/env python3
|
2021-05-28 05:00:04 -04:00
|
|
|
import base64
|
2017-10-05 15:21:08 -04:00
|
|
|
import os
|
2013-08-23 14:49:06 -04:00
|
|
|
import subprocess
|
2021-05-28 05:00:04 -04:00
|
|
|
import sys
|
2013-08-23 14:49:06 -04:00
|
|
|
|
|
|
|
short_user = sys.argv[1]
|
|
|
|
api_key = sys.argv[2]
|
|
|
|
ccache_data_encoded = sys.argv[3]
|
|
|
|
|
|
|
|
# Update the Kerberos ticket cache file
|
2021-05-28 07:19:40 -04:00
|
|
|
program_name = f"zmirror-{short_user}"
|
|
|
|
with open(f"/home/zulip/ccache/{program_name}", "wb") as f:
|
2013-08-23 14:49:06 -04:00
|
|
|
f.write(base64.b64decode(ccache_data_encoded))
|
|
|
|
|
|
|
|
# Setup API key
|
2021-05-28 07:19:40 -04:00
|
|
|
api_key_path = f"/home/zulip/api-keys/{program_name}"
|
2016-03-10 12:18:37 -05:00
|
|
|
open(api_key_path, "w").write(api_key + "\n")
|
2013-08-23 14:49:06 -04:00
|
|
|
|
|
|
|
# Setup supervisord configuration
|
2021-05-28 07:19:40 -04:00
|
|
|
supervisor_path = f"/etc/supervisor/conf.d/zulip/{program_name}.conf"
|
2017-10-05 15:21:08 -04:00
|
|
|
template = os.path.join(os.path.dirname(__file__), "zmirror_private.conf.template")
|
2016-03-10 12:18:37 -05:00
|
|
|
template_data = open(template).read()
|
2021-05-28 07:19:40 -04:00
|
|
|
session_path = f"/home/zulip/zephyr_sessions/{program_name}"
|
2013-10-01 13:45:29 -04:00
|
|
|
|
|
|
|
# Preserve mail zephyrs forwarding setting across rewriting the config file
|
|
|
|
|
|
|
|
try:
|
2020-04-09 20:14:01 -04:00
|
|
|
if "--forward-mail-zephyrs" in open(supervisor_path).read():
|
2021-05-28 05:03:46 -04:00
|
|
|
template_data = template_data.replace(
|
|
|
|
"--use-sessions", "--use-sessions --forward-mail-zephyrs"
|
|
|
|
)
|
2013-10-01 13:45:29 -04:00
|
|
|
except Exception:
|
|
|
|
pass
|
2016-03-10 12:18:37 -05:00
|
|
|
open(supervisor_path, "w").write(template_data.replace("USERNAME", short_user))
|
2013-08-23 14:49:06 -04:00
|
|
|
|
|
|
|
# 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])
|