-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] configs: new config infra with pydantic, with config via en…
…v and dynamic rootfs support (#264) This PR introduces otaclient package scope configuration, otaclient runtime configurable configs via environmental variables with validation and dynamic rooted path constants support. * Standalone otaclient.configs package for holding otaclient package scope configs Configs for otaclient top-level modules(i.e., app) and common used configs (i.e., debug configs, logging configs, etc,) are grouped together and placed under otaclient.configs package. * pydantic as configs storage back-end With pydantic as configs storing back-end, validation to otaclient configs are introduced and enforced. Other features from pydantic are also become available for Configs classes instances. * User configurable configs can be configured via env var With pydantic and pydantic-settings, some of the configs(configs in app_cfg.ConfigurableConfigs, debug_cfg and logging_cfg) that configure otaclient's runtime behavior are exposed to end-user and configurable via environmental variables. * Rootfs specifying support and dynamic rooted path constants support Now for otaclient running under container environment, the actual host rootfs mount point can be specified by OTA_HOST_ROOTFS, allowing otaclient to properly calling commands and executables from the host.
- Loading branch information
1 parent
0a9b2e4
commit df81898
Showing
53 changed files
with
2,092 additions
and
1,018 deletions.
There are no files selected for viewing
This file contains 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 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 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 2022 TIER IV, INC. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import os.path | ||
from otaclient._utils.typing import StrOrPath | ||
|
||
|
||
def replace_root(path: StrOrPath, old_root: StrOrPath, new_root: StrOrPath) -> str: | ||
"""Replace a <path> relative to <old_root> to <new_root>. | ||
For example, if path="/abc", old_root="/", new_root="/new_root", | ||
then we will have "/new_root/abc". | ||
""" | ||
# normalize all the input args | ||
path = os.path.normpath(path) | ||
old_root = os.path.normpath(old_root) | ||
new_root = os.path.normpath(new_root) | ||
|
||
if not (old_root.startswith("/") and new_root.startswith("/")): | ||
raise ValueError(f"{old_root=} and/or {new_root=} is not valid root") | ||
if os.path.commonpath([path, old_root]) != old_root: | ||
raise ValueError(f"{old_root=} is not the root of {path=}") | ||
return os.path.join(new_root, os.path.relpath(path, old_root)) |
This file contains 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,51 @@ | ||
# Copyright 2022 TIER IV, INC. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from pathlib import Path | ||
from typing import Any, Callable, TypeVar, Union | ||
from typing_extensions import Concatenate, ParamSpec | ||
|
||
P = ParamSpec("P") | ||
RT = TypeVar("RT") | ||
|
||
StrOrPath = Union[str, Path] | ||
|
||
|
||
def copy_callable_typehint(_source: Callable[P, Any]): | ||
"""This helper function return a decorator that can type hint the target | ||
function as the _source function. | ||
At runtime, this decorator actually does nothing, but just return the input function as it. | ||
But the returned function will have the same type hint as the source function in ide. | ||
It will not impact the runtime behavior of the decorated function. | ||
""" | ||
|
||
def _decorator(target) -> Callable[P, Any]: | ||
return target | ||
|
||
return _decorator | ||
|
||
|
||
def copy_callable_typehint_to_method(_source: Callable[P, Any]): | ||
"""Works the same as copy_callable_typehint, but omit the first arg.""" | ||
|
||
def _decorator(target: Callable[..., RT]) -> Callable[Concatenate[Any, P], RT]: | ||
return target # type: ignore | ||
|
||
return _decorator | ||
|
||
|
||
def check_port(_in: Any) -> bool: | ||
return isinstance(_in, int) and _in >= 0 and _in <= 65535 |
This file contains 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 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
Oops, something went wrong.