Skip to content

Commit

Permalink
feat: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jomrr committed Feb 19, 2024
0 parents commit 94eaa1e
Show file tree
Hide file tree
Showing 20 changed files with 976 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Release artifacts ------------------------------------------------------------
build/
dist/
# Dependencies -----------------------------------------------------------------
collections/
roles/
requirements.txt
requirements.yml
# IDE files and directories ----------------------------------------------------
.idea/
.vscode/
# Python files and directories -------------------------------------------------
.venv/
__pycache__/
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
repos:
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.15.0
hooks:
- id: commitizen
stages: [commit-msg]
136 changes: 136 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Makefile for ansible-molecule-images
# name: ansible-molecule-images
# file: Makefile

# --- Python virtual environment -----------------------------------------------
DEPS := requirements.yml
REQS := requirements.txt
VENV := .venv
PIP := $(VENV)/bin/pip
# --- Ansible ------------------------------------------------------------------
GALAXY := $(VENV)/bin/ansible-galaxy
INVENTORY := $(VENV)/bin/ansible-inventory --list --limit
PLAYBOOK := $(VENV)/bin/ansible-playbook
# --- Makefile -----------------------------------------------------------------
LIMIT ?= fedora
# no autocomplete for dynamic targets ... :(
#distributions := $(shell $(INVENTORY) | jq -r 'keys[] | \
# select(. != "_meta" and . != "all") | @sh' | tr -d "'" | tr '\n' ' ')
# list of all supported distributions
distributions := \
almalinux \
alpine \
amazonlinux \
archlinux \
debian \
fedora \
opensuse \
oraclelinux \
ubuntu

# --- Python virtual environment and general make targets ----------------------

# default target
.PHONY: help

help:
@echo "Usage: make [target] [LIMIT=<hostname or group>]"
@echo
@echo "Targets:"
@echo " all - build all distributions"
@echo " clean - remove virtual environment"
@echo " dist-clean - remove virtual environment and build artifacts"
@echo " install - create virtual environment"
@echo " limit - build artifacts limited to LIMIT parameter"
@echo " upgrade - update requirements in virtual environment"
@echo " <distro> - build specific distribution"
@echo
@echo "Supported distributions:"
@echo " $(distributions)"

$(DEPS):
@echo "---" > $@
@echo "collections:" >> $@
@echo " - containers.podman" >> $@
@echo "" >> $@

$(REQS):
@echo "ansible >= 2.15" > $@
@echo "commitizen" >> $@
@echo "pre-commit" >> $@
@echo "python-semantic-release" >> $@

$(VENV): $(REQS) $(DEPS)
@python3 -m venv $(VENV)
@$(PIP) install --upgrade pip
@$(PIP) install -r requirements.txt
@$(GALAXY) install -r $(DEPS)
@pre-commit install --hook-type commit-msg

.PHONY: install upgrade clean dist-clean

install: $(VENV)

upgrade: $(REQS)
@$(PIP) install --upgrade pip
@$(PIP) install --upgrade -r $(REQS)
@$(GALAXY) install --force -r $(DEPS)

clean:
@rm -rf $(VENV) $(REQS)

dist-clean: clean
@rm -rf build/ dist/

# --- Ansible/Build targets ----------------------------------------------------
.PHONY: $(distributions) all limit

$(distributions): $(VENV)
@$(PLAYBOOK) playbooks/build.yml --limit=$@

all: $(distributions)

limit: $(VENV)
@$(PLAYBOOK) playbooks/build.yml --limit=$(LIMIT)

# --- git targets --------------------------------------------------------------
.PHONY: checkout-dev commit start-feature merge-feature-to-dev prepare-release

# checkout the dev branch
checkout-dev:
@git checkout dev

# commit changes to the current branch
commit:
@git add .
@git commit

# start a new feature branch
start-feature:
@git checkout -b $(FEATURE) dev

# merge a feature branch to dev
merge-feature-to-dev:
@git checkout dev
@git merge $(FEATURE)
@git branch -d $(FEATURE)

# prepare a release and merge dev to main
prepare-release:
@git push origin dev
@git checkout main
@git merge dev
@git push origin main
@git checkout dev

# bump the version number and update the changelog
version:
@git checkout main
@semantic-release version

# create a new Git tag and build the distribution files
publish:
@git checkout main
@semantic-release publish
@git push origin main --tags
@git checkout dev
166 changes: 166 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# ansible-molecule-images

![GitHub](https://img.shields.io/github/license/jam82/ansible-molecule-images) ![GitHub last commit](https://img.shields.io/github/last-commit/jam82/ansible-molecule-images) ![GitHub issues](https://img.shields.io/github/issues-raw/jam82/ansible-molecule-images)

This repository contains a Makefile, Ansible inventory and playbook for building
Docker images running an init system for use in Ansible Molecule tests.

> This is the successor of the buildah scripts from [buildah-molecule-images](https://github.com/jam82/buildah-molecule-images).
## TL;DR

```bash
sudo dnf -y install git make python3 python3-pip python3-virtualenv

mkdir -p ~/src/ansible && cd ~/src/ansible

git clone https://github.com/jam82/ansible-molecule-images

cd ansible-molecule-images

pip install --user --upgrade pip
pip install --user keyring

# set docker registry user and secret, add to ~/.bashrc or ~/.bashrc.d/env
keyring set docker user
keyring set docker secret

# nvim ~/.bashrc.d/env
export DOCKER_USER=$(keyring get docker user)
export DOCKER_SECRET=$(keyring get docker secret)

# optionally edit containers.yml and add more registries and credentials under key push_registries.
# nvim containers.yml
make install

make <all|almalinux|alpine|amazonlinux|archlinux|debian|fedora|opensuse|oralcelinux|ubuntu>

# to upgrade the virtualenv packages and ansible-galaxy dependencies run:
make upgrade
```

## Description

The playbook [`playbooks/build.yml`](playbooks/build.yml) utilizes the `ansible.builtin.template` module to render Dockerfiles to the directory `build/{{ inventory_hostname }}/Dockerfile` and then uses the `containers.podman,podman_image` module to build and push the images to the configured registries from the inventory file [`containers.yml`](containers.yml). In this file the registries and its credentials are listed as `list of dictionaries` under the key `push_registries`. The name of the built image is configured in `build_image` as host_var, the tags in `build_tags`, also as host_vars.

## Inventory variables in [`containers.yml`](containers.yml)

Any of these variables can be set at all, group or host level, where host level has the highest precedence.

| variable | scope | type | default | description |
| -------- | ----- | ---- | ------- | ----------- |
| ansible_connection | all | str | local | defines ansible_connection=local for all inventory hosts |
| build_image | group | str | None | name/repo of the built image, the namespace is used from `push_registries.item.username` |
| build_maintainer | all | str | Jonas Mauer <<jam@kabelmail.net>> | build maintainer used in `MAINTAINER` and `ENV maintainer=` statement in generated Dockerfiles |
| build_tags | host | list=str | None | tags for the built images, the first listed tag will be used in FROM statement in Dockerfile, so e.g. `[39, latest]` will use tag `39` to build the image and push it tagged as `39` and `latest` |
| container_image | group | str | None | name of the image to use in FROM statement in generated Dockerfile |
| container_registry | all | str | docker.io | the container registry from where an image is pulled, used in FROM statement in generated Dockerfile |
| push_registries | all | list=dict | [ { name: docker.io, username: "{{ lookup('ansible.builtin.env', 'DOCKER_USER') }}", password: "{{ lookup('ansible.builtin.env', 'DOCKER_USER') }}" } ] | list of registriy definitions as dictionaries to where the built images are pushed |

## Getting Started

This provides an overview of the prerequisites and ansible dependencies, as well as some usage examples.

### Prerequisites

The following prerequisites must be installed before using this playbook.

#### System packages (Fedora)

> The following packages need to be installed manually, adopt to your distribution as package names may vary:
- `git`
- `make`
- `python3` >= 3.6
- `python3-pip`
- `python3-virtualenv`

#### Python (requirements.txt)

The python prerquisites are installed in a virtualenv `.venv` via the Makefile with `make install`.

- ansible >= 2.15

To install it manually for your user without virtualenv run:

```bash
pip install --user --upgrade pip
pip install --user --upgrade ansible >= 2.15
```

For development the following are also installed by `make install`:

- commitizen
- pre-commit
- python-semantic-release

### Dependencies (requirements.yml)

The `containers.podman` collection will be installed in the virtualenv during `make install`.

To install it manually for your regular user without virtualenv run:

```bash
ansible-galaxy collection install containers.podman
```

As a requirements.yml file

```yaml
---
# name: ansible-molecule-images
# file: requirements.yml

collections:
- containers.podman

roles: []

```
it can be installed with:
```bash
ansible-galaxy install -r requiremens.yml
```

### Usage / Examples

#### Build and push images for `Fedora` and `Debian`

All Fedora images are created in parallel, after completion the Debian images are created.

```bash
make fedora debian
```

#### Build and push images for `Fedora` and `Debian` in parallel

This will make `make` run 2 jobs simultaniously, but mind the resource consumption:

```bash
make -j 2 fedora debian
```

This basically translates to the two commands:

```bash
ansible-playbook playbooks/build.yml --limit=fedora &
ansible-playbook playbooks/build.yml --limit=debian &
```

The inventory `containers.yml` is configured as static yaml inventory in `ansible.cfg` and therefore implicitly used by `ansible-playbook`.

## License

This content is published under the [MIT License](LICENSE).

## Author(s)

This content was created in 2024 by Jonas Mauer (@jam82).

Thanks to [@fgoebel](https://github.com/fgoebel) for his contributions to [buildah-molecule-images](https://github.com/jam82/buildah-molecule-images). They are included here. Looking forward to collaborating again.

## References

- [Ansible Docs - containers.podman.podman_image module](https://docs.ansible.com/ansible/latest/collections/containers/podman/podman_image_module.html)
10 changes: 10 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[defaults]
inventory = containers.yml
playbook_dir = ./playbooks
remote_tmp = /tmp/ansible

forks = 8
pipelining = False

[inventory]
enable_plugins = yaml
Loading

0 comments on commit 94eaa1e

Please sign in to comment.