1
0
Fork 0

Merge branch 'master' of github.com:byjg/docker-easy-haproxy

This commit is contained in:
Joao Magalhaes 2022-08-20 18:33:36 +00:00
commit 9b7e2b6c1d
11 changed files with 112 additions and 10 deletions

View file

@ -62,6 +62,23 @@ jobs:
with:
images: ${{ secrets.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}
- uses: actions/github-script@v6
id: tags
with:
script: |
tags = "${{ join(steps.meta.outputs.tags, ',') }}"
result = []
tags.split(",").forEach(function (item) {
short_tag = item.trim().split(":")[1];
if (short_tag != "latest") {
result.push(short_tag);
}
})
return result.join(",");
result-encoding: string
- name: Get result
run: echo "${{steps.set-result.outputs.result}}"
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
@ -69,6 +86,8 @@ jobs:
with:
context: .
file: Dockerfile
build-args: |
RELEASE_VERSION_ARG="${{ join(steps.tags.outputs.result, ',') }}"
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' || github.event.inputs.push == 'true' }}
tags: ${{ steps.meta.outputs.tags }}

View file

@ -1,5 +1,9 @@
FROM alpine:3.16
ARG RELEASE_VERSION_ARG
ENV RELEASE_VERSION=$RELEASE_VERSION_ARG
WORKDIR /scripts
COPY requirements.txt /scripts

View file

@ -1,6 +1,8 @@
VERSION := $(shell git rev-parse --short HEAD)
.PHONY: build
build:
docker build -t byjg/easy-haproxy -t byjg/easy-haproxy:local .
docker build -t byjg/easy-haproxy --build-arg RELEASE_VERSION_ARG="$(VERSION)" -t byjg/easy-haproxy:local .
.PHONY: test
test:

View file

@ -85,6 +85,12 @@ docker run --network easyhaproxy byjg/easyhaproxy
docker run --network easyhaproxy myimage
```
or, if the container is already created you can join it using the command:
```
docker network connect easyhaproxy mycontainer
```
### EASYHAPROXY_DISCOVER: swarm
This method requires a functional Docker Swarm Cluster. The system will search for the labels in all containers on all

View file

@ -0,0 +1,5 @@
_
___ __ _ ____ _ ___| |_ __ _ _ __ _ _ _____ ___ _
/ -_) _` (_-< || |___| ' \/ _` | '_ \ '_/ _ \ \ / || |
\___\__,_/__/\_, | |_||_\__,_| .__/_| \___/_\_\\_, |
|__/ |_| |__/

View file

@ -1,8 +1,14 @@
#!/usr/bin/env bash
source /scripts/functions.sh
if [ ! -f /scripts/letsencrypt_hosts.txt ]; then
exit 0
fi
# Semaphore
if [ -f /tmp/certbot-lock ]; then
echo "[CERTBOT_JOB] $(date +"$EASYHAPROXY_DATEFORMAT") Another process is running"
log "notice" "CERTBOT_JOB" "Another process is running"
exit 0
fi
@ -26,7 +32,7 @@ for domain in $(cat /scripts/letsencrypt_hosts.txt); do
done
if [ -n "$REQUEST_CERTS" ]; then
echo "[CERTBOT_JOB] $(date +"$EASYHAPROXY_DATEFORMAT") Requesting certificates for $REQUEST_CERTS"
log "info" "CERTBOT_JOB" "Requesting certificates for $REQUEST_CERTS"
certbot certonly \
--standalone \
--preferred-challenges http \
@ -41,7 +47,7 @@ if [ -n "$REQUEST_CERTS" ]; then
fi
if [ -n "$RENEW_CERTS" ]; then
echo "[CERTBOT_JOB] $(date +"$EASYHAPROXY_DATEFORMAT") Resquesting renew certificated fort $RENEW_CERTS"
log "info" "CERTBOT_JOB" "Resquesting renew certificated fort $RENEW_CERTS"
certbot renew --post-hook "/scripts/certbot_to_haproxy.sh"
fi

View file

@ -1,5 +1,7 @@
#!/bin/bash
source /scripts/functions.sh
# Loop through all Let's Encrypt certificates
for CERTIFICATE in `find /etc/letsencrypt/live/* -type d`; do
CERTIFICATE=`basename $CERTIFICATE`

View file

@ -0,0 +1,9 @@
#!/bin/bash
function log() {
# ARGS:
# - loglevel
# - app
# - message
echo [$2] $(date +"$EASYHAPROXY_DATEFORMAT") [$1]: $3
}

View file

@ -1,9 +1,16 @@
#!/usr/bin/env bash
source /scripts/functions.sh
cd /scripts
RELOAD="true"
if [[ "static|docker|swarm" != *"$EASYHAPROXY_DISCOVER"* ]];then
log "error" "CONF_CHECK" "EASYHAPROXY_DISCOVER should be 'static', 'docker', or 'swarm'. I got '$EASYHAPROXY_DISCOVER' instead."
exit 1
fi
if [[ "$EASYHAPROXY_DISCOVER" == "static" ]]; then
CONTROL_FILE="/etc/haproxy/haproxy.cfg"
touch ${CONTROL_FILE}
@ -35,7 +42,7 @@ else
RELOAD="false"
else
python3 swarm.py > /etc/haproxy/haproxy.cfg
echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") New configuration found"
log "info" "CONF_CHECK" "New configuration found"
fi
fi
@ -44,23 +51,23 @@ if cmp -s ${CONTROL_FILE} ${CONTROL_FILE}.old ; then
fi
if [[ ! -z "$1" ]]; then
echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") Initial configuration. Skip certbot."
log "info" "CONF_CHECK" "Initial configuration. Skip certbot."
else
/scripts/certbot.sh
fi
# If Certbot reloads successfully will create the file /tmp/force-reload
if [ -f /tmp/force-reload ]; then
echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") New certificates found..."
log "info" "CONF_CHECK" "New certificates found..."
RELOAD="true"
rm /tmp/force-reload
fi
if [[ ! -z "$1" ]]; then
echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") Starting haproxy..."
log "info" "CONF_CHECK" "Starting haproxy..."
/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg $(ls /etc/haproxy/conf.d/*.cfg 2>/dev/null | xargs -I{} echo -f {}) -p /run/haproxy.pid -S /var/run/haproxy.sock &
elif [[ "$RELOAD" == "true" ]]; then
echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") Reloading..."
log "info" "CONF_CHECK" "Reloading..."
/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg $(ls /etc/haproxy/conf.d/*.cfg 2>/dev/null | xargs -I{} echo -f {}) -p /run/haproxy.pid -x /var/run/haproxy.sock -sf $(cat /run/haproxy.pid) &
fi

View file

@ -1,5 +1,7 @@
#!/usr/bin/env bash
source /scripts/functions.sh
/usr/sbin/haproxy -v
if [ -z "$EASYHAPROXY_DATEFORMAT" ]; then
@ -10,10 +12,18 @@ if [ -z "$EASYHAPROXY_REFRESH_CONF" ]; then
export EASYHAPROXY_REFRESH_CONF=10
fi
cat banner.txt
echo Release: $RELEASE_VERSION
echo
echo "Environment"
env | sort | grep 'HAPROXY' | xargs -I{} echo " - {} "
echo
/scripts/haproxy-reload.sh initial
while true; do
sleep $EASYHAPROXY_REFRESH_CONF
echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") - Heartbeat."
log "info" "CONF_CHECK" "Heartbeat."
/scripts/haproxy-reload.sh
done

32
docker-compose.yml Normal file
View file

@ -0,0 +1,32 @@
version: "3"
services:
easyhaproxy:
image: byjg/easy-haproxy:4.1.0
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- certs_letsencrypt:/certs/letsencrypt
- certs_haproxy:/certs/haproxy
environment:
EASYHAPROXY_DISCOVER: docker
EASYHAPROXY_LABEL_PREFIX: easyhaproxy
EASYHAPROXY_LETSENCRYPT_EMAIL: changeme@example.org
EASYHAPROXY_SSL_MODE: "default"
HAPROXY_CUSTOMERRORS: "true"
HAPROXY_USERNAME: admin
HAPROXY_PASSWORD: password
HAPROXY_STATS_PORT: 1936
ports:
- "80:80/tcp"
- "443:443/tcp"
- "1936:1936/tcp"
volumes:
certs_letsencrypt:
certs_haproxy:
networks:
easyhaproxy:
driver: bridge