Repository navigation
Expand file tree
/
Copy pathdocker-compose-build.sh
More file actions
executable file
·112 lines (93 loc) · 3.2 KB
/
Copy pathdocker-compose-build.sh
File metadata and controls
executable file
·112 lines (93 loc) · 3.2 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
#!/usr/bin/env bash
set -euo pipefail
DOCKER_COMPOSE_FILE="docker-compose-build.yml"
EXPECTED_OWNER="1001:1001"
BACKEND_CHECK_ATTEMPTS=60
BACKEND_CHECK_DELAY_SECONDS=10
UNKNOWN_ARGUMENTS=()
while (($#)); do
case "$1" in
-f)
shift
if (($# == 0)); then
printf 'Argument -f requires a compose file path.\n' >&2
exit 1
fi
DOCKER_COMPOSE_FILE="$1"
;;
*)
UNKNOWN_ARGUMENTS+=("$1")
;;
esac
shift
done
if ((${#UNKNOWN_ARGUMENTS[@]} > 0)); then
printf 'Unknown argument(s): %s. Supported arguments: -f <compose-file>.\n' "${UNKNOWN_ARGUMENTS[*]}" >&2
exit 1
fi
prepare_meta_file() {
VERSION=$(git describe --tags --always)
BUILD=$(git rev-parse --short HEAD)
echo "CISO_ASSISTANT_VERSION=${VERSION}" >.meta
echo "CISO_ASSISTANT_BUILD=${BUILD}" >>.meta
cp .meta ./backend/ciso_assistant/.meta
cp .meta ./backend/.meta
}
# On macOS, GNU stat (-c) is not available by default. We use this as a simple
# Linux detector and skip chown on macOS (Docker Desktop emulates ownership).
is_linux_gnu_stat() {
stat -c '%u:%g' . >/dev/null 2>&1
}
get_owner_linux() {
stat -c '%u:%g' "$1"
}
wait_for_backend() {
for i in $(seq 1 "$BACKEND_CHECK_ATTEMPTS"); do
if docker compose -f "${DOCKER_COMPOSE_FILE}" exec -T backend curl --fail --silent http://localhost:8000/api/health/ >/dev/null 2>&1; then
echo "Backend is ready."
return
fi
if [ "$i" -eq "$BACKEND_CHECK_ATTEMPTS" ]; then
timeout_seconds=$((BACKEND_CHECK_ATTEMPTS * BACKEND_CHECK_DELAY_SECONDS))
echo "Backend did not become ready within ${timeout_seconds}s. Recent backend logs:"
docker compose -f "${DOCKER_COMPOSE_FILE}" logs --tail=50 backend
exit 1
fi
sleep "$BACKEND_CHECK_DELAY_SECONDS"
done
}
# Enable BuildKit for faster builds
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
echo "Using Docker Compose file: \"${DOCKER_COMPOSE_FILE}\""
# Check if database already exists
if [ -f db/ciso-assistant.sqlite3 ]; then
echo "The database seems already created."
echo "For successive runs, you can now use 'docker compose -f ${DOCKER_COMPOSE_FILE} up'."
else
prepare_meta_file
echo "Building containers..."
docker compose -f "${DOCKER_COMPOSE_FILE}" build --pull
mkdir -p ./db
if is_linux_gnu_stat; then
DB_OWNER="$(get_owner_linux ./db)"
if [ "$DB_OWNER" != "$EXPECTED_OWNER" ]; then
echo "Fixing ownership of ./db (was $DB_OWNER, expected $EXPECTED_OWNER)"
if ! chown -R "$EXPECTED_OWNER" ./db 2>/dev/null; then
echo "chown failed, retrying with sudo..."
sudo chown -R "$EXPECTED_OWNER" ./db
fi
fi
else
echo "Non-Linux (no GNU stat detected): skipping ownership fix for ./db"
fi
echo "Starting services..."
docker compose -f "${DOCKER_COMPOSE_FILE}" up -d
echo "Waiting for CISO Assistant backend to be ready, please wait ..."
wait_for_backend
echo "Initialize your superuser account..."
docker compose -f "${DOCKER_COMPOSE_FILE}" exec backend python manage.py createsuperuser
echo "🚀 CISO Assistant is ready!"
echo "Connect to CISO Assistant on https://localhost:8443"
echo "For successive runs, you can now use 'docker compose -f ${DOCKER_COMPOSE_FILE} up'."
fi