-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.sh
executable file
·109 lines (87 loc) · 3.5 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
. ./helpers/colors.sh
. ./helpers/filesAndFolderActions.sh
success "Start environment initialization..."
# ENV files init
switchPath=`pwd -P`
envFile='.env'
usedPortsFile='.used-ports'
fileFromExample ${envFile}
fileFromExample ${usedPortsFile}
. ./.env
success "Parsing CLI options"
OPTS=`getopt -o p:h:c: --long project:,project-host-base:,compose: -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then error "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "${OPTS}"
while true; do
case "$1" in
--help ) HELP=true; shift ;;
-p | --project ) project="$2"; shift; shift ;;
-h | --project-host-base ) projectHostBase="$2"; shift; shift ;;
-c | --compose ) compose="$2"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
success "Creating project structure:"
info "Project: ${project}"
info "Host Base: *.${projectHostBase}"
info "Git repo or path in project for docker-env: ${compose}"
if [[ ${project} == '' || ${projectHostBase} == '' || ${compose} == '' ]]
then
error 'Project, repo and project-host options are mandatory to enter!'
exit 1;
fi
# Check git clone ability
if [[ ${compose} == 'git' && ! ($(git ls-remote ${compose})) ]]; then
error 'There is no access to clone doker-compose repo for project!'
exit 1;
# Put Failure actions here...
else
rm -rf tmp
# Put Success actions here...
fi
# Project structure initialization
logsPath=${projectsPath}/${project}/logs
mkdir -p ${logsPath}
success "Project logs path '${logsPath}' is created"
stages=( 'production' 'stage' 'test' )
instances=( 'green' 'blue' )
lastUsedPort=$(tail -1 "${usedPortsFile}")
let "nextPort = ${lastUsedPort} - (${lastUsedPort} - 8000)/10"
for stage in "${stages[@]}"
do
nginxStagePathEnabled=${nginxEnabledConfigs}/${project}/${stage}
nginxStagePathAvailable=${nginxAvailableConfigs}/${project}/${stage}
mkdir -p ${nginxStagePathAvailable}
success "Nginx available configs path '${nginxStagePathAvailable}' created"
mkdir -p ${nginxStagePathEnabled}
success "Nginx enabled config path '${nginxStagePathEnabled}' created"
let "nextPort += 10"
for instance in "${instances[@]}"
do
let "nextPort += 1"
echo ${nextPort} >> ${usedPortsFile}
projectInstancePath=${projectsPath}/${project}/${stage}/${instance}
if [[ ! -d ${projectInstancePath} ]]; then
mkdir -p ${projectInstancePath}
success "Project instance path '${projectInstancePath}' is created"
fi
nginxConfig=${nginxStagePathAvailable}/${instance}
cp ./templates/nginx.conf ${nginxConfig}
sed -i "s,NAME,${project},g" ${nginxConfig}
sed -i "s,STAGE,${stage},g" ${nginxConfig}
sed -i "s,INSTANCE_PORT,${nextPort},g" ${nginxConfig}
sed -i "s,INSTANCE,${instance},g" ${nginxConfig}
sed -i "s,HOST,${stage}.${projectHostBase},g" ${nginxConfig}
sed -i "s,LOGS_PATH,${logsPath},g" ${nginxConfig}
success "Nginx config '${nginxConfig}' is created"
git clone -q ${compose} ${projectInstancePath}
cp ${projectInstancePath}/.env.example ${projectInstancePath}/.env
sed -i "s,NGINX_HTTP_PORT=80,NGINX_HTTP_PORT=${nextPort},g" ${projectInstancePath}/.env
sed -i "s,INSTANCE=develop,INSTANCE=${instance},g" ${projectInstancePath}/.env
ln -s ${switchPath}/templates/stop.sh ${projectInstancePath}/stop.sh
ln -s ${switchPath}/templates/restart.sh ${projectInstancePath}/restart.sh
done
done
success "Job is done!"