-
-
Notifications
You must be signed in to change notification settings - Fork 453
Add FreeBSD operations & facts #1246
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab19881
Add FreeBSD operations & facts
DtxdF cd2d05f
Escape more strings
DtxdF d30f408
Fix logic in SRV_CUSTOM
DtxdF 4e14259
Fix type-checking errors
DtxdF b402716
Add tests for FreeBSD operations & facts
DtxdF 73adda8
Remove unused `enum` library
DtxdF e16bb2c
Remove conflicting blank line
DtxdF 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import Optional | ||
|
|
||
| from pyinfra.api import FactBase | ||
| from pyinfra.api.command import QuoteString, make_formatted_string_command | ||
|
|
||
|
|
||
| class ServiceScript(FactBase): | ||
| @staticmethod | ||
| def command(srvname: str, jail: Optional[str] = None): | ||
| if jail is None: | ||
| jail = "" | ||
|
|
||
| return make_formatted_string_command( | ||
| ( | ||
| "for service in `service -j {0} -l`; do " | ||
| 'if [ {1} = \\"$service\\" ]; ' | ||
| 'then echo \\"$service\\"; ' | ||
| "fi; " | ||
| "done" | ||
| ), | ||
| QuoteString(jail), | ||
| QuoteString(srvname), | ||
| ) | ||
|
|
||
|
|
||
| class ServiceStatus(FactBase): | ||
| @staticmethod | ||
| def command(srvname: str, jail: Optional[str] = None): | ||
| if jail is None: | ||
| jail = "" | ||
|
|
||
| return make_formatted_string_command( | ||
| ( | ||
| "service -j {0} {1} status > /dev/null 2>&1; " | ||
| "if [ $? -eq 0 ]; then " | ||
| "echo running; " | ||
| "fi" | ||
| ), | ||
| QuoteString(jail), | ||
| QuoteString(srvname), | ||
| ) | ||
|
|
||
|
|
||
| class Sysrc(FactBase): | ||
| @staticmethod | ||
| def command(parameter: str, jail: Optional[str] = None): | ||
| if jail is None: | ||
| command = make_formatted_string_command( | ||
| ("sysrc -in -- {0} || true"), QuoteString(parameter) | ||
| ) | ||
| else: | ||
| command = make_formatted_string_command( | ||
| ("sysrc -j {0} -in -- {1} || true"), QuoteString(jail), QuoteString(parameter) | ||
| ) | ||
|
|
||
| return command | ||
|
|
||
|
|
||
| class PkgPackage(FactBase): | ||
| @staticmethod | ||
| def command(package: str, jail: Optional[str] = None): | ||
| if jail is None: | ||
| command = make_formatted_string_command( | ||
| ("pkg info -E -- {0} 2> /dev/null || true"), QuoteString(package) | ||
| ) | ||
| else: | ||
| command = make_formatted_string_command( | ||
| ("pkg -j {0} info -E -- {1} 2> /dev/null || true"), | ||
| QuoteString(jail), | ||
| QuoteString(package), | ||
| ) | ||
|
|
||
| return command |
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,12 @@ | ||
| # This file only exists to support: | ||
| # from pyinfra.operations import freebsd | ||
| # freebsd.X.Y | ||
|
|
||
| from glob import glob | ||
| from os import path | ||
|
|
||
| module_filenames = glob(path.join(path.dirname(__file__), "*.py")) | ||
| module_names = [path.basename(name)[:-3] for name in module_filenames] | ||
| __all__ = [name for name in module_names if name != "__init__"] | ||
|
|
||
| from . import * # noqa | ||
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,70 @@ | ||
| """ | ||
| Fetch and install binary updates to FreeBSD. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import List, Optional, Union | ||
|
|
||
| from pyinfra.api import QuoteString, StringCommand, operation | ||
|
|
||
|
|
||
| @operation() | ||
| def update( | ||
| force: bool = False, | ||
| basedir: Optional[str] = None, | ||
| workdir: Optional[str] = None, | ||
| conffile: Optional[str] = None, | ||
| jail: Optional[str] = None, | ||
| key: Optional[str] = None, | ||
| currently_running: Optional[str] = None, | ||
| server: Optional[str] = None, | ||
| ): | ||
| """ | ||
| Based on the currently installed world and the configuration options set, fetch | ||
| all available binary updates and install them. | ||
|
|
||
| + force: See ``-F`` in ``freebsd-update(8)``. | ||
| + basedir: See ``-b`` in ``freebsd-update(8)``. | ||
| + workdir: See ``-d`` in ``freebsd-update(8)``. | ||
| + conffile: See ``-f`` in ``freebsd-update(8)``. | ||
| + jail: See ``-j`` in ``freebsd-update(8)``. | ||
| + key: See ``-k`` in ``freebsd-update(8)``. | ||
| + currently_running: See ``--currently-running`` in ``freebsd-update(8)``. | ||
| + server: See ``-s`` in ``freebsd-update(8)``. | ||
|
|
||
| **Example:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| freebsd_update.update() | ||
| """ | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.extend(["PAGER=cat", "freebsd-update", "--not-running-from-cron"]) | ||
|
|
||
| if force: | ||
| args.append("-F") | ||
|
|
||
| if basedir is not None: | ||
| args.extend(["-b", QuoteString(basedir)]) | ||
|
|
||
| if workdir is not None: | ||
| args.extend(["-d", QuoteString(workdir)]) | ||
|
|
||
| if conffile is not None: | ||
| args.extend(["-f", QuoteString(conffile)]) | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| if key is not None: | ||
| args.extend(["-k", QuoteString(key)]) | ||
|
|
||
| if server is not None: | ||
| args.extend(["-s", QuoteString(server)]) | ||
|
|
||
| args.extend(["fetch", "install"]) | ||
|
|
||
| yield StringCommand(*args) |
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,219 @@ | ||
| """ | ||
| Manage FreeBSD packages. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import List, Optional, Union | ||
|
|
||
| from pyinfra import host | ||
| from pyinfra.api import QuoteString, StringCommand, operation | ||
| from pyinfra.facts.freebsd import PkgPackage | ||
|
|
||
|
|
||
| @operation() | ||
| def update(jail: Optional[str] = None, force: bool = False, reponame: Optional[str] = None): | ||
| """ | ||
| Update the local catalogues of the enabled package repositories. | ||
|
|
||
| + jail: See ``-j`` in ``pkg(8)``. | ||
| + force: See ``-f`` in ``pkg-update(8)``. | ||
| + reponame: See ``-r`` in ``pkg-update(8)`` | ||
|
|
||
| **Examples:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| # host | ||
| pkg.update() | ||
|
|
||
| # jail | ||
| pkg.update( | ||
| jail="nginx" | ||
| ) | ||
| """ | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.append("pkg") | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| args.extend(["update"]) | ||
|
|
||
| if force: | ||
| args.append("-f") | ||
|
|
||
| if reponame is not None: | ||
| args.extend(["-r", QuoteString(reponame)]) | ||
|
|
||
| yield StringCommand(*args) | ||
|
|
||
|
|
||
| @operation() | ||
| def upgrade(jail: Optional[str] = None, force: bool = False, reponame: Optional[str] = None): | ||
| """ | ||
| Perform upgrades of package software distributions. | ||
|
|
||
| + jail: See ``-j`` in ``pkg(8)``. | ||
| + force: See ``-f`` in ``pkg-upgrade(8)``. | ||
| + reponame: See ``-r`` in ``pkg-upgrade(8)``. | ||
|
|
||
| **Examples:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| # host | ||
| pkg.upgrade() | ||
|
|
||
| # jail | ||
| pkg.upgrade( | ||
| jail="nginx" | ||
| ) | ||
| """ | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.append("pkg") | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| args.extend(["upgrade", "-y"]) | ||
|
|
||
| if force: | ||
| args.append("-f") | ||
|
|
||
| if reponame is not None: | ||
| args.extend(["-r", QuoteString(reponame)]) | ||
|
|
||
| yield StringCommand(*args) | ||
|
|
||
|
|
||
| @operation() | ||
| def install(package: str, jail: Optional[str] = None, reponame: Optional[str] = None): | ||
| """ | ||
| Install packages from remote packages repositories or local archives. | ||
|
|
||
| + package: Package to install. | ||
| + jail: See ``-j`` in ``pkg(8)``. | ||
| + reponame: See ``-r`` in ``pkg-install(8)``. | ||
|
|
||
| **Example:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| pkg.install("nginx") | ||
| """ | ||
|
|
||
| if host.get_fact(PkgPackage, package=package, jail=jail): | ||
| host.noop(f"Package '{package}' already installed") | ||
| return | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.append("pkg") | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| args.extend(["install", "-y"]) | ||
|
|
||
| if reponame is not None: | ||
| args.extend(["-r", QuoteString(reponame)]) | ||
|
|
||
| args.extend(["--", QuoteString(package)]) | ||
|
|
||
| yield StringCommand(*args) | ||
|
|
||
|
|
||
| @operation() | ||
| def remove(package: str, jail: Optional[str] = None): | ||
| """ | ||
| Deletes packages from the database and the system. | ||
|
|
||
| + package: Package to remove. | ||
| + jail: See ``-j`` in ``pkg(8)``. | ||
|
|
||
| **Example:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| pkg.remove("nginx") | ||
| """ | ||
|
|
||
| if not host.get_fact(PkgPackage, package=package, jail=jail): | ||
| host.noop(f"Package '{package}' cannot be found") | ||
| return | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.append("pkg") | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| args.extend(["remove", "-y"]) | ||
|
|
||
| args.extend(["--", QuoteString(package)]) | ||
|
|
||
| yield StringCommand(*args) | ||
|
|
||
|
|
||
| @operation() | ||
| def autoremove(jail: Optional[str] = None): | ||
| """ | ||
| Remove orphan packages. | ||
|
|
||
| + jail: See ``-j`` in ``pkg(8)``. | ||
|
|
||
| **Example:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| pkg.autoremove() | ||
| """ | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.append("pkg") | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| args.extend(["autoremove", "-y"]) | ||
|
|
||
| yield StringCommand(*args) | ||
|
|
||
|
|
||
| @operation() | ||
| def clean(all_pkg: bool = False, jail: Optional[str] = None): | ||
| """ | ||
| Clean the local cache of fetched remote packages. | ||
|
|
||
| + all_pkg: See ``-a`` in ``pkg-clean(8)``. | ||
| + jail: See ``-j`` in ``pkg(8)``. | ||
|
|
||
| **Example:** | ||
|
|
||
| .. code:: python | ||
|
|
||
| pkg.clean( | ||
| all_pkg=True | ||
| ) | ||
| """ | ||
|
|
||
| args: List[Union[str, "QuoteString"]] = [] | ||
|
|
||
| args.append("pkg") | ||
|
|
||
| if jail is not None: | ||
| args.extend(["-j", QuoteString(jail)]) | ||
|
|
||
| args.extend(["clean", "-y"]) | ||
|
|
||
| if all_pkg: | ||
| args.append("-a") | ||
|
|
||
| yield StringCommand(*args) |
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.
I really like the use of a submodule for all the
freebsdoperations, neat.