-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-update.sh
More file actions
executable file
·223 lines (198 loc) · 7.48 KB
/
Copy pathauto-update.sh
File metadata and controls
executable file
·223 lines (198 loc) · 7.48 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
#!/bin/bash
set -euo pipefail
# Color variables
COLOR_DEFAULT='\033[0m'
COLOR_SUCCESS='\033[0;32m'
COLOR_ERROR='\033[0;31m'
COLOR_WARNING='\033[1;33m'
COLOR_INFO='\033[0;34m'
# Variables
REPOSITORY_ORGANIZATION="Combodo"
REPOSITORY_NAME="docker-environment"
RELEASE_ZIP_URL="https://github.com/${REPOSITORY_ORGANIZATION}/${REPOSITORY_NAME}/archive/refs/tags"
TMP_CURRENT_FOLDER="/tmp/docker-environment-update"
# Save current folder path so we can return to it later
CURRENT_FOLDER="$(pwd)"
LOG_FILE="${CURRENT_FOLDER}/auto-update.log"
# Helpers
print_default() {
echo -e "${COLOR_DEFAULT}$1${COLOR_DEFAULT}"
}
print_inline_default() {
echo -n -e "${COLOR_DEFAULT}$1${COLOR_DEFAULT}"
}
print_success() {
echo -e "${COLOR_SUCCESS}$1${COLOR_DEFAULT}"
}
print_error() {
echo -e "${COLOR_ERROR}$1${COLOR_DEFAULT}"
}
print_warning() {
echo -e "${COLOR_WARNING}$1${COLOR_DEFAULT}"
}
print_inline_warning() {
echo -n -e "${COLOR_WARNING}$1${COLOR_DEFAULT}"
}
print_info() {
echo -e "${COLOR_INFO}$1${COLOR_DEFAULT}"
}
# Helper to run a command and log its output (stdout+stderr) for debug purposes
# Usage: `run_and_log <mode> <command> [args...]` where <mode> is 'w' to overwrite the log file or 'a' to append
run_and_log() {
local mode="$1"
shift
if [ "$mode" = "w" ]; then
"$@" > "$LOG_FILE" 2>&1
else
"$@" >> "$LOG_FILE" 2>&1
fi
}
# Init log file
run_and_log w echo "Auto-update launched on $(date '+%Y-%m-%d %H:%M:%S')"
# Load current environment variables
if [ -f .env.local ]; then
set -a
source .env.local
set +a
print_info "Environment variables loaded from .env.local."
elif [ -f .env ]; then
set -a
source .env
set +a
print_info "Environment variables loaded from .env."
else
print_error "Neither .env.local nor .env file could be found, update can't proceed."
fi
print_inline_default "Do you want to proceed? (y/n) [y] "
read -r proceed_answer
proceed_answer=${proceed_answer:-y}
if [[ "$proceed_answer" != "y" ]]; then
print_error "Update aborted."
exit 1
fi
print_default ""
# Step 1
print_default "+------------------------------+"
print_default "| Step 1/4 Update source files |"
print_default "+------------------------------+"
if [ -d .git ]; then
print_default "Git clone detected, updating it via git pull..."
if ! git pull --rebase; then
print_inline_warning "Uncommitted files detected, do you want to force the update? You will loose you uncommitted files. (y/n) [n] "
read -r force_git_reset_answer
force_git_reset_answer=${force_git_reset_answer:-n}
if [[ "${force_git_reset_answer}" == "y" ]]; then
print_default "Reverting uncommitted changes..."
git reset --hard
print_default "Pulling latest changes..."
git pull --rebase
else
print_error "Update aborted."
exit 1
fi
fi
else
print_default "No Git clone detected, assuming it's an unzipped release. Retrieving last release from GitHub..."
# Find latest release
LATEST_RELEASE=$(curl -s "https://api.github.com/repos/${REPOSITORY_ORGANIZATION}/${REPOSITORY_NAME}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$LATEST_RELEASE" ]; then
print_error "Could not find latest release."
exit 1
fi
print_info "Latest release found: ${LATEST_RELEASE}"
print_inline_warning "You are about to deploy v${LATEST_RELEASE} of the environment into ${CURRENT_FOLDER}, do you want to proceed? (y/n) [n] "
read -r deploy_git_release_answer
deploy_git_release_answer=${deploy_git_release_answer:-n}
if [[ "${deploy_git_release_answer}" != "y" ]]; then
print_error "Update aborted."
exit 1
fi
# Extract release zip to temporary folder
print_default "Creating temporary folder (${TMP_CURRENT_FOLDER}) to download and extract the latest release..."
mkdir -p "${TMP_CURRENT_FOLDER}"
cd "${TMP_CURRENT_FOLDER}"
print_default "Downloading latest release..."
run_and_log a curl -L -o latest.zip "$RELEASE_ZIP_URL/$LATEST_RELEASE.zip"
print_default "Extracting latest release..."
run_and_log a unzip -o latest.zip
# Copy files to current folder (except .git if existing but it should not)
# - Note: The --no-perms option is used to preserved permissions of existing files
print_default "Copying files to current folder (${CURRENT_FOLDER})..."
run_and_log a rsync -a --no-perms --exclude='.git' "${REPOSITORY_NAME}-${LATEST_RELEASE}/" "${CURRENT_FOLDER}/"
cd "${CURRENT_FOLDER}"
# Clean temporary folder
print_default "Cleaning up temporary folder..."
rm -rf "${TMP_CURRENT_FOLDER}"
fi
print_success "Update complete."
print_default ""
# Step 2
print_default "+--------------------------------------------------------------+"
print_default "| Step 2/4 Synchronize .env.local with new variables from .env |"
print_default "+--------------------------------------------------------------+"
if [ -f .env.local ]; then
print_default "Looking for variables that are not already in .env.local..."
# For each var that of .env that is not already in .env.local, append it at the end of .env.local
while IFS= read -r line; do
# Ignore empty lines and comments
if [[ "$line" =~ ^[[:space:]]*$ || "$line" =~ ^[[:space:]]*# ]]; then
continue
fi
var_name="${line%%=*}"
# Ignore variables with empty names
if [[ -z "${var_name}" ]]; then
continue
fi
# Ignore variable that already exists in .env.local
if grep -qE "^${var_name}=" .env.local; then
continue
fi
# Ajoute un commentaire avec la date avant d'ajouter la variable
echo "# Added from .env during Docker Environment update on $(date '+%Y-%m-%d %H:%M:%S')" >> .env.local
echo "${line}" >> .env.local
print_default "Added ${var_name} to .env.local"
done < .env
print_success "Synchronization complete."
else
print_warning "No .env.local file found, you should consider creating one based on .env to override default variables."
fi
print_default ""
# Step 3
print_default "+---------------------------------------------+"
print_default "| Step 3/4 Deploy default configuration files |"
print_default "+---------------------------------------------+"
print_default "Copying files from 'build/default_configuration' to ${CONF_FOLDER}"
cp -R build/default_configuration/* $CONF_FOLDER
print_success "Deployment complete."
print_default ""
# Step 4
print_default "+-----------------------------------------+"
print_default "| Step 4/4 Rebuild and restart containers |"
print_default "+-----------------------------------------+"
print_inline_default "Do you want to rebuild and restart the Docker containers now? (y/n) [y] "
read -r rebuild_containers_answer
rebuild_containers_answer=${rebuild_containers_answer:-y}
if [[ "${rebuild_containers_answer}" == "y" ]]; then
print_inline_default "Do you want to rebuild container using Docker cache? (y/n) [y] "
read -r rebuild_containers_using_cache_answer
rebuild_containers_using_cache_answer=${rebuild_containers_using_cache_answer:-y}
print_default "Stopping containers..."
docker compose down
print_default "Rebuilding containers..."
if [[ "${rebuild_containers_using_cache_answer}" == "y" ]]; then
docker compose build
else
docker compose build --no-cache
fi
print_default "Starting containers..."
if [ -f .env.local ]; then
docker compose --env-file .env.local up -d
else
docker compose up -d
fi
print_success "Containers restarted."
else
print_warning "Rebuild and restart skipped. Please remember to manually rebuild and restart your Docker containers to apply the updates."
fi
print_default ""
print_success "Update and deployment successful!"