From d5b58e8df3138ae2c664ef11f71af7b2debd0e2d Mon Sep 17 00:00:00 2001 From: xenia Date: Fri, 11 Aug 2023 17:07:09 +0200 Subject: [PATCH] Avoid database starting when other devshell is already running, allow devshell in non-root folders --- buildScripts/setupEnv.sh | 67 ++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/buildScripts/setupEnv.sh b/buildScripts/setupEnv.sh index 1a57c1e..23642ee 100644 --- a/buildScripts/setupEnv.sh +++ b/buildScripts/setupEnv.sh @@ -1,3 +1,9 @@ +cd "$(git rev-parse --show-toplevel)" + +# Check for lock +[ -f matabas-db/dev-lock ] +IS_OWNER=$? # non-zero is owner + set -e export PGPORT=2137 @@ -8,43 +14,58 @@ log() { printf "\e[1m\e[38;5;87m==>\e[0m\e[1m %s%s\e[0m\n" "$1" "${2:-.}" } +# Only trapped if owner exitHook() { + cd "$(git rev-parse --show-toplevel)" log "Stoppar brevekorren" pg_ctl stop --pgdata=./matabas-db + rm -f matabas-db/dev-lock } -log "Letar efter existerande brevekorresmapp" -[ -d ./matabas-db ] || { - log "Finns ej, skapar brevekorresmapp (./matabas-db)" - pg_ctl initdb --pgdata=./matabas-db -o "--username=$PGUSER" +setupDb() { + log "Letar efter existerande brevekorresmapp" + [ -d ./matabas-db ] || { + log "Finns ej, skapar brevekorresmapp (./matabas-db)" + pg_ctl initdb --pgdata=./matabas-db -o "--username=$PGUSER" + } + + [ -f matabas-db/matabas.log ] && { + log "Flyttar journalen från senaste sittningen till journalarkivet (./matabas-db/matabas.log.old)" + cat matabas-db/matabas.log >> matabas-db/matabas.log.old + rm -f matabas-db/matabas.log + } + + log "Startar brevekorren" + pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p $PGPORT -k /tmp" + log "Låser databasen" + :> matabas-db/dev-lock + trap exitHook EXIT + + log "Skapar matabas" + createdb -h localhost -p "$PGPORT" matabas 2>>./matabas-db/matabas.log || log "Matabasen finns, skriver ej över" + + log "Ställer in schema.sql" + psql -h localhost -p "$PGPORT" "$PBDATABASE" < schema.sql } -[ -f matabas-db/matabas.log ] && { - log "Flyttar journalen från senaste sittningen till journalarkivet (./matabas-db/matabas.log.old)" - cat matabas-db/matabas.log >> matabas-db/matabas.log.old - rm -f matabas-db/matabas.log -} - -log "Startar brevekorren" -pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p $PGPORT -k /tmp" -trap exitHook EXIT - -log "Skapar matabas" -createdb -h localhost -p "$PGPORT" matabas 2>>./matabas-db/matabas.log || log "Matabasen finns, skriver ej över" - -log "Ställer in schema.sql" -psql -h localhost -p "$PGPORT" "$PBDATABASE" < schema.sql - P="$PWD" clean() { - log "Rensar byggnad" - rm -rf artifacts + log "Rensar byggnad" + rm -rf artifacts } build() { - source "$P/buildScripts/build.sh" + source "$P/buildScripts/build.sh" } +if [ "$IS_OWNER" -ne 0 ] ; then + setupDb +else + log "Startar som icke-ägare, startar ej databasen" +fi + log "Redo" "!" set +e + +cd - >/dev/null 2>&1