summaryrefslogtreecommitdiff
path: root/src/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers')
-rwxr-xr-xsrc/helpers/add-system-version.py2
-rwxr-xr-xsrc/helpers/config_dependency.py2
-rwxr-xr-xsrc/helpers/geoip-update.py2
-rwxr-xr-xsrc/helpers/priority.py2
-rwxr-xr-xsrc/helpers/read-saved-value.py2
-rwxr-xr-xsrc/helpers/reset_section.py124
-rwxr-xr-xsrc/helpers/run-config-activation.py2
-rwxr-xr-xsrc/helpers/run-config-migration.py9
-rwxr-xr-xsrc/helpers/set_vyconf_backend.py43
-rwxr-xr-xsrc/helpers/show_commit_data.py2
-rwxr-xr-xsrc/helpers/strip-private.py2
-rwxr-xr-xsrc/helpers/teardown-config-session.py27
-rwxr-xr-xsrc/helpers/test_commit.py2
-rwxr-xr-xsrc/helpers/vyconf_cli.py47
-rwxr-xr-xsrc/helpers/vyos-boot-config-loader.py2
-rwxr-xr-xsrc/helpers/vyos-check-wwan.py2
-rwxr-xr-xsrc/helpers/vyos-config-encrypt.py2
-rwxr-xr-xsrc/helpers/vyos-failover.py2
-rwxr-xr-xsrc/helpers/vyos-interface-rescan.py2
-rwxr-xr-xsrc/helpers/vyos-load-balancer.py8
-rwxr-xr-xsrc/helpers/vyos-load-config.py101
-rwxr-xr-xsrc/helpers/vyos-merge-config.py143
-rwxr-xr-xsrc/helpers/vyos-save-config.py2
-rwxr-xr-xsrc/helpers/vyos-sudo.py33
-rwxr-xr-xsrc/helpers/vyos_config_sync.py2
-rwxr-xr-xsrc/helpers/vyos_net_name2
26 files changed, 378 insertions, 191 deletions
diff --git a/src/helpers/add-system-version.py b/src/helpers/add-system-version.py
index 5270ee7d3..70bbd2202 100755
--- a/src/helpers/add-system-version.py
+++ b/src/helpers/add-system-version.py
@@ -1,6 +1,6 @@
#!/usr/bin/python3
-# Copyright 2019-2024 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 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
diff --git a/src/helpers/config_dependency.py b/src/helpers/config_dependency.py
index 817bcc65a..d6358bef8 100755
--- a/src/helpers/config_dependency.py
+++ b/src/helpers/config_dependency.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2023 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/geoip-update.py b/src/helpers/geoip-update.py
index 061c95401..22d26e538 100755
--- a/src/helpers/geoip-update.py
+++ b/src/helpers/geoip-update.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2021 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/priority.py b/src/helpers/priority.py
index 04186104c..6630889af 100755
--- a/src/helpers/priority.py
+++ b/src/helpers/priority.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/read-saved-value.py b/src/helpers/read-saved-value.py
index 1463e9ffe..f4048f373 100755
--- a/src/helpers/read-saved-value.py
+++ b/src/helpers/read-saved-value.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2023 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/reset_section.py b/src/helpers/reset_section.py
new file mode 100755
index 000000000..7e464afd5
--- /dev/null
+++ b/src/helpers/reset_section.py
@@ -0,0 +1,124 @@
+#!/usr/bin/env python3
+#
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
+#
+# 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 argparse
+import sys
+import os
+import grp
+
+from vyos.configsession import ConfigSession
+from vyos.config import Config
+from vyos.configdiff import get_config_diff
+from vyos.xml_ref import is_leaf
+
+
+CFG_GROUP = 'vyattacfg'
+DEBUG = False
+
+
+def type_str_to_list(value):
+ if isinstance(value, str):
+ return value.split()
+ raise argparse.ArgumentTypeError('path must be a whitespace separated string')
+
+
+parser = argparse.ArgumentParser()
+parser.add_argument('path', type=type_str_to_list, help='section to reload/rollback')
+parser.add_argument('--pid', help='pid of config session')
+
+group = parser.add_mutually_exclusive_group()
+group.add_argument('--reload', action='store_true', help='retry proposed commit')
+group.add_argument(
+ '--rollback', action='store_true', default=True, help='rollback to stable commit'
+)
+
+args = parser.parse_args()
+
+path = args.path
+reload = args.reload
+rollback = args.rollback
+pid = args.pid
+
+try:
+ if is_leaf(path):
+ sys.exit('path is leaf node: neither allowed nor useful')
+except ValueError:
+ if DEBUG:
+ sys.exit('nonexistent path: neither allowed nor useful')
+ else:
+ sys.exit()
+
+test = Config()
+in_session = test.in_session()
+
+if in_session:
+ if reload:
+ sys.exit('reset_section reload not available inside of a config session')
+
+ diff = get_config_diff(test)
+ if not diff.is_node_changed(path):
+ # No discrepancies at path after commit, hence no error to revert.
+ sys.exit()
+
+ del diff
+else:
+ if not reload:
+ sys.exit('reset_section rollback not available outside of a config session')
+
+del test
+
+
+session_id = int(pid) if pid else os.getppid()
+
+if in_session:
+ # check hint left by vyshim when ConfigError is from apply stage
+ hint_name = f'/tmp/apply_{session_id}'
+ if not os.path.exists(hint_name):
+ # no apply error; exit
+ sys.exit()
+ else:
+ # cleanup hint and continue with reset
+ os.unlink(hint_name)
+
+cfg_group = grp.getgrnam(CFG_GROUP)
+os.setgid(cfg_group.gr_gid)
+os.umask(0o002)
+
+shared = not bool(reload)
+
+session = ConfigSession(session_id, shared=shared)
+
+session_env = session.get_session_env()
+config = Config(session_env)
+
+d = config.get_config_dict(path, effective=True, get_first_key=True)
+
+if in_session:
+ session.discard()
+
+session.delete(path)
+session.commit()
+
+if not d:
+ # nothing more to do in either case of reload/rollback
+ sys.exit()
+
+session.set_section(path, d)
+out = session.commit()
+print(out)
diff --git a/src/helpers/run-config-activation.py b/src/helpers/run-config-activation.py
index 58293702a..f20adff1e 100755
--- a/src/helpers/run-config-activation.py
+++ b/src/helpers/run-config-activation.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/run-config-migration.py b/src/helpers/run-config-migration.py
index e6ce97363..6a3533644 100755
--- a/src/helpers/run-config-migration.py
+++ b/src/helpers/run-config-migration.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2019-2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
@@ -19,6 +19,7 @@ import sys
import time
from argparse import ArgumentParser
from shutil import copyfile
+from vyos.utils.file import read_file
from vyos.migrate import ConfigMigrate
from vyos.migrate import ConfigMigrateError
@@ -76,3 +77,9 @@ except ConfigMigrateError as e:
if backup is not None and not config_migrate.config_modified:
os.unlink(backup)
+
+# T1771: add knob on Kernel command-line to simulate failed config migrator run
+# used to test if the automatic image reboot works.
+kernel_cmdline = read_file('/proc/cmdline')
+if 'vyos-fail-migration' in kernel_cmdline.split():
+ sys.exit(1)
diff --git a/src/helpers/set_vyconf_backend.py b/src/helpers/set_vyconf_backend.py
new file mode 100755
index 000000000..dddbe12f6
--- /dev/null
+++ b/src/helpers/set_vyconf_backend.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+#
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
+#
+# 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/>.
+#
+#
+
+# N.B. only for use within testing framework; explicit invocation will leave
+# system in inconsistent state.
+
+import os
+import sys
+from argparse import ArgumentParser
+
+from vyos.utils.backend import set_vyconf_backend
+
+if os.getuid() != 0:
+ sys.exit('Requires root privileges')
+
+parser = ArgumentParser()
+parser.add_argument('--disable', action='store_true',
+ help='enable/disable vyconf backend')
+parser.add_argument('--no-prompt', action='store_true',
+ help='confirm without prompt')
+
+args = parser.parse_args()
+
+match args.disable:
+ case False:
+ set_vyconf_backend(True, no_prompt=args.no_prompt)
+ case True:
+ set_vyconf_backend(False, no_prompt=args.no_prompt)
diff --git a/src/helpers/show_commit_data.py b/src/helpers/show_commit_data.py
index d507ed9a4..85ee64cb1 100755
--- a/src/helpers/show_commit_data.py
+++ b/src/helpers/show_commit_data.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2025 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/strip-private.py b/src/helpers/strip-private.py
index cb29069cf..71b7c079a 100755
--- a/src/helpers/strip-private.py
+++ b/src/helpers/strip-private.py
@@ -1,6 +1,6 @@
#!/usr/bin/python3
-# Copyright 2021-2023 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 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
diff --git a/src/helpers/teardown-config-session.py b/src/helpers/teardown-config-session.py
new file mode 100755
index 000000000..c30303c99
--- /dev/null
+++ b/src/helpers/teardown-config-session.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+#
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
+#
+# 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 sys
+
+from vyos.vyconf_session import VyconfSession
+
+if len(sys.argv) < 2:
+ sys.exit('session pid is required')
+
+pid = sys.argv[1]
+
+vc = VyconfSession(pid=pid)
+vc.teardown()
diff --git a/src/helpers/test_commit.py b/src/helpers/test_commit.py
index 00a413687..cfff85b9d 100755
--- a/src/helpers/test_commit.py
+++ b/src/helpers/test_commit.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2025 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyconf_cli.py b/src/helpers/vyconf_cli.py
new file mode 100755
index 000000000..53542dd63
--- /dev/null
+++ b/src/helpers/vyconf_cli.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+#
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
+#
+# 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 sys
+
+from vyos.vyconf_session import VyconfSession
+
+
+pid = os.getppid()
+
+vs = VyconfSession(pid=pid)
+
+script_path = sys.argv[0]
+script_name = os.path.basename(script_path)
+# drop prefix 'vy_' if present
+if script_name.startswith('vy_'):
+ func_name = script_name[3:]
+else:
+ func_name = script_name
+
+if hasattr(vs, func_name):
+ func = getattr(vs, func_name)
+else:
+ sys.exit(f'Call unimplemented: {func_name}')
+
+out = func()
+if isinstance(out, bool):
+ # for use in shell scripts
+ sys.exit(int(not out))
+
+print(out)
diff --git a/src/helpers/vyos-boot-config-loader.py b/src/helpers/vyos-boot-config-loader.py
index 42de696ce..e20b8415f 100755
--- a/src/helpers/vyos-boot-config-loader.py
+++ b/src/helpers/vyos-boot-config-loader.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2019 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos-check-wwan.py b/src/helpers/vyos-check-wwan.py
index 334f08dd3..4768ddf3f 100755
--- a/src/helpers/vyos-check-wwan.py
+++ b/src/helpers/vyos-check-wwan.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2021 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos-config-encrypt.py b/src/helpers/vyos-config-encrypt.py
index 84860bd6a..5f49ca119 100755
--- a/src/helpers/vyos-config-encrypt.py
+++ b/src/helpers/vyos-config-encrypt.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos-failover.py b/src/helpers/vyos-failover.py
index 348974364..96db947e1 100755
--- a/src/helpers/vyos-failover.py
+++ b/src/helpers/vyos-failover.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2022-2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos-interface-rescan.py b/src/helpers/vyos-interface-rescan.py
index 012357259..fea9bca1c 100755
--- a/src/helpers/vyos-interface-rescan.py
+++ b/src/helpers/vyos-interface-rescan.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2021 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos-load-balancer.py b/src/helpers/vyos-load-balancer.py
index 30329fd5c..850c5142e 100755
--- a/src/helpers/vyos-load-balancer.py
+++ b/src/helpers/vyos-load-balancer.py
@@ -1,6 +1,6 @@
#!/usr/bin/python3
-# Copyright 2024-2025 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 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
@@ -246,6 +246,7 @@ if __name__ == '__main__':
# Main loop
+ init = True;
try:
while True:
ip_change = False
@@ -273,6 +274,11 @@ if __name__ == '__main__':
if state_changed and state['failure_count'] >= int(health_conf['failure_count']):
state['state'] = False
state['state_changed'] = True
+
+ #Force state changed to trigger the first write
+ if init == True:
+ state['state_changed'] = True
+ init = False
if state['state_changed']:
state['if_addr'] = get_ipv4_address(ifname)
diff --git a/src/helpers/vyos-load-config.py b/src/helpers/vyos-load-config.py
index 16083fd41..01a6a88dc 100755
--- a/src/helpers/vyos-load-config.py
+++ b/src/helpers/vyos-load-config.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2019-2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
@@ -16,84 +16,57 @@
#
#
-"""Load config file from within config session.
-Config file specified by URI or path (without scheme prefix).
-Example: load https://somewhere.net/some.config
- or
- load /tmp/some.config
-"""
-
import os
import sys
-import gzip
+import argparse
import tempfile
-import vyos.defaults
-import vyos.remote
-from vyos.configsource import ConfigSourceSession, VyOSError
+
+from vyos.remote import get_config_file
+from vyos.config import Config
from vyos.migrate import ConfigMigrate
from vyos.migrate import ConfigMigrateError
+from vyos.load_config import load as load_config
-class LoadConfig(ConfigSourceSession):
- """A subclass for calling 'loadFile'.
- This does not belong in configsource.py, and only has a single caller.
- """
- def load_config(self, path):
- return self._run(['/bin/cli-shell-api','loadFile',path])
-
-file_name = sys.argv[1] if len(sys.argv) > 1 else 'config.boot'
-configdir = vyos.defaults.directories['config']
-protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp']
-def get_local_config(filename):
- if os.path.isfile(filename):
- fname = filename
- elif os.path.isfile(os.path.join(configdir, filename)):
- fname = os.path.join(configdir, filename)
- else:
- sys.exit(f"No such file '{filename}'")
+parser = argparse.ArgumentParser()
+parser.add_argument('config_file', help='config file to load')
+parser.add_argument(
+ '--migrate', action='store_true', help='migrate config file before merge'
+)
- if fname.endswith('.gz'):
- with gzip.open(fname, 'rb') as f:
- try:
- config_str = f.read().decode()
- except OSError as e:
- sys.exit(e)
- else:
- with open(fname, 'r') as f:
- try:
- config_str = f.read()
- except OSError as e:
- sys.exit(e)
+args = parser.parse_args()
- return config_str
-
-if any(file_name.startswith(f'{x}://') for x in protocols):
- config_string = vyos.remote.get_remote_config(file_name)
- if not config_string:
- sys.exit(f"No such config file at '{file_name}'")
-else:
- config_string = get_local_config(file_name)
+file_name = args.config_file
-config = LoadConfig()
+# pylint: disable=consider-using-with
+file_path = tempfile.NamedTemporaryFile(delete=False).name
+err = get_config_file(file_name, file_path)
+if err:
+ os.remove(file_path)
+ sys.exit(err)
-print(f"Loading configuration from '{file_name}'")
+if args.migrate:
+ migrate = ConfigMigrate(file_path)
+ try:
+ migrate.run()
+ except ConfigMigrateError as e:
+ os.remove(file_path)
+ sys.exit(e)
-with tempfile.NamedTemporaryFile() as fp:
- with open(fp.name, 'w') as fd:
- fd.write(config_string)
+config = Config()
- config_migrate = ConfigMigrate(fp.name)
- try:
- config_migrate.run()
- except ConfigMigrateError as err:
- sys.exit(err)
+if config.vyconf_session is not None:
+ out, err = config.vyconf_session.load_config(file_path)
+ if err:
+ os.remove(file_path)
+ sys.exit(out)
+ print(out)
+else:
+ load_config(file_path)
- try:
- config.load_config(fp.name)
- except VyOSError as err:
- sys.exit(err)
+os.remove(file_path)
if config.session_changed():
print("Load complete. Use 'commit' to make changes effective.")
else:
- print("No configuration changes to commit.")
+ print('No configuration changes to commit.')
diff --git a/src/helpers/vyos-merge-config.py b/src/helpers/vyos-merge-config.py
index 5ef845ac2..e8a696eb5 100755
--- a/src/helpers/vyos-merge-config.py
+++ b/src/helpers/vyos-merge-config.py
@@ -1,108 +1,101 @@
#!/usr/bin/python3
-# Copyright 2019-2024 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 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 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 library is distributed in the hope that it will be useful,
+# 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
-# Lesser General Public License for more details.
+# 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/>.
+#
#
-# 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 os
import sys
+import shlex
+import argparse
import tempfile
-import vyos.defaults
-import vyos.remote
+from vyos.remote import get_config_file
from vyos.config import Config
from vyos.configtree import ConfigTree
+from vyos.configtree import mask_inclusive
+from vyos.configtree import merge
from vyos.migrate import ConfigMigrate
from vyos.migrate import ConfigMigrateError
-from vyos.utils.process import cmd
-from vyos.utils.process import DEVNULL
+from vyos.load_config import load_explicit
-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]
+parser = argparse.ArgumentParser()
+parser.add_argument('config_file', help='config file to merge from')
+parser.add_argument(
+ '--destructive', action='store_true', help='replace values with those of merge file'
+)
+parser.add_argument('--paths', nargs='+', help='only merge from listed paths')
+parser.add_argument(
+ '--migrate', action='store_true', help='migrate config file before merge'
+)
-configdir = vyos.defaults.directories['config']
+args = parser.parse_args()
-protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp']
+file_name = args.config_file
+paths = [shlex.split(s) for s in args.paths] if args.paths else []
-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)
-
-with tempfile.NamedTemporaryFile() as file_to_migrate:
- with open(file_to_migrate.name, 'w') as fd:
- fd.write(config_file)
-
- config_migrate = ConfigMigrate(file_to_migrate.name)
+# pylint: disable=consider-using-with
+file_path = tempfile.NamedTemporaryFile(delete=False).name
+err = get_config_file(file_name, file_path)
+if err:
+ os.remove(file_path)
+ sys.exit(err)
+
+if args.migrate:
+ migrate = ConfigMigrate(file_path)
try:
- config_migrate.run()
+ migrate.run()
except ConfigMigrateError as e:
+ os.remove(file_path)
sys.exit(e)
-merge_config_tree = ConfigTree(config_file)
+with open(file_path) as f:
+ merge_str = f.read()
+
+merge_ct = ConfigTree(merge_str)
-effective_config = Config()
-effective_config_tree = effective_config._running_config
+if paths:
+ mask = ConfigTree('')
+ for p in paths:
+ mask.set(p)
-effective_cmds = effective_config_tree.to_commands()
-merge_cmds = merge_config_tree.to_commands()
+ merge_ct = mask_inclusive(merge_ct, mask)
-effective_cmd_list = effective_cmds.splitlines()
-merge_cmd_list = merge_cmds.splitlines()
+with open(file_path, 'w') as f:
+ f.write(merge_ct.to_string())
-effective_cmd_set = set(effective_cmd_list)
-add_cmds = [ cmd for cmd in merge_cmd_list if cmd not in effective_cmd_set ]
+config = Config()
-path = None
-if (len(sys.argv) > 2):
- path = sys.argv[2:]
- if (not effective_config_tree.exists(path) and not
- merge_config_tree.exists(path)):
- print("path {} does not exist in either effective or merge"
- " config; will use root.".format(path))
- path = None
- else:
- path = " ".join(path)
+if config.vyconf_session is not None:
+ out, err = config.vyconf_session.merge_config(
+ file_path, destructive=args.destructive
+ )
+ if err:
+ os.remove(file_path)
+ sys.exit(out)
+ print(out)
+else:
+ session_ct = config.get_config_tree()
+ merge_res = merge(session_ct, merge_ct, destructive=args.destructive)
-if path:
- add_cmds = [ cmd for cmd in add_cmds if path in cmd ]
+ load_explicit(merge_res)
-for add in add_cmds:
- try:
- cmd(f'/opt/vyatta/sbin/my_{add}', shell=True, stderr=DEVNULL)
- except OSError as err:
- print(err)
+os.remove(file_path)
-if effective_config.session_changed():
+if config.session_changed():
print("Merge complete. Use 'commit' to make changes effective.")
else:
- print("No configuration changes to commit.")
+ print('No configuration changes to commit.')
diff --git a/src/helpers/vyos-save-config.py b/src/helpers/vyos-save-config.py
index fa2ea0ce4..adf62b71d 100755
--- a/src/helpers/vyos-save-config.py
+++ b/src/helpers/vyos-save-config.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2023 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos-sudo.py b/src/helpers/vyos-sudo.py
deleted file mode 100755
index 75dd7f29d..000000000
--- a/src/helpers/vyos-sudo.py
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/usr/bin/env 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 os
-import sys
-
-from vyos.utils.permission import is_admin
-
-
-if __name__ == '__main__':
- if len(sys.argv) < 2:
- print('Missing command argument')
- sys.exit(1)
-
- if not is_admin():
- print('This account is not authorized to run this command')
- sys.exit(1)
-
- os.execvp('sudo', ['sudo'] + sys.argv[1:])
diff --git a/src/helpers/vyos_config_sync.py b/src/helpers/vyos_config_sync.py
index 9d9aec376..80bfb6d17 100755
--- a/src/helpers/vyos_config_sync.py
+++ b/src/helpers/vyos_config_sync.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2023-2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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
diff --git a/src/helpers/vyos_net_name b/src/helpers/vyos_net_name
index f5de182c6..6ad7af2d6 100755
--- a/src/helpers/vyos_net_name
+++ b/src/helpers/vyos_net_name
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2021-2024 VyOS maintainers and contributors
+# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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