Skip to content

Commit 796da77

Browse files
committed
Improved option --setup-docker-stack.
Signed-off-by: Exadra37 <exadra37@gmail.com>
1 parent a1065ff commit 796da77

File tree

3 files changed

+100
-23
lines changed

3 files changed

+100
-23
lines changed

bin/parse-env-var.sh

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,76 @@ set -e
44

55
function Parse_Env_Var_From_Env_File()
66
{
7-
local env_var="${1?}"
7+
local _env_var="${1?}"
88

9-
local env_file="${2?}"
9+
local _env_file="${2?}"
1010

1111
# TODO: only grep lines that are not commented out.
12-
local env_var_line="$(grep -i "${env_var}=" "${env_file}")"
12+
local _env_var_line="$(grep -i "${_env_var}=" "${_env_file}")"
1313

1414
# PHP_IMAGE=exadra37/php7-fpm:latest -> exadra37/php7-fpm:latest
15-
local docker_image="${env_var_line##*=}"
15+
local _env_var_default_value="${_env_var_line##*=}"
1616

17-
echo "${docker_image}"
17+
echo "${_env_var_default_value}"
1818
}
1919

2020
function Parse_Env_Var_From_Docker_Compose()
2121
{
22-
local env_var="${1?}"
22+
local _env_var="${1?}"
2323

24-
local docker_compose_file="${2?}"
24+
local _docker_compose_file="${2?}"
2525

26-
local env_var_line="$(grep -i "${env_var}:-" "${docker_compose_file}")"
26+
local _env_var_line="$(grep -i "${_env_var}:-" "${_docker_compose_file}")"
2727

2828
# image: ${PHP_IMAGE:-exadra37/php7-fpm:latest} -> exadra37/php7-fpm:latest
29-
local docker_image="${env_var_line##*:-}"
29+
local _env_var_default_value="${_env_var_line##*:-}"
3030

3131
# removes } from end of string
32-
local docker_image="${docker_image::-1}"
32+
local _env_var_default_value="${_env_var_default_value::-1}"
3333

34-
echo "${docker_image}"
34+
echo "${_env_var_default_value}"
35+
}
36+
37+
function Parse_Left_Env_Var_Value_From_Env_File()
38+
{
39+
local _env_var="${1?}"
40+
41+
local _env_file="${2?}"
42+
43+
local _env_var_value="$(Parse_Env_Var_From_Env_File "${_env_var}" "${_env_file}")"
44+
45+
echo "${_env_var_value%:*}"
46+
}
47+
48+
function Parse_Left_Env_Var_Value_From_Docker_Compose()
49+
{
50+
local _env_var="${1?}"
51+
52+
local _docker_compose_file="${2?}"
53+
54+
local _env_var_value="$(Parse_Env_Var_From_Docker_Compose "${_env_var}" "${_docker_compose_file}")"
55+
56+
echo "${_env_var_value%:*}"
57+
}
58+
59+
function Parse_Right_Env_Var_Value_From_Env_File()
60+
{
61+
local _env_var="${1?}"
62+
63+
local _env_file="${2?}"
64+
65+
local _env_var_value="$(Parse_Env_Var_From_Env_File "${_env_var}" "${_env_file}")"
66+
67+
echo "${_env_var_value#*:}"
68+
}
69+
70+
function Parse_Right_Env_Var_Value_From_Docker_Compose()
71+
{
72+
local _env_var="${1?}"
73+
74+
local _docker_compose_file="${2?}"
75+
76+
local _env_var_value="$(Parse_Env_Var_From_Docker_Compose "${_env_var}" "${_docker_compose_file}")"
77+
78+
echo "${_env_var_value#*:}"
3579
}

bin/php7-docker-stack-setup.sh

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ set -e
88

99
function Create_Dir_If_Not_Exists()
1010
{
11-
local dir_path="${1}"
11+
local _dir_path="${1}"
1212

13-
[ -d "${dir_path}" ] || mkdir -p "${dir_path}"
13+
if [ -n "${_dir_path}" ] && [ ! -d "${_dir_path}" ]
14+
then
15+
printf "\nCreated Dir: ${_dir_path}"
16+
17+
mkdir -p "${_dir_path}"
18+
19+
# add an empty line for layout purposes
20+
echo
21+
fi
1422
}
1523

1624
function Copy_Docker_Dir_To_Project_Root()
@@ -19,25 +27,49 @@ set -e
1927
cp -rn ./vendor/exadra37-docker-compose/php7-docker-stack/src/docker .
2028
}
2129

22-
function Create_Default_Host_Dirs()
30+
function Create_Default_Host_Dirs_From_Env_Vars()
2331
{
32+
local _env_var
33+
local -a _env_vars
34+
35+
IFS=',' read -a _env_vars <<< "${1?}"
36+
local _env_file="${2?}"
37+
local _docker_compose_file="${3?}"
38+
2439
printf "\n---> Create Docker Stack Default Host Dirs <---"
2540

26-
# creating the default dirs to persist the containers data. otherwise they will
27-
# be created as root user.
28-
Create_Dir_If_Not_Exists ~/.dockerize/storage/database/mysql
29-
Create_Dir_If_Not_Exists ~/.dockerize/storage/elastic-search/public
30-
Create_Dir_If_Not_Exists ~/.dockerize/storage/elastic-search/internal
41+
for _env_var in "${_env_vars[@]}"
42+
do
43+
Create_Dir_If_Not_Exists "$(Parse_Left_Env_Var_Value_From_Env_File "${_env_var}" "${_env_file}")"
44+
45+
Create_Dir_If_Not_Exists "$(Parse_Left_Env_Var_Value_From_Docker_Compose "${_env_var}" "${_docker_compose_file}")"
46+
done
47+
48+
# add an empty line for layout purposes
49+
echo
50+
51+
return 0
3152
}
3253

3354
function Set_Env_Vars()
3455
{
3556
printf "\n---> Setup Env Vars <---\n\n"
57+
58+
printf "\nDB_HOST=database" >> .env
59+
printf "\nCACHE_DRIVER=redis" >> .env
60+
printf "\nSESSION_DRIVER=redis" >> .env
61+
printf "\nQUEUE_DRIVER=beanstalkd" >> .env
62+
printf "\nREDIS_HOST=cache" >> .env
63+
printf "\nHTTP_PORT_MAP=8000:80" >> .env
3664
}
3765

3866
function Setup_Docker_Stack()
3967
{
40-
Create_Default_Host_Dirs
68+
local _host_dir_env_vars="${1?}"
69+
local _env_file="${2?}"
70+
local _docker_compose_file="${3?}"
71+
72+
Create_Default_Host_Dirs_From_Env_Vars "${_host_dir_env_vars}" "${_env_file}" "${_docker_compose_file}"
4173
Copy_Docker_Dir_To_Project_Root
4274
Set_Env_Vars
4375
}

bin/server

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ set -e
1717
# Defaults
1818
########################################################################################################################
1919

20-
env_vars="CACHE_IMAGE,CRON_JOBS_IMAGE,DATABASE_IMAGE,HTTP_IMAGE,LOGGER_IMAGE,MONITOR_IMAGE,PHP_IMAGE,QUEUE_IMAGE,DEV_CLI_IMAGE,DATABASE_CLI_IMAGE"
2120
sudo_prefix=$(Sudo_Prefix)
21+
env_vars="CACHE_IMAGE,CRON_JOBS_IMAGE,DATABASE_IMAGE,HTTP_IMAGE,LOGGER_IMAGE,MONITOR_IMAGE,PHP_IMAGE,QUEUE_IMAGE,DEV_CLI_IMAGE,DATABASE_CLI_IMAGE"
22+
host_dir_env_vars="DATABASE_VOLUME_MAP,LOGGER_VOLUME_MAP"
2223

2324

2425
########################################################################################################################
@@ -104,7 +105,7 @@ set -e
104105
Show_Help "${script_dir}"
105106
;;
106107
--setup-docker-stack)
107-
Setup_Docker_Stack
108+
Setup_Docker_Stack "${host_dir_env_vars}" "${env_file}" "${docker_compose_file}"
108109
exit 0
109110
;;
110111
--update-docker-images)
@@ -149,7 +150,7 @@ set -e
149150
# to run when the required docker image needs to be build locally.
150151
if [ ! -d ./docker ]
151152
then
152-
Setup_Docker_Stack
153+
Setup_Docker_Stack "${host_dir_env_vars}" "${env_file}" "${docker_compose_file}"
153154
fi
154155

155156
# By default we pull docker images on each server up or server run

0 commit comments

Comments
 (0)