Skip to content

Commit 0309c3e

Browse files
lun-4Fizzadar
authored andcommitted
Add pkgin facts & operations (#659)
* Add pkgin.PkginPackages fact * Add pkgin operations * Add missing '-y' parameter to pkgin commands * Fix typo on doc comment
1 parent 328ab46 commit 0309c3e

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

pyinfra/facts/pkgin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pyinfra.api import FactBase
2+
3+
from .util.packaging import parse_packages
4+
5+
PKGIN_REGEX = r'^([a-zA-Z\-0-9]+)-([0-9\.]+\-?[a-z0-9]*)\s'
6+
7+
8+
class PkginPackages(FactBase):
9+
'''
10+
Returns a dict of installed pkgin packages:
11+
12+
.. code:: python
13+
14+
{
15+
'package_name': ['version'],
16+
}
17+
'''
18+
19+
command = 'pkgin list'
20+
requires_command = 'pkgin'
21+
22+
default = dict
23+
24+
def process(self, output):
25+
return parse_packages(PKGIN_REGEX, output, lower=False)

pyinfra/operations/pkgin.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'''
2+
Manage pkgin packages.
3+
'''
4+
5+
from pyinfra.api import operation
6+
from pyinfra.facts.pkgin import PkginPackages
7+
8+
from .util.packaging import ensure_packages
9+
10+
11+
@operation
12+
def upgrade(state=None, host=None):
13+
'''
14+
Upgrades all pkgin packages.
15+
'''
16+
17+
yield 'pkgin -y upgrade'
18+
19+
_upgrade = upgrade # noqa: E305
20+
21+
22+
@operation
23+
def update(state=None, host=None):
24+
'''
25+
Updates pkgin repositories.
26+
'''
27+
28+
yield 'pkgin -y update'
29+
30+
_update = update # noqa: E305
31+
32+
33+
@operation
34+
def packages(
35+
packages=None, present=True, latest=False,
36+
update=False, upgrade=False,
37+
state=None, host=None,
38+
):
39+
'''
40+
Add/remove/update pkgin packages.
41+
42+
+ packages: list of packages to ensure
43+
+ present: whether the packages should be installed
44+
+ latest: whether to upgrade packages without a specified version
45+
+ update: run ``pkgin update`` before installing packages
46+
+ upgrade: run ``pkgin upgrade`` before installing packages
47+
48+
Examples:
49+
50+
.. code:: python
51+
52+
# Update package list and install packages
53+
pkgin.packages(
54+
name='Install tmux and Vim',
55+
packages=['tmux', 'vim'],
56+
update=True,
57+
)
58+
59+
# Install the latest versions of packages (always check)
60+
pkgin.packages(
61+
name='Install latest Vim',
62+
packages=['vim'],
63+
latest=True,
64+
)
65+
'''
66+
67+
if update:
68+
yield _update(state=state, host=host)
69+
70+
if upgrade:
71+
yield _upgrade(state=state, host=host)
72+
73+
# TODO support glob for specific versions (it isn't as simple
74+
# as apt's, as pkgin supports something like 'mysql-server>=5.6<5.7')
75+
yield ensure_packages(
76+
host, packages, host.get_fact(PkginPackages), present,
77+
install_command='pkgin -y install',
78+
uninstall_command='pkgin -y remove',
79+
upgrade_command='pkgin -y upgrade',
80+
latest=latest,
81+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"command": "pkgin list",
3+
"requires_command": "pkgin",
4+
"output": [
5+
"SDL2-2.0.14nb2 Simple DirectMedia Layer - cross-platform multimedia library",
6+
"bash-5.1.4 The GNU Bourne Again Shell",
7+
"ca-certificates-20200601nb1 Root CA certificates from the Mozilla Project",
8+
"clang-10.0.1nb3 C language family frontend for LLVM",
9+
"cmake-3.19.7 Cross platform make"
10+
],
11+
"fact": {
12+
"SDL2": [
13+
"2.0.14nb2"
14+
],
15+
"bash": [
16+
"5.1.4"
17+
],
18+
"ca-certificates": [
19+
"20200601nb1"
20+
],
21+
"clang": [
22+
"10.0.1nb3"
23+
],
24+
"cmake": [
25+
"3.19.7"
26+
]
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"args": ["curl"],
3+
"facts": {
4+
"pkgin.PkginPackages": {}
5+
},
6+
"commands": [
7+
"pkgin -y install curl"
8+
]
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"args": [["curl", "i-dont-exist"]],
3+
"kwargs": {
4+
"present": false
5+
},
6+
"facts": {
7+
"pkgin.PkginPackages": {
8+
"curl": "1"
9+
}
10+
},
11+
"commands": [
12+
"pkgin -y remove curl"
13+
]
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"args": ["curl"],
3+
"kwargs": {
4+
"latest": true
5+
},
6+
"facts": {
7+
"pkgin.PkginPackages": {}
8+
},
9+
"commands": [
10+
"pkgin -y install curl"
11+
],
12+
"idempotent": false,
13+
"disable_itempotent_warning_reason": "package upgrades are always executed"
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"args": ["curl"],
3+
"kwargs": {
4+
"update": true,
5+
"upgrade": true
6+
},
7+
"facts": {
8+
"pkgin.PkginPackages": {
9+
"curl": ["1"]
10+
}
11+
},
12+
"commands": [
13+
"pkgin -y update",
14+
"pkgin -y upgrade"
15+
],
16+
"idempotent": false,
17+
"disable_itempotent_warning_reason": "package upgrades are always executed"
18+
}

0 commit comments

Comments
 (0)