Files
stepca-docker/setup.sh

96 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# env setup script for step-ca docker compose
# 2023-01-06 by dede & dany
# variables setup
PROJECT_NAME=step-ca
#COMPOSE_DOWNLOAD_URL=
# starting the setup
echo "$PROJECT_NAME docker compose setup"
echo "> env setup for $PROJECT_NAME with docker compose"
# check for existing compose file to not break an existing setup
if [ -n "$COMPOSE_DOWNLOAD_URL" ]; then
if [[ -f "docker-compose.yml" ]]; then
echo "> docker-compose.yml file already exists. setup will not run"
exit 1
else
echo "> downloading docker-compose.yml file from $COMPOSE_DOWNLOAD_URL"
wget -O docker-compose.yml "$COMPOSE_DOWNLOAD_URL"
fi
fi
# check for existing .env file to not break an existing setup
if [[ -f .env ]]; then
echo "> .env file already exists. setup will not run. either rename or delete it first."
echo -n "do you want to continue anyway and create a new .env file? [y/N]: "
read -r continue
if [[ $continue =~ ^([yY])$ ]]; then
mv ".env" ".env_backup"
echo "> saved existing .env file to .env_backup"
else
exit 1
fi
elif [[ ! -f env.sample ]]; then
echo "> env.sample file is missing. cannot run."
exit 2
fi
# create .env file
echo -e "# env file for $PROJECT_NAME compose project" > .env
echo -e "# generated by ${0##*/} on $(date +"%F %H:%M") by $USER\n" >> .env
# step through the list of lines and ask for each key to use default value or set value
# for each line in configfile
while read -r -u 3 line; do
# keep blank and comment lines
if [[ -z $line ]] || [[ $line == \#* ]]; then
echo "$line" >> .env
# handle variable lines
else
# split line by '=' into key and value
readarray -d "=" -t currvar<<< "$line"
# preset value with default
value=${currvar[1]::-1}
# check if there's a password to generate
if [[ ${currvar[0]} =~ (PASS|PASSWORD|PWD|PASSWD)$ ]]; then
value=$(openssl rand -base64 29 | tr -d "=+/" | cut -c1-25)
fi
# ask for user input
echo -n "please enter value for ${currvar[0]} [$value]: "
read -r userinput
# set userinput as new value if any
if [ -n "$userinput" ]; then
value=$userinput
fi
# check if there's a directory to create
if [[ ${currvar[0]} =~ (DIR|DIRECTORY)$ ]]; then
if [[ ! -d $value ]]; then
echo -n "do you want to create the directory? [Y/n]: "
read -r createdir
if [[ $createdir =~ ^([yY]|)$ ]]; then
mkdir -p "$value"
echo "directory created."
fi
else
echo "directory already exists."
if [ -n "$(ls -A "$value")" ]; then
echo "WATCH OUT! the directory is NOT empty. please ensure this is intended."
fi
fi
fi
# now append the line
echo "${currvar[0]}=$value" >> .env
fi
done 3< env.sample