2022-05-09 15:13:38 +02:00
#!/bin/bash -e
baseDir() {
# see https://stackoverflow.com/questions/59895
SOURCE=${BASH_SOURCE[0]}
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
echo $DIR
}
BASE=$(baseDir)
cd $BASE
source ./lib/functions.sh
2022-05-11 10:30:18 +02:00
ACTION=$1
export PROJECT=$2
2022-05-09 15:13:38 +02:00
if [[ -z $1 || -z $2 ]]; then
printUsage
exit 1
fi
2022-05-11 10:30:18 +02:00
case "$PROJECT" in
2022-05-09 15:13:38 +02:00
ccp)
#nothing extra to do
;;
2022-10-25 11:45:01 +02:00
bbmri)
2022-05-09 15:13:38 +02:00
#nothing extra to do
;;
2023-05-10 12:54:05 +02:00
minimal)
#nothing extra to do
;;
2022-05-09 15:13:38 +02:00
*)
printUsage
exit 1
;;
esac
2023-11-27 19:39:16 +01:00
# TODO: Please add proper documentation for variable priorities (1. secrets, 2. vars, 3. PROJECT.local.conf, 4. PROJECT.conf, 5. ???
2022-05-17 18:04:15 +02:00
loadVars() {
# Load variables from /etc/bridgehead and /srv/docker/bridgehead
set -a
source /etc/bridgehead/$PROJECT.conf || fail_and_report 1 "/etc/bridgehead/$PROJECT.conf not found"
if [ -e /etc/bridgehead/$PROJECT.local.conf ]; then
log INFO "Applying /etc/bridgehead/$PROJECT.local.conf"
source /etc/bridgehead/$PROJECT.local.conf || fail_and_report 1 "Found /etc/bridgehead/$PROJECT.local.conf but failed to import"
fi
fetchVarsFromVaultByFile /etc/bridgehead/$PROJECT.conf || fail_and_report 1 "Unable to fetchVarsFromVaultByFile"
2023-11-16 14:21:19 +01:00
setHostname
2022-05-17 18:04:15 +02:00
[ -e ./$PROJECT/vars ] && source ./$PROJECT/vars
set +a
2022-10-06 16:27:52 +02:00
2022-05-17 18:04:15 +02:00
OVERRIDE=${OVERRIDE:=""}
2023-05-10 12:54:05 +02:00
# minimal contains shared components, so potential overrides must be applied in every project
if [ -f "minimal/docker-compose.override.yml" ]; then
2023-05-19 13:53:03 +02:00
log INFO "Applying Bridgehead common components override (minimal/docker-compose.override.yml)"
2023-05-10 12:54:05 +02:00
OVERRIDE+=" -f ./minimal/docker-compose.override.yml"
fi
2022-05-17 18:04:15 +02:00
if [ -f "$PROJECT/docker-compose.override.yml" ]; then
log INFO "Applying $PROJECT/docker-compose.override.yml"
OVERRIDE+=" -f ./$PROJECT/docker-compose.override.yml"
fi
detectCompose
2023-10-05 09:43:57 +02:00
setupProxy
2024-01-08 13:03:12 +01:00
# Set some project-independent default values
: ${ENVIRONMENT:=production}
2024-01-08 15:49:46 +01:00
case "$ENVIRONMENT" in
"production")
2024-01-09 11:43:57 +01:00
export FOCUS_TAG=main
2024-01-08 15:49:46 +01:00
;;
"test")
2024-01-09 11:43:57 +01:00
export FOCUS_TAG=develop
2024-01-08 15:49:46 +01:00
;;
*)
report_error 7 "Environment \"$ENVIRONMENT\" is unknown. Assuming production. FIX THIS!"
2024-01-09 11:43:57 +01:00
export FOCUS_TAG=main
2024-01-08 15:49:46 +01:00
;;
esac
2023-11-07 15:55:26 +01:00
sync_secrets
2022-05-17 18:04:15 +02:00
}
2022-10-28 10:12:21 +02:00
2022-05-11 10:30:18 +02:00
case "$ACTION" in
2022-05-09 15:13:38 +02:00
start)
2022-05-17 18:04:15 +02:00
loadVars
2022-10-06 10:45:50 +02:00
hc_send log "Bridgehead $PROJECT startup: Checking requirements ..."
2022-05-09 15:13:38 +02:00
checkRequirements
2022-10-06 10:45:50 +02:00
hc_send log "Bridgehead $PROJECT startup: Requirements checked out. Now starting bridgehead ..."
2023-06-29 09:52:11 +02:00
exec $COMPOSE -p $PROJECT -f ./minimal/docker-compose.yml -f ./$PROJECT/docker-compose.yml $OVERRIDE up --abort-on-container-exit
2022-05-09 15:13:38 +02:00
;;
stop)
2022-05-17 18:04:15 +02:00
loadVars
2023-05-19 13:53:03 +02:00
# HACK: This is temporarily to properly shut down false bridgehead instances (bridgehead-ccp instead ccp)
2023-05-10 12:54:05 +02:00
$COMPOSE -p bridgehead-$PROJECT -f ./minimal/docker-compose.yml -f ./$PROJECT/docker-compose.yml $OVERRIDE down
2023-06-29 10:27:00 +02:00
exec $COMPOSE -p $PROJECT -f ./minimal/docker-compose.yml -f ./$PROJECT/docker-compose.yml $OVERRIDE down
2023-03-08 10:00:38 +01:00
;;
is-running)
2023-03-08 10:37:37 +01:00
bk_is_running
exit $?
2022-05-09 15:13:38 +02:00
;;
2024-02-14 15:28:16 +01:00
logs)
loadVars
shift 2
exec $COMPOSE -p $PROJECT -f ./minimal/docker-compose.yml -f ./$PROJECT/docker-compose.yml $OVERRIDE logs -f $@
;;
2022-05-09 15:13:38 +02:00
update)
2022-05-17 18:04:15 +02:00
loadVars
2022-05-11 10:30:18 +02:00
exec ./lib/update-bridgehead.sh $PROJECT
2022-05-09 15:13:38 +02:00
;;
install)
2023-02-24 16:32:17 +01:00
source ./lib/prepare-system.sh NODEV
loadVars
exec ./lib/install-bridgehead.sh $PROJECT
;;
dev-install)
exec ./lib/prepare-system.sh DEV
2022-05-17 18:04:15 +02:00
loadVars
exec ./lib/install-bridgehead.sh $PROJECT
2022-05-09 15:13:38 +02:00
;;
uninstall)
2022-05-17 18:04:15 +02:00
exec ./lib/uninstall-bridgehead.sh $PROJECT
2022-05-09 15:13:38 +02:00
;;
2023-08-15 15:42:42 +02:00
adduser)
2023-07-25 14:08:54 +02:00
loadVars
2023-08-09 09:10:20 +02:00
log "INFO" "Adding encrypted credentials in /etc/bridgehead/$PROJECT.local.conf"
2023-08-09 09:24:23 +02:00
read -p "Please choose the component (LDM_AUTH|NNGM_AUTH) you want to add a user to : " COMPONENT
read -p "Please enter a username: " USER
read -s -p "Please enter a password (will not be echoed): "$'\n' PASSWORD
add_basic_auth_user $USER $PASSWORD $COMPONENT $PROJECT
2023-07-25 14:08:54 +02:00
;;
2022-10-17 14:38:34 +02:00
enroll)
2022-05-17 18:04:15 +02:00
loadVars
2023-08-07 13:00:37 +02:00
do_enroll $PROXY_ID
2022-10-17 14:38:34 +02:00
;;
2022-10-06 10:45:50 +02:00
preRun | preUpdate)
fixPermissions
;;
postRun | postUpdate)
2022-10-05 19:58:07 +02:00
;;
2022-05-09 15:13:38 +02:00
*)
printUsage
exit 1
;;
esac
exit 0