-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·295 lines (255 loc) · 7.73 KB
/
start.sh
File metadata and controls
executable file
·295 lines (255 loc) · 7.73 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env bash
set -e; # Exit on error
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
cd "${SCRIPTPATH}"
APPLICATION_UID=${APPLICATION_UID:-1000}
APPLICATION_GID=${APPLICATION_GID:-1000}
APPLICATION_USER=${APPLICATION_USER:-application}
APPLICATION_GROUP=${APPLICATION_GROUP:-application}
# Fix special user permissions
if [ "$(id -u)" != "1000" ]; then
grep -q '^APPLICATION_UID=' .env && sed -i 's/^APPLICATION_UID=.*/APPLICATION_UID='$(id -u)'/' .env || echo 'APPLICATION_UID='$(id -u) >> .env
grep -q '^APPLICATION_GID=' .env && sed -i 's/^APPLICATION_GID=.*/APPLICATION_GID='$(id -g)'/' .env || echo 'APPLICATION_GID='$(id -g) >> .env
fi;
setTerminalTitle() {
echo -ne "\033]0;${1}\007"
#echo -e "\033]2;${1}\007"
}
loadEnvironmentVariables() {
if [ -f ".env" ]; then
source .env
fi
if [ -f ".env.local" ]; then
source .env.local
fi
}
isContextDevelopment() {
# Symfony
APP_ENV=${APP_ENV:-}
if [ "${APP_ENV}" == "dev" ]; then
echo 1; return;
fi
# TYPO3
TYPO3_CONTEXT=${TYPO3_CONTEXT:-}
if [ "${TYPO3_CONTEXT:0:11}" == "Development" ]; then
echo 1; return;
fi
# Wordpress
WP_ENVIRONMENT_TYPE=${WP_ENVIRONMENT_TYPE:-}
if [ "${WP_ENVIRONMENT_TYPE:0:11}" == "development" ]; then
echo 1; return;
fi
# Custom
ENV_DOCKER_CONTEXT=${ENV_DOCKER_CONTEXT:-}
if [ "${ENV_DOCKER_CONTEXT:0:11}" == "Development" ]; then
echo 1; return;
fi
echo 0;
}
setDockerComposeFile() {
local developmentSuffix=''
if [ "$(isContextDevelopment)" == "1" ]; then
developmentSuffix='.dev'
fi
DOCKER_COMPOSE_FILE="compose${developmentSuffix}.yaml"
if [ -e "compose${developmentSuffix}.yml" ]; then
DOCKER_COMPOSE_FILE="compose${developmentSuffix}.yml"
elif [ -e "docker-compose${developmentSuffix}.yaml" ]; then
DOCKER_COMPOSE_FILE="docker-compose${developmentSuffix}.yaml"
elif [ -e "docker-compose${developmentSuffix}.yml" ]; then
DOCKER_COMPOSE_FILE="docker-compose${developmentSuffix}.yml"
fi
}
dockerComposeCmd() {
docker-compose -f "${DOCKER_COMPOSE_FILE}" "${@:1}"
}
checkRoot() {
if [[ $EUID -ne 0 ]]; then
echo 'You must be a root user!' 2>&1
exit 1
fi
}
gitCheckBranch() {
if [ -d ".git" ]; then
if [[ $(git symbolic-ref --short -q HEAD) != "${1}" ]]; then
echo "ERROR: Git is not on branch ${1}!"
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
fi
}
gitCheckDirty() {
if [ -d ".git" ]; then
if [[ $(git diff --stat) != '' ]]; then
echo
git status --porcelain
echo
read -p 'Git is dirty... Continue? [y/N] ' -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
fi
fi
}
findBinaryByWhich() {
set +e
binary=$(which "${1}")
if [ $? -ne 0 ]; then
return
fi
set -e
echo ${binary}
return
}
setPermissions() {
chown -R ${APPLICATION_UID}:${APPLICATION_GID} .
find . -type d -exec chmod ugo+rx,ug+w {} \;
find . -type f -exec chmod ugo+r,ug+w {} \;
}
gitPull() {
if [ -d ".git" ]; then
git pull "${@:1}"
fi
}
composerInstall() {
if [ -f "composer.json" ]; then
${BIN_PHP} ${BIN_COMPOSER} --no-interaction install "${@:1}"
fi
}
symfonyUpdateDatabase() {
if [ -f "symfony.lock" ] && [ ! -z "${DATABASE_URL}" ]; then
read -p 'Update database schema? [y/N] ' -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
${BIN_PHP} ./bin/console doctrine:schema:update --force
fi
fi
}
symfonyClearCache() {
if [ -f "symfony.lock" ]; then
${BIN_PHP} ./bin/console cache:clear --no-warmup
${BIN_PHP} ./bin/console cache:warmup
fi
}
# ./start.sh deploy-images [8.0]...
deployImages() {
versions=( 8.5 8.4 8.3 8.2 8.1 ); versionsToRemove=( 8.0 7.4 7.3 7.2 7.1 )
servers=( apache nginx )
if [ "$#" -gt 0 ]; then
versions=( "$@" )
fi
# Pull images
for version in "${versions[@]}"; do
for server in "${servers[@]}"; do
setTerminalTitle "Pull ${server} ${version} ..."
echo "# Pull ${server} ${version} ..."
docker pull webdevops/php-${server}-dev:${version}
done
done
# Build images
for version in "${versions[@]}"; do
for server in "${servers[@]}"; do
setTerminalTitle "Build ${server} ${version} ..."
echo "# Build ${server} ${version} ..."
docker build --build-arg FROM=webdevops/php-${server}-dev:${version} \
--no-cache --file Dockerfile --tag cyb10101/php-dev:${server}-${version} .
done
done
# Push images
for version in "${versions[@]}"; do
for server in "${servers[@]}"; do
setTerminalTitle "Push ${server} ${version} ..."
echo "# Push ${server} ${version} ..."
docker push cyb10101/php-dev:${server}-${version}
done
done
# Clean up
set +e
for version in "${versionsToRemove[@]}"; do
for server in "${servers[@]}"; do
setTerminalTitle "Remove ${server} ${version} ..."
echo "# Remove ${server} ${version} ..."
docker images --filter=reference="cyb10101/php-dev:${server}-${version}" -q | xargs -r docker image rm '{}'
docker images --filter=reference="webdevops/php-${server}-dev:${version}" -q | xargs -r docker image rm '{}'
done
done
set -e
setTerminalTitle ""
}
runDeploy() {
checkRoot
gitCheckBranch ${GIT_BRANCH}
gitCheckDirty
# Task 1: Deploy Git as root in server
gitPull origin ${GIT_BRANCH}
setPermissions
# Task 2: Deploy as user in container (Docker)
startFunction start
startFunction exec-web ./start.sh deployDirect
# Task 2: Deploy as user in system (Switch from root to user)
#if [ -z "${RUN_AS_USERNAME}" ]; then echo 'Error variable RUN_AS_USERNAME is empty!'; exit 1; fi
#runuser -u ${RUN_AS_USERNAME} -- ./start.sh deployDirect
# Task 2: Deploy directly (Webhosting)
#startFunction deployDirect
}
# Deploy directly (In Docker container or Webhosting)
deployDirect() {
# For in Container or Webhosting (SSH-Key for private repositories needed)
#gitPull origin ${GIT_BRANCH}
composerInstall
symfonyUpdateDatabase
symfonyClearCache
}
loadEnvironmentVariables
if [ -z "${BIN_PHP}" ]; then BIN_PHP=$(findBinaryByWhich php); fi
if [ -z "${BIN_COMPOSER}" ]; then BIN_COMPOSER=$(findBinaryByWhich composer); fi
GIT_BRANCH="${GIT_BRANCH:-master}"
RUN_AS_USERNAME=${RUN_AS_USERNAME:-}
setDockerComposeFile
if [ ! -e "${DOCKER_COMPOSE_FILE}" ]; then
echo "Docker compose file '${DOCKER_COMPOSE_FILE}' not found!"; exit 1
fi
startFunction() {
case ${1} in
start)
startFunction pull && \
startFunction build && \
startFunction up
;;
up)
dockerComposeCmd up -d
;;
down)
dockerComposeCmd down --remove-orphans
;;
login-root)
dockerComposeCmd exec web bash
;;
login)
startFunction bash
;;
bash)
dockerComposeCmd exec -u ${APPLICATION_USER} web bash
;;
zsh)
dockerComposeCmd exec -u ${APPLICATION_USER} web zsh
;;
exec-web)
dockerComposeCmd exec -u ${APPLICATION_USER} web "${@:2}"
;;
deploy-images)
deployImages "${@:2}"
;;
deploy)
runDeploy
;;
deployDirect)
deployDirect
;;
*)
dockerComposeCmd "${@:1}"
;;
esac
}
startFunction "${@:1}"
exit $?