summaryrefslogtreecommitdiff
path: root/src/helpers/vyos-save-config.py
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-10-04 11:10:22 -0500
committerJohn Estabrook <jestabro@vyos.io>2023-10-05 00:09:17 -0500
commit27605426a4ad613f45d36e7db5b1664dc3192981 (patch)
tree5ff43cf51738d245a016fd4c34b5d111e92add73 /src/helpers/vyos-save-config.py
parent26b8e8af88dcab4988389afd8bdc4a8ec33f5ffe (diff)
downloadvyos-1x-27605426a4ad613f45d36e7db5b1664dc3192981.tar.gz
vyos-1x-27605426a4ad613f45d36e7db5b1664dc3192981.zip
config: T5631: save copy of config in JSON format on commit
Diffstat (limited to 'src/helpers/vyos-save-config.py')
-rwxr-xr-xsrc/helpers/vyos-save-config.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/helpers/vyos-save-config.py b/src/helpers/vyos-save-config.py
index 8af4a7916..518bd9864 100755
--- a/src/helpers/vyos-save-config.py
+++ b/src/helpers/vyos-save-config.py
@@ -19,6 +19,7 @@ import os
import re
import sys
from tempfile import NamedTemporaryFile
+from argparse import ArgumentParser
from vyos.config import Config
from vyos.remote import urlc
@@ -28,8 +29,15 @@ from vyos.defaults import directories
DEFAULT_CONFIG_PATH = os.path.join(directories['config'], 'config.boot')
remote_save = None
-if len(sys.argv) > 1:
- save_file = sys.argv[1]
+parser = ArgumentParser(description='Save configuration')
+parser.add_argument('file', type=str, nargs='?', help='Save configuration to file')
+parser.add_argument('--write-json-file', type=str, help='Save JSON of configuration to file')
+args = parser.parse_args()
+file = args.file
+json_file = args.write_json_file
+
+if file is not None:
+ save_file = file
else:
save_file = DEFAULT_CONFIG_PATH
@@ -51,6 +59,13 @@ with open(write_file, 'w') as f:
f.write("\n")
f.write(system_footer())
+if json_file is not None and ct is not None:
+ try:
+ with open(json_file, 'w') as f:
+ f.write(ct.to_json())
+ except OSError as e:
+ print(f'failed to write JSON file: {e}')
+
if remote_save is not None:
try:
remote_save.upload(write_file)