1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-22 07:10:09 +01:00

Complete Docker Container redo, making updates possible.

- Through symlinks.

Refactored the set env variables function.
This commit is contained in:
Daan 2024-10-23 16:41:03 +02:00
parent 1cc321ddff
commit c837ab8693
3 changed files with 65 additions and 33 deletions

View File

@ -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. # 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. # 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 COPY ./src /setup/app/src
# Set the volume to be used for WireGuard configuration persistency. # Set the volume to be used for WireGuard configuration persistency.

View File

@ -1,6 +1,6 @@
services: services:
wireguard-dashboard: wireguard-dashboard:
image: test:latest image: donaldzou/wgdashboard:latest
restart: unless-stopped restart: unless-stopped
container_name: wgdashboard container_name: wgdashboard
environment: environment:
@ -12,12 +12,11 @@ services:
ports: ports:
- 10086:10086/tcp - 10086:10086/tcp
- 51820:51820/udp - 51820:51820/udp
volumes: # Can be customized to only the /opt/wireguarddashboard/src/db folder with the /opt/wireguarddashboard/src/wg-dashboard.ini file. volumes:
- ./app:/opt/wireguarddashboard - conf:/etc/wireguard
- ./conf:/etc/wireguard - data:/data
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
volumes: volumes:
app:
conf: conf:
data:

View File

@ -10,13 +10,24 @@ ensure_installation() {
if [ -z "$(ls -A "${WGDASH}")" ]; then if [ -z "$(ls -A "${WGDASH}")" ]; then
echo "Detected empty directory, moving over..." echo "Detected empty directory, moving over..."
mv /setup/app/* "${WGDASH}" # Moving over source files. (This does not include src/db and src/wg-dashboard.ini folder and file.)
#mv /setup/app/.* "${WGDASH}" 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 python3 -m venv "${WGDASH}"/src/venv
. "${WGDASH}/src/venv/bin/activate" . "${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/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 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 ENV VARS ===
set_envvars() { set_envvars() {
#printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n" printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n"
# Changing the DNS used for clients and the dashboard itself. # Path to the configuration file (exists because of previous function).
if [ "${global_dns}" != "$(grep "peer_global_dns = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then config_file="/opt/wireguarddashboard/src/wg-dashboard.ini"
echo "Changing default dns."
# 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 else
echo "DNS is set correctly." echo "Config file is not empty"
fi
# Setting the public IP of the WireGuard Dashboard container host. If not defined, it will trying fetching it using a curl to ifconfig.me. cat /opt/wireguarddashboard/src/wg-dashboard.ini
if [ "${public_ip}" = "0.0.0.0" ]; then # Check and update the DNS if it has changed
default_ip=$(curl -s ifconfig.me) current_dns=$(grep "peer_global_dns = " "$config_file" | awk '{print $NF}')
echo "Trying to fetch the Public-IP using ifconfig.me: ${default_ip}" 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 # Determine the public IP and update if necessary
elif [ "${public_ip}" != "$(grep "remote_endpoint = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then if [ "${public_ip}" = "0.0.0.0" ]; then
echo "Setting the Public-IP using given variable: ${public_ip}" 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 fi
} }
# === CORE SERVICES === # === CORE SERVICES ===
start_core() { start_core() {
printf "\n---------------------- STARTING CORE -----------------------\n" printf "\n---------------------- STARTING CORE -----------------------\n"
@ -116,7 +150,7 @@ start_core() {
echo "Activating Python venv and executing the WireGuard Dashboard service." echo "Activating Python venv and executing the WireGuard Dashboard service."
. "${WGDASH}"/src/venv/bin/activate . "${WGDASH}"/src/venv/bin/activate
cd "${WGDASH}"/src || return 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. # Isolated peers feature, first converting the existing configuration files and the given names to arrays.
local configurations=(/etc/wireguard/*) local configurations=(/etc/wireguard/*)
@ -199,12 +233,8 @@ start_core() {
ensure_blocking() { ensure_blocking() {
#printf "\n-------------- ENSURING CONTAINER CONTINUATION -------------\n" #printf "\n-------------- ENSURING CONTAINER CONTINUATION -------------\n"
. "${WGDASH}"/src/venv/bin/activate
cd "${WGDASH}"/src || return
bash wgd.sh restart
sleep 1s 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. # 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 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 # Execute functions for the WireGuard Dashboard services, then set the environment variables
ensure_installation ensure_installation
set_envvars
clean_up clean_up
start_core start_core
set_envvars
ensure_blocking ensure_blocking