[Scripts] Add menus to db script

This commit is contained in:
Aaro Koivukangas
2026-08-19 12:15:58 +03:00
parent e17465150b
commit 230b9fd076
2 changed files with 75 additions and 14 deletions
+71 -13
View File
@@ -35,6 +35,7 @@ PROJECT_ROOT="$(git rev-parse --show-toplevel)"
LOCAL_DUMP_DIR="$PROJECT_ROOT/.databasedumps/local_containers" LOCAL_DUMP_DIR="$PROJECT_ROOT/.databasedumps/local_containers"
S3_DUMP_DIR="$PROJECT_ROOT/.databasedumps/s3" S3_DUMP_DIR="$PROJECT_ROOT/.databasedumps/s3"
TEST_DUMP_DIR="$PROJECT_ROOT/.databasedumps/test" # Location where docker pulls .sql files TEST_DUMP_DIR="$PROJECT_ROOT/.databasedumps/test" # Location where docker pulls .sql files
DUMP_DIRS=("$LOCAL_DUMP_DIR" "$S3_DUMP_DIR" "$TEST_DUMP_DIR")
S3_CONFIG_FILE=~/.s3cfg S3_CONFIG_FILE=~/.s3cfg
S3_BUCKET="s3://oodikone-test" S3_BUCKET="s3://oodikone-test"
@@ -87,9 +88,10 @@ upload_s3_db () {
# Update registry with an existing dump located in .databasedumps/$2 # Update registry with an existing dump located in .databasedumps/$2
build_and_push_image () { build_and_push_image () {
local database="$1" local database="$1"
local location="$2"
infomsg "Building docker image for $database\n" infomsg "Building docker image for $database\n"
docker build "$PROJECT_ROOT" -t "$REGISTRY/$database" -f db.Dockerfile --build-arg DB_NAME="$database" docker build "$PROJECT_ROOT" -t "$REGISTRY/$database" -f db.Dockerfile --build-arg DB_NAME="$database" --build-arg DUMP_LOCATION="$location"
successmsg "$database docker image built" successmsg "$database docker image built"
infomsg "Pushing $database to $REGISTRY\n" infomsg "Pushing $database to $REGISTRY\n"
docker push "$REGISTRY/$database" docker push "$REGISTRY/$database"
@@ -125,35 +127,91 @@ do_for_all () {
successmsg "$1 ran successfully" successmsg "$1 ran successfully"
} }
init_menu () { ask_for_input () {
local cmd="$1"
shift # Removes $1 from "$@"
echo
init_database_menu
select database in "${options[@]}"; do
"$cmd" "$database" "$@"
break
done
}
init_action_menu () {
PS3="Please enter your choice: " PS3="Please enter your choice: "
options=( options=(
"Pull dumps from s3" "Pull dumps from s3"
"Update dumps in s3 (s3 dumps)"
"Dump from running docker images" "Dump from running docker images"
"Update dumps in s3 (local docker dumps)" "Update dumps in s3"
"Push new images to registry" "Push new images to registry"
) )
} }
init_mode_menu () {
PS3="Select command mode: "
options=(
"Run command for all databases"
"Select single database"
)
}
init_database_menu () {
PS3="Select database: "
options=("${DATABASES[@]}")
}
init_dump_location_menu () {
PS3="Select dump location: "
options=("${DUMP_DIRS[@]}")
}
# === RUN === # === RUN ===
init_menu
while true; do while true; do
init_mode_menu
select mode in "${options[@]}"; do
case "$mode" in
"Run command for all databases")
cmd="do_for_all"
break;;
"Select single database")
cmd="ask_for_input"
break;;
esac
done
echo
init_dump_location_menu
select loc in "${options[@]}"; do
location="$loc"
break
done
echo
init_action_menu
select opt in "${options[@]}"; do select opt in "${options[@]}"; do
case "$opt" in case "$opt" in
"Pull dumps from s3") "Pull dumps from s3")
do_for_all download_s3_db "$S3_DUMP_DIR";; $cmd download_s3_db "$S3_DUMP_DIR"
"Update dumps in s3 (s3 dumps)") break;;
do_for_all upload_s3_db;;
"Dump from running docker images") "Dump from running docker images")
do_for_all dump_local_db "$LOCAL_DUMP_DIR";; $cmd dump_local_db "$LOCAL_DUMP_DIR"
"Update dumps in s3 (local docker dumps)") break;;
do_for_all upload_s3_db "$LOCAL_DUMP_DIR";; "Update dumps in s3")
$cmd upload_s3_db "$location"
break;;
"Push new images to registry") "Push new images to registry")
do_for_all build_and_push_image;; $cmd build_and_push_image "$location"
break;;
esac esac
done done
echo
done done
+4 -1
View File
@@ -4,6 +4,9 @@ FROM postgres:15.10
ARG DB_NAME ARG DB_NAME
ENV DB_NAME=$DB_NAME ENV DB_NAME=$DB_NAME
ARG DUMP_LOCATION
ENV DUMP_LOCATION=$DUMP_LOCATION
ENV PGDATA=/data ENV PGDATA=/data
ENV POSTGRES_USER=postgres ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=postgres ENV POSTGRES_PASSWORD=postgres
@@ -12,7 +15,7 @@ ENV POSTGRES_DB=$DB_NAME
# RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends pgcli # RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends pgcli
COPY ./.databasedumps/test/$DB_NAME.sql /docker-entrypoint-initdb.d/ COPY $DUMP_LOCATION/$DB_NAME.sql /docker-entrypoint-initdb.d/
EXPOSE 5432 EXPOSE 5432
HEALTHCHECK --interval=30s --timeout=5s --retries=10 --start-interval=3s --start-period=15s \ HEALTHCHECK --interval=30s --timeout=5s --retries=10 --start-interval=3s --start-period=15s \