#!/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="" if parse_yn_bool "${CREATE_ENV}" || parse_yn_bool "${CREATE_SECRET_ENV}"; then YAML_ENV=" env_file:" fi if parse_yn_bool "${CREATE_ENV}"; then YAML_ENV="${YAML_ENV} - .env" touch "${NAME}/.env" fi if parse_yn_bool "${CREATE_SECRET_ENV}"; then YAML_ENV="${YAML_ENV} - .secret.env" touch "${NAME}/.secret.env" fi # Create the docker-compose file cat < "${NAME}/docker-compose.yml" version: '3' services: app: image: ${IMAGE} restart: always${YAML_ENV} labels: com.centurylinklabs.watchtower.enable: $(parse_yn_str "${WATCHTOWER}") networks: apps: aliases: - ${NAME} networks: apps: external: true EOF 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 < "$caddy_path/${DOMAIN}.conf" ${DOMAIN} { import default reverse_proxy ${NAME}:${PORT} } EOF else echo "Caddy config directory not found, skipping caddy config" fi