-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdev-worker
More file actions
236 lines (214 loc) · 6.42 KB
/
Copy pathdev-worker
File metadata and controls
236 lines (214 loc) · 6.42 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
#!/bin/sh
# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
set -eu
usage() {
cat <<'EOF'
Usage: sh ./dev-worker <worker-id> <command> [arguments...]
Commands:
up Start the isolated worker and wait for Nextcloud installation
exec <command...> Run a command in the worker's Nextcloud container
logs Print worker logs
config Render the resolved Compose configuration
status Run occ status in the worker
urls Print the worker's Nextcloud and Mailpit URLs
db-exec <command...> Run a command in the selected database container
destroy Stop the worker and remove only its mutable state
Environment:
DB_TYPE=sqlite|mysql|mariadb|pgsql Database backend
DB_HOST=<hostname> Connection hostname (defaults to DB_TYPE)
MARIADB_VERSION=<version> MariaDB series (default: 10.11)
DB_SQL_MODE=<modes> Optional global SQL mode for MySQL-compatible backends
PHP_VERSION=<version> Existing PHP image selector
VERSION_NEXTCLOUD=<ref> Existing Nextcloud ref selector
NEXTCLOUD_HOST=<hostname> Public hostname (default: <project>.localhost)
NEXTCLOUD_PROTOCOL=http|https Public protocol (default: https)
NCDD_WORKSPACE=<path> Optional directory mounted as /var/www/html/apps-extra
WORKER_READY_TIMEOUT=180 Seconds to wait for a clean installation
EOF
}
[ "$#" -ge 2 ] || {
usage >&2
exit 2
}
worker_id="$1"
command="$2"
shift 2
case "$worker_id" in
*[!a-z0-9_-]*|'')
echo "Invalid worker id: $worker_id" >&2
exit 2
;;
esac
case "$worker_id" in
[a-z0-9]*) ;;
*)
echo "Worker id must start with a lowercase letter or digit: $worker_id" >&2
exit 2
;;
esac
repo_root="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
db_type="${DB_TYPE:-mysql}"
workspace="${NCDD_WORKSPACE:-}"
if [ -n "$workspace" ]; then
[ -d "$workspace" ] || {
echo "NCDD_WORKSPACE is not a directory: $workspace" >&2
exit 2
}
workspace="$(CDPATH= cd -- "$workspace" && pwd -P)"
export NCDD_WORKSPACE="$workspace"
fi
case "$db_type" in
sqlite|mysql|mariadb|pgsql) ;;
*)
echo "Unsupported DB_TYPE: $db_type" >&2
exit 2
;;
esac
worker_root="$repo_root/.workers/$worker_id"
worker_volumes_dir="$worker_root/volumes"
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-ncdev-$worker_id}"
export WORKER_VOLUMES_DIR="$worker_volumes_dir"
export DB_TYPE="$db_type"
export DB_HOST="${DB_HOST:-$db_type}"
if [ "$db_type" = "sqlite" ]; then
export DB_SERVICE_TYPE=mysql
export DB_DRIVER=sqlite
elif [ "$db_type" = "mariadb" ]; then
mariadb_version="${MARIADB_VERSION:-10.11}"
mariadb_suffix="$(printf '%s' "$mariadb_version" | tr -d '.')"
case "$mariadb_suffix" in
*[!0-9]*|'')
echo "Invalid MARIADB_VERSION: $mariadb_version" >&2
exit 2
;;
esac
mariadb_service="mariadb$mariadb_suffix"
if ! grep -q "^ $mariadb_service:" "$repo_root/.docker/database-services.yml"; then
echo "Unsupported MARIADB_VERSION: $mariadb_version" >&2
exit 2
fi
export DB_SERVICE_TYPE="$mariadb_service"
export DB_DRIVER=mysql
else
export DB_SERVICE_TYPE="${DB_SERVICE_TYPE:-$db_type}"
fi
compose() {
if [ -n "$workspace" ]; then
docker compose --project-directory "$repo_root" \
--file "$repo_root/docker-compose.yml" \
--file "$repo_root/.docker/workspace.yml" "$@"
else
docker compose --project-directory "$repo_root" \
--file "$repo_root/docker-compose.yml" "$@"
fi
}
wait_for_mariadb() {
timeout="${WORKER_READY_TIMEOUT:-180}"
started="$(date +%s)"
while :; do
if compose exec -T database mariadb-admin ping -h 127.0.0.1 -uroot -p"${MYSQL_ROOT_PASSWORD:-root}" --silent >/dev/null 2>&1; then
return 0
fi
now="$(date +%s)"
if [ $((now - started)) -ge "$timeout" ]; then
echo "MariaDB for worker $worker_id did not become ready within ${timeout}s." >&2
compose logs --no-color database >&2 || true
return 1
fi
sleep 1
done
}
wait_until_ready() {
timeout="${WORKER_READY_TIMEOUT:-180}"
started="$(date +%s)"
while :; do
if compose exec -T nextcloud occ status 2>/dev/null | grep -q 'installed: true'; then
return 0
fi
now="$(date +%s)"
if [ $((now - started)) -ge "$timeout" ]; then
echo "Worker $worker_id did not become ready within ${timeout}s." >&2
compose logs --no-color nextcloud >&2 || true
return 1
fi
sleep 2
done
}
case "$command" in
up)
mkdir -p "$worker_volumes_dir"
if [ "$db_type" = "sqlite" ]; then
compose up -d nextcloud nginx proxy-coordinator mailpit redis
elif [ "$db_type" = "mariadb" ]; then
compose up -d database
wait_for_mariadb
if [ -n "${DB_SQL_MODE:-}" ]; then
case "${DB_SQL_MODE}" in
*[!A-Z0-9_,]*) echo "DB_SQL_MODE contains unsupported characters" >&2; exit 2 ;;
esac
compose exec -T database mariadb -uroot -p"${MYSQL_ROOT_PASSWORD:-root}" -e "SET GLOBAL sql_mode='${DB_SQL_MODE}'"
fi
compose up -d
else
compose up -d
fi
wait_until_ready
;;
exec)
[ "$#" -gt 0 ] || {
echo "exec requires a command" >&2
exit 2
}
compose exec -T nextcloud "$@"
;;
logs)
compose logs --no-color "$@"
;;
config)
mkdir -p "$worker_volumes_dir"
compose config "$@"
;;
status)
compose exec -T nextcloud occ status
;;
urls)
printf 'Nextcloud: %s://%s\n' "${NEXTCLOUD_PROTOCOL:-https}" "${NEXTCLOUD_HOST:-$COMPOSE_PROJECT_NAME.localhost}"
printf 'Mailpit: https://%s-mailpit.localhost\n' "$COMPOSE_PROJECT_NAME"
;;
db-exec)
[ "$db_type" != "sqlite" ] || {
echo "SQLite workers do not have a database container" >&2
exit 3
}
[ "$#" -gt 0 ] || {
echo "db-exec requires a command" >&2
exit 2
}
compose exec -T database "$@"
;;
destroy)
compose down --remove-orphans
# Mutable bind mounts contain files created by container UIDs. Clean the
# worker root through the existing Nextcloud image as root instead of
# relying on host ownership. The optional workspace is outside worker_root
# and is never removed.
if [ -d "$worker_root" ]; then
docker compose --project-directory "$repo_root" --file "$repo_root/docker-compose.yml" \
run --rm --no-deps -u 0 --entrypoint sh -v "$worker_root:/worker" nextcloud \
-c 'find /worker -mindepth 1 -exec rm -rf -- {} +' >/dev/null 2>&1 || true
fi
case "$worker_root" in
"$repo_root"/.workers/"$worker_id"|"$repo_root"/.workers/"$worker_id"/) ;;
*)
echo "Refusing to remove unexpected worker path: $worker_root" >&2
exit 4
;;
esac
rm -rf -- "$worker_root"
;;
*)
usage >&2
exit 2
;;
esac