#!/bin/sh set -e echoerr() { printf "%s\n" "$*" >&2; } SEMAPHORE_CONFIG_PATH="${SEMAPHORE_CONFIG_PATH:-/etc/semaphore}" SEMAPHORE_TMP_PATH="${SEMAPHORE_TMP_PATH:-/tmp/semaphore}" # Semaphore database env config SEMAPHORE_DB_HOST="${SEMAPHORE_DB_HOST:-0.0.0.0}" SEMAPHORE_DB_PORT="${SEMAPHORE_DB_PORT:-3306}" SEMAPHORE_DB="${SEMAPHORE_DB:-semaphore}" SEMAPHORE_DB_USER="${SEMAPHORE_DB_USER:-semaphore}" SEMAPHORE_DB_PASS="${SEMAPHORE_DB_PASS:-semaphore}" # Email alert env config SEMAPHORE_WEB_ROOT="${SEMAPHORE_WEB_ROOT:-}" # Semaphore Admin env config SEMAPHORE_ADMIN="${SEMAPHORE_ADMIN:-admin}" SEMAPHORE_ADMIN_EMAIL="${SEMAPHORE_ADMIN_EMAIL:-admin@localhost}" SEMAPHORE_ADMIN_NAME="${SEMAPHORE_ADMIN_NAME:-Semaphore Admin}" SEMAPHORE_ADMIN_PASSWORD="${SEMAPHORE_ADMIN_PASSWORD:-semaphorepassword}" #Semaphore LDAP env config SEMAPHORE_LDAP_ACTIVATED="${SEMAPHORE_LDAP_ACTIVATED:-no}" SEMAPHORE_LDAP_HOST="${SEMAPHORE_LDAP_HOST:-}" SEMAPHORE_LDAP_PORT="${SEMAPHORE_LDAP_PORT:-}" SEMAPHORE_LDAP_NEEDTLS="${SEMAPHORE_LDAP_NEEDTLS:-no}" SEMAPHORE_LDAP_DN_BIND="${SEMAPHORE_LDAP_DN_BIND:-}" SEMAPHORE_LDAP_PASSWORD="${SEMAPHORE_LDAP_PASSWORD:-}" SEMAPHORE_LDAP_DN_SEARCH="${SEMAPHORE_LDAP_DN_SEARCH:-}" SEMAPHORE_LDAP_SEARCH_FILTER="${SEMAPHORE_LDAP_SEARCH_FILTER:-(uid=%s)}" SEMAPHORE_LDAP_MAPPING_DN="${SEMAPHORE_LDAP_MAPPING_DN:-dn}" SEMAPHORE_LDAP_MAPPING_USERNAME="${SEMAPHORE_LDAP_MAPPING_USERNAME:-uid}" SEMAPHORE_LDAP_MAPPING_FULLNAME="${SEMAPHORE_LDAP_MAPPING_FULLNAME:-cn}" SEMAPHORE_LDAP_MAPPING_EMAIL="${SEMAPHORE_LDAP_MAPPING_EMAIL:-mail}" # create semaphore temporary directory if non existent [ -d "${SEMAPHORE_TMP_PATH}" ] || mkdir -p "${SEMAPHORE_TMP_PATH}" || { echo "Can't create Semaphore tmp path ${SEMAPHORE_TMP_PATH}." exit 1 } # create semaphore config directory if non existent [ -d "${SEMAPHORE_CONFIG_PATH}" ] || mkdir -p "${SEMAPHORE_CONFIG_PATH}" || { echo "Can't create Semaphore Config path ${SEMAPHORE_CONFIG_PATH}." exit 1 } # wait on db to be up echoerr "Attempting to connect to database ${SEMAPHORE_DB} on ${SEMAPHORE_DB_HOST}:${SEMAPHORE_DB_PORT} with user ${SEMAPHORE_DB_USER} ..." TIMEOUT=30 while ! mysqladmin ping -h"$SEMAPHORE_DB_HOST" -P "$SEMAPHORE_DB_PORT" -u "$SEMAPHORE_DB_USER" --password="$SEMAPHORE_DB_PASS" --silent >/dev/null 2>&1; do TIMEOUT=$(expr $TIMEOUT - 1) if [ $TIMEOUT -eq 0 ]; then echoerr "Could not connect to database server. Exiting." exit 1 fi echo -n "." sleep 1 done # Create a config if it does not exist in the current config path if [ ! -f "${SEMAPHORE_CONFIG_PATH}/config.json" ]; then echoerr "Generating ${SEMAPHORE_TMP_PATH}/config.stdin ..." cat << EOF > "${SEMAPHORE_TMP_PATH}/config.stdin" 1 ${SEMAPHORE_DB_HOST}:${SEMAPHORE_DB_PORT} ${SEMAPHORE_DB_USER} ${SEMAPHORE_DB_PASS} ${SEMAPHORE_DB} ${SEMAPHORE_TMP_PATH} ${SEMAPHORE_WEB_ROOT} no no ${SEMAPHORE_LDAP_ACTIVATED} EOF if [ "${SEMAPHORE_LDAP_ACTIVATED}" = "yes" ]; then cat << EOF >> "${SEMAPHORE_TMP_PATH}/config.stdin" ${SEMAPHORE_LDAP_HOST}:${SEMAPHORE_LDAP_PORT} ${SEMAPHORE_LDAP_NEEDTLS} ${SEMAPHORE_LDAP_DN_BIND} ${SEMAPHORE_LDAP_PASSWORD} ${SEMAPHORE_LDAP_DN_SEARCH} ${SEMAPHORE_LDAP_SEARCH_FILTER} ${SEMAPHORE_LDAP_MAPPING_DN} ${SEMAPHORE_LDAP_MAPPING_USERNAME} ${SEMAPHORE_LDAP_MAPPING_FULLNAME} ${SEMAPHORE_LDAP_MAPPING_EMAIL} EOF fi; cat << EOF >> "${SEMAPHORE_TMP_PATH}/config.stdin" yes ${SEMAPHORE_CONFIG_PATH} ${SEMAPHORE_ADMIN} ${SEMAPHORE_ADMIN_EMAIL} ${SEMAPHORE_ADMIN_NAME} ${SEMAPHORE_ADMIN_PASSWORD} EOF cat "${SEMAPHORE_TMP_PATH}/config.stdin" $1 -setup - < "${SEMAPHORE_TMP_PATH}/config.stdin" echoerr "Run Semaphore with semaphore -config ${SEMAPHORE_CONFIG_PATH}/config.json" fi # run our command exec "$@"