Skip to content

Commit

Permalink
Add govtool stack and update configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed May 7, 2024
1 parent 0fc0f03 commit d9c5e08
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 33 deletions.
5 changes: 2 additions & 3 deletions tests/test-infrastructure/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
secrets/
configs/
docker-compose-rendered.yml
docker-compose-swarm-rendered.yml
docker-compose-services-rendered.yml
/*-rendered.yml

7 changes: 7 additions & 0 deletions tests/test-infrastructure/build-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

export BASE_IMAGE_NAME="govtool"
# build the base image
docker build -t "$BASE_IMAGE_NAME"/backend-base -f ../../govtool/backend/Dockerfile.base ../../govtool/backend
docker compose -f ./docker-compose-govtool.yml build
docker compose -f ./docker-compose.yml build
13 changes: 13 additions & 0 deletions tests/test-infrastructure/configs_template/backend_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dbsyncconfig" : {
"host" : "postgres",
"dbname" : "${DBSYNC_DATABASE}",
"user" : "postgres",
"password" : "${POSTGRES_PASSWORD}",
"port" : 5432
},
"port" : 8080,
"host" : "0.0.0.0",
"cachedurationseconds": 20,
"sentrydsn": "https://username:password@senty.host/id"
}
12 changes: 5 additions & 7 deletions tests/test-infrastructure/deploy-swarm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
## ./deploy-swarm prepare
##
set -eo pipefail
set -a
. ./.env
set +a
. ./scripts/deploy-stack.sh
load_env

if [ "$1" == "destroy" ]
then
Expand All @@ -33,15 +32,14 @@ elif [ "$1" == "prepare" ]
then
## apply the enviroment to services compose file
## and deploy the stack
envsubst < ./docker-compose-services.yml > ./docker-compose-services-rendered.yml
docker stack deploy -c './docker-compose-services-rendered.yml' ${STACK_NAME}-services
deploy-stack ${STACK_NAME}-services './docker-compose-services-rendered.yml'

elif [ "$1" == "finalize" ]
then
## apply the environment to compose file
## deploy the govtool test infrastructure stack
envsubst < ./docker-compose.yml > ./docker-compose-rendered.yml
docker stack deploy -c './docker-compose-rendered.yml' ${STACK_NAME}
deploy-stack ${STACK_NAME} './docker-compose-rendered.yml'

else
echo "Something is wrong with the command"
echo
Expand Down
51 changes: 51 additions & 0 deletions tests/test-infrastructure/docker-compose-govtool.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: "3.9"
networks:
frontend:
external: true
postgres:
external: true
configs:
config.json:
name: govtool_backend_config.json
external: true
services:
backend:
image: govtool/backend
build:
context: ../../govtool/backend
args:
BASE_IMAGE_TAG: govtool/backend-base
entrypoint:
- sh
- -c
- vva-be -c /config.json start-app
environment:
VIRTUAL_HOST: https://${BASE_DOMAIN} -> :8080
VIRTUAL_HOST_2: https://${BASE_DOMAIN}/swagger -> :8080/swagger
VIRTUAL_HOST_3: https://${BASE_DOMAIN}/api/ -> :8080/

networks:
- frontend
- postgres
configs:
- config.json
deploy:
restart_policy:
delay: "30s"
placement:
constraints:
- node.labels.govtool==true
frontend:
image: govtool/frontend
build:
context: ../../govtool/frontend
environment:
VIRTUAL_HOST: https://${BASE_DOMAIN}
networks:
- frontend
deploy:
restart_policy:
delay: "30s"
placement:
constraints:
- node.labels.govtool==true
3 changes: 2 additions & 1 deletion tests/test-infrastructure/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ secrets:
external: true
name: ${STACK_NAME}_dbsync_database

## secrets syntax for docker compose stack
# secrets syntax for docker compose stack
##
# secrets:
# postgres_user:
# file: "./secrets/${STACK_NAME}_postgres_user"
Expand Down
38 changes: 16 additions & 22 deletions tests/test-infrastructure/gen-configs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ set +a

# Function to generate a random secret in base64 format without padding and '+'
function generate_secret() {
openssl rand -base64 16 | tr -d '=+/'
local filename=$2
local var_name=$1
if [ -s "$filename" ]; then
export "$var_name"=$(<"$filename")
else
local secret=$(openssl rand -base64 16 | tr -d '=+/')
echo -n "$secret" > "$filename"
export "$var_name"="$secret"
fi
}


if [ "$1" == "clean" ]; then

# Create secrets from files
Expand All @@ -45,35 +52,22 @@ if [ "$1" == "clean" ]; then
exit 0
fi

## Check if one fo the secrets already exists
if [[ -f ./secrets/postgres_user ]]
then
echo "File ./secrets/postgres_user already exists."
echo "Assuming that the secrets were already generated"
echo " Use:"
echo " > ./gen-configs.sh clean"
echo " To clean up the configs and secrets"
exit 0
fi

## create dir if not present.
mkdir -p ./configs;
mkdir -p ./secrets;


# Generate random secrets
export POSTGRES_USER=postgres
export POSTGRES_PASSWORD=$(generate_secret)
metrics_api_secret=$(generate_secret)
DBSYNC_DATABASE="${STACK_NAME}_dbsync"


export DBSYNC_DATABASE="${STACK_NAME}_dbsync"

# Save secrets to files
echo -n $POSTGRES_USER > ./secrets/postgres_user
echo -n $POSTGRES_PASSWORD > ./secrets/postgres_password
echo -n $metrics_api_secret > ./secrets/metrics_api_secret
echo -n "$DBSYNC_DATABASE" > ./secrets/dbsync_database

# generate or load the secret
generate_secret "POSTGRES_PASSWORD" "./secrets/postgres_password"

## loop over templates and update them.
for CONFIG_FILE in $(ls ./configs_template)
do
Expand All @@ -96,7 +90,7 @@ docker info | grep 'Swarm: active' > /dev/null 2>/dev/null || exit 0
# Create secrets from files
ls ./secrets | while IFS= read -r SECRET_FILE; do
SECRET_NAME=$(basename "$SECRET_FILE")
echo -n "Creating Secret: ${STACK_NAME}_${SECRET_NAME}: "
echo -n "Secret: ${STACK_NAME}_${SECRET_NAME}: "
cat "./secrets/$SECRET_NAME" | (docker secret create "${STACK_NAME}_${SECRET_NAME}" -) || true
done

Expand All @@ -105,6 +99,6 @@ done
for CONFIG_FILE in $(ls ./configs)
do
CONFIG_NAME=$(basename $CONFIG_FILE)
echo -n "Creating Config: ${STACK_NAME}_${CONFIG_NAME}: "
echo -n "Config: ${STACK_NAME}_${CONFIG_NAME}: "
cat "./configs/$CONFIG_NAME" | (docker config create "${STACK_NAME}_${CONFIG_NAME}" -) || true
done
47 changes: 47 additions & 0 deletions tests/test-infrastructure/scripts/deploy-stack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
## Docker swarm doesn't read .env file.
## This script reads env file and variables
## and apply them to compose file and
## then execute `docker stack deploy`

set -eo pipefail

function load_env(){
set -a
. ./.env
set +a
}

function help_deploy(){
echo "Something is wrong with the command"
echo
echo " Usage:"
echo " $0 [stack-name] [filename]"
echo
}

function deploy-stack(){
## apply the environment to compose file
## deploy the govtool test infrastructure stack
## first argument is stack name and 2nd argument is the file name
STACK_NAME=$1
COMPOSE_FILE=$2
FILENAME=$(basename -- "$COMPOSE_FILE")
EXTENSION="${FILENAME##*.}"
FILENAME_WITHOUT_EXT="${FILENAME%.*}"
RENDERED_FILENAME="${FILENAME_WITHOUT_EXT}-rendered.${EXTENSION}"
envsubst < "$COMPOSE_FILE" > "$RENDERED_FILENAME"
echo docker stack deploy -c "$RENDERED_FILENAME" ${STACK_NAME}
}


if [ "$#" -eq 0 ]; then
exit 0
elif [ "$#" -ne 2 ];
then
help_deploy
exit 1
else
load_env
deploy-stack "$1" "$2"
fi

0 comments on commit d9c5e08

Please sign in to comment.