-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate-docker.sh
More file actions
executable file
·105 lines (87 loc) · 4 KB
/
update-docker.sh
File metadata and controls
executable file
·105 lines (87 loc) · 4 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
#!/bin/bash
cd "$(dirname "$0")"
dockerBin=$(/usr/bin/which docker)
cronMode=false
if [ "$1" == "--cron" ]; then
cronMode=true
fi
# Pull latest changes to docker-compose.yml and configuration files (manual only)
if [ "$cronMode" = false ]; then
git pull
fi
# Pull new images
$dockerBin compose pull
# Start containers. The php entrypoint will clone/update fewohbee via git.
$dockerBin compose stop
$dockerBin compose up --force-recreate -d
if [ "$cronMode" = false ]; then
# Wait for fewohbee to finish setup (git clone/pull + composer + migrations)
echo "Waiting for fewohbee to finish setup ..."
until [ "$($dockerBin compose exec -T php /bin/sh -c 'cat /firstrun' 2>/dev/null)" == "1" ]; do
echo " still waiting ..."
sleep 10
done
# Sync new environment variables from the now-running container into .env (manual only)
echo "Checking for new environment variables ..."
containerEnvDist=$($dockerBin compose exec --user www-data -T php /bin/sh -c "cat fewohbee/.env.dist" 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$containerEnvDist" ]; then
echo "Warning: Could not read .env.dist from container. Skipping env sync."
else
addedVars=0
commentBuffer=""
while IFS= read -r line; do
# Accumulate comments and empty lines to carry them along with their variable
if [[ "$line" =~ ^[[:space:]]*# ]] || [[ -z "${line// }" ]]; then
[ -n "$commentBuffer" ] && commentBuffer+=$'\n'
commentBuffer+="$line"
continue
fi
# Extract variable name (everything before the first =)
varName="${line%%=*}"
if [[ -z "$varName" ]]; then
commentBuffer=""
continue
fi
# DB_SERVER_VERSION is hardcoded in docker-compose.yml, skip it
if [[ "$varName" == "DB_SERVER_VERSION" ]]; then
commentBuffer=""
continue
fi
# Add if not already present in .env
if ! grep -q "^${varName}=" .env; then
if [ $addedVars -eq 0 ]; then
printf "\n# Variables added by update-docker.sh on %s\n" "$(date '+%Y-%m-%d')" >> .env
fi
# Write accumulated comments first, then the variable
if [ -n "$commentBuffer" ]; then
printf "\n%s\n" "$commentBuffer" >> .env
fi
printf "%s\n" "$line" >> .env
# Also add the variable to the environment: sections in both compose files
# (inserted before the # new-vars-marker comment, which appears in php and cron services)
for composeFile in docker-compose.yml docker-compose.no-ssl.yml; do
if [ -f "$composeFile" ] && grep -q "# new-vars-marker" "$composeFile" \
&& ! grep -q -- "- ${varName}=" "$composeFile"; then
tmpfile=$(mktemp)
awk -v varline=" - ${varName}=\${${varName}}" \
'/# new-vars-marker/ { print varline } { print }' \
"$composeFile" > "$tmpfile" && mv "$tmpfile" "$composeFile"
fi
done
echo " Added: $varName"
addedVars=$((addedVars + 1))
fi
# Reset comment buffer after each variable (whether added or already present)
commentBuffer=""
done <<< "$containerEnvDist"
if [ $addedVars -gt 0 ]; then
echo "$addedVars new variable(s) added to .env and docker-compose.yml / docker-compose.no-ssl.yml."
echo "Please review the new variables in .env and adjust values if needed."
echo "Restarting php and cron containers to apply new environment variables ..."
$dockerBin compose up --force-recreate -d php cron
else
echo "No new environment variables found."
fi
fi
fi
docker image prune -f