diff --git a/nginx/Makefile b/nginx/Makefile new file mode 100644 index 0000000..6406454 --- /dev/null +++ b/nginx/Makefile @@ -0,0 +1,63 @@ +.PHONY: all install uninstall pre-requisites clean dryrun + +PROJECT_NAME := $(shell basename "$${PWD}") +QUADLET_FILES = $(wildcard *.container *.volume *.network *.pod *.build) +SYSTEMD_FILES = $(wildcard *.service *.target *.timer) +SYSTEMD_UNIT_NAMES := $(wildcard *.service *.target *.timer) +SYSTEMD_MAIN_UNIT_NAMES := $(wildcard *.target) +QUADLET_UNIT_NAMES := $(patsubst %.container, %.service, $(wildcard *.container)) \ + $(patsubst %.volume, %-volume.service, $(wildcard *.volume)) \ + $(patsubst %.network, %-network.service, $(wildcard *.network)) \ + $(patsubst %.pod, %-pod.service, $(wildcard *.pod)) \ + $(patsubst %.build, %-build.service, $(wildcard *.build)) +CONFIG_FILES = $(wildcard config/*) +TARGET_QUADLET_FILES = $(addprefix /etc/containers/systemd/, $(QUADLET_FILES)) +TARGET_SYSTEMD_FILES = $(addprefix /etc/systemd/system/, $(SYSTEMD_FILES)) +TARGET_CONFIG_FILES = $(patsubst config/%, /etc/quadlets/$(PROJECT_NAME)/%, $(CONFIG_FILES)) + +pre-requisites: + @test "$$(id -u)" -eq 0 || (echo "This Makefile must be run as root" >&2; exit 1) + +all: install + +dryrun: + QUADLET_UNIT_DIRS="$$PWD" /usr/lib/systemd/system-generators/podman-system-generator -dryrun > /dev/null + +/etc/containers/systemd/%.container: %.container + install -D -m 0644 -o root -g root $< $@ + +/etc/containers/systemd/%.volume: %.volume + install -D -m 0644 -o root -g root $< $@ + +/etc/containers/systemd/%.network: %.network + install -D -m 0644 -o root -g root $< $@ + +/etc/containers/systemd/%.pod: %.pod + install -D -m 0644 -o root -g root $< $@ + +/etc/containers/systemd/%.build: %.build + install -D -m 0644 -o root -g root $< $@ + +/etc/systemd/system/%.service: %.service + install -D -m 0644 -o root -g root $< $@ + +/etc/systemd/system/%.target: %.target + install -D -m 0644 -o root -g root $< $@ + +/etc/quadlets/$(PROJECT_NAME)/%: config/% + install -D -m 0644 -o root -g root $< $@ + +install: pre-requisites dryrun $(TARGET_QUADLET_FILES) $(TARGET_SYSTEMD_FILES) $(TARGET_CONFIG_FILES) + systemctl daemon-reload + systemd-analyze --generators=true verify $(QUADLET_UNIT_NAMES) $(SYSTEMD_UNIT_NAMES) + systemctl enable $(SYSTEMD_UNIT_NAMES) + systemctl start $(SYSTEMD_MAIN_UNIT_NAMES) + +uninstall: pre-requisites + systemctl --no-block disable $(SYSTEMD_UNIT_NAMES) + systemctl --no-block stop $(SYSTEMD_UNIT_NAMES) $(QUADLET_UNIT_NAMES) + rm -f $(TARGET_QUADLET_FILES) $(TARGET_SYSTEMD_FILES) $(TARGET_CONFIG_FILES) + systemctl daemon-reload + +clean: + rm -rf /var/lib/quadlets/$(PROJECT_NAME)/ /etc/quadlets/$(PROJECT_NAME)/ diff --git a/nginx/config/config.env b/nginx/config/config.env new file mode 100644 index 0000000..29e3f0e --- /dev/null +++ b/nginx/config/config.env @@ -0,0 +1,4 @@ +GIT_REPO=https://github.com/nmasse-itix/podman-quadlet-cookbook.git +GIT_BRANCH=main +NGINX_PORT=80 +NGINX_HOST=localhost \ No newline at end of file diff --git a/nginx/nginx-init.container b/nginx/nginx-init.container new file mode 100644 index 0000000..a211cfb --- /dev/null +++ b/nginx/nginx-init.container @@ -0,0 +1,44 @@ +[Unit] +Description=Initialize Nginx root directory from a Git repository +Documentation=https://hub.docker.com/r/alpine/git +After=network-online.target +Before=nginx-server.service + +# Only start if the service has NOT been configured +ConditionPathExists=/etc/quadlets/nginx/config.env +ConditionPathExists=!/var/lib/quadlets/nginx/.git + +# Start/stop this unit when the target is started/stopped +PartOf=nginx.target + +[Container] +ContainerName=nginx-init-job +Image=docker.io/alpine/git:latest + +# Network configuration +Network=host + +# Those environment variables will be injected by podman into the container +EnvironmentFile=/etc/quadlets/nginx/config.env + +# Clone the website repository +Exec=clone -b ${GIT_BRANCH} ${GIT_REPO} /var/git + +# Volume mounts +Volume=/var/lib/quadlets/nginx:/var/git:z + +[Service] +Restart=no +TimeoutStartSec=30 + +# These environment variables are sourced to be used by systemd in the Exec* commands +EnvironmentFile=/etc/quadlets/nginx/config.env + +# Skaffold filesystem + fix permissions +ExecStartPre=install -m 0755 -o root -g root -d /var/lib/quadlets/nginx + +# This container is a job - run once to completion +Type=oneshot + +[Install] +WantedBy=nginx.target diff --git a/nginx/nginx-server.container b/nginx/nginx-server.container new file mode 100644 index 0000000..a5fe1eb --- /dev/null +++ b/nginx/nginx-server.container @@ -0,0 +1,45 @@ +[Unit] +Description=Nginx HTTP Server +Documentation=https://hub.docker.com/_/nginx +After=network.target nginx-init.service +Requires=nginx-init.service +Before=nginx.target + +# Only start if Nginx has been configured +ConditionPathExists=/etc/quadlets/nginx/config.env +ConditionPathExists=/var/lib/quadlets/nginx/.git + +# Start/stop this unit when the target is started/stopped +PartOf=nginx.target + +[Container] +ContainerName=nginx-server +Image=docker.io/library/nginx:mainline-alpine + +# Network configuration +Network=host + +# Those environment variables will be injected by podman into the container +EnvironmentFile=/etc/quadlets/nginx/config.env + +# Volume mounts +Volume=/var/lib/quadlets/nginx/nginx/website:/usr/share/nginx/html:z + +# Health check +HealthCmd=curl -sSf -o /dev/null http://localhost/ +HealthInterval=30s +HealthTimeout=10s +HealthStartPeriod=10s +HealthRetries=3 + +[Service] +Restart=always +RestartSec=10 +TimeoutStartSec=120 +TimeoutStopSec=30 + +# These environment variables are sourced to be used by systemd in the Exec* commands +EnvironmentFile=/etc/quadlets/nginx/config.env + +[Install] +WantedBy=nginx.target diff --git a/nginx/nginx.target b/nginx/nginx.target new file mode 100644 index 0000000..7338457 --- /dev/null +++ b/nginx/nginx.target @@ -0,0 +1,12 @@ +[Unit] +Description=Nginx Service Target +Documentation=man:systemd.target(5) +Wants=nginx-server.service nginx-init.service +After=nginx-server.service nginx-init.service +# Allow isolation - can stop/start this target independently +AllowIsolate=yes +# Only start if Nginx has been configured +ConditionPathExists=/etc/quadlets/nginx/config.env + +[Install] +WantedBy=multi-user.target