summaryrefslogtreecommitdiff
path: root/src/helpers
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@sentrium.io>2019-05-29 12:05:33 -0500
committerGitHub <noreply@github.com>2019-05-29 12:05:33 -0500
commit0a16c85a8b6aa728e142156e0e985ac21916db20 (patch)
tree2c6dabba3712f9f05662844fbd4b9994ce937a05 /src/helpers
parentbf0f721432fa05bbc7058a0b43e2acf4ad1f30e3 (diff)
parent456abc2aa4ae10981c2aec2d2e6d975ef30fb8d6 (diff)
downloadvyos-1x-0a16c85a8b6aa728e142156e0e985ac21916db20.tar.gz
vyos-1x-0a16c85a8b6aa728e142156e0e985ac21916db20.zip
Merge pull request #68 from jestabro/merge-config
T1397: Rewrite the config merge script
Diffstat (limited to 'src/helpers')
-rwxr-xr-xsrc/helpers/vyos-merge-config.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/helpers/vyos-merge-config.py b/src/helpers/vyos-merge-config.py
new file mode 100755
index 000000000..f0d5d1595
--- /dev/null
+++ b/src/helpers/vyos-merge-config.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python3
+
+# Copyright 2019 VyOS maintainers and contributors <maintainers@vyos.io>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import os
+import subprocess
+import vyos.defaults
+import vyos.remote
+from vyos.config import Config
+from vyos.configtree import ConfigTree
+
+
+if (len(sys.argv) < 2):
+ print("Need config file name to merge.")
+ print("Usage: merge <config file> [config path]")
+ sys.exit(0)
+
+file_name = sys.argv[1]
+
+configdir = vyos.defaults.directories['config']
+
+protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp']
+
+if any(x in file_name for x in protocols):
+ config_file = vyos.remote.get_remote_config(file_name)
+ if not config_file:
+ sys.exit("No config file by that name.")
+else:
+ canonical_path = "{0}/{1}".format(configdir, file_name)
+ first_err = None
+ try:
+ with open(canonical_path, 'r') as f:
+ config_file = f.read()
+ except Exception as err:
+ first_err = err
+ try:
+ with open(file_name, 'r') as f:
+ config_file = f.read()
+ except Exception as err:
+ print(first_err)
+ print(err)
+ sys.exit(1)
+
+path = None
+if (len(sys.argv) > 2):
+ path = " ".join(sys.argv[2:])
+
+merge_config_tree = ConfigTree(config_file)
+
+effective_config = Config()
+
+output_effective_config = effective_config.show_config()
+effective_config_tree = ConfigTree(output_effective_config)
+
+effective_cmds = effective_config_tree.to_commands()
+merge_cmds = merge_config_tree.to_commands()
+
+effective_cmd_list = effective_cmds.splitlines()
+merge_cmd_list = merge_cmds.splitlines()
+
+effective_cmd_set = set(effective_cmd_list)
+add_cmds = [ cmd for cmd in merge_cmd_list if cmd not in effective_cmd_set ]
+
+if path:
+ if not effective_config.exists(path):
+ print("path {} does not exist in running config; will use "
+ "root.".format(path))
+ else:
+ add_cmds = [ cmd for cmd in add_cmds if path in cmd ]
+
+for cmd in add_cmds:
+ cmd = "/opt/vyatta/sbin/my_" + cmd
+
+ try:
+ subprocess.check_call(cmd, shell=True)
+ except subprocess.CalledProcessError as err:
+ print("Called process error: {}.".format(err))
+
+if effective_config.session_changed():
+ print("Merge complete. Use 'commit' to make changes effective.")
+else:
+ print("No configuration changes to commit.")