From c837ab8693a5a4196c8be07771b038970af41935 Mon Sep 17 00:00:00 2001 From: Daan Date: Wed, 23 Oct 2024 16:41:03 +0200 Subject: [PATCH] Complete Docker Container redo, making updates possible. - Through symlinks. Refactored the set env variables function. --- Dockerfile | 5 ++- docker/compose.yaml | 11 +++--- entrypoint.sh | 82 +++++++++++++++++++++++++++++++-------------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/Dockerfile b/Dockerfile index e74f075..09c38b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,10 @@ ENV WGDASH=/opt/wireguarddashboard # Removing the Linux Image package to preserve space on the image, for this reason also deleting apt lists, to be able to install packages: run apt update. # Doing WireGuard Dashboard installation measures. Modify the git clone command to get the preferred version, with a specific branch for example. -RUN mkdir -p /setup/conf && mkdir /setup/app && mkdir ${WGDASH} +RUN mkdir -p /setup/conf \ + && mkdir /setup/app \ + && mkdir ${WGDASH} \ + && mkdir /data COPY ./src /setup/app/src # Set the volume to be used for WireGuard configuration persistency. diff --git a/docker/compose.yaml b/docker/compose.yaml index 68e7efc..b0f5617 100644 --- a/docker/compose.yaml +++ b/docker/compose.yaml @@ -1,6 +1,6 @@ services: wireguard-dashboard: - image: test:latest + image: donaldzou/wgdashboard:latest restart: unless-stopped container_name: wgdashboard environment: @@ -12,12 +12,11 @@ services: ports: - 10086:10086/tcp - 51820:51820/udp - volumes: # Can be customized to only the /opt/wireguarddashboard/src/db folder with the /opt/wireguarddashboard/src/wg-dashboard.ini file. - - ./app:/opt/wireguarddashboard - - ./conf:/etc/wireguard + volumes: + - conf:/etc/wireguard + - data:/data cap_add: - NET_ADMIN - volumes: - app: conf: + data: diff --git a/entrypoint.sh b/entrypoint.sh index bf5e20c..6fe0368 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -10,13 +10,24 @@ ensure_installation() { if [ -z "$(ls -A "${WGDASH}")" ]; then echo "Detected empty directory, moving over..." - mv /setup/app/* "${WGDASH}" - #mv /setup/app/.* "${WGDASH}" + # Moving over source files. (This does not include src/db and src/wg-dashboard.ini folder and file.) + mv -v /setup/app/* "${WGDASH}" + + if [ ! -d "/data/db" ]; then + echo "Creating database dir" + mkdir /data/db + ln -s /data/db ${WGDASH}/src/db + fi + + if [ ! -f "/data/wg-dashboard.ini" ]; then + echo "Creating wg-dashboard.ini file" + touch /data/wg-dashboard.ini + ln -s /data/wg-dashboard.ini ${WGDASH}/src/wg-dashboard.ini + fi python3 -m venv "${WGDASH}"/src/venv . "${WGDASH}/src/venv/bin/activate" - # Extra step for Alpine mv /usr/lib/python3.12/site-packages/psutil* "${WGDASH}"/src/venv/lib/python3.12/site-packages mv /usr/lib/python3.12/site-packages/bcrypt* "${WGDASH}"/src/venv/lib/python3.12/site-packages @@ -84,31 +95,54 @@ clean_up() { # === SET ENV VARS === set_envvars() { - #printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n" + printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n" - # Changing the DNS used for clients and the dashboard itself. - if [ "${global_dns}" != "$(grep "peer_global_dns = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then - echo "Changing default dns." + # Path to the configuration file (exists because of previous function). + config_file="/opt/wireguarddashboard/src/wg-dashboard.ini" + + # Check if the file is empty + if [ ! -s "$config_file" ]; then + echo "Config file is empty. Creating [Peers] section." + + # Create [Peers] section with initial values + { + echo "[Peers]" + echo "remote_endpoint = ${public_ip}" + echo "peer_global_dns = ${global_dns}" + } > "$config_file" - #sed -i "s/^DNS = .*/DNS = ${global_dns}/" /etc/wireguard/wg0.conf # Uncomment if you want to have DNS on server-level. - sed -i "s/^peer_global_dns = .*/peer_global_dns = ${global_dns}/" /opt/wireguarddashboard/src/wg-dashboard.ini else - echo "DNS is set correctly." - fi + echo "Config file is not empty" - # Setting the public IP of the WireGuard Dashboard container host. If not defined, it will trying fetching it using a curl to ifconfig.me. - if [ "${public_ip}" = "0.0.0.0" ]; then - default_ip=$(curl -s ifconfig.me) - echo "Trying to fetch the Public-IP using ifconfig.me: ${default_ip}" + cat /opt/wireguarddashboard/src/wg-dashboard.ini + # Check and update the DNS if it has changed + current_dns=$(grep "peer_global_dns = " "$config_file" | awk '{print $NF}') + if [ "${global_dns}" != "$current_dns" ]; then + echo "Changing default DNS." + sed -i "s/^peer_global_dns = .*/peer_global_dns = ${global_dns}/" "$config_file" + else + echo "DNS is set correctly." + fi - sed -i "s/^remote_endpoint = .*/remote_endpoint = ${default_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini - elif [ "${public_ip}" != "$(grep "remote_endpoint = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then - echo "Setting the Public-IP using given variable: ${public_ip}" + # Determine the public IP and update if necessary + if [ "${public_ip}" = "0.0.0.0" ]; then + default_ip=$(curl -s ifconfig.me) + echo "Trying to fetch the Public-IP using ifconfig.me: ${default_ip}" + sed -i "s/^remote_endpoint = .*/remote_endpoint = ${default_ip}/" "$config_file" + else + current_ip=$(grep "remote_endpoint = " "$config_file" | awk '{print $NF}') + if [ "${public_ip}" != "$current_ip" ]; then + echo "Setting the Public-IP using given variable: ${public_ip}" + sed -i "s/^remote_endpoint = .*/remote_endpoint = ${public_ip}/" "$config_file" + fi + + fi - sed -i "s/^remote_endpoint = .*/remote_endpoint = ${public_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini fi } + + # === CORE SERVICES === start_core() { printf "\n---------------------- STARTING CORE -----------------------\n" @@ -116,7 +150,7 @@ start_core() { echo "Activating Python venv and executing the WireGuard Dashboard service." . "${WGDASH}"/src/venv/bin/activate cd "${WGDASH}"/src || return - bash wgd.sh start &>> /dev/null + bash wgd.sh start # Isolated peers feature, first converting the existing configuration files and the given names to arrays. local configurations=(/etc/wireguard/*) @@ -199,12 +233,8 @@ start_core() { ensure_blocking() { #printf "\n-------------- ENSURING CONTAINER CONTINUATION -------------\n" - . "${WGDASH}"/src/venv/bin/activate - cd "${WGDASH}"/src || return - bash wgd.sh restart - sleep 1s - echo "Ensuring container continuation." + echo -e "\nEnsuring container continuation." # This function checks if the latest error log is created and tails it for docker logs uses. if find "/opt/wireguarddashboard/src/log" -mindepth 1 -maxdepth 1 -type f | read -r; then @@ -220,7 +250,7 @@ ensure_blocking() { # Execute functions for the WireGuard Dashboard services, then set the environment variables ensure_installation +set_envvars clean_up start_core -set_envvars ensure_blocking \ No newline at end of file