Skip to content

Commit

Permalink
Migrate all the packaging operations to use explicit fact imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed Jul 10, 2021
1 parent 3abe2db commit 7547bfc
Show file tree
Hide file tree
Showing 103 changed files with 321 additions and 293 deletions.
3 changes: 2 additions & 1 deletion pyinfra/operations/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''

from pyinfra.api import operation
from pyinfra.facts.apk import ApkPackages

from .util.packaging import ensure_packages

Expand Down Expand Up @@ -73,7 +74,7 @@ def packages(
yield _upgrade(state=state, host=host)

yield ensure_packages(
host, packages, host.fact.apk_packages, present,
host, packages, host.get_fact(ApkPackages), present,
install_command='apk add',
uninstall_command='apk del',
upgrade_command='apk upgrade',
Expand Down
39 changes: 21 additions & 18 deletions pyinfra/operations/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from six.moves.urllib.parse import urlparse

from pyinfra.api import operation, OperationError
from pyinfra.facts.apt import parse_apt_repo
from pyinfra.facts.apt import AptKeys, AptSources, parse_apt_repo
from pyinfra.facts.deb import DebPackage, DebPackages
from pyinfra.facts.files import File
from pyinfra.facts.gpg import GpgKey
from pyinfra.facts.server import Date

from . import files
from .util.packaging import ensure_packages
Expand Down Expand Up @@ -62,10 +66,10 @@ def key(src=None, keyserver=None, keyid=None, state=None, host=None):
)
'''

existing_keys = host.fact.apt_keys
existing_keys = host.get_fact(AptKeys)

if src:
key_data = host.fact.gpg_key(src)
key_data = host.get_fact(GpgKey, src=src)
if key_data:
keyid = list(key_data.keys())

Expand Down Expand Up @@ -130,7 +134,7 @@ def repo(src, present=True, filename=None, state=None, host=None):
filename = '/etc/apt/sources.list'

# Work out if the repo exists already
apt_sources = host.fact.apt_sources
apt_sources = host.get_fact(AptSources)

is_present = False
repo = parse_apt_repo(src)
Expand Down Expand Up @@ -231,14 +235,13 @@ def deb(src, present=True, force=False, state=None, host=None):
src = temp_filename

# Check for file .deb information (if file is present)
info = host.fact.deb_package(src)
info = host.get_fact(DebPackage, name=src)
current_packages = host.get_fact(DebPackages)

exists = False

# We have deb info! Check against installed packages
if info:
current_packages = host.fact.deb_packages

if (
info['name'] in current_packages
and info.get('version') in current_packages[info['name']]
Expand All @@ -257,7 +260,7 @@ def deb(src, present=True, force=False, state=None, host=None):
yield 'dpkg --force-confdef --force-confold -i {0}'.format(src)

if info:
host.fact.deb_packages[info['name']] = [info.get('version')]
current_packages[info['name']] = [info.get('version')]
else:
host.noop('deb {0} is installed'.format(original_src))

Expand All @@ -268,7 +271,7 @@ def deb(src, present=True, force=False, state=None, host=None):
noninteractive_apt('remove', force=force),
info['name'],
)
host.fact.deb_packages.pop(info['name'])
current_packages.pop(info['name'])
else:
host.noop('deb {0} is not installed'.format(original_src))

Expand All @@ -294,11 +297,11 @@ def update(cache_time=None, touch_periodic=False, state=None, host=None):
# If cache_time check when apt was last updated, prevent updates if within time
if cache_time:
# Ubuntu provides this handy file
cache_info = host.fact.file(APT_UPDATE_FILENAME)
cache_info = host.get_fact(File, path=APT_UPDATE_FILENAME)

# Time on files is not tz-aware, and will be the same tz as the server's time,
# so we can safely remove the tzinfo from host.fact.date before comparison.
host_cache_time = host.fact.date.replace(tzinfo=None) - timedelta(seconds=cache_time)
# so we can safely remove the tzinfo from the Date fact before comparison.
host_cache_time = host.get_fact(Date).replace(tzinfo=None) - timedelta(seconds=cache_time)
if cache_info and cache_info['mtime'] and cache_info['mtime'] > host_cache_time:
host.noop('apt is already up to date')
return
Expand All @@ -311,9 +314,9 @@ def update(cache_time=None, touch_periodic=False, state=None, host=None):
if cache_time:
yield 'touch {0}'.format(APT_UPDATE_FILENAME)
if cache_info is None:
host.fact._create(
'file',
args=(APT_UPDATE_FILENAME,),
host.create_fact(
File,
kwargs={'path': APT_UPDATE_FILENAME},
data={'mtime': datetime.utcnow()},
)
else:
Expand Down Expand Up @@ -390,10 +393,10 @@ def packages(
latest=True,
)
# Note: host.fact.os_version is the same as `uname -r` (ex: '4.15.0-72-generic')
# Note: host.get_fact(OsVersion) is the same as `uname -r` (ex: '4.15.0-72-generic')
apt.packages(
name='Install kernel headers',
packages=['linux-headers-{}'.format(host.fact.os_version)],
packages=['linux-headers-{}'.format(host.get_fact(OsVersion))],
update=True,
)
'''
Expand Down Expand Up @@ -425,7 +428,7 @@ def packages(

# Compare/ensure packages are present/not
yield ensure_packages(
host, packages, host.fact.deb_packages, present,
host, packages, host.get_fact(DebPackages), present,
install_command=noninteractive_apt(install_command, force=force),
uninstall_command=noninteractive_apt(uninstall_command, force=force),
upgrade_command=noninteractive_apt(upgrade_command, force=force),
Expand Down
16 changes: 11 additions & 5 deletions pyinfra/operations/brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
'''

from pyinfra.api import operation
from pyinfra.facts.brew import new_cask_cli
from pyinfra.facts.brew import (
BrewCasks,
BrewPackages,
BrewTaps,
BrewVersion,
new_cask_cli,
)

from .util.packaging import ensure_packages

Expand Down Expand Up @@ -74,7 +80,7 @@ def packages(
yield _upgrade(state=state, host=host)

yield ensure_packages(
host, packages, host.fact.brew_packages, present,
host, packages, host.get_fact(BrewPackages), present,
install_command='brew install',
uninstall_command='brew uninstall',
upgrade_command='brew upgrade',
Expand All @@ -84,7 +90,7 @@ def packages(


def cask_args(host):
return ('', ' --cask') if new_cask_cli(host.fact.brew_version) else ('cask ', '')
return ('', ' --cask') if new_cask_cli(host.get_fact(BrewVersion)) else ('cask ', '')


@operation(
Expand Down Expand Up @@ -137,7 +143,7 @@ def casks(
args = cask_args(host)

yield ensure_packages(
host, casks, host.fact.brew_casks, present,
host, casks, host.get_fact(BrewCasks), present,
install_command='brew %sinstall%s' % args,
uninstall_command='brew %suninstall%s' % args,
upgrade_command='brew %supgrade%s' % args,
Expand Down Expand Up @@ -173,7 +179,7 @@ def tap(src, present=True, state=None, host=None):
'''

taps = host.fact.brew_taps
taps = host.get_fact(BrewTaps)
is_tapped = src in taps

if present:
Expand Down
3 changes: 2 additions & 1 deletion pyinfra/operations/choco.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''

from pyinfra.api import operation
from pyinfra.facts.choco import ChocoPackages

from .util.packaging import ensure_packages

Expand Down Expand Up @@ -32,7 +33,7 @@ def packages(packages=None, present=True, latest=False, state=None, host=None):
'''

yield ensure_packages(
host, packages, host.fact.choco_packages, present,
host, packages, host.get_fact(ChocoPackages), present,
install_command='choco install -y',
uninstall_command='choco uninstall -y -x',
upgrade_command='choco update -y',
Expand Down
14 changes: 9 additions & 5 deletions pyinfra/operations/dnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.facts.rpm import RpmPackageProvides, RpmPackages

from . import files
from .util.packaging import ensure_packages, ensure_rpm, ensure_yum_repo
Expand All @@ -24,7 +25,7 @@ def key(src, state=None, host=None):
.. code:: python
linux_id = host.fact.linux_distribution['release_meta'].get('ID')
linux_id = host.get_fact(LinuxDistribution)['release_meta'].get('ID')
dnf.key(
name='Add the Docker CentOS gpg key',
src='https://download.docker.com/linux/{}/gpg'.format(linux_id),
Expand Down Expand Up @@ -101,10 +102,10 @@ def rpm(src, present=True, state=None, host=None):
.. code:: python
major_centos_version = host.get_fact(LinuxDistribution)['major']
dnf.rpm(
name='Install EPEL rpm to enable EPEL repo',
src='https://dl.fedoraproject.org/pub/epel/epel-release-latest-'
'{{ host.fact.linux_distribution.major }}.noarch.rpm',
src='https://dl.fedoraproject.org/pub/epel/epel-release-latest-{}.noarch.rpm'.format(major_centos_version),
)
'''

Expand Down Expand Up @@ -183,12 +184,15 @@ def packages(
uninstall_command.append(extra_uninstall_args)

yield ensure_packages(
host, packages, host.fact.rpm_packages, present,
host, packages, host.get_fact(RpmPackages), present,
install_command=' '.join(install_command),
uninstall_command=' '.join(uninstall_command),
upgrade_command='dnf update -y',
version_join='=',
lower=False, # dnf packages are case sensitive
latest=latest,
expand_package_fact=host.fact.rpm_package_provides,
expand_package_fact=lambda package: host.get_fact(
RpmPackageProvides,
name=package,
),
)
3 changes: 2 additions & 1 deletion pyinfra/operations/gem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''

from pyinfra.api import operation
from pyinfra.facts.gem import GemPackages

from .util.packaging import ensure_packages

Expand Down Expand Up @@ -31,7 +32,7 @@ def packages(packages=None, present=True, latest=False, state=None, host=None):
'''

yield ensure_packages(
host, packages, host.fact.gem_packages, present,
host, packages, host.get_fact(GemPackages), present,
install_command='gem install',
uninstall_command='gem uninstall',
upgrade_command='gem update',
Expand Down
3 changes: 2 additions & 1 deletion pyinfra/operations/npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.facts.npm import NpmPackages

from .util.packaging import ensure_packages

Expand All @@ -23,7 +24,7 @@ def packages(packages=None, present=True, latest=False, directory=None, state=No
Package versions can be pinned like npm: ``<pkg>@<version>``.
'''

current_packages = host.fact.npm_packages(directory)
current_packages = host.get_fact(NpmPackages, directory=directory)

install_command = (
'npm install -g'
Expand Down
8 changes: 6 additions & 2 deletions pyinfra/operations/pacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''

from pyinfra.api import operation
from pyinfra.facts.pacman import PacmanPackages, PacmanUnpackGroup

from .util.packaging import ensure_packages

Expand Down Expand Up @@ -64,9 +65,12 @@ def packages(
yield _upgrade(state=state, host=host)

yield ensure_packages(
host, packages, host.fact.pacman_packages, present,
host, packages, host.get_fact(PacmanPackages), present,
install_command='pacman --noconfirm -S',
uninstall_command='pacman --noconfirm -R',
lower=False,
expand_package_fact=host.fact.pacman_unpack_group,
expand_package_fact=lambda package: host.get_fact(
PacmanUnpackGroup,
name=package,
),
)
4 changes: 2 additions & 2 deletions pyinfra/operations/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pyinfra.api import operation
from pyinfra.facts.files import File
from pyinfra.facts.pip import PipPackages

from . import files
from .util.packaging import ensure_packages
Expand Down Expand Up @@ -44,7 +45,6 @@ def virtualenv(
if present is False:
if host.get_fact(File, path=activate_script_path):
yield files.directory(path, present=False, state=state, host=host)
# host.fact._delete('file', args=(activate_script_path,))
else:
host.noop('virtualenv {0} does not exist'.format(path))

Expand Down Expand Up @@ -182,7 +182,7 @@ def packages(

# Handle passed in packages
if packages:
current_packages = host.fact.pip_packages(pip)
current_packages = host.get_fact(PipPackages, pip=pip)

yield ensure_packages(
host, packages, current_packages, present,
Expand Down
15 changes: 9 additions & 6 deletions pyinfra/operations/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.facts.files import File
from pyinfra.facts.pkg import PkgPackages
from pyinfra.facts.server import Arch, Os, OsVersion, Which

from .util.packaging import ensure_packages

Expand Down Expand Up @@ -39,25 +42,25 @@ def packages(packages=None, present=True, pkg_path=None, state=None, host=None):
'''

if present is True:
if not pkg_path and not host.fact.file('/etc/installurl'):
host_os = host.fact.os or ''
if not pkg_path and not host.get_fact(File, path='/etc/installurl'):
host_os = host.get_fact(Os) or ''
pkg_path = 'http://ftp.{http}.org/pub/{os}/{version}/packages/{arch}/'.format(
http=host_os.lower(),
os=host_os,
version=host.fact.os_version,
arch=host.fact.arch,
version=host.get_fact(OsVersion),
arch=host.get_fact(Arch),
)

# FreeBSD used "pkg ..." and OpenBSD uses "pkg_[add|delete]"
is_pkg = host.fact.which('pkg')
is_pkg = host.get_fact(Which, command='pkg')
install_command = 'pkg install -y' if is_pkg else 'pkg_add'
uninstall_command = 'pkg delete -y' if is_pkg else 'pkg_delete'

if pkg_path:
install_command = 'PKG_PATH={0} {1}'.format(pkg_path, install_command)

yield ensure_packages(
host, packages, host.fact.pkg_packages, present,
host, packages, host.get_fact(PkgPackages), present,
install_command=install_command,
uninstall_command=uninstall_command,
)
Loading

0 comments on commit 7547bfc

Please sign in to comment.