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

Docker version example #6

Open
wants to merge 6 commits into
base: main
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
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 the host data 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
10 changes: 10 additions & 0 deletions example/@vagrant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": {
"centos7": {
"docker_version": "23.0.0*"
},
"almalinux8": {
"docker_version": "23.0.0*"
}
}
}
16 changes: 6 additions & 10 deletions example/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ Vagrant.configure('2') do |config|
# Disable /vagrant synced folder
config.vm.synced_folder '.', '/vagrant', disabled: true

config.vm.define :ubuntu16 do |ubuntu|
ubuntu.vm.box = 'bento/ubuntu-16.04'
end

config.vm.define :ubuntu18 do |ubuntu|
ubuntu.vm.box = 'bento/ubuntu-18.04'
end

config.vm.define :ubuntu20 do |ubuntu|
ubuntu.vm.box = 'bento/ubuntu-20.04'
end

config.vm.define :debian9 do |debian|
debian.vm.box = 'bento/debian-9.11'
config.vm.define :ubuntu22 do |ubuntu|
ubuntu.vm.box = 'bento/ubuntu-22.04'
end

config.vm.define :debian10 do |debian|
debian.vm.box = 'bento/debian-10'
end

config.vm.define :debian11 do |debian|
debian.vm.box = 'bento/debian-11'
end

config.vm.define :centos7 do |centos|
centos.vm.box = 'bento/centos-7'
end
Expand Down
1 change: 1 addition & 0 deletions example/group_data/@vagrant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker_version = "5:23.0.0*" # Only valid for APT distros, see @vagrant.json fot YUM distros
34 changes: 28 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(operator):
docker_packages = [
"docker-ce",
"docker-ce-cli",
"docker-ce-rootless-extras",
]
if not host.data.docker_version:
return docker_packages

return [f"{pkg}{operator}{host.data.docker_version}" for pkg in docker_packages]



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 @@ -75,10 +94,13 @@ def deploy_docker(config=None):
"""

if host.get_fact(DebPackages):
_apt_install()
packages = get_pkgs_to_install('=')
_apt_install(packages)
elif host.get_fact(RpmPackages):
packages = get_pkgs_to_install('-')
_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