-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalon-save-config.py
59 lines (45 loc) · 1.52 KB
/
halon-save-config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3.7
"""Fetch configuration from HALON and save as JSON"""
import argparse
import base64
import json
from datetime import datetime, timezone
import requests
def main() -> None:
"""Main functions"""
parser = argparse.ArgumentParser(description="Save HALON config")
parser.add_argument(
"--config",
metavar="filename",
required=False,
help="Config file",
default="halon-save-config.json",
)
parser.add_argument(
"--output", metavar="filename", required=False, help="Output file"
)
args = parser.parse_args()
# read configuration
with open(args.config) as input_file:
config = json.load(input_file)
halon_url = f"https://{config['hostname']}/api/1.0.0"
# create session for Halon
session = requests.Session()
session.auth = (config["username"], config["password"])
if "cacert" in config:
session.verify = config["cacert"]
# fetch configuration from Halon
response = session.get(f"{halon_url}/config/revisions/HEAD")
response.raise_for_status()
# digest current configuration
last_halon_config = response.json()
last_halon_revision = last_halon_config["id"]
# archive configurations
archive_path = config.get("archive_path", ".")
output_filename = (
args.output or f"{archive_path}/halon-config-{last_halon_revision}.json"
)
with open(output_filename, "wt") as f:
f.write(json.dumps(last_halon_config, indent=4))
if __name__ == "__main__":
main()