1#!/bin/bash
  2
  3# Name of project settings file to read overall project settings from
  4PROJECT_ENV=project.env
  5
  6# Name of the per-environment settings file inside env/<environment name> to read environment
  7# settings from
  8LOCAL_ENV=local.env
  9
 10# Name of the simulated host /etc/passwd file to pass to the container
 11# so it inherits our permissions
 12# docker compose as of v2.17.2 requires the initial dot or containers will
 13# fail to start
 14CHART_HOST_PASSWD=.host.passwd
 15
 16# Name of the simulated host /etc/passwd file to pass to the container
 17# so it inherits our permissions
 18# docker compose as of v2.17.2 requires the initial dot or containers will
 19# fail to start
 20CHART_HOST_GROUP=.host.group
 21
 22# Config file we create to hold our user and group ID for container spawn
 23HOST_ENV=.host.env
 24
 25# Combine all the separate environment files into a single file for compose to read
 26COMBINED_ENV=.env
 27
 28# Implement --help option
 29if [[ "${1}" == "--help" ]]; then
 30    echo "usage: docker-start [--reload] [--terminal] [--services] [--run] [SERVICES] [PARAMS]"
 31    exit
 32fi
 33
 34# Change to original directory if docker-start was a symlink
 35HERE="$(dirname $(readlink -f ${BASH_SOURCE[0]}))"
 36echo "Changed to ${HERE}"
 37cd ${HERE}
 38THIS_DIR=$(basename ${HERE})
 39
 40# Detect the environment in use by looking for a subdirectory of docker/env/*
 41# which matches the end of the directory name this script is in
 42if [[ -v "${CHART_ENV}" ]]; then
 43    echo "Using environment ${CHART_ENV} from environment"
 44else
 45    ENV=""
 46    echo "Seeking environment configuration in env/ subdirectory to match ${THIS_DIR}/ (set \$CHART_ENV to override)"
 47    # Look for the longest directory name in env/ which matches the end of our current directory name
 48    for TEST_ENV in $(for i in `ls env`; do LEN=`expr length $i`; echo $LEN $i; done | sort -rn | cut -d' ' -f 2); do
 49        echo "this dir ${THIS_DIR} testing ${TEST_ENV}"
 50        if [[ "${THIS_DIR}" =~ ${TEST_ENV}$ ]]; then
 51            echo "  Detected match"
 52            CHART_ENV=${TEST_ENV}
 53            break
 54        fi
 55    done
 56fi
 57
 58# Make sure we got an environment name and that it exists
 59if [[ "${CHART_ENV}" == "" ]]; then
 60    echo "Environment not detected, exiting"
 61    exit -1
 62fi
 63echo "Using environment ${CHART_ENV}"
 64if [[ ! -d "env/${CHART_ENV}" ]]; then
 65    echo "Config directory env/${CHART_ENV} not found, exiting"
 66    exit 01
 67fi
 68
 69# Source it so we can use CHART_PROFILE
 70source "env/${CHART_ENV}/${LOCAL_ENV}"
 71if [[ -z "${CHART_PROFILE}" ]]; then
 72    echo "CHART_PROFILE not set in ${LOCAL_ENV}, exiting"
 73fi
 74echo profile is ${CHART_PROFILE}
 75
 76# Set profile flags
 77PROFILE_FLAGS="--profile ${CHART_PROFILE}"
 78# echo "Using profile flags ${PROFILE_FLAGS}"
 79
 80# Create synthetic passwd and group files for the container
 81getent passwd $(id -un) > ${CHART_HOST_PASSWD}
 82getent group  $(id -gn) > ${CHART_HOST_GROUP}
 83# echo "Created ~/.chart.passwd and ~/.chart.group"
 84
 85# Set identifiers for compose.yaml to use
 86cat <<EOF > ${HOST_ENV}
 87CHART_HOST_PASSWD=${CHART_HOST_PASSWD}
 88CHART_HOST_GROUP=${CHART_HOST_GROUP}
 89CHART_USER_ID=$(id -u)
 90CHART_GROUP_ID=$(id -g)
 91EOF
 92
 93# Set project and environment flags
 94cat ${PROJECT_ENV} env/${CHART_ENV}/${LOCAL_ENV} ${HOST_ENV} > $COMBINED_ENV
 95
 96# Source project environment variables to set CHART_PROJECT_NAME for the passswords test
 97source ${PROJECT_ENV}
 98
 99<<<hidden due to potential security issue>>>
100<<<hidden due to potential security issue>>>
101<<<hidden due to potential security issue>>>
102<<<hidden due to potential security issue>>>
103    exit -1
104fi
105
106# Implement --reload option (dangerous because it reboots all container on the server,
107# not just ones for this project)
108if [[ "${1}" == "--reload" ]]; then
109    echo "Reloading all containers"
110    docker compose ${PROFILE_FLAGS} down
111    docker compose ${PROFILE_FLAGS} up --detach
112    exit
113fi
114
115# Implement --run option to run a service in the foreground
116if [[ "${1}" == "--run" ]]; then
117    echo "Run service in the foreground"
118    shift
119    echo "exec docker compose run --rm $@"
120    exec docker compose run --rm $@
121fi
122
123# Option to hide color codes in case it fails on some terminals
124DISPLAY_FLAGS=""
125if [[ "${NO_ANSI}" == "1" ]]; then
126    DISPLAY_FLAGS="--ansi never"
127fi
128
129# Start everything in the profile, unless user overrides with additional command line flags
130echo "Run services in background"
131echo "docker compose ${PROFILE_FLAGS} up --detach $@"
132docker compose ${PROFILE_FLAGS} ${DISPLAY_FLAGS} up --detach $@