Repository navigation
Expand file tree
/
Copy path__common.sh
More file actions
executable file
·198 lines (164 loc) · 5.94 KB
/
Copy path__common.sh
File metadata and controls
executable file
·198 lines (164 loc) · 5.94 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
#!/usr/bin/env bash
# set -o errexit tells the shell to exit as soon as a command exits with non-zero status, i.e. fails
set -o errexit
set -o nounset
set -o pipefail
# :- is an shell operator. If FS_ENV is set and not the empty string, use FS_ENV, otherwise use dev
export FS_ENV=${FS_ENV:-dev}
# user identification number of the current user
if [[ "${DOCKER_HOST:-}" == *"podman"* ]] || command -v podman-compose >/dev/null 2>&1 || podman compose version >/dev/null 2>&1; then
CURRENT_USER="0:0"
else
CURRENT_USER=$(id -u):$(id -g)
fi
export CURRENT_USER
MYSQL_USERNAME=${MYSQL_USERNAME:-root}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-root}
# docker-compose arguments:
# -T : do not allocate a TTY: not necessary since we just execute a command
# but need not interactivity
# see: https://docs.docker.com/compose/reference/exec/
# sh -c "..." : run the command "..." in a shell
# BASH_SOURCE is an array with the filenames of the files that were called to get here
# so BASH_SOURCE[0] is the filename (with path) of this file
# different to $0 when this file is sourced with "." or source as in many of the scripts
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
function log-header() {
# print a log header, take one argument as the printed title
local text=$1;
echo
echo "============================================"
echo " $text"
echo "============================================"
}
function sql-query() {
local database=$1 query=$2;
"$dir"/docker-compose exec -T db sh -c "mariadb --password=$MYSQL_PASSWORD $database --execute=\"$query\""
}
function sql-file() {
local database=$1 filename=$2;
echo "Executing sql file $FS_ENV/$database $filename"
"$dir"/docker-compose exec -T db sh -c "mariadb --password=$MYSQL_PASSWORD $database < /app/$filename"
}
function sql-dump() {
"$dir"/docker-compose exec -T db mariadb-dump --password="$MYSQL_PASSWORD" foodsharing "$@"
}
function sql_dump_to_file() {
local filename=$1
"$dir"/docker-compose exec -T db sh -c "mariadb-dump --password=$MYSQL_PASSWORD foodsharing > /tmp/dump.sql"
"$dir"/docker-compose cp db:/tmp/dump.sql $filename
}
function exec-in-container() {
local container=$1; shift;
local command=$*;
"$dir"/docker-compose exec -T --user "$CURRENT_USER" "$container" sh -c "HOME=./ $command"
}
function exec-in-container-with-image-user() {
local container=$1; shift;
local command=$*;
"$dir"/docker-compose exec -T "$container" sh -c "HOME=./ $command"
}
function run-in-container() {
local container=$1; shift;
local command=$*;
"$dir"/docker-compose run --rm --no-deps --user "$CURRENT_USER" "$container" sh -c "HOME=./ $command"
}
function run-in-container-with-service-ports() {
local container=$1; shift;
local command=$*;
"$dir"/docker-compose run --rm --no-deps --user "$CURRENT_USER" --service-ports "$container" sh -c "HOME=./ $command"
}
function exec-in-container-asroot() {
local container=$1; shift;
local command=$*;
"$dir"/docker-compose exec --user root -T "$container" sh -c "$command"
}
function run-in-container-asroot() {
local container=$1; shift;
local command=$*;
# run : create a new container to execute the command
# --user root : set the user who executes the command
# --rm : remove the container after executing the command
# sh -c "..." : what is executed in the container: a shell that
# interprets "..."
"$dir"/docker-compose run --rm --no-deps --user root "$container" sh -c "$command"
}
function dropdb() {
local database=$1;
echo "Dropping database $FS_ENV/$database"
sql-query mysql "drop database if exists $database"
}
function createdb() {
local database=$1;
echo "Creating database $FS_ENV/$database"
sql-query mysql "\
create database if not exists $database; \
alter database $database character set = utf8mb4 collate = utf8mb4_unicode_ci; \
"
}
function recreatedb() {
local database=$1;
dropdb "$database"
createdb "$database"
}
function migratedb() {
echo "Migrating database for $FS_ENV"
local container=${1:-app}
exec-in-container "$container" vendor/bin/phinx migrate
}
function wait-for-mariadb() {
exec-in-container-asroot db "while ! mariadb --password=$MYSQL_PASSWORD --silent --execute='select 1' >/dev/null 2>&1; do sleep 1; done"
}
function wait-for-assets() {
start_time=$(date +%s)
while ! [ "$(ls -A assets)" ]; do
sleep 1
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
echo -ne "\rElapsed time: ${elapsed_time}s"
done
echo
}
function wait-for-api-dump() {
while [ $(wc -l < docs/data/api_dump.json) -lt 1000 ];
do
sleep 1;
echo -ne ".";
done
echo
}
function gitlab_start_section() {
local section_id=$1; shift
local message=$*
echo -e "\e[0Ksection_start:$(date +%s):section_$section_id\r\e[0K$message"
}
function gitlab_end_section() {
local section_id=$1
echo -e "\e[0Ksection_end:$(date +%s):section_$section_id\r\e[0K"
}
function run_codeception() {
exec-in-container-asroot app rm --recursive --force cache/.views-cache cache/di-cache.php
exec-in-container app vendor/bin/codecept run "$@"
}
function get-expected-container-count() {
"$dir"/docker-compose config --services | wc -l | tr -d '[:space:]'
}
function get-running-container-count() {
"$dir"/docker-compose ps --status running --format '{{.Name}}' 2>/dev/null | wc -l | tr -d '[:space:]' || echo "0"
}
# Services that are optional (may fail or not be running)
OPTIONAL_SERVICES="docs mailqueuerunner"
function is-environment-ready() {
local expected_count running_count optional_count
expected_count=$(get-expected-container-count)
# Count optional services that are defined
optional_count=0
for service in $OPTIONAL_SERVICES; do
if "$dir"/docker-compose config --services 2>/dev/null | grep -q "^${service}$"; then
((optional_count++)) || true
fi
done
running_count=$(get-running-container-count)
# Environment is ready if running >= expected - optional
[[ "$running_count" -ge $((expected_count - optional_count)) ]]
}