Add import (config patch) script.
This commit is contained in:
parent
f7a88cd6cb
commit
c8dae280e4
80
import_mailman3_list_config.py
Normal file
80
import_mailman3_list_config.py
Normal file
@ -0,0 +1,80 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import sys
|
||||
import requests
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
REST_USER="restadmin"
|
||||
REST_PATH="http://127.0.0.1:8001/3.0/lists"
|
||||
|
||||
BANNER="""
|
||||
_) | __ / _|_)
|
||||
` \ _` | | | ` \ _` | \ _ \ _| _ \ \ _| | _` |
|
||||
_|_|_|\__,_|_|_|_|_|_|\__,_|_| _|___/\__|\___/_| _|_| _|\__, |
|
||||
____/
|
||||
"""
|
||||
|
||||
|
||||
def parse_args(args):
|
||||
parser = argparse.ArgumentParser("load a json config into local mailman configuration")
|
||||
|
||||
parser.add_argument("list", help="list name (eg `test`)")
|
||||
parser.add_argument("patch", help="config to push to list", type=Path)
|
||||
|
||||
return parser.parse_args(args)
|
||||
|
||||
def main(args):
|
||||
print(BANNER)
|
||||
pargs = parse_args(args[1:])
|
||||
|
||||
if (not pargs.patch.exists()):
|
||||
print("need input patchfile (json)")
|
||||
return 1
|
||||
with pargs.patch.open() as inf:
|
||||
p = json.load(inf)
|
||||
|
||||
rest_pass = Path("/run/secrets/mailman_rest_password").read_text().strip()
|
||||
rest_auth = HTTPBasicAuth(REST_USER, rest_pass)
|
||||
|
||||
# determine if list exists, if not create with defaults
|
||||
result = requests.get(REST_PATH, auth=rest_auth)
|
||||
if result.ok:
|
||||
if (not (pargs.list in [x['fqdn_listname'] for x in result.json()['entries']])):
|
||||
# create list
|
||||
print("list not found: creating")
|
||||
result = requests.post(f"{REST_PATH}", auth=rest_auth, json={"fqdn_listname": pargs.list})
|
||||
if (result.ok):
|
||||
print("success")
|
||||
print(result)
|
||||
print(result.text)
|
||||
else:
|
||||
print("failed")
|
||||
print(result)
|
||||
print(result.reason)
|
||||
return 1
|
||||
# list exists
|
||||
else:
|
||||
print("cannot get list of lists")
|
||||
print(result)
|
||||
print(result.reason)
|
||||
return 1
|
||||
|
||||
|
||||
# patch config
|
||||
result = requests.patch(f"{REST_PATH}/{pargs.list}/config", auth=rest_auth, json=p)
|
||||
if result.ok:
|
||||
print("success")
|
||||
print(result)
|
||||
print(result.text)
|
||||
else:
|
||||
print("failed")
|
||||
print(result)
|
||||
print(result.reason)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv))
|
Loading…
Reference in New Issue
Block a user