Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ A basic [pyinfra](https://pyinfra.com) deploy that installs and optionally confi
+ Debian 8/9/10
+ CentOS 7/8

This deploy installs packages in the `docker-ce` ecosystem (`docker-ce`/`docker-ce-cli`/`docker-ce-rootless-extras`) You can specify `docker_version` in your inventory data file and it will install that version for all `docker-ce` packages.

## Usage

See [the example](https://github.com/Fizzadar/pyinfra-docker/tree/master/example) for a more complete example.
Expand Down
33 changes: 27 additions & 6 deletions pyinfra_docker/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@
from pyinfra.operations import apt, dnf, files, yum


def _apt_install():
DEFAULTS = {
"docker_version": None,
}


def get_pkgs_to_install():
docker_packages = [
"docker-ce",
"docker-ce-cli",
"docker-ce-rootless-extras",
]
if not host.data.docker_version:
return docker_packages

return [f"{pkg}={host.data.docker_version}" for pkg in docker_packages]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.




def _apt_install(packages):
apt.packages(
name="Install apt requirements to use HTTPS",
packages=["apt-transport-https", "ca-certificates"],
Expand Down Expand Up @@ -40,12 +58,13 @@ def _apt_install():

apt.packages(
name="Install Docker via apt",
packages="docker-ce",
packages=packages,
update=add_apt_repo.changed, # update if we added the repo
allow_downgrades=True,
)


def _yum_or_dnf_install(yum_or_dnf):
def _yum_or_dnf_install(yum_or_dnf, packages):
yum_or_dnf.repo(
name="Add the Docker yum repo",
src="https://download.docker.com/linux/centos/docker-ce.repo",
Expand All @@ -60,12 +79,12 @@ def _yum_or_dnf_install(yum_or_dnf):

yum_or_dnf.packages(
name="Install Docker via yum",
packages=["docker-ce"],
packages=packages,
extra_install_args=extra_install_args,
)


@deploy("Deploy Docker")
@deploy("Deploy Docker", data_defaults=DEFAULTS)
def deploy_docker(config=None):
"""
Install Docker on the target machine.
Expand All @@ -74,11 +93,13 @@ def deploy_docker(config=None):
config: filename or dict of JSON data
"""

packages = get_pkgs_to_install()
if host.get_fact(DebPackages):
_apt_install()
_apt_install(packages)
elif host.get_fact(RpmPackages):
_yum_or_dnf_install(
dnf if host.get_fact(Which, command="dnf") else yum,
packages,
)
else:
raise DeployError(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

if __name__ == "__main__":
setup(
version="2.0",
version="2.1",
name="pyinfra-docker",
description="Install & configure Docker with `pyinfra`.",
long_description=readme,
Expand Down