2019-07-06 06:03:08 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
This tool is for updating API key field `zuliprc` files of dummy users
|
|
|
|
in development environment, with the correct keys from the database.
|
|
|
|
Ensure running this outside of vagrant environment.
|
|
|
|
Usage:
|
|
|
|
./tools/update-zuliprc-api-field /path/to/zuliprc_dev
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import configparser
|
|
|
|
import os
|
2020-09-03 05:02:44 +02:00
|
|
|
import shlex
|
2019-07-06 06:03:08 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.insert(0, ZULIP_PATH)
|
|
|
|
from scripts.lib.zulip_tools import is_vagrant_env_host
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"path", metavar="FILE", nargs="+", help="config file of dummy users in development server"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-07-06 06:03:08 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
zuliprc_paths_list = args.path
|
|
|
|
for zuliprc_path in zuliprc_paths_list:
|
|
|
|
zuliprc = configparser.ConfigParser()
|
2021-02-12 08:20:45 +01:00
|
|
|
result = ""
|
2019-07-06 06:03:08 +02:00
|
|
|
try:
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(zuliprc_path) as f:
|
2019-07-06 06:03:08 +02:00
|
|
|
zuliprc.read_file(f, zuliprc_path)
|
2021-02-12 08:20:45 +01:00
|
|
|
api_details = zuliprc["api"]
|
|
|
|
email = api_details["email"]
|
|
|
|
key = api_details["key"]
|
|
|
|
site = api_details["site"]
|
|
|
|
if "localhost" not in site:
|
|
|
|
result = "ABORTED"
|
|
|
|
reason = "Script to be used for development server config files only"
|
2019-07-06 06:03:08 +02:00
|
|
|
except (KeyError, configparser.MissingSectionHeaderError):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = "FAILURE"
|
|
|
|
reason = "Could not parse file due to missing required fields/sections"
|
2019-07-06 06:03:08 +02:00
|
|
|
except FileNotFoundError:
|
2021-02-12 08:20:45 +01:00
|
|
|
result = "ABORTED"
|
|
|
|
reason = "No zuliprc file found at specified path"
|
2019-07-06 06:03:08 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if result not in ("ABORTED", "FAILURE"):
|
2019-07-06 06:03:08 +02:00
|
|
|
# Make sure the cwd is the root of Zulip checkout.
|
|
|
|
os.chdir(ZULIP_PATH)
|
|
|
|
|
|
|
|
if is_vagrant_env_host(ZULIP_PATH):
|
2021-02-12 08:19:30 +01:00
|
|
|
arguments = [
|
2021-02-12 08:20:45 +01:00
|
|
|
"vagrant",
|
|
|
|
"ssh",
|
|
|
|
"--command",
|
|
|
|
"./manage.py print_initial_password " + shlex.quote(email),
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2019-07-06 06:03:08 +02:00
|
|
|
else:
|
|
|
|
# Support users who don't have vagrant based setup
|
2021-02-12 08:20:45 +01:00
|
|
|
arguments = ["./manage.py", "print_initial_password", email]
|
2019-07-06 06:03:08 +02:00
|
|
|
# We redirect 'stderr' to 'stdout' to avoid 'Connection to 127.0.0.1 closed'
|
|
|
|
# appearing after this script finishes.
|
2022-01-22 07:52:54 +01:00
|
|
|
output = subprocess.check_output(arguments, stderr=subprocess.STDOUT, text=True)
|
2019-07-06 06:03:08 +02:00
|
|
|
new_key = output.split()[6]
|
|
|
|
|
|
|
|
if new_key != key:
|
|
|
|
try:
|
2021-02-12 08:20:45 +01:00
|
|
|
zuliprc.set("api", "key", new_key)
|
|
|
|
with open(zuliprc_path, "w+") as w:
|
2019-07-06 06:03:08 +02:00
|
|
|
zuliprc.write(w)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = "SUCCESS"
|
|
|
|
reason = f"API field updated for user {email}"
|
2020-04-09 21:51:58 +02:00
|
|
|
except OSError:
|
2021-02-12 08:20:45 +01:00
|
|
|
result = "FAILURE"
|
|
|
|
reason = "Writing to file unsuccessful"
|
2019-07-06 06:03:08 +02:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
result = "SUCCESS"
|
|
|
|
reason = f"API key for user {email} is already consistent"
|
|
|
|
print(f"{zuliprc_path}: {result}: {reason}")
|