Skip to content

Commit

Permalink
Modify testing workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
medanthelinium committed May 21, 2024
1 parent 2baa99b commit 8826c9f
Showing 11 changed files with 177 additions and 25 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -5,6 +5,10 @@ on:
push:
workflow_dispatch:

env:
ANSIBLE_ROLES_PATH: roles
ANSIBLE_HASH_BEHAVIOUR: merge

jobs:
lint:
runs-on: ubuntu-latest
@@ -18,7 +22,7 @@ jobs:
python-version: '3.x'

- name: Install dependencies.
run: pip install netaddr yamllint ansible-lint ansible
run: pip install -r requirements.txt

- name: Run ansible-lint
run: "ansible-lint"
@@ -37,10 +41,7 @@ jobs:
- name: Install dependencies.
run: |
python -m pip install --upgrade pip
pip install netaddr ansible docker molecule molecule-plugins
- name: Install Galaxy dependencies.
run: ansible-galaxy collection install community.docker
pip install -r requirements.txt
- name: Run molecule
run: "molecule test"
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,27 +15,57 @@ Create a template in the role that manages your docker container with the follow
```

Configure the container parameters using Ansible variables
```
```yaml
example_container:
container_name: example
image: example:latest
container_stop_timeout: 55
volumes:
"/etc/config.cfg": { host_dir: "/var/example/config.cfg", relabel: unshared, read_only: yes }
ports:
8080: 80
environment:
KEY: "value"
entrypoint:
/etc/entrypoint
command:
echo "hello world"
container_name: example
image: example:latest
container_stop_timeout: 55
volumes:
"/etc/config.cfg": { host_dir: "/var/example/config.cfg", relabel: unshared, read_only: yes }
ports:
8080: 80
environment:
KEY: "value"
entrypoint:
/etc/entrypoint
command:
echo "hello world"
```
Finally, provision the service file:
```
```yaml
- name: Install systemd service for example_container
template:
src: example_container.service
dest: "/etc/systemd/system/example_container.service"
```
### Custom networks
This role can be used to create custom Docker networks in the following format:
```yaml
docker:
networks:
- name: example_network
subnet: 10.22.11.0/24
- name: example_network_2
subnet: 172.156.11.0/24
```
The networks will then be created automatically on system boot using SystemD services.
A container can then be connected to a network as follows:
```yaml
example_container:
network:
name: example_network
ip: 10.22.11.21
```
If the appropriate network exists, its SystemD service will be added as a dependency to the container's service.
Alternatively, if you'd like to omit the IP address (for example, with `host` network), the following structure can be used:
```yaml
example_container:
network: example_network
```
1 change: 1 addition & 0 deletions defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
docker:
networks: []
repository:
apt: "deb [arch={{ 'arm64' if ansible_architecture == 'aarch64' else 'amd64' }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
key: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
6 changes: 6 additions & 0 deletions meta/.requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
collections:
- name: https://github.com/ansible-collections/community.general
type: git
- name: https://github.com/ansible-collections/community.docker
type: git
3 changes: 2 additions & 1 deletion molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
dependency:
name: galaxy
name: shell
command: ansible-galaxy install -r meta/.requirements.yml -p roles/ --force
driver:
name: docker
platforms:
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ansible
docker
molecule
molecule-plugins
ansible-lint
yamllint
4 changes: 4 additions & 0 deletions tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -10,3 +10,7 @@

- name: Configure DNS for docker
ansible.builtin.include_tasks: dns.yaml

- name: Configure docker networks
when: docker.networks
ansible.builtin.include_tasks: networks.yaml
31 changes: 31 additions & 0 deletions tasks/networks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Create docker networks
loop: "{{ docker.networks }}"
register: docker_network_template_config_result
ansible.builtin.template:
src: systemd/docker-network.service
dest: "/etc/systemd/system/docker-network@{{ item.name }}"
owner: root
mode: "0644"

- name: Template the docket-network script
ansible.builtin.file:
path: "/usr/local/bin/docker-network.sh"
src: "docker-network.sh"
mode: "0755"
owner: root

- name: Activate the docker network services
loop: "{{ docker.networks }}"
ansible.builtin.service:
name: "docker-network@{{ item.name }}.service"
state: started
daemon_reload: yes

- name: Reload docker-network@{{ item.name }}.service

Check failure on line 25 in tasks/networks.yaml

GitHub Actions / lint

name[template]

Jinja templates should only be at the end of 'name'

Check warning on line 25 in tasks/networks.yaml

GitHub Actions / lint

no-handler

Tasks that run when changed should likely be handlers.
loop: "{{ docker.networks }}"
when: docker_network_template_config_result.changed
ansible.builtin.service:
name: docker-network@{{ item.name }}.service
daemon_reload: yes
state: reloaded
41 changes: 41 additions & 0 deletions templates/docker-network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /usr/bin/env bash
action=$1
network_name=$2
subnet=$3
driver=$4

tempfolder="/tmp/docker-networks"
mkdir -p $tempfolder

case $action in
start)
docker network create --driver=$driver --subnet=$subnet $network_name
containers_in_network="$tempfolder/containers_in_network_$network_name"
if [ -f "$containers_in_network" ]; then
for i in `cat $containers_in_network`; do
echo $i
docker network connect $network_name $i;
done;
fi;
rm -f $containers_in_network
;;
reload)
containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $network_name`
for i in $containers_in_network; do
docker network disconnect -f $network_name $i;
done;
docker network rm $network_name
docker network create --driver=$driver --subnet=$subnet $network_name
for i in $containers_in_network; do
docker network connect $network_name $i;
done;
;;
stop)
containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $network_name`
echo $containers_in_network > "$tempfolder/containers_in_network_$network_name"
for i in $containers_in_network; do
docker network disconnect -f $network_name $i;
done;
docker network rm $network_name
;;
esac
28 changes: 24 additions & 4 deletions templates/systemd/container.service
Original file line number Diff line number Diff line change
@@ -50,12 +50,20 @@ ExecStartPre=docker create --name={{ container.container_name }} --rm \
{%- for variable, value in (container.environment.items()|rejectattr('1', 'eq', none) if container.environment|default(none) else []) %}
--env={{ variable|quote }}={{ value|quote }} \
{%- endfor %}
{%- if container.network|default(none) %}
--network={{ container.network|quote }} \

{%- if container.network is mapping %}
{%- if container.network.name|default(none) %}
--network={{ container.network.name|quote }} \
{%- endif %}
{%- if container.network.ip|default(none) %}
--ip={{ container.network.ip|quote }} \
{%- endif %}
{%- if (container.ip|default(none)) and (container.network|default(none)) %}
--ip={{ container.ip|quote }} \
{%- else %}
{%- if container.network | default(none) %}
--network={{ container.network | quote }} \
{%- endif %}
{%- endif %}

{%- if container.entrypoint|default(none) %}
--entrypoint={{ container.entrypoint|quote }} \
{%- endif %}
@@ -76,6 +84,18 @@ WantedBy=docker.service
{% macro All(container) -%}
{{ Unit(container) }}

{%- if container.network | default(none) %}
{%- if container.network is mapping and (container.network.name | default(none)) %}
Requires={{ container.network.name }}
{%- else %}
{%- if container.network is not mapping and container.network != host %}
Requires={{ container.network }}
{% -endif %}


{% endif %}


{{ Service(container) }}

{{ Install(container) }}
11 changes: 11 additions & 0 deletions templates/systemd/docker-network.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Requires=docker.service
After=docker.service

[Service]
Restart=always
RestartSec=10
TimeoutStopSec=60
ExecStart=/usr/local/bin/docker-network.sh start {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }}
ExecReload=/usr/local/bin/docker-network.sh reload {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }}
ExecStop=/usr/local/bin/docker-network.sh stop {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }}

0 comments on commit 8826c9f

Please sign in to comment.