From f4d26084a2d20ac16430968f6bebb58b2d3339e8 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Fri, 2 Dec 2022 16:47:52 +0100 Subject: [PATCH] Change configuration creation by adding init command --- README.md | 13 ++++++++++--- scripts/create-config.sh | 4 +++- scripts/entrypoint.sh | 21 +++++++++++++++------ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 82474c1..5a69878 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ If no environment variables are set, config creation script will ask you for set Run the service to create required keys and config files in directory `config.d`. It will print out used configuration params. ``` -$ docker-compose run wg +$ docker-compose run wg init Starting wireguard_wg_1 ... done Attaching to wireguard_wg_1 @@ -48,13 +48,14 @@ wg_1 | - Generating 5 client configs and client QR codes wireguard_wg_1 exited with code 0 ``` -### Run the service +### Start the service Start the service in detached mode. ``` $ docker-compose up -d ``` +If creation of config files was skipped, configuration files will be created on first start. ### List server and client configs @@ -97,7 +98,13 @@ Stop the service and run the following command to remove existing config files. ``` $ docker-compose run wg purge ``` -Run command `docker-compose up` again to generate new config files. +Reinitialize configureation + +``` +$ docker-compose run wg init +``` + +Restart service. ## Client configurations diff --git a/scripts/create-config.sh b/scripts/create-config.sh index bd20c1c..1535f8b 100755 --- a/scripts/create-config.sh +++ b/scripts/create-config.sh @@ -4,11 +4,13 @@ rm *.conf 2>/dev/null rm *-client_*.png 2>/dev/null +echo "Create configuration files" + while [[ -z $DEVICE ]]; do echo -n "Device (eg tun0): " read DEVICE done -echo " - Writing config to file $DEVICE.conf" +echo " - Writing server config to file $DEVICE.conf" while [[ -z $SERVER_HOST ]]; do echo -n "Endpoint hostname: " diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index dc81c9d..15d99d7 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -6,12 +6,6 @@ if [ -z $DEVICE ]; then DEVICE="tun0" fi -if [ ! -f "/etc/wireguard/$DEVICE.conf" ]; then - cd /etc/wireguard - /scripts/create-config.sh - exit 0 -fi - case "$1" in 'ls-configs' | 'ls') cd /etc/wireguard @@ -41,6 +35,16 @@ case "$1" in /scripts/show-client.sh $2 exit 0 ;; + 'init') + if [ ! -f "/etc/wireguard/$DEVICE.conf" ]; then + cd /etc/wireguard + /scripts/create-config.sh + exit 0 + else + echo "Existing config found: Run command 'purge' first." + exit 1 + fi + ;; 'purge') cd /etc/wireguard rm *.conf *.png @@ -55,11 +59,16 @@ case "$1" in echo "add Add new client" echo "rm Remove client by ID" echo "show Show client config with qrcode" + echo "init Initialize service by creating config files" echo "purge Remove server config and all client configs" echo "help Show this help message" echo ;; *) + if [ ! -f "/etc/wireguard/$DEVICE.conf" ]; then + cd /etc/wireguard + /scripts/create-config.sh + fi echo "Starting wg-quick on $DEVICE" touch "${WG_LOG_FILE}" wg-quick up $DEVICE