Skip to content

Commit b91acb7

Browse files
authored
Add FreeBSD operations & facts (#1246)
1 parent efb7cb3 commit b91acb7

File tree

39 files changed

+927
-1
lines changed

39 files changed

+927
-1
lines changed

pyinfra/facts/freebsd.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from __future__ import annotations
2+
3+
from typing_extensions import Optional
4+
5+
from pyinfra.api import FactBase
6+
from pyinfra.api.command import QuoteString, make_formatted_string_command
7+
8+
9+
class ServiceScript(FactBase):
10+
@staticmethod
11+
def command(srvname: str, jail: Optional[str] = None):
12+
if jail is None:
13+
jail = ""
14+
15+
return make_formatted_string_command(
16+
(
17+
"for service in `service -j {0} -l`; do "
18+
'if [ {1} = \\"$service\\" ]; '
19+
'then echo \\"$service\\"; '
20+
"fi; "
21+
"done"
22+
),
23+
QuoteString(jail),
24+
QuoteString(srvname),
25+
)
26+
27+
28+
class ServiceStatus(FactBase):
29+
@staticmethod
30+
def command(srvname: str, jail: Optional[str] = None):
31+
if jail is None:
32+
jail = ""
33+
34+
return make_formatted_string_command(
35+
(
36+
"service -j {0} {1} status > /dev/null 2>&1; "
37+
"if [ $? -eq 0 ]; then "
38+
"echo running; "
39+
"fi"
40+
),
41+
QuoteString(jail),
42+
QuoteString(srvname),
43+
)
44+
45+
46+
class Sysrc(FactBase):
47+
@staticmethod
48+
def command(parameter: str, jail: Optional[str] = None):
49+
if jail is None:
50+
command = make_formatted_string_command(
51+
("sysrc -in -- {0} || true"), QuoteString(parameter)
52+
)
53+
else:
54+
command = make_formatted_string_command(
55+
("sysrc -j {0} -in -- {1} || true"), QuoteString(jail), QuoteString(parameter)
56+
)
57+
58+
return command
59+
60+
61+
class PkgPackage(FactBase):
62+
@staticmethod
63+
def command(package: str, jail: Optional[str] = None):
64+
if jail is None:
65+
command = make_formatted_string_command(
66+
("pkg info -E -- {0} 2> /dev/null || true"), QuoteString(package)
67+
)
68+
else:
69+
command = make_formatted_string_command(
70+
("pkg -j {0} info -E -- {1} 2> /dev/null || true"),
71+
QuoteString(jail),
72+
QuoteString(package),
73+
)
74+
75+
return command
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file only exists to support:
2+
# from pyinfra.operations import freebsd
3+
# freebsd.X.Y
4+
5+
from glob import glob
6+
from os import path
7+
8+
module_filenames = glob(path.join(path.dirname(__file__), "*.py"))
9+
module_names = [path.basename(name)[:-3] for name in module_filenames]
10+
__all__ = [name for name in module_names if name != "__init__"]
11+
12+
from . import * # noqa
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Fetch and install binary updates to FreeBSD.
3+
"""
4+
5+
from __future__ import annotations
6+
7+
from typing_extensions import List, Optional, Union
8+
9+
from pyinfra.api import QuoteString, StringCommand, operation
10+
11+
12+
@operation()
13+
def update(
14+
force: bool = False,
15+
basedir: Optional[str] = None,
16+
workdir: Optional[str] = None,
17+
conffile: Optional[str] = None,
18+
jail: Optional[str] = None,
19+
key: Optional[str] = None,
20+
currently_running: Optional[str] = None,
21+
server: Optional[str] = None,
22+
):
23+
"""
24+
Based on the currently installed world and the configuration options set, fetch
25+
all available binary updates and install them.
26+
27+
+ force: See ``-F`` in ``freebsd-update(8)``.
28+
+ basedir: See ``-b`` in ``freebsd-update(8)``.
29+
+ workdir: See ``-d`` in ``freebsd-update(8)``.
30+
+ conffile: See ``-f`` in ``freebsd-update(8)``.
31+
+ jail: See ``-j`` in ``freebsd-update(8)``.
32+
+ key: See ``-k`` in ``freebsd-update(8)``.
33+
+ currently_running: See ``--currently-running`` in ``freebsd-update(8)``.
34+
+ server: See ``-s`` in ``freebsd-update(8)``.
35+
36+
**Example:**
37+
38+
.. code:: python
39+
40+
freebsd_update.update()
41+
"""
42+
43+
args: List[Union[str, "QuoteString"]] = []
44+
45+
args.extend(["PAGER=cat", "freebsd-update", "--not-running-from-cron"])
46+
47+
if force:
48+
args.append("-F")
49+
50+
if basedir is not None:
51+
args.extend(["-b", QuoteString(basedir)])
52+
53+
if workdir is not None:
54+
args.extend(["-d", QuoteString(workdir)])
55+
56+
if conffile is not None:
57+
args.extend(["-f", QuoteString(conffile)])
58+
59+
if jail is not None:
60+
args.extend(["-j", QuoteString(jail)])
61+
62+
if key is not None:
63+
args.extend(["-k", QuoteString(key)])
64+
65+
if server is not None:
66+
args.extend(["-s", QuoteString(server)])
67+
68+
args.extend(["fetch", "install"])
69+
70+
yield StringCommand(*args)

pyinfra/operations/freebsd/pkg.py

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
"""
2+
Manage FreeBSD packages.
3+
"""
4+
5+
from __future__ import annotations
6+
7+
from typing_extensions import List, Optional, Union
8+
9+
from pyinfra import host
10+
from pyinfra.api import QuoteString, StringCommand, operation
11+
from pyinfra.facts.freebsd import PkgPackage
12+
13+
14+
@operation()
15+
def update(jail: Optional[str] = None, force: bool = False, reponame: Optional[str] = None):
16+
"""
17+
Update the local catalogues of the enabled package repositories.
18+
19+
+ jail: See ``-j`` in ``pkg(8)``.
20+
+ force: See ``-f`` in ``pkg-update(8)``.
21+
+ reponame: See ``-r`` in ``pkg-update(8)``
22+
23+
**Examples:**
24+
25+
.. code:: python
26+
27+
# host
28+
pkg.update()
29+
30+
# jail
31+
pkg.update(
32+
jail="nginx"
33+
)
34+
"""
35+
36+
args: List[Union[str, "QuoteString"]] = []
37+
38+
args.append("pkg")
39+
40+
if jail is not None:
41+
args.extend(["-j", QuoteString(jail)])
42+
43+
args.extend(["update"])
44+
45+
if force:
46+
args.append("-f")
47+
48+
if reponame is not None:
49+
args.extend(["-r", QuoteString(reponame)])
50+
51+
yield StringCommand(*args)
52+
53+
54+
@operation()
55+
def upgrade(jail: Optional[str] = None, force: bool = False, reponame: Optional[str] = None):
56+
"""
57+
Perform upgrades of package software distributions.
58+
59+
+ jail: See ``-j`` in ``pkg(8)``.
60+
+ force: See ``-f`` in ``pkg-upgrade(8)``.
61+
+ reponame: See ``-r`` in ``pkg-upgrade(8)``.
62+
63+
**Examples:**
64+
65+
.. code:: python
66+
67+
# host
68+
pkg.upgrade()
69+
70+
# jail
71+
pkg.upgrade(
72+
jail="nginx"
73+
)
74+
"""
75+
76+
args: List[Union[str, "QuoteString"]] = []
77+
78+
args.append("pkg")
79+
80+
if jail is not None:
81+
args.extend(["-j", QuoteString(jail)])
82+
83+
args.extend(["upgrade", "-y"])
84+
85+
if force:
86+
args.append("-f")
87+
88+
if reponame is not None:
89+
args.extend(["-r", QuoteString(reponame)])
90+
91+
yield StringCommand(*args)
92+
93+
94+
@operation()
95+
def install(package: str, jail: Optional[str] = None, reponame: Optional[str] = None):
96+
"""
97+
Install packages from remote packages repositories or local archives.
98+
99+
+ package: Package to install.
100+
+ jail: See ``-j`` in ``pkg(8)``.
101+
+ reponame: See ``-r`` in ``pkg-install(8)``.
102+
103+
**Example:**
104+
105+
.. code:: python
106+
107+
pkg.install("nginx")
108+
"""
109+
110+
if host.get_fact(PkgPackage, package=package, jail=jail):
111+
host.noop(f"Package '{package}' already installed")
112+
return
113+
114+
args: List[Union[str, "QuoteString"]] = []
115+
116+
args.append("pkg")
117+
118+
if jail is not None:
119+
args.extend(["-j", QuoteString(jail)])
120+
121+
args.extend(["install", "-y"])
122+
123+
if reponame is not None:
124+
args.extend(["-r", QuoteString(reponame)])
125+
126+
args.extend(["--", QuoteString(package)])
127+
128+
yield StringCommand(*args)
129+
130+
131+
@operation()
132+
def remove(package: str, jail: Optional[str] = None):
133+
"""
134+
Deletes packages from the database and the system.
135+
136+
+ package: Package to remove.
137+
+ jail: See ``-j`` in ``pkg(8)``.
138+
139+
**Example:**
140+
141+
.. code:: python
142+
143+
pkg.remove("nginx")
144+
"""
145+
146+
if not host.get_fact(PkgPackage, package=package, jail=jail):
147+
host.noop(f"Package '{package}' cannot be found")
148+
return
149+
150+
args: List[Union[str, "QuoteString"]] = []
151+
152+
args.append("pkg")
153+
154+
if jail is not None:
155+
args.extend(["-j", QuoteString(jail)])
156+
157+
args.extend(["remove", "-y"])
158+
159+
args.extend(["--", QuoteString(package)])
160+
161+
yield StringCommand(*args)
162+
163+
164+
@operation()
165+
def autoremove(jail: Optional[str] = None):
166+
"""
167+
Remove orphan packages.
168+
169+
+ jail: See ``-j`` in ``pkg(8)``.
170+
171+
**Example:**
172+
173+
.. code:: python
174+
175+
pkg.autoremove()
176+
"""
177+
178+
args: List[Union[str, "QuoteString"]] = []
179+
180+
args.append("pkg")
181+
182+
if jail is not None:
183+
args.extend(["-j", QuoteString(jail)])
184+
185+
args.extend(["autoremove", "-y"])
186+
187+
yield StringCommand(*args)
188+
189+
190+
@operation()
191+
def clean(all_pkg: bool = False, jail: Optional[str] = None):
192+
"""
193+
Clean the local cache of fetched remote packages.
194+
195+
+ all_pkg: See ``-a`` in ``pkg-clean(8)``.
196+
+ jail: See ``-j`` in ``pkg(8)``.
197+
198+
**Example:**
199+
200+
.. code:: python
201+
202+
pkg.clean(
203+
all_pkg=True
204+
)
205+
"""
206+
207+
args: List[Union[str, "QuoteString"]] = []
208+
209+
args.append("pkg")
210+
211+
if jail is not None:
212+
args.extend(["-j", QuoteString(jail)])
213+
214+
args.extend(["clean", "-y"])
215+
216+
if all_pkg:
217+
args.append("-a")
218+
219+
yield StringCommand(*args)

0 commit comments

Comments
 (0)