-
Notifications
You must be signed in to change notification settings - Fork 422
T8445: T8335: Extend config activation system #5096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jestabro
wants to merge
13
commits into
vyos:current
Choose a base branch
from
jestabro:extend-activation-system
base: current
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f090edd
T8445: generate list of activation scripts at build time
jestabro aa4b6f0
T8445: add activation utils
jestabro 36728f6
T8445: add falsy callable
jestabro 24017a9
T8445: add revised boot activation script runner
jestabro a5647f1
T8445: catch script runner error in vyos-router
jestabro 2c59e58
T8445: add activation op-mode script
jestabro f1c891b
T8445: add op-mode definitions
jestabro f09cb9b
T8445: normalize top-level help message for 'set'
jestabro 4f0b424
T8445: add example activation script
jestabro fd6c226
T8445: add activation script to indicate first installed boot
jestabro 06104ae
T8335: use activation script for installation of config path hint
jestabro 443985f
T8445: update ethernet offload support activation script
jestabro 6c4122f
T8445: set activation hint during install image
jestabro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <interfaceDefinition> | ||||||
| <node name="set"> | ||||||
| <properties> | ||||||
| <help>Set parameters and behaviors</help> | ||||||
| </properties> | ||||||
| <children> | ||||||
| <node name="system"> | ||||||
| <properties> | ||||||
| <help>Set system operational parameters</help> | ||||||
| </properties> | ||||||
| <children> | ||||||
| <node name="activation"> | ||||||
| <properties> | ||||||
| <help>Activation scripts at boot</help> | ||||||
| </properties> | ||||||
| <children> | ||||||
| <tagNode name="script"> | ||||||
| <properties> | ||||||
| <help>Script name</help> | ||||||
| <completionHelp> | ||||||
| <script>${vyos_op_scripts_dir}/activation.py show_list</script> | ||||||
| </completionHelp> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a constraint to allow only files which are present in the supported directory. Also prevent escape paths like |
||||||
| </properties> | ||||||
| <children> | ||||||
| <tagNode name="active"> | ||||||
| <properties> | ||||||
| <help>Set activation on reboot</help> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| <completionHelp> | ||||||
| <script>${vyos_op_scripts_dir}/activation.py show_opts</script> | ||||||
| </completionHelp> | ||||||
| </properties> | ||||||
| <command>${vyos_op_scripts_dir}/activation.py set_active --name "${5}" --value "${7}"</command> | ||||||
| </tagNode> | ||||||
| </children> | ||||||
| </tagNode> | ||||||
| </children> | ||||||
| </node> | ||||||
| </children> | ||||||
| </node> | ||||||
| </children> | ||||||
| </node> | ||||||
| <node name="show"> | ||||||
| <children> | ||||||
| <node name="system"> | ||||||
| <properties> | ||||||
| <help>Show system information</help> | ||||||
| </properties> | ||||||
| <children> | ||||||
| <node name="activation"> | ||||||
| <properties> | ||||||
| <help>Activation scripts at boot</help> | ||||||
| </properties> | ||||||
| <command>${vyos_op_scripts_dir}/activation.py show</command> | ||||||
| </node> | ||||||
| </children> | ||||||
| </node> | ||||||
| </children> | ||||||
| </node> | ||||||
| </interfaceDefinition> | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright (C) VyOS Inc. | ||
| # | ||
| # 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 json | ||
| import typing | ||
| from pathlib import Path | ||
|
|
||
| from vyos.base import Warning as Warn | ||
| from vyos.defaults import activation_list | ||
| from vyos.defaults import activation_init | ||
| from vyos.defaults import activation_hint | ||
| from vyos.defaults import directories | ||
|
|
||
|
|
||
| ActiveOpt = typing.Literal['enabled', 'once', 'off', 'never'] | ||
|
|
||
|
|
||
| def get_activation_scripts() -> dict: | ||
| list_path = Path(activation_list) | ||
| return json.loads(list_path.read_text()) | ||
|
|
||
|
|
||
| def set_activation(file_name: str, value: ActiveOpt): | ||
| script_dict = get_activation_scripts() | ||
| file_key = Path(file_name).stem | ||
| script_dict[file_key] = value | ||
| list_path = Path(activation_list) | ||
| list_path.write_text(json.dumps(script_dict)) | ||
|
|
||
|
|
||
| def get_activation(file_name: str) -> ActiveOpt: | ||
| script_dict = get_activation_scripts() | ||
| file_key = Path(file_name).stem | ||
| return script_dict[file_key] | ||
|
|
||
|
|
||
| def is_active(file_name: str) -> bool: | ||
| script_dict = get_activation_scripts() | ||
| file_key = Path(file_name).stem | ||
| if script_dict[file_key] in ('enabled', 'once'): | ||
| return True | ||
| return False | ||
jestabro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def stable_update(new: dict, old: dict): | ||
| res = {} | ||
| for key in new.keys(): | ||
| res[key] = old[key] if key in old.keys() else new[key] | ||
| return res | ||
|
|
||
|
|
||
| def init_activation_list() -> bool: | ||
| """Init if activation_hint exists, left on install_image or if image was | ||
| built as raw_image""" | ||
|
|
||
| init_hint = Path(activation_hint) | ||
| if not init_hint.exists(): | ||
| return False | ||
|
|
||
| init_hint.unlink() | ||
| init_list = Path(activation_init) | ||
| data_list = Path(activation_list) | ||
| data_obj = json.loads(init_list.read_text()) | ||
| data_list.write_text(json.dumps(data_obj)) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def refresh_activation_list(): | ||
| """Refresh activation list, as will be needed after image update""" | ||
|
|
||
| if init_activation_list(): | ||
| return | ||
|
|
||
| new_list_path = Path(directories['data']).joinpath(Path(activation_list).name) | ||
| if not new_list_path.exists(): | ||
| return | ||
|
|
||
| new_obj = json.loads(new_list_path.read_text()) | ||
|
|
||
| orig_list_path = Path(activation_list) | ||
| if orig_list_path.exists(): | ||
| orig_obj = json.loads(orig_list_path.read_text()) | ||
| if orig_obj == new_obj: | ||
| return | ||
|
|
||
| obj = stable_update(new_obj, orig_obj) | ||
| else: | ||
| obj = new_obj | ||
|
|
||
| orig_list_path.write_text(json.dumps(obj)) | ||
|
|
||
|
|
||
| first_installed_boot_file = '/run/first_installed_boot' | ||
|
|
||
|
|
||
| def set_first_installed_boot(): | ||
| try: | ||
| Path(first_installed_boot_file).touch(exist_ok=False) | ||
| except FileExistsError: | ||
| Warn('redundant set of first_installed_boot') | ||
|
|
||
|
|
||
| def is_first_installed_boot(): | ||
| return Path(first_installed_boot_file).exists() | ||
|
|
||
|
|
||
| def set_config_path_hint(): | ||
| """The config hint allows subsequent installs to find previous disk | ||
| resident config data. It is traditionally added as part of the image | ||
| install procedure, however, for raw image builds an alternative is | ||
| needed.""" | ||
| Path(directories['config']).joinpath('.vyatta_config').touch(exist_ok=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright (C) VyOS Inc. | ||
| # | ||
| # 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/>. | ||
|
|
||
|
|
||
| """Module for functors and higher order functions.""" | ||
|
|
||
|
|
||
| class FalseCallable: | ||
| """Define falsy callable for use as default value for getattr of a | ||
| function from a module defined by vyos.utils.system.load_as_module""" | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| pass | ||
|
|
||
| def __bool__(self): | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Copyright (C) VyOS Inc. | ||
| # | ||
| # 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 re | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def filter_key(s: Path): | ||
| s = s.stem | ||
| return re.match(r'\d+\-.+', s) | ||
|
|
||
|
|
||
| def sort_key(s: Path): | ||
| s = s.stem | ||
| pre, rem = re.match(r'(\d+)(?:-)(.+)', s).groups() | ||
| return int(pre), rem | ||
|
|
||
|
|
||
| activation_dir = 'src/activation-scripts' | ||
| activation_list = 'data/activation-list' | ||
| activation_list_init = 'data/activation-init' | ||
|
|
||
| activation_scripts = Path(activation_dir).glob('*.py') | ||
|
|
||
| filtered = filter(filter_key, activation_scripts) | ||
| script_list = sorted(filtered, key=sort_key) | ||
|
|
||
| script_dict = dict.fromkeys(map(lambda s: s.stem, script_list), 'off') | ||
| script_dict_init = dict.fromkeys(map(lambda s: s.stem, script_list), 'enabled') | ||
|
|
||
| Path(activation_list).write_text(json.dumps(script_dict)) | ||
| Path(activation_list_init).write_text(json.dumps(script_dict_init)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Copyright (C) VyOS Inc. | ||
| # | ||
| # 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/>. | ||
|
|
||
|
|
||
| from vyos.configtree import ConfigTree | ||
| from vyos.system.image import is_live_boot | ||
| from vyos.utils.activate import set_activation | ||
| from vyos.utils.activate import set_first_installed_boot | ||
| from vyos.utils.activate import is_first_installed_boot | ||
|
|
||
|
|
||
| def pre_condition() -> bool: | ||
| return not is_live_boot() | ||
|
|
||
|
|
||
| def activate(_config: ConfigTree) -> None: | ||
| pass | ||
|
|
||
|
|
||
| def post_condition() -> None: | ||
| set_first_installed_boot() | ||
| if is_first_installed_boot(): | ||
| set_activation(__file__, 'never') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Copyright (C) VyOS Inc. | ||
| # | ||
| # 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/>. | ||
|
|
||
|
|
||
| from vyos.configtree import ConfigTree | ||
| from vyos.system.image import is_live_boot | ||
| from vyos.utils.activate import set_activation | ||
| from vyos.utils.activate import set_config_path_hint | ||
| from vyos.utils.activate import is_first_installed_boot | ||
|
|
||
|
|
||
| def pre_condition() -> bool: | ||
| return not is_live_boot() | ||
|
|
||
|
|
||
| def activate(_config: ConfigTree) -> None: | ||
| pass | ||
|
|
||
|
|
||
| def post_condition() -> None: | ||
| if is_first_installed_boot(): | ||
| set_config_path_hint() | ||
| set_activation(__file__, 'never') |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do not check the details in the description, the CLI, at first glance, wants to "activate" the system with a license key.
At least that's what I associate with it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps 'activation unit' is preferable ? That would avoid some confusion for the op-mode directive ...