Skip to content

Commit

Permalink
Improve bin/start and remove backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
rangerz committed Oct 19, 2020
1 parent e944eb7 commit 9efdf0a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 25 deletions.
14 changes: 7 additions & 7 deletions compose/bin/removevolumes
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
current_folder=${PWD##*/}
volume_prefix=`echo $current_folder | awk '{print tolower($0)}' | sed 's/\.//g'`
docker volume rm ${volume_prefix}_appdata
docker volume rm ${volume_prefix}_dbdata
docker volume rm ${volume_prefix}_rabbitmqdata
docker volume rm ${volume_prefix}_sockdata
docker volume rm ${volume_prefix}_ssldata
CUR_DIR=${PWD##*/}
VOLUME_PREFIX=$(echo $CUR_DIR | awk '{print tolower($0)}' | sed 's/\.//g')
docker volume rm ${VOLUME_PREFIX}_appdata
docker volume rm ${VOLUME_PREFIX}_dbdata
docker volume rm ${VOLUME_PREFIX}_rabbitmqdata
docker volume rm ${VOLUME_PREFIX}_sockdata
docker volume rm ${VOLUME_PREFIX}_ssldata
3 changes: 1 addition & 2 deletions compose/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ BASE_URL=${1:-magento2.test}

bin/stop

docker-compose -f docker-compose.yml up -d
bin/start --no-dev
[ $? != 0 ] && echo "Failed to start Docker services" && exit
sleep 5 #Ensure containers are started...

echo "Copying all files from host to container..."
rm -rf src/vendor #Clear for step below
Expand Down
4 changes: 2 additions & 2 deletions compose/bin/setup-grunt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
DEFAULT_THEME_ID="select value from core_config_data where path = 'design/theme/theme_id'"
THEME_PATH="select theme_path from theme where theme_id in ($DEFAULT_THEME_ID);"
VENDOR_THEME=`bin/n98-magerun2 db:query "$THEME_PATH" | sed -n 2p | cut -d$'\r' -f1`
THEME=`echo $VENDOR_THEME | cut -d'/' -f2`
VENDOR_THEME=$(bin/n98-magerun2 db:query "$THEME_PATH" | sed -n 2p | cut -d$'\r' -f1)
THEME=$(echo $VENDOR_THEME | cut -d'/' -f2)

# Generate local-theme.js for custom theme
read -d '' GEN_THEME_JS << EOM
Expand Down
102 changes: 88 additions & 14 deletions compose/bin/start
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
#!/bin/bash

function scanPorts {
# Includes ports of Nginx, Mysql, ElasticSearch, Redis, and Rabbitmq
local ports="80 443 3306 5672 9200 9300 15672"
local mode=$1 # before, after
local isValid=true

for port in $ports; do
if [ $mode = "before" ]; then
curl --silent --output /dev/null 127.0.0.1:$port
if [ $? = 0 ]; then
echo "Port [$port] is already allocated"
isValid=false
fi
else # after
curl --silent --output /dev/null 127.0.0.1:$port
if [ $? != 0 ]; then
echo "Waiting for port [$port] to be ready..."
isValid=false
fi
fi
done

[ $isValid = true ] && return 0 || return 1
}

# 1. Scan available ports if docker containers are not running
docker-compose exec app id >/dev/null 2>&1 || scanPorts "before"
[ $? != 0 ] && echo "Failed to start docker because some ports are already allocated" && exit 1

# 2. Scan volumn files
# Check if volume files exist to avoid creating an empty folder
function parseYaml {
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|,$s\]$s\$|]|" \
Expand Down Expand Up @@ -26,25 +58,67 @@ function parseYaml {
}'
}

# Check if volume files exist to avoid creating an empty folder
VOLUME_LIST=`parseYaml docker-compose.dev.yml services_app_volumes`
IGNORE_LIST="./src/app/code ./src/m2-hotfixes ./src/patches ./src/var/log ./src/var/report ./src"
IS_VALID=true
# Loop through all files missing from the docker-compose.dev.yml file
for file in $VOLUME_LIST; do
if [ ! -e $file ] && [[ ! " $IGNORE_LIST " =~ " $file " ]]; then
echo "$file: No such file or directory"
IS_VALID=false
fi
done
# Wait to exit until all missing files have been outputted to the user
[ $IS_VALID = false ] && echo "Failed to start docker for missing volume files" && exit
function scanVolumeFiles {
local volume_list=$1
local ignore_list="./src/app/code ./src/m2-hotfixes ./src/patches ./src/var/log ./src/var/report ./src"
local isValid=true

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d --remove-orphans "$@"
for file in $volume_list; do
if [ ! -e $file ] && [[ ! " $ignore_list " =~ " $file " ]]; then
echo "$file: No such file or directory"
isValid=false
fi
done

[ $isValid = true ] && return 0 || return 1
}

if [ "$1" != "--no-dev" ]; then
VOLUME_LIST=$(parseYaml docker-compose.dev.yml services_app_volumes)
# Loop through all files missing from the docker-compose.dev.yml file
scanVolumeFiles "$VOLUME_LIST"
# Wait to exit until all missing files have been outputted to the user
[ $? = false ] && echo "Failed to start docker for missing volume files" && exit 1
fi

# 3. Start all docker container
if [ "$1" == "--no-dev" ]; then
docker-compose -f docker-compose.yml up -d --remove-orphans
else
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d --remove-orphans "$@"
fi
[ $? != 0 ] && exit 1

## Blackfire support
# ------------------
## First register the blackfire agent with:
#bin/root blackfire-agent --register --server-id={YOUR_SERVER_ID} --server-token={YOUR_SERVER_TOKEN}
## Then uncomment the below line (and leave uncommented) to start the agent automatically with bin/start:
#bin/root /etc/init.d/blackfire-agent start

echo "Waiting for all services of containers to be ready..."
sleep 10

# 4. Scan allocated ports, retry 5 times
count=0
until [ "$count" -ge 5 ]
do
scanPorts "after" && break
sleep 5
count=$((count+1))
done

# 5. Check service health, retry 5 times
count=0
until [ "$count" -ge 5 ]
do
bin/clinotty curl --silent --output /dev/null http://elasticsearch:9200/_cat/health?h=st \
&& curl --silent --output /dev/null http://guest:guest@localhost:15672/api/overview \
&& bin/redis redis-cli ping > /dev/null \
&& bin/mysql -e "show databases;" >/dev/null \
&& break
sleep 5
count=$((count+1))
done

echo "All services of containers are started"

0 comments on commit 9efdf0a

Please sign in to comment.