Skip to content

Commit

Permalink
Completed docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelOyegbemi committed Jul 11, 2020
1 parent 5f2b562 commit af17865
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -63,3 +63,6 @@ typings/
# VSCode config files
.vscode/
*.code-workspace

# Backups
db-backup/
28 changes: 28 additions & 0 deletions arch/conf.d/development.conf
@@ -0,0 +1,28 @@
server {
listen 80;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:5000/;
}

# Block all git access
if ($request_uri ~* ^.*\.git.*$) {
return 404;
}

# Enable Gzip for
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types image/jpeg image/png text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
gzip_min_length 70000;
gunzip on;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
}
25 changes: 9 additions & 16 deletions arch/scripts/entrypoint.sh
@@ -1,15 +1,15 @@
#!/usr/bin/env bash

export WORKING_DIR=/var/www/
export WORKING_DIR=/var/www
export APP_WORKSPACE=${APP_WORKSPACE:-$APP_NAME}
export APP_ENV="${$APP_ENV:=local}"
export APP_ENV=${APP_ENV:-local}

echo ">>Preparing app $APP_NAME..."
echo ">>APP_ENV: $APP_ENV"

if [ $APP_ENV != "local" ]; then
if [[ $APP_ENV != "local" && $APP_ENV != "test" ]]; then
# Download and setup repository if not in local environment
repo.sh setup
source repo.sh init
fi

#Change directory to APP_WORKSPACE
Expand All @@ -24,17 +24,10 @@ if [ $APP_ENV == "test" ]; then
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/run-test.sh" || exit 1
echo ">>Test ran successfully";exit 0
else
#Run migrations
[ $UNDO_MIGRATION_ON_STARTUP == 1 ] && echo ">>Undoing database migrations" && yarn db:migrate:undo
echo ">>Running database migrations"
yarn db:migrate
fi

if [ $APP_ENV == "local" ]; then
echo ">>Running local server..."
yarn dev
else
# Setup and run server
#Do any necessary setup Move this back
chmod +x "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/setup.sh"
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/setup.sh" || echo ">>Can not find setup script" && exit 1
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/setup.sh" || exit 1
fi

chmod +x "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/start-server.sh"
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/start-server.sh" || exit 1
19 changes: 8 additions & 11 deletions arch/scripts/repo.sh
Expand Up @@ -6,10 +6,10 @@ set -o pipefail
GIT_OAUTH2_USER="${GIT_OAUTH2_USER:-oauth2}"
GIT_BRANCH="${GIT_BRANCH:-master}"

ACTION="${1:-setup}"
ACTION="${1:-init}"
shift

VALID_ACTIONS=("checkout" "pull" "clone" "reset" "status" "setup")
VALID_ACTIONS=("checkout" "pull" "clone" "reset" "status" "init")
if [[ ! " ${VALID_ACTIONS[*]} " == *" $ACTION "* ]]; then
printf '>>ERROR: invalid action %s \nAction is the first argument and can only be any of:\n %s\n' "$ACTION" "$(IFS=, ; echo "${VALID_ACTIONS[*]}")" >&2
exit 1
Expand Down Expand Up @@ -51,11 +51,6 @@ done

if [ -z "$APP_WORKSPACE" ]; then
echo ">>Need to set APP_NAME env variable";exit 1
elif [ -d "$APP_WORKSPACE" ]; then
echo ">>Changing directory to $APP_WORKSPACE"
cd "$APP_WORKSPACE"
else
echo ">>Workspace directory $APP_WORKSPACE doesn't exist";exit 1
fi

if [ -z "$FULL_GIT_URL" ]; then
Expand Down Expand Up @@ -83,9 +78,10 @@ checkout ()
pull ()
{
echo ">>Changing directory to $WORKING_DIR/$APP_WORKSPACE"
cd "$WORKING_DIR/$APP_WORKSPACE" || echo ">>Directory $WORKING_DIR/$APP_WORKSPACE does not exits" && exit 1
cd "$WORKING_DIR/$APP_WORKSPACE" || exit 1
echo ">>Pulling latest changes from branch $GIT_BRANCH into $WORKING_DIR/$APP_WORKSPACE"
git pull $FULL_GIT_URL $GIT_BRANCH
checkout
}

clone ()
Expand All @@ -96,13 +92,14 @@ clone ()
git clone $FULL_GIT_URL "$WORKING_DIR/$APP_WORKSPACE"
if [ -d "$WORKING_DIR/$APP_WORKSPACE/.git" ]; then
echo ">>Successfull clone $REPOSITORY_URL"
checkout
else
echo ">>Error occurred, could not clone $REPOSITORY_URL, Exiting..."
exit 1
fi
}

setup ()
init ()
{
if [ -d "$WORKING_DIR/$APP_WORKSPACE" ]; then
if [ -d "$WORKING_DIR/$APP_WORKSPACE/.git" ]; then
Expand Down Expand Up @@ -150,6 +147,6 @@ elif [ "$ACTION" == "reset" ]; then
fi
elif [ "$ACTION" == "status" ]; then
status
elif [ "$ACTION" == "setup" ]; then
setup
elif [ "$ACTION" == "init" ]; then
init
fi
16 changes: 11 additions & 5 deletions arch/scripts/run-test.sh
Expand Up @@ -3,8 +3,14 @@
set -e
set -o pipefail

echo ">>Running lint with 'yarn lint'..."
yarn lint

echo ">>Running test with 'yarn test'..."
yarn test
if [ "$TEST_ONLY" == "1" ]; then
echo ">>Running test with 'yarn test:all'..."
yarn test:all
else
echo ">>Running lint with 'yarn lint'..."
yarn lint
echo ">>Build app 'yarn build'..."
yarn build
echo ">>Running test with 'yarn test'..."
yarn test
fi
22 changes: 19 additions & 3 deletions arch/scripts/setup.sh 100644 → 100755
Expand Up @@ -3,6 +3,22 @@
set -e
set -o pipefail

#Building app
echo ">>Building app with 'yarn build'"
yarn build
if [ "$APP_ENV" != "local" ]; then
#Update nginx config and start it if not on local environment
echo ">>Copying nginx configuration..."
cat "$WORKING_DIR/$APP_WORKSPACE/arch/conf.d/$APP_ENV.conf" > /etc/nginx/conf.d/default.conf
echo ">>Starting nginx..."
nginx || exit 1

## Copy environment file
cp ".env.$APP_ENV" ".env"
fi

if [ "$UNDO_MIGRATION_ON_STARTUP" == 1 ]; then
echo ">>Undoing database migrations..."
yarn db:migrate:undo
fi

#Run migrations
echo ">>Running database migrations..."
yarn db:migrate
15 changes: 15 additions & 0 deletions arch/scripts/start-server.sh
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e
set -o pipefail

if [ $APP_ENV == "local" ]; then
echo ">>Running local server..."
yarn dev
else
# Build and run server
echo ">>Building app with 'yarn build...'"
yarn build
echo "Starting $APP_ENV server..."
yarn start
fi
18 changes: 9 additions & 9 deletions docker-compose.yml
@@ -1,8 +1,7 @@
version: '3'
services:
app:
build:
context: .
account:
build: .
ports:
- ${PORT:-5000}:${PORT:-5000}
volumes:
Expand All @@ -13,12 +12,12 @@ services:
- APP_ENV=local
- PORT=5000
- UNDO_MIGRATION_ON_STARTUP=0
- REDIS_URL=redis://account_service_cache
- DB_URL=postgres://edustripe:test@account_service_db/edustripe_account
- DB_URL_TEST=postgres://edustripe:test@account_service_db/edustripe_account_test
- REDIS_URL=redis://edustripe_account_service_cache
- DB_URL=postgres://edustripe:test@edustripe_account_service_db/edustripe_account
- DB_URL_TEST=postgres://edustripe:test@edustripe_account_service_db/edustripe_account_test
- APP_NAME=account-service
- GIT_BRANCH=development
container_name: account_service_app
container_name: edustripe_account_service
depends_on:
- db
- cache
Expand All @@ -29,17 +28,18 @@ services:
- ${POSTGRES_HOST_PORT:-5433}:5432
volumes:
- ./arch/db/setup-test-db.sql:/docker-entrypoint-initdb.d/setup-test-db.sql
- ./db-backup/:/var/lib/postgresql/data/
environment:
- POSTGRES_HOST_PORT=5433
- POSTGRES_USER=edustripe
- POSTGRES_PASSWORD=test
- POSTGRES_DB=edustripe_account
container_name: account_service_db
container_name: edustripe_account_service_db

cache:
image: redis:alpine
ports:
- ${REDIS_HOST_PORT:-6380}:6379
environment:
- REDIS_HOST_PORT=6380
container_name: account_service_cache
container_name: edustripe_account_service_cache

0 comments on commit af17865

Please sign in to comment.