Skip to content

Commit

Permalink
Merge pull request #14 from ai-cfia/JolanThomassin/issue13
Browse files Browse the repository at this point in the history
Fixes #13, Update Server Configuration
  • Loading branch information
JolanThomassin authored Sep 13, 2023
2 parents c8ddfe8 + 8ba5038 commit 2b0f0c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
10 changes: 9 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 21 additions & 11 deletions bin/lib.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
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

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"
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
echo "One or more variables are not defined, the program cannot continue"
exit 1
fi
done
}

export PGOPTIONS="--search_path=$LOUIS_SCHEMA,public"
export PGBASE
Expand All @@ -27,16 +36,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"
33 changes: 21 additions & 12 deletions bin/postgres.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
#!/bin/bash
DIRNAME=$(dirname "$(realpath "$0")")
. "$DIRNAME"/lib.sh

DIRNAME=$(dirname `realpath $0`)
. $DIRNAME/lib.sh

PGDATA=$HOME/pgdata
if [ -z "${!PGDATA}" ]; then
PGDATA=$HOME/pgdata
fi

if [ ! -d $PGDATA ]; then
mkdir -p $PGDATA
if [ ! -d "$PGDATA" ]; then
mkdir -p "$PGDATA"
echo "PGDATA $PGDATA directory does not exist, creating it"
fi

STATUS=`docker inspect louis-db-server -f '{{.State.Status}}'`
check_environment_variables_defined DB_SERVER_CONTAINER_NAME PGPASSWORD

STATUS=$(docker inspect "$DB_SERVER_CONTAINER_NAME" -f '{{.State.Status}}')

if [ "$STATUS" = "exited" ]; then
docker start louis-db-server
echo "container $DB_SERVER_CONTAINER_NAME exist but has exited, restarting"
docker start "$DB_SERVER_CONTAINER_NAME"

elif [ "$STATUS" != "running" ]; then
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
13 changes: 6 additions & 7 deletions bin/setup-db-docker.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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"
$DOCKER_EXEC createdb -E utf-8 $PGBASE
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
$DOCKER_EXEC pgxn install vector
Expand Down

0 comments on commit 2b0f0c4

Please sign in to comment.