Add dependency check and improve error handling.

This commit is contained in:
redxef 2021-12-22 17:28:00 +01:00
parent fce34cdff0
commit eaaab4281e

View file

@ -2,16 +2,38 @@
INSTALLATION_NAME='{INSTALLATION_NAME}'
TAG='{TAG}'
DEPENDENCIES='sed awk shuf dirname docker'
conf_file="/etc/$INSTALLATION_NAME.conf"
source "/etc/$INSTALLATION_NAME.conf"
for cmd in $DEPENDENCIES; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "$INSTALLATION_NAME: no such command '$cmd'" >&2
exit 1
fi
done
## Read the target configuration file name
# Can mention multiple files in one line seperated by space.
# Setting the variable multiple times is the same as
# when all values were set on one line delimited by a space.
# The final configuration file is drawn randomly from the
# resulting set of file.
ovpn_configuration_file="$(sed -n '/^[^#]/ s/OVPN_CONFIGURATION_FILE[^=]*=//p' "$conf_file" | tr ' ' '\n')"
ovpn_configuration_file="$(echo "$ovpn_configuration_file" | shuf -n1)"
ovpn_configuration_file="$(echo "$ovpn_configuration_file" | awk '{if (substr($0, 1, 1) != "/") {print "/etc/openvpn/client/"$0} else {print $0}}')"
ovpn_configuration_file_parent_dir="$(dirname "$ovpn_configuration_file")"
echo "ovpn_configuration_file=$ovpn_configuration_file"
if ! stat "$ovpn_configuration_file"; then
exit 1
fi
docker run -it --cap-add NET_ADMIN \
-v /etc/passwd:/etc/passwd \
-v /etc/openvpn/client:/etc/openvpn/client \
-v "$ovpn_configuration_file_parent_dir:$ovpn_configuration_file_parent_dir" \
-v "$HOME:$HOME" \
-e OVPN_USER="$USER" \
-e OVPN_HOME="$HOME" \
-e OVPN_CONFIGURATION_FILE="/etc/openvpn/client/$OVPN_CONFIGURATION_FILE" \
-e OVPN_CONFIGURATION_FILE="$ovpn_configuration_file" \
"$TAG" sh