Skip to content

Commit

Permalink
feature : Can set a different port number for your app & !Make Checki…
Browse files Browse the repository at this point in the history
…ng States Phase much safer

Can set a different port number for your app
PROJECT_PORT=80
!Make Checking States Phase much safer
  • Loading branch information
patternhelloworld committed Sep 7, 2023
1 parent 6d7a118 commit d1833b8
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 83 deletions.
42 changes: 36 additions & 6 deletions .docker/nginx/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
#!/bin/bash

set_expose_and_app_port(){

if [[ -z ${1} ]]
then
echo "[ERROR] The 'project_port' has not been passed. Terminate the entire process to prevent potential errors." && exit 1
fi

if echo "${1}" | grep -Eq '^\[[0-9]+,[0-9]+\]$'; then
IFS=',' read -ra values <<< "${project_port//[^0-9,]}"
expose_port=${values[0]}
app_port=${values[1]}
else
expose_port="$project_port"
app_port="$project_port"
fi
}

project_name=$(printenv PROJECT_NAME)
project_port=$(printenv PROJECT_PORT)

if ! echo "$project_port" | grep -Eq '^\[[0-9]+,[0-9]+\]$|^[0-9]+$'; then
echo "[ERROR] project_port on .env is a wrong type. (ex. [30000,3000] or 8888 formats are available). Correct .env, and re-run ./run.sh." && exit 1
fi
set_expose_and_app_port ${project_port}

echo "[DEBUG] expose_port : ${expose_port} , app_port : ${app_port}"

app_url=$(printenv APP_URL)
protocol=$(echo ${app_url} | awk -F[/:] '{print $1}')
consul_key=$(echo $(printenv CONSUL_KEY_VALUE_STORE) | cut -d "/" -f6)\\/$(echo $(printenv CONSUL_KEY_VALUE_STORE) | cut -d "/" -f7)
Expand All @@ -25,7 +51,9 @@ echo "[NOTICE] Locate the template file for ${protocol}."
sleep 3
mv /ctmpl/${protocol}/nginx.conf.ctmpl /etc/consul-templates

sed -i -e "s/###PROJECT_PORT###/${project_port}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "project_port (${project_port}) replacement failure." && exit 1)
sed -i -e "s/###EXPOSE_PORT###/${expose_port}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "expose_port (${expose_port}) replacement failure." && exit 1)
sed -i -e "s/###APP_PORT###/${app_port}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "app_port (${app_port}) replacement failure." && exit 1)

sed -i -e "s/###PROJECT_NAME###/${project_name}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "project_name (${project_name}) replacement failure." && exit 1)
sed -i -e "s/###CONSUL_KEY###/${consul_key}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "consul_key (${consul_key}) replacement failure." && exit 1)
sed -i -e "s/###NGINX_CLIENT_MAX_BODY_SIZE###/${nginx_client_max_body_size}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "nginx_client_max_body_size (${nginx_client_max_body_size}) replacement failure." && exit 1)
Expand Down Expand Up @@ -69,13 +97,15 @@ if [[ ${protocol} = 'https' ]]; then


app_host=$(echo ${app_url} | awk -F[/:] '{print $4}')
echo "[DEBUG] app_host : ${app_host}"

escaped_app_url=$(echo ${app_url} | sed 's/\//\\\//g')
#escaped_app_url=$(echo ${app_url} | sed 's/\//\\\//g')
#echo "[DEBUG] escaped_app_url : ${escaped_app_url}"

sed -i -e "s/###APP_URL###/${escaped_app_url}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "APP_URL on .env failed to be applied." && exit 1)
sleep 1
sed -i -e "s/###APP_HOST###/${app_host}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "APP_HOST on .env failed to be applied." && exit 1)
sleep 1
#sed -i -e "s/###APP_URL###/${escaped_app_url}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "APP_URL on .env failed to be applied." && exit 1)
# sleep 1
#sed -i -e "s/###APP_HOST###/${app_host}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "APP_HOST on .env failed to be applied." && exit 1)
#sleep 1
sed -i -e "s/###COMMERCIAL_SSL_NAME###/${commercial_ssl_name}/" /etc/consul-templates/nginx.conf.ctmpl || (echo "commercial_ssl_name (${commercial_ssl_name}) on .env failed to be applied." && exit 1)
fi

Expand Down
8 changes: 7 additions & 1 deletion .env.example.local
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ GIT_TOKEN_IMAGE_LOAD_FROM_PASSWORD=xxx

PROJECT_NAME=xxx
PROJECT_LOCATION=xxx

# [EXPOSE_PORT,APP_PORT]
# If you set PROJECT_PORT=[Number], EXPOSE_PORT,APP_PORT are all set to be [Number]
# Samples : PROJECT_PORT=[18200,8200], PROJECT_PORT=8200
PROJECT_PORT=80
# Example (8093,8094,11000...)
# [Number],[Number]
# Currently, ADDITIONAL_PORTS for EXPOSE_PORT,APP_PORT should be the same value.
# Samples : ADDITIONAL_PORTS=5005,5006
ADDITIONAL_PORTS=8093

CONSUL_KEY_VALUE_STORE=http://consul:8500/v1/kv/deploy/xxx
Expand Down
7 changes: 6 additions & 1 deletion .env.example.real
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ GIT_TOKEN_IMAGE_LOAD_FROM_PASSWORD=yyy

PROJECT_NAME=xxx
PROJECT_LOCATION=xxx
# [EXPOSE_PORT,APP_PORT]
# If you set PROJECT_PORT=[Number], EXPOSE_PORT,APP_PORT are all set to be [Number]
# Samples : PROJECT_PORT=[18200,8200], PROJECT_PORT=8200
PROJECT_PORT=xxx
# Example (8093,8094,11000...)
# [Number],[Number]
# Currently, ADDITIONAL_PORTS for EXPOSE_PORT,APP_PORT should be the same value.
# Samples : ADDITIONAL_PORTS=5005,5006
ADDITIONAL_PORTS=8093

CONSUL_KEY_VALUE_STORE=http://consul:8500/v1/kv/deploy/xxx
Expand Down
4 changes: 2 additions & 2 deletions .env.java.real
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
HOST_IP=host.docker.internal
APP_ENV=real

APP_URL=http://localhost:8200
APP_URL=http://localhost:18200

USE_COMMERCIAL_SSL=yyy
COMMERCIAL_SSL_NAME=yyy
Expand All @@ -24,7 +24,7 @@ GIT_TOKEN_IMAGE_LOAD_FROM_PASSWORD=xxx

PROJECT_NAME=spring-sample-h-auth
PROJECT_LOCATION=/var/www/server/spring-sample-h-auth
PROJECT_PORT=8200
PROJECT_PORT=[18200,8200]
# Example (8093,8094,11000...)
ADDITIONAL_PORTS=5005

Expand Down
4 changes: 2 additions & 2 deletions .env.node.local
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
HOST_IP=host.docker.internal
APP_ENV=local

APP_URL=http://localhost:3000
APP_URL=http://localhost:13000

USE_COMMERCIAL_SSL=yyy
COMMERCIAL_SSL_NAME=yyy
Expand All @@ -24,7 +24,7 @@ GIT_TOKEN_IMAGE_LOAD_FROM_PASSWORD=xxx

PROJECT_NAME=node-express-boilerplate
PROJECT_LOCATION=/usr/src/node-app
PROJECT_PORT=3000
PROJECT_PORT=[13000,3000]
# Example (8093,8094,11000...)
ADDITIONAL_PORTS=

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Let me continually explain how to use Docker-Blue-Green-Runner with the followin
- The image or Dockerfile in your app must contain "bash" & "curl"
- Do NOT use local & real at the same time (There's no reason to do so, but just in case...)

## How to Start with a Node Sample (Local, PORT: 3000).
## How to Start with a Node Sample (Local).

A Node.js sample project (https://github.com/hagopj13/node-express-boilerplate) that has been receiving a lot of stars, comes with an MIT License and serves as an example for demonstrating how to use Docker-Blue-Green-Runner.

Expand All @@ -57,7 +57,7 @@ cp -f .env.node.local .env
bash run.sh
```

## How to Start with a PHP Sample (Local, PORT: 8080).
## How to Start with a PHP Sample (Local).

A PHP sample project (https://github.com/Andrew-Kang-G/laravel-crud-boilerplate) that comes with an MIT License and serves as an example for demonstrating how to use Docker-Blue-Green-Runner.

Expand All @@ -79,7 +79,7 @@ bash run.sh
```
and test with the Postman samples (./samples/laravel-crud-boilerplate/reference/postman) and debug with the following instruction ( https://github.com/Andrew-Kang-G/laravel-crud-boilerplate#debugging ).

## How to Start with a PHP Sample (Real, PORT: 8080).
## How to Start with a PHP Sample (Real).

Differences between ``./samples/laravel-crud-boilerplate/Dockerfile.local`` and ``./samples/laravel-crud-boilerplate/Dockerfile.real``

Expand Down Expand Up @@ -107,7 +107,7 @@ bash run.sh
```
Open https://localhost:8080 (NO http. see .env. if you'd like http, change APP_URL) in your browser, and test with the Postman samples (./samples/laravel-crud-boilerplate/reference/postman) and debug with the following instruction ( https://github.com/Andrew-Kang-G/laravel-crud-boilerplate#debugging ).

## How to Start with a Java Sample (Local & Real, PORT: 8200).
## How to Start with a Java Sample (Local & Real).
```shell
# First, as the sample project requires MySQL8, run it separately.
# You can use your own MySQL8 Docker or just clone "https://github.com/Andrew-Kang-G/docker-my-sql-replica"
Expand Down
8 changes: 4 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ load_nginx_docker_image(){
docker rmi -f ${load_from_registry_image_with_env}-nginx-${app_version}|| exit 1
else

echo "[NOTICE] Build the ${project_name}-nginx image (using cache)."
echo "[NOTICE] As !NGINX_RESTART is true, which means there will be a short-downtime for Nginx, build the ${project_name}-nginx image (using cache)."
docker build --build-arg DISABLE_CACHE=${CUR_TIME} --build-arg protocol="${protocol}" --tag ${project_name}-nginx -f ./.docker/nginx/Dockerfile . || exit 1

fi
Expand Down Expand Up @@ -238,7 +238,7 @@ load_app_docker_image() {

nginx_restart(){

echo "[NOTICE] Terminate NGINX container and network."
echo "[NOTICE] As !NGINX_RESTART is true, which means there will be a short-downtime for Nginx, terminate Nginx container and network."

# docker-compose -f docker-compose-${project_name}-${app_env}.yml down || echo "[DEBUG] A1"
docker-compose -f docker-compose-${project_name}-nginx.yml down || echo "[DEBUG] N1"
Expand All @@ -251,7 +251,7 @@ nginx_restart(){

consul_restart(){

echo "[NOTICE] Terminate CONSUL container and network."
echo "[NOTICE] As !CONSUL_RESTART is true, which means there will be a short-downtime for CONSUL, terminate CONSUL container and network."

#docker-compose -f docker-compose-${project_name}-${app_env}.yml down || echo "[DEBUG] C-A1"
#docker-compose -f docker-compose-${project_name}-nginx.yml down || echo "[DEBUG] C-N1"
Expand Down Expand Up @@ -295,7 +295,7 @@ load_all_containers(){
echo "[NOTICE] Load '${project_name}-${new_state} container'."
docker-compose -f docker-compose-${project_name}-${app_env}.yml stop ${project_name}-${new_state} || echo "[NOTICE] The previous ${new_state} Container has been stopped, if exists."
docker-compose -f docker-compose-${project_name}-${app_env}.yml rm -f ${project_name}-${new_state} || echo "[NOTICE] The previous ${new_state} Container has been removed, if exists."
docker-compose -f docker-compose-${project_name}-${app_env}.yml up -d ${project_name}-${new_state} || (echo "[ERROR] Critical - App ${new_state} UP failure" && exit 1)
docker-compose -f docker-compose-${project_name}-${app_env}.yml up -d ${project_name}-${new_state} || (echo "[ERROR] App ${new_state} UP failure, however that does NOT affect the current deployment, as this is Blue-Green Deployment." && exit 1)
echo "[NOTICE] '${project_name}-${new_state} container' : successfully loaded."

echo "[NOTICE] Check the status inside of the container."
Expand Down
Loading

0 comments on commit d1833b8

Please sign in to comment.