From 7757421042e1e503bb4a7b44c5e25f8f8916e4a8 Mon Sep 17 00:00:00 2001 From: JolanThomassin Date: Fri, 8 Sep 2023 19:23:06 +0000 Subject: [PATCH 1/5] Fixes #13, Update Server Configuration --- bin/lib.sh | 14 +++++++------- bin/postgres.sh | 3 +++ bin/setup-db-docker.sh | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bin/lib.sh b/bin/lib.sh index edab6f0..f498cad 100644 --- a/bin/lib.sh +++ b/bin/lib.sh @@ -9,13 +9,13 @@ else echo "WARNING: File $ENV_FILE does not exist, relying on environment variables" fi -REQUIRED_ENVIRONMENT_VARIABLES="LOUIS_DSN PGBASE PGUSER PGPASSWORD PGHOST OPENAI_API_KEY AZURE_OPENAI_SERVICE LOUIS_SCHEMA" -for VARIABLE in $REQUIRED_ENVIRONMENT_VARIABLES; do - if [ -z "${!VARIABLE}" ]; then - echo "Environment variable $VARIABLE is not set" - exit 1 - fi -done +# REQUIRED_ENVIRONMENT_VARIABLES="LOUIS_DSN PGBASE PGUSER PGPASSWORD PGHOST OPENAI_API_KEY AZURE_OPENAI_SERVICE LOUIS_SCHEMA" +# for VARIABLE in $REQUIRED_ENVIRONMENT_VARIABLES; do +# if [ -z "${!VARIABLE}" ]; then +# echo "Environment variable $VARIABLE is not set" +# exit 1 +# fi +# done export PGOPTIONS="--search_path=$LOUIS_SCHEMA,public" export PGBASE diff --git a/bin/postgres.sh b/bin/postgres.sh index c47c751..ff6491e 100755 --- a/bin/postgres.sh +++ b/bin/postgres.sh @@ -12,8 +12,11 @@ fi STATUS=`docker inspect louis-db-server -f '{{.State.Status}}'` if [ "$STATUS" = "exited" ]; then + echo container exist but as exited, restarting docker start louis-db-server elif [ "$STATUS" != "running" ]; then + # check PGPASWORD before using it + echo container does not exist, creating docker run --name louis-db-server \ -e POSTGRES_PASSWORD=$PGPASSWORD \ --network louis_network \ diff --git a/bin/setup-db-docker.sh b/bin/setup-db-docker.sh index 15b4d23..76c236a 100755 --- a/bin/setup-db-docker.sh +++ b/bin/setup-db-docker.sh @@ -6,7 +6,7 @@ if [ -z "$PGBASE" ]; then exit 1 fi DOCKER_EXEC="docker exec -it louis-db-server" -$DOCKER_EXEC createdb -E utf-8 $PGBASE +$DOCKER_EXEC createdb -E utf-8 -U postgres $PGBASE $DOCKER_EXEC $PSQL_ADMIN -c "CREATE USER $USER; ALTER USER $USER WITH SUPERUSER;" $DOCKER_EXEC pip install pgxnclient $DOCKER_EXEC pgxn install vector From 74d69c4b8d5bd3826842edd2c787c127e4e4ea64 Mon Sep 17 00:00:00 2001 From: JolanThomassin Date: Tue, 12 Sep 2023 18:33:27 +0000 Subject: [PATCH 2/5] Fixes #13, Parameter validation script --- bin/lib.sh | 12 ++++++++++++ bin/postgres.sh | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/bin/lib.sh b/bin/lib.sh index f498cad..334e1a9 100644 --- a/bin/lib.sh +++ b/bin/lib.sh @@ -9,6 +9,7 @@ else echo "WARNING: File $ENV_FILE does not exist, relying on environment variables" fi +# This section got replaced by the "is_parameter_empty" function # REQUIRED_ENVIRONMENT_VARIABLES="LOUIS_DSN PGBASE PGUSER PGPASSWORD PGHOST OPENAI_API_KEY AZURE_OPENAI_SERVICE LOUIS_SCHEMA" # for VARIABLE in $REQUIRED_ENVIRONMENT_VARIABLES; do # if [ -z "${!VARIABLE}" ]; then @@ -17,6 +18,17 @@ fi # fi # done +# check if given parameter is defined or empty +is_parameter_empty () { + if [ -z "$1" ]; then + # Parameter is not defined or empty. + return 0 + else + # Parameter is defined and not empty. + return 1 + fi +} + export PGOPTIONS="--search_path=$LOUIS_SCHEMA,public" export PGBASE export PGDATABASE diff --git a/bin/postgres.sh b/bin/postgres.sh index ff6491e..9674695 100755 --- a/bin/postgres.sh +++ b/bin/postgres.sh @@ -12,18 +12,38 @@ fi STATUS=`docker inspect louis-db-server -f '{{.State.Status}}'` if [ "$STATUS" = "exited" ]; then - echo container exist but as exited, restarting - docker start louis-db-server + # Check if $DB_SERVER_CONTAINER_NAME is empty + source ./lib.sh + is_parameter_empty $DB_SERVER_CONTAINER_NAME + is_parameter_empty_DB_SERVER_CONTAINER_NAME=$? + + if [ $is_parameter_empty_DB_SERVER_CONTAINER_NAME -eq 0 ]; + then + echo "DB_SERVER_CONTAINER_NAME is not defined or empty." + else + echo "container exist $DB_SERVER_CONTAINER_NAME but has exited, restarting" + docker start louis-db-server + fi + elif [ "$STATUS" != "running" ]; then - # check PGPASWORD before using it - echo container does not exist, creating - docker run --name louis-db-server \ - -e POSTGRES_PASSWORD=$PGPASSWORD \ - --network louis_network \ - --mount type=bind,src=$PGDATA,target=/var/lib/postgresql/data \ - --publish 5432:5432 \ - --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro \ - -d louis-postgres + # Check if $PGPASSWORD is empty + source ./lib.sh + is_parameter_empty $PGPASSWORD + is_parameter_empty_PGPASSWORD=$? + + if [ $is_parameter_empty_PGPASSWORD -eq 0 ]; + then + echo "PGPASSWORD is not defined or empty." + else + echo container does not exist, creating + docker run --name louis-db-server \ + -e POSTGRES_PASSWORD=$PGPASSWORD \ + --network louis_network \ + --mount type=bind,src=$PGDATA,target=/var/lib/postgresql/data \ + --publish 5432:5432 \ + --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro \ + -d louis-postgres + fi else echo "Postgres is already running" fi From 0ce58462b6e69a8eb78f5d735c27c909b02fb48f Mon Sep 17 00:00:00 2001 From: JolanThomassin Date: Tue, 12 Sep 2023 19:37:03 +0000 Subject: [PATCH 3/5] Fixes #13, Rewrite of Parameter validation script --- bin/lib.sh | 28 +++++++++++----------------- bin/postgres.sh | 42 ++++++++++++------------------------------ 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/bin/lib.sh b/bin/lib.sh index 334e1a9..930fff8 100644 --- a/bin/lib.sh +++ b/bin/lib.sh @@ -9,23 +9,17 @@ else echo "WARNING: File $ENV_FILE does not exist, relying on environment variables" fi -# This section got replaced by the "is_parameter_empty" function -# REQUIRED_ENVIRONMENT_VARIABLES="LOUIS_DSN PGBASE PGUSER PGPASSWORD PGHOST OPENAI_API_KEY AZURE_OPENAI_SERVICE LOUIS_SCHEMA" -# for VARIABLE in $REQUIRED_ENVIRONMENT_VARIABLES; do -# if [ -z "${!VARIABLE}" ]; then -# echo "Environment variable $VARIABLE is not set" -# exit 1 -# fi -# done - -# check if given parameter is defined or empty -is_parameter_empty () { - if [ -z "$1" ]; then - # Parameter is not defined or empty. - return 0 - else - # Parameter is defined and not empty. - return 1 +check_environment_variables_defined () { + variable_not_set=0 + for VARIABLE in "$@"; do + if [ -z "${!VARIABLE}" ]; then + echo "Environment variable $VARIABLE is not set" + variable_not_set=1 + fi + done + + if [ $variable_not_set -eq 1 ]; then + exit 1 fi } diff --git a/bin/postgres.sh b/bin/postgres.sh index 9674695..e82fcc1 100755 --- a/bin/postgres.sh +++ b/bin/postgres.sh @@ -11,39 +11,21 @@ fi STATUS=`docker inspect louis-db-server -f '{{.State.Status}}'` -if [ "$STATUS" = "exited" ]; then - # Check if $DB_SERVER_CONTAINER_NAME is empty - source ./lib.sh - is_parameter_empty $DB_SERVER_CONTAINER_NAME - is_parameter_empty_DB_SERVER_CONTAINER_NAME=$? +check_environment_variables_defined DB_SERVER_CONTAINER_NAME PGPASSWORD - if [ $is_parameter_empty_DB_SERVER_CONTAINER_NAME -eq 0 ]; - then - echo "DB_SERVER_CONTAINER_NAME is not defined or empty." - else - echo "container exist $DB_SERVER_CONTAINER_NAME but has exited, restarting" - docker start louis-db-server - fi +if [ "$STATUS" = "exited" ]; then + echo "container $DB_SERVER_CONTAINER_NAME exist but has exited, restarting" + docker start louis-db-server elif [ "$STATUS" != "running" ]; then - # Check if $PGPASSWORD is empty - source ./lib.sh - is_parameter_empty $PGPASSWORD - is_parameter_empty_PGPASSWORD=$? - - if [ $is_parameter_empty_PGPASSWORD -eq 0 ]; - then - echo "PGPASSWORD is not defined or empty." - else - echo container does not exist, creating - docker run --name louis-db-server \ - -e POSTGRES_PASSWORD=$PGPASSWORD \ - --network louis_network \ - --mount type=bind,src=$PGDATA,target=/var/lib/postgresql/data \ - --publish 5432:5432 \ - --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro \ - -d louis-postgres - fi + echo container does not exist, creating + docker run --name louis-db-server \ + -e POSTGRES_PASSWORD=$PGPASSWORD \ + --network louis_network \ + --mount type=bind,src=$PGDATA,target=/var/lib/postgresql/data \ + --publish 5432:5432 \ + --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro \ + -d louis-postgres else echo "Postgres is already running" fi From dc31e13163fd55ee7be8680e9c85793b496867db Mon Sep 17 00:00:00 2001 From: JolanThomassin Date: Tue, 12 Sep 2023 20:47:12 +0000 Subject: [PATCH 4/5] Fixes #13, Added extensions to devcontainer.json --- .devcontainer/devcontainer.json | 10 +++++++++- bin/lib.sh | 35 ++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index dc03e15..565a5fa 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -18,7 +18,15 @@ ], // Configure tool-specific properties. - // "customizations": {}, + "customizations": { + "vscode":{ + "extensions": [ + "timonwong.shellcheck", + "GitHub.vscode-pull-request-github", + "charliermarsh.ruff" + ] + } + }, // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root" diff --git a/bin/lib.sh b/bin/lib.sh index 930fff8..4478c7b 100644 --- a/bin/lib.sh +++ b/bin/lib.sh @@ -1,26 +1,28 @@ -DIRNAME=$(dirname `realpath $0`) +#!/bin/bash +DIRNAME=$(dirname $(realpath $0)) PARENT_DIR=$DIRNAME/.. -PROJECT_DIR=`realpath $PARENT_DIR` +PROJECT_DIR=$(realpath $PARENT_DIR) ENV_FILE=$PROJECT_DIR/.env if [ -f "$ENV_FILE" ]; then - . $ENV_FILE + # shellcheck source=lib.sh + . "$ENV_FILE" else echo "WARNING: File $ENV_FILE does not exist, relying on environment variables" fi check_environment_variables_defined () { - variable_not_set=0 - for VARIABLE in "$@"; do - if [ -z "${!VARIABLE}" ]; then - echo "Environment variable $VARIABLE is not set" - variable_not_set=1 + variable_not_set=0 + for VARIABLE in "$@"; do + if [ -z "${!VARIABLE}" ]; then + echo "Environment variable $VARIABLE is not set" + variable_not_set=1 + fi + done + + if [ $variable_not_set -eq 1 ]; then + exit 1 fi - done - - if [ $variable_not_set -eq 1 ]; then - exit 1 - fi } export PGOPTIONS="--search_path=$LOUIS_SCHEMA,public" @@ -33,16 +35,17 @@ export PGHOST export PGPASSFILE export PGPASSWORD -VERSION15=`psql --version | grep 15.` +VERSION15=$(psql --version | grep 15.) if [ -z "$VERSION15" ]; then echo "postgresql-client-15 required" exit 1 fi -PSQL_ADMIN="psql -v ON_ERROR_STOP=1 --single-transaction -d $PGBASE" -TODAY=`date +%Y-%m-%d` +TODAY=$(date +%Y-%m-%d) if [ -z "$PGDUMP_FILENAME" ]; then PGDUMP_FILENAME=$PROJECT_DIR/dumps/$TODAY.$PGBASE.pg_dump fi + +export PSQL_ADMIN="psql -v ON_ERROR_STOP=1 --single-transaction -d $PGBASE" From 8ba5038299f766eb87195449770fd159aea6e0a2 Mon Sep 17 00:00:00 2001 From: JolanThomassin Date: Wed, 13 Sep 2023 15:33:57 +0000 Subject: [PATCH 5/5] Fixes #13, Update to Environment Variables --- bin/lib.sh | 1 + bin/postgres.sh | 32 ++++++++++++++++++-------------- bin/setup-db-docker.sh | 11 +++++------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/bin/lib.sh b/bin/lib.sh index 4478c7b..a6ed502 100644 --- a/bin/lib.sh +++ b/bin/lib.sh @@ -21,6 +21,7 @@ check_environment_variables_defined () { done if [ $variable_not_set -eq 1 ]; then + echo "One or more variables are not defined, the program cannot continue" exit 1 fi } diff --git a/bin/postgres.sh b/bin/postgres.sh index e82fcc1..3baaaab 100755 --- a/bin/postgres.sh +++ b/bin/postgres.sh @@ -1,31 +1,35 @@ #!/bin/bash +DIRNAME=$(dirname "$(realpath "$0")") +. "$DIRNAME"/lib.sh -DIRNAME=$(dirname `realpath $0`) -. $DIRNAME/lib.sh - -PGDATA=$HOME/pgdata - -if [ ! -d $PGDATA ]; then - mkdir -p $PGDATA +if [ -z "${!PGDATA}" ]; then + PGDATA=$HOME/pgdata fi -STATUS=`docker inspect louis-db-server -f '{{.State.Status}}'` +if [ ! -d "$PGDATA" ]; then + mkdir -p "$PGDATA" + echo "PGDATA $PGDATA directory does not exist, creating it" +fi check_environment_variables_defined DB_SERVER_CONTAINER_NAME PGPASSWORD +STATUS=$(docker inspect "$DB_SERVER_CONTAINER_NAME" -f '{{.State.Status}}') + if [ "$STATUS" = "exited" ]; then echo "container $DB_SERVER_CONTAINER_NAME exist but has exited, restarting" - docker start louis-db-server + docker start "$DB_SERVER_CONTAINER_NAME" elif [ "$STATUS" != "running" ]; then - echo container does not exist, creating - docker run --name louis-db-server \ - -e POSTGRES_PASSWORD=$PGPASSWORD \ + + echo "container $DB_SERVER_CONTAINER_NAME does not exist, creating" + + docker run --name "$DB_SERVER_CONTAINER_NAME" \ + -e POSTGRES_PASSWORD="$PGPASSWORD" \ --network louis_network \ - --mount type=bind,src=$PGDATA,target=/var/lib/postgresql/data \ + --mount type=bind,src="$PGDATA",target=/var/lib/postgresql/data \ --publish 5432:5432 \ --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro \ - -d louis-postgres + -d "louis-postgres" else echo "Postgres is already running" fi diff --git a/bin/setup-db-docker.sh b/bin/setup-db-docker.sh index 76c236a..3281f69 100755 --- a/bin/setup-db-docker.sh +++ b/bin/setup-db-docker.sh @@ -1,11 +1,10 @@ -DIRNAME=`dirname $0` +#!/bin/bash +DIRNAME=$(dirname "$0") . $DIRNAME/lib.sh -if [ -z "$PGBASE" ]; then - echo "PGBASE is not set" - exit 1 -fi -DOCKER_EXEC="docker exec -it louis-db-server" +check_environment_variables_defined PGBASE DB_SERVER_CONTAINER_NAME PSQL_ADMIN + +DOCKER_EXEC="docker exec -it $DB_SERVER_CONTAINER_NAME" $DOCKER_EXEC createdb -E utf-8 -U postgres $PGBASE $DOCKER_EXEC $PSQL_ADMIN -c "CREATE USER $USER; ALTER USER $USER WITH SUPERUSER;" $DOCKER_EXEC pip install pgxnclient