Use configargparse to allow reading config from file

This commit is contained in:
Adam Goldsmith 2023-09-02 22:31:06 -04:00
parent b7043750bb
commit 1b9c2936a3
2 changed files with 13 additions and 15 deletions

View File

@ -16,11 +16,11 @@ Update Mailman 3 lists via a json API of the form:
} }
""" """
import argparse
import json import json
import os import os
import email.utils import email.utils
import configargparse
import mailmanclient import mailmanclient
import requests import requests
@ -207,8 +207,13 @@ def main(
def parse_arguments(): def parse_arguments():
argp = argparse.ArgumentParser(description=__doc__) argp = configargparse.ArgumentParser(description=__doc__)
argp.add("-c", "--config", is_config_file=True, help="Config file path")
argp.add_argument("--api", required=True, help="API endpoint to retrieve JSON from") argp.add_argument("--api", required=True, help="API endpoint to retrieve JSON from")
argp.add_argument(
"--api-auth", required=True, help="API Authorization header token"
)
argp.add_argument("--mail-host", help="Base domain for all lists", required=True) argp.add_argument("--mail-host", help="Base domain for all lists", required=True)
argp.add_argument( argp.add_argument(
@ -219,6 +224,9 @@ def parse_arguments():
argp.add_argument( argp.add_argument(
"--mailman-user", help="Username for Mailman3 REST API", default="restadmin" "--mailman-user", help="Username for Mailman3 REST API", default="restadmin"
) )
argp.add_argument(
"--mailman-pass", help="Password for Mailman3 REST API", required=True
)
argp.add_argument( argp.add_argument(
"--cf-auth-token", help="Auth token for CloudFlare API", required=True "--cf-auth-token", help="Auth token for CloudFlare API", required=True
@ -249,20 +257,15 @@ def parse_arguments():
if __name__ == "__main__": if __name__ == "__main__":
args = parse_arguments() args = parse_arguments()
for env_var in ("API_AUTH", "MAILMAN_PASS"):
if env_var not in os.environ:
print(f"Missing {env_var} environment variable")
exit(-1)
mailman_client = mailmanclient.Client( mailman_client = mailmanclient.Client(
args.mailman_url + "/3.1", args.mailman_url + "/3.1",
args.mailman_user, args.mailman_user,
os.environ["MAILMAN_PASS"], args.mailman_pass,
) )
main( main(
args.api, args.api,
os.environ["API_AUTH"], args.api_auth,
mailman_client, mailman_client,
args.dry_run, args.dry_run,
args.mail_host, args.mail_host,

View File

@ -6,9 +6,4 @@ User=mailman
Group=mailman Group=mailman
Type=oneshot Type=oneshot
TimeoutStartSec=600 TimeoutStartSec=600
EnvironmentFile=/opt/mailman-sync/env ExecStart=/usr/bin/python3 /opt/mailman-sync/mailman_sync.py --config /opt/mailman-sync/config.yml
ExecStart=/usr/bin/python3 /opt/mailman-sync/mailman_sync.py \
--api https://paperwork.claremontmakerspace.org/api/v1/paperwork/department/mailing_lists.json \
--mail-host claremontmakerspace.org \
--mailman-url http://localhost:8001 \
--mailman-user restadmin