summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-08-24 13:38:20 -0500
committerGitHub <noreply@github.com>2023-08-24 13:38:20 -0500
commit5ea0428a08ae9e85fe2ebb6bb85dca07c5eb694f (patch)
tree3872c70363ac05d5af49c95a622fae12062aaa78 /src
parent250a5d13c9e463b04fde0216b133065f26f11430 (diff)
parente4831c7ac93c50d80e0bad4b66c034030ad0bce9 (diff)
downloadvyos-1x-5ea0428a08ae9e85fe2ebb6bb85dca07c5eb694f.tar.gz
vyos-1x-5ea0428a08ae9e85fe2ebb6bb85dca07c5eb694f.zip
Merge pull request #2164 from jestabro/save-config
save-config: T4292: rewrite vyatta-save-config.pl to Python
Diffstat (limited to 'src')
-rwxr-xr-xsrc/helpers/vyos-save-config.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/helpers/vyos-save-config.py b/src/helpers/vyos-save-config.py
new file mode 100755
index 000000000..2812155e8
--- /dev/null
+++ b/src/helpers/vyos-save-config.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2023 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#
+import os
+import re
+import sys
+from tempfile import NamedTemporaryFile
+
+from vyos.config import Config
+from vyos.remote import urlc
+from vyos.component_version import system_footer
+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]
+else:
+ save_file = DEFAULT_CONFIG_PATH
+
+if re.match(r'\w+:/', save_file):
+ try:
+ remote_save = urlc(save_file)
+ except ValueError as e:
+ sys.exit(e)
+
+config = Config()
+ct = config.get_config_tree(effective=True)
+
+write_file = save_file if remote_save is None else NamedTemporaryFile(delete=False).name
+with open(write_file, 'w') as f:
+ f.write(ct.to_string())
+ f.write("\n")
+ f.write(system_footer())
+
+if remote_save is not None:
+ try:
+ remote_save.upload(write_file)
+ finally:
+ os.remove(write_file)