Skip to content
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

Add FreeBSD operations & facts #1246

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions pyinfra/facts/freebsd.py
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 {1}`; do "
'if [ {1} = \\"$service\\" ]; '
'then printf \\"%s\\\\n\\" \\"$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
12 changes: 12 additions & 0 deletions pyinfra/operations/freebsd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file only exists to support:
Copy link
Member

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 freebsd operations, neat.

# 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
70 changes: 70 additions & 0 deletions pyinfra/operations/freebsd/freebsd_update.py
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)
219 changes: 219 additions & 0 deletions pyinfra/operations/freebsd/pkg.py
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):
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):
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)
Loading