-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker-compose-oroplatform
executable file
·293 lines (251 loc) · 12.5 KB
/
docker-compose-oroplatform
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
#!/bin/bash
set -e
#set -x
DIR=$(dirname $0)
export PWD=$(pwd)
find-up () {
path=${2-$PWD}
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
if [[ -z $COMPOSE_PROJECT_APPDIR ]]; then
export COMPOSE_PROJECT_APPDIR=$(find-up composer.json "$PWD/www")
fi
if [[ -z $COMPOSE_PROJECT_APPDIR ]]; then
if [ -z "$(ls -A $PWD)" ]; then
export COMPOSE_PROJECT_APPDIR="$PWD"
else
echo "Application root folder not found, and current directory is not empty, terminating..."
exit 1
fi
fi
if [[ ! -d "$COMPOSE_PROJECT_APPDIR" ]]; then
mkdir -p "$COMPOSE_PROJECT_APPDIR"
fi
cd "$COMPOSE_PROJECT_APPDIR"
if [[ -f "$COMPOSE_PROJECT_APPDIR/.dockenv" ]]; then
set -o allexport
source "$COMPOSE_PROJECT_APPDIR/.dockenv"
set +o allexport
fi
if [[ -f "$COMPOSE_PROJECT_APPDIR/.dockerenv" ]]; then
set -o allexport
source "$COMPOSE_PROJECT_APPDIR/.dockerenv"
set +o allexport
fi
if [[ -f "$COMPOSE_PROJECT_APPDIR/.env" ]]; then
set -o allexport
source "$COMPOSE_PROJECT_APPDIR/.env"
set +o allexport
fi
if [[ -z $COMPOSE_PROJECT_COMPOSER_AUTH ]] && [[ -z $COMPOSER_AUTH ]] && which php > /dev/null 2>&1 && which composer > /dev/null 2>&1; then
COMPOSER_AUTH_JSON=$(php -d display_startup_errors=0 $(which composer) config --working-dir=/tmp --no-interaction --global home 2> /dev/null)/auth.json
if [[ -f "${COMPOSER_AUTH_JSON}" ]]; then
COMPOSE_PROJECT_COMPOSER_AUTH=$(cat "${COMPOSER_AUTH_JSON}")
fi
fi
export COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME-$(basename $(pwd))}
export COMPOSE_PROJECT_PORT_PREFIX=${COMPOSE_PROJECT_PORT_PREFIX-"301"}
export COMPOSE_PROJECT_PORT_HTTP="${COMPOSE_PROJECT_PORT_PREFIX}80"
export COMPOSE_PROJECT_PORT_XHGUI="${COMPOSE_PROJECT_PORT_PREFIX}81"
export COMPOSE_PROJECT_PORT_MYSQL="${COMPOSE_PROJECT_PORT_PREFIX}06"
export COMPOSE_PROJECT_PORT_PGSQL="${COMPOSE_PROJECT_PORT_PREFIX}32"
export COMPOSE_PROJECT_PORT_ELASTICSEARCH="${COMPOSE_PROJECT_PORT_PREFIX}92"
export COMPOSE_PROJECT_PORT_MAIL_WEBGUI="${COMPOSE_PROJECT_PORT_PREFIX}25"
export COMPOSE_PROJECT_COMPOSER_AUTH=${COMPOSE_PROJECT_COMPOSER_AUTH-${COMPOSER_AUTH-""}}
export COMPOSE_PROJECT_CONFIG_DIR="$HOME/.$(basename $0)/${COMPOSE_PROJECT_NAME}"
if [[ ! -d "${COMPOSE_PROJECT_CONFIG_DIR}" ]];then
mkdir -p "${COMPOSE_PROJECT_CONFIG_DIR}"
fi
rsync -r "${DIR}/" "${COMPOSE_PROJECT_CONFIG_DIR}/"
docker info > /dev/null
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN-$(which docker) compose}";
DOCKER_COMPOSE_VERSION=$($DOCKER_COMPOSE_BIN version | grep '[0-9]\+\.[0-9]\+\.[0-9]\+' -o | awk -F. '{ print $1 }')
if [[ -f "${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose.yml" ]]; then
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN} -f ${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose.yml"
fi
if docker system info --format '{{.SecurityOptions}}' | grep "name=rootless" > /dev/null 2>&1; then
# Use root user in the rootless mode
export COMPOSE_PROJECT_USER_NAME="root"
else
export COMPOSE_PROJECT_USER_NAME="developer"
fi
if [[ -z $ORO_DB_DRIVER ]] && [[ -f "$COMPOSE_PROJECT_APPDIR/config/parameters.yml" ]]; then
export ORO_DB_DRIVER=$(cat "$COMPOSE_PROJECT_APPDIR/config/parameters.yml" | grep 'database_driver:' | awk -F: '{ print $2 }')
fi
if [[ -z $ORO_DB_DRIVER ]] && [[ -f "$COMPOSE_PROJECT_APPDIR/.env-app.local" ]]; then
export ORO_DB_DRIVER=$(cat "$COMPOSE_PROJECT_APPDIR/.env-app.local" | grep 'ORO_DB_URL=' | awk -F= '{ print $2 }' | awk -F: '{ print $1 }')
fi
if [[ -z $ORO_DB_DRIVER ]] && [[ -f "$COMPOSE_PROJECT_APPDIR/.env-app" ]]; then
export ORO_DB_DRIVER=$(cat "$COMPOSE_PROJECT_APPDIR/.env-app" | grep 'ORO_DB_URL=' | awk -F= '{ print $2 }' | awk -F: '{ print $1 }')
fi
if [[ $ORO_DB_DRIVER == "pdo_pgsql" ]] || [[ $ORO_DB_DRIVER == "pdo_mysql" ]] || [[ $ORO_DB_DRIVER == "postgres" ]] || [[ $ORO_DB_DRIVER == "mysql" ]] ;then
if [[ $ORO_DB_DRIVER == "pdo_pgsql" ]] || [[ $ORO_DB_DRIVER == "postgres" ]];then
export COMPOSE_PROJECT_DATABASE_PORT=${COMPOSE_PROJECT_DATABASE_PORT:-5432}
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN} -f ${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose-pgsql.yml"
elif [[ $ORO_DB_DRIVER == "pdo_mysql" ]] || [[ $ORO_DB_DRIVER == "mysql" ]];then
export COMPOSE_PROJECT_DATABASE_PORT=${COMPOSE_PROJECT_DATABASE_PORT:-3306}
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN} -f ${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose-mysql.yml"
fi
if ${DOCKER_COMPOSE_BIN} ps --services | grep database > /dev/null; then
SERVICE_DATABASE_ID=$(${DOCKER_COMPOSE_BIN} ps -q database)
if [[ "" != $SERVICE_DATABASE_ID ]] && docker inspect -f '{{ .Config.Image }}' "$SERVICE_DATABASE_ID" | grep 'busybox'; then
${DOCKER_COMPOSE_BIN} stop database
${DOCKER_COMPOSE_BIN} rm -f database
fi
fi
else
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN} -f ${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose-dummy.yml"
fi
if [[ -f "$COMPOSE_PROJECT_APPDIR/.docker-compose.user.yml" ]]; then
>&2 echo "==> Additional docker compose config will be used: $COMPOSE_PROJECT_APPDIR/.docker-compose.user.yml"
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN} -f $COMPOSE_PROJECT_APPDIR/.docker-compose.user.yml"
fi
if [[ -f "$COMPOSE_PROJECT_APPDIR/.php-version" ]] && [[ -z $COMPOSE_PROJECT_PHP_VERSION ]]; then
export COMPOSE_PROJECT_PHP_VERSION=$(cat $COMPOSE_PROJECT_APPDIR/.php-version | head -n1)
>&2 echo "==> Found .php-version with following version: $COMPOSE_PROJECT_PHP_VERSION"
fi
if [[ -f "$COMPOSE_PROJECT_APPDIR/.phprc" ]] && [[ -z $COMPOSE_PROJECT_PHP_VERSION ]]; then
export COMPOSE_PROJECT_PHP_VERSION=$(cat $COMPOSE_PROJECT_APPDIR/.phprc | head -n1)
>&2 echo "==> Found .phprc with following version: $COMPOSE_PROJECT_PHP_VERSION"
fi
if [[ -f "$COMPOSE_PROJECT_APPDIR/.nvmrc" ]] && [[ -z $COMPOSE_PROJECT_NODE_VERSION ]]; then
export COMPOSE_PROJECT_NODE_VERSION=$(cat $COMPOSE_PROJECT_APPDIR/.nvmrc | head -n1)
>&2 echo "==> Found .nvmrc with following version: $COMPOSE_PROJECT_NODE_VERSION"
fi
if [[ "$OSTYPE" == "darwin"* ]]; then
export COMPOSE_PROJECT_MODE="mutagen"
fi
if [[ $COMPOSE_PROJECT_MODE == "mutagen" ]] && [[ -f "${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose-mutagen.yml" ]]; then
DOCKER_COMPOSE_BIN="${DOCKER_COMPOSE_BIN} -f ${COMPOSE_PROJECT_CONFIG_DIR}/docker-compose-mutagen.yml"
MUTAGEN_SESSION_NAME=$(echo ${COMPOSE_PROJECT_NAME}-appcode | awk '{print tolower($0)}' | sed 's/[^0-9a-z]/-/g')
WAIT_SYNC="1"
if docker volume ls | grep "^${COMPOSE_PROJECT_NAME}_appcode$" > /dev/null 2>&1; then
if [[ $(docker volume inspect -f '{{ .Options.type }}' ${COMPOSE_PROJECT_NAME}_appcode) == "nfs" ]]; then
docker volume rm ${COMPOSE_PROJECT_NAME}_appcode
fi
fi
if [[ $1 == "down" ]]; then
if mutagen sync list | grep -i "Name: ${MUTAGEN_SESSION_NAME}" > /dev/null 2>&1; then
mutagen sync terminate "${MUTAGEN_SESSION_NAME}"
fi
WAIT_SYNC=""
elif echo $1 | grep -v -i '^build\|pull' > /dev/null; then
if [[ "0" -eq $(${DOCKER_COMPOSE_BIN} ps -q mutagen 2> /dev/null | wc -l > /dev/null) ]]; then
${DOCKER_COMPOSE_BIN} up -d mutagen
fi
DOCKER_CONTAINER_NAME_MUTAGEN=$(docker inspect -f '{{.Name}}' $(${DOCKER_COMPOSE_BIN} ps -q mutagen) | cut -c2-)
if ! mutagen sync list | grep -i "Name: ${MUTAGEN_SESSION_NAME}" > /dev/null 2>&1; then
mutagen sync create --sync-mode=two-way-resolved \
--scan-mode=accelerated \
--watch-mode-alpha=portable \
--watch-mode-beta=force-poll \
--watch-polling-interval-beta=30 \
--ignore=".idea" \
--ignore=".history" \
--ignore=".mysql.initdb.d" \
--ignore=".psql.initdb.d" \
--ignore=".git" \
--ignore-vcs \
--symlink-mode=ignore \
--name=${MUTAGEN_SESSION_NAME} \
"$COMPOSE_PROJECT_APPDIR" docker://${COMPOSE_PROJECT_USER_NAME}@${DOCKER_CONTAINER_NAME_MUTAGEN}/var/www
else
WAIT_SYNC=""
fi
if [[ $WAIT_SYNC == "1" ]]; then
until mutagen sync list "${MUTAGEN_SESSION_NAME}" | grep 'Status:' | grep 'Watching for changes' > /dev/null 2>&1; do
if which tput > /dev/null 2>&1; then
tput cuu 1 && tput el
fi
mutagen sync list "${MUTAGEN_SESSION_NAME}" | grep 'Status:' | echo $(awk -F: '{ print $2 }')
sleep 3
done
fi
fi
fi
if [[ $DOCKER_COMPOSE_VERSION -gt 1 ]]; then
DOCKER_COMPOSE_RUN_CMD="${DOCKER_COMPOSE_BIN} run -i --rm"
else
DOCKER_COMPOSE_RUN_CMD="${DOCKER_COMPOSE_BIN} run --rm"
fi
if docker system info --format '{{.SecurityOptions}}' | grep "name=rootless" > /dev/null; then
DOCKER_COMPOSE_RUN_CMD="${DOCKER_COMPOSE_RUN_CMD} --user root"
fi
if [[ $1 == "up" ]]; then
printf "\033[0;37m[${COMPOSE_PROJECT_NAME}] Application URL: http://localhost:${COMPOSE_PROJECT_PORT_HTTP}/\033[0m\n"
printf "\033[0;37m[${COMPOSE_PROJECT_NAME}] XHGUI URL: http://localhost:${COMPOSE_PROJECT_PORT_XHGUI}/\033[0m\n"
printf "\033[0;37m[${COMPOSE_PROJECT_NAME}] Mailhog URL: http://localhost:${COMPOSE_PROJECT_PORT_MAIL_WEBGUI}/\033[0m\n"
printf "\033[0;37m[${COMPOSE_PROJECT_NAME}] Elasticsearch URL: http://localhost:${COMPOSE_PROJECT_PORT_ELASTICSEARCH}/\033[0m\n"
printf "\033[0;37m[${COMPOSE_PROJECT_NAME}] MySql Address: 127.0.0.1:${COMPOSE_PROJECT_PORT_MYSQL}\033[0m\n"
printf "\033[0;37m[${COMPOSE_PROJECT_NAME}] PgSql Address: 127.0.0.1:${COMPOSE_PROJECT_PORT_PGSQL}\033[0m\n"
fi
if echo $1 | grep -i 'import' | grep -i 'db\|database' > /dev/null; then
if [[ -z $2 ]]; then
echo "Data file was not provided"
exit 1
fi
if [[ -r $2 ]]; then
DB_DUMP=$(realpath $2)
elif [[ -r "$COMPOSE_PROJECT_APPDIR/$2" ]]; then
DB_DUMP=$(realpath "$COMPOSE_PROJECT_APPDIR/$2")
fi
if [[ -z $DB_DUMP ]] || [[ ! -r $DB_DUMP ]]; then
echo "Data file '$2' does not readable"
exit 1
fi
DB_DUMP_BASENAME=$(echo "${DB_DUMP##*/}")
if [[ $ORO_DB_DRIVER == "pdo_pgsql" ]] || [[ $ORO_DB_DRIVER == "postgres" ]];then
DB_IMPORT_CMD="PGPASSWORD=\$DATABASE_PASSWORD psql --set ON_ERROR_STOP=on -h \$DATABASE_HOST -p \$DATABASE_PORT -U \$DATABASE_USER -d \$DATABASE_NAME -1 >/dev/null"
elif [[ $ORO_DB_DRIVER == "pdo_mysql" ]] || [[ $ORO_DB_DRIVER == "mysql" ]];then
DB_IMPORT_CMD="sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/DEFINER=CURRENT_USER \*/' | MYSQL_PWD=\$DATABASE_PASSWORD mysql -h\$DATABASE_HOST -P\$DATABASE_PORT -u\$DATABASE_USER \$DATABASE_NAME"
fi
if echo ${DB_DUMP_BASENAME} | grep -i 'sql\.gz$' > /dev/null; then
DB_IMPORT_CMD="gunzip | ${DB_IMPORT_CMD}"
fi
DB_IMPORT_CMD="cat /${DB_DUMP_BASENAME} | ${DB_IMPORT_CMD}"
echo "Starting import of the database dump..."
exec ${DOCKER_COMPOSE_RUN_CMD} -v "${DB_DUMP}:/${DB_DUMP_BASENAME}" database-cli bash -c "$DB_IMPORT_CMD"
fi
if echo $1 | grep -i 'export\|dump' | grep -i 'db\|database' > /dev/null; then
DB_DUMP=$COMPOSE_PROJECT_APPDIR/database-$(date +'%Y%m%d%H%M%S').sql.gz
DB_DUMP_BASENAME=$(echo "${DB_DUMP##*/}")
touch "${DB_DUMP}"
if [[ $ORO_DB_DRIVER == "pdo_pgsql" ]] || [[ $ORO_DB_DRIVER == "postgres" ]];then
DB_EXPORT_CMD="PGPASSWORD=\$DATABASE_PASSWORD pg_dump -Fp --clean --if-exists -h \$DATABASE_HOST -p \$DATABASE_PORT -U \$DATABASE_USER -d \$DATABASE_NAME"
elif [[ $ORO_DB_DRIVER == "pdo_mysql" ]] || [[ $ORO_DB_DRIVER == "mysql" ]];then
DB_EXPORT_CMD="MYSQL_PWD=\$DATABASE_PASSWORD mysqldump --no-tablespaces --column-statistics=0 --set-gtid-purged=OFF --quick --max-allowed-packet=16M --disable-keys --hex-blob --no-autocommit --insert-ignore --skip-lock-tables --single-transaction -h\$DATABASE_HOST -P\$DATABASE_PORT -u\$DATABASE_USER \$DATABASE_NAME | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/DEFINER=CURRENT_USER \*/'"
fi
echo "/* Starting export of the database... */"
echo "/* Dumping database to: $DB_DUMP */"
exec ${DOCKER_COMPOSE_RUN_CMD} -v "${DB_DUMP}:/${DB_DUMP_BASENAME}" database-cli bash -c "$DB_EXPORT_CMD | gzip > /${DB_DUMP_BASENAME}"
fi
if echo $1 | grep -i '^mysql' > /dev/null; then
for i in "$@"; do
i="${i//\\/\\\\}"
i="${i//$/\\$}"
C="$C \"${i//\"/\\\"}\""
done
MYSQL_CMD="MYSQL_PWD=\$DATABASE_PASSWORD mysql -h\$DATABASE_HOST -P\$DATABASE_PORT -u\$DATABASE_USER \$DATABASE_NAME"
exec ${DOCKER_COMPOSE_RUN_CMD} database-cli bash -c "$MYSQL_CMD"
fi
if echo $1 | grep -i '^psql' > /dev/null; then
for i in "$@"; do
i="${i//\\/\\\\}"
i="${i//$/\\$}"
C="$C \"${i//\"/\\\"}\""
done
PSQL_CMD="PGPASSWORD=\$DATABASE_PASSWORD psql -h \$DATABASE_HOST -p \$DATABASE_PORT -U \$DATABASE_USER -d \$DATABASE_NAME"
exec ${DOCKER_COMPOSE_RUN_CMD} database-cli bash -c "$PSQL_CMD"
fi
if echo $1 | grep -i '^composer\|bin/\|npm\|php\|node\|yarn\|bash\|symfony' > /dev/null; then
for i in "$@"; do
i="${i//\\/\\\\}"
i="${i//$/\\$}"
C="$C \"${i//\"/\\\"}\""
done
exec ${DOCKER_COMPOSE_RUN_CMD} cli bash -c "exec $C"
fi
exec ${DOCKER_COMPOSE_BIN} "$@"