services/_utils/create-service.sh

90 lines
1.9 KiB
Bash
Raw Normal View History

2023-06-13 08:59:25 +00:00
#!/usr/bin/env bash
set -e
# Get variables for docker-compose
read -rp "Enter the service name: " NAME
read -rp "Enter the service image: " IMAGE
read -rp "Enable watchtower? (y/N): " WATCHTOWER
# Get variables for env files
read -rp "Create .env file? (y/N): " CREATE_ENV
read -rp "Create .secret.env file? (y/N): " CREATE_SECRET_ENV
# Get variables for caddy
read -rp "Enter the service domain: " DOMAIN
read -rp "Enter the service port: " PORT
read -rp "Enter the target host: " HOST
mkdir -p "${NAME}"
parse_yn_bool() {
bool_lower=$(echo "${1}" | tr '[:upper:]' '[:lower:]')
# Map y/yes to true, otherwise false
if [ "${bool_lower}" == "y" ] || [ "${bool_lower}" == "yes" ]; then
return 0
else
return 1
fi
}
parse_yn_str() {
if parse_yn_bool "${1}"; then
echo "true"
else
echo "false"
fi
}
# Create the env files
YAML_ENV=""
2023-06-13 09:09:53 +00:00
if parse_yn_bool "${CREATE_ENV}" || parse_yn_bool "${CREATE_SECRET_ENV}"; then
YAML_ENV="
env_file:"
2023-06-13 08:59:25 +00:00
fi
2023-06-13 09:09:53 +00:00
if parse_yn_bool "${CREATE_ENV}"; then
2023-06-13 08:59:25 +00:00
YAML_ENV="${YAML_ENV}
- .env"
touch "${NAME}/.env"
fi
2023-06-13 09:09:53 +00:00
if parse_yn_bool "${CREATE_SECRET_ENV}"; then
2023-06-13 08:59:25 +00:00
YAML_ENV="${YAML_ENV}
- .secret.env"
touch "${NAME}/.secret.env"
fi
# Create the docker-compose file
cat <<EOF > "${NAME}/docker-compose.yml"
version: '3'
services:
app:
image: ${IMAGE}
2023-06-13 09:09:53 +00:00
restart: always${YAML_ENV}
2023-06-13 08:59:25 +00:00
labels:
com.centurylinklabs.watchtower.enable: $(parse_yn_str "${WATCHTOWER}")
networks:
apps:
aliases:
- ${NAME}
2023-06-13 08:59:25 +00:00
networks:
apps:
external: true
EOF
2023-06-13 09:09:53 +00:00
caddy_path="caddy/config/conf.${HOST}.d"
if [ ! -d "${caddy_path}" ]; then
echo "Caddy config directory for host '${HOST}' not found, trying default"
caddy_path="caddy/config/conf.d"
fi
if [ -d "${caddy_path}" ]; then
cat <<EOF > "$caddy_path/${DOMAIN}.conf"
2023-06-13 08:59:25 +00:00
${DOMAIN} {
import default
reverse_proxy ${NAME}:${PORT}
2023-06-13 08:59:25 +00:00
}
EOF
2023-06-13 09:09:53 +00:00
else
echo "Caddy config directory not found, skipping caddy config"
fi