- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 27
/
entrypoint.sh
295 lines (248 loc) · 9.27 KB
/
entrypoint.sh
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
user_name="nonRootUser"
group_name="nonRootGroup"
. /env.sh
# Function to check if a specific MigrationId exists in the __EFMigrationsHistory table
check_migration_exists() {
local migration_id=$1
# Check if the __EFMigrationsHistory table exists
local table_exists=$(psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -h "$POSTGRES_HOST" -tAc "SELECT to_regclass('public.\"__EFMigrationsHistory\"');")
if [ "$table_exists" = "public.__EFMigrationsHistory" ]; then
# Table exists, check if the migration_id exists
local exists=$(psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -h "$POSTGRES_HOST" -tAc "SELECT 1 FROM public.\"__EFMigrationsHistory\" WHERE \"MigrationId\" = '$migration_id';")
if [ "$exists" = "1" ]; then
return 0 # MigrationId exists
else
return 1 # MigrationId does not exist
fi
else
# Table does not exist
return 1
fi
}
# Perform the database migration check and update
perform_migration_update() {
local migration_id_to_check="20240804181253_Custom_Setup"
local new_migration_id="20240815125224_Initital"
local product_version="8.0.8"
if check_migration_exists "$migration_id_to_check"; then
echo "MigrationId $migration_id_to_check exists. Performing DELETE and INSERT operations."
psql -U $POSTGRES_USER -d $POSTGRES_DB -h $POSTGRES_HOST -c 'DELETE FROM public."__EFMigrationsHistory";'
psql -U $POSTGRES_USER -d $POSTGRES_DB -h $POSTGRES_HOST -c "INSERT INTO public.\"__EFMigrationsHistory\"(\"MigrationId\", \"ProductVersion\") VALUES ('$new_migration_id', '$product_version');"
echo "MigrationId updated to $new_migration_id with ProductVersion $product_version."
else
echo "MigrationId $migration_id_to_check does not exist. No changes made."
fi
}
moveFilesAndDeleteDir() {
local source_dir=$1
local destination_dir=$2
# Check if the source directory exists
if [ -d "$source_dir" ]; then
# Ensure the destination directory exists, create if not
if [ ! -d "$destination_dir" ]; then
mkdir -p "$destination_dir"
fi
# Check if there are any files or directories in source_dir
local files=("$source_dir"/*)
if [ -e "${files[0]}" ]; then
# Move files from source to destination
mv "$source_dir"/* "$destination_dir"/ 2>/dev/null
# Check if move operation was successful
if [ $? -eq 0 ]; then
echo "Files moved successfully from $source_dir to $destination_dir."
else
echo "Failed to move files from $source_dir to $destination_dir."
return # Exit the function if moving files failed
fi
else
echo "No files to move from $source_dir."
fi
# Remove the source directory
rmdir "$source_dir" 2>/dev/null
if [ $? -eq 0 ]; then
echo "Source directory $source_dir removed."
else
echo "Failed to remove source directory $source_dir. It might not be empty."
fi
fi
}
# Function to check for any file ready to be restored in /config/DB/Restore
check_files_ready_for_restore() {
local file_found=1 # Default to no files found (1 means no files ready)
# Check if the restore directory exists
if [ ! -d "$RESTORE_DIR" ]; then
echo "Restore path does not exist: $RESTORE_DIR"
return 1
fi
# Find files matching the backup pattern
local files=("$RESTORE_DIR"/backup_*.tar.gz)
if [ ${#files[@]} -eq 0 ] || [ ! -e "${files[0]}" ]; then
echo "No backup files found in $RESTORE_DIR."
return 1
fi
# Check each file for size
for file in "${files[@]}"; do
if [ -s "$file" ]; then
echo "File is ready for restore: $(basename "$file")"
file_found=0 # Set to 0 if a file is found and is not empty
break
else
echo "Found an empty backup file: $(basename "$file")"
fi
done
return $file_found
}
rename_directory() {
local src="$1"
local dest="$2"
# Check if the source directory exists
if [ ! -d "$src" ]; then
return 1
fi
# Check for case sensitivity and existence of the destination directory
if [ "$src" = "$dest" ]; then
#echo "Source and destination are the same in a case-insensitive filesystem."
return 1
elif [ -d "$dest" ]; then
#echo "Destination directory already exists: $dest"
return 1
fi
# Perform the rename
mv "$src" "$dest"
if [ $? -eq 0 ]; then
echo "Directory renamed successfully from $src to $dest"
else
echo "Failed to rename directory from $src to $dest"
return 1
fi
}
wait_for_postgres() {
local host="$1"
local port="$2"
local max_attempts="$3"
local wait_interval="$4"
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if pg_isready -h "$host" -p "$port" >/dev/null 2>&1; then
echo "PostgreSQL is ready on $host:$port"
return 0
else
attempt=$((attempt + 1))
echo "Attempt $attempt: PostgreSQL is not yet ready on $host:$port. Retrying in $wait_interval seconds..."
sleep "$wait_interval"
fi
done
echo "Error: PostgreSQL on $host:$port is not ready after $max_attempts attempts."
return 1
}
# Check if PUID or PGID is set to a non-root value
if [ "$PUID" -ne 0 ]; then
if getent passwd $PUID >/dev/null 2>&1; then
user_name=$(getent passwd $PUID | cut -d: -f1)
else
useradd --uid $PUID -K UID_MIN=10 --comment "nonRootUser" --shell /bin/bash nonRootUser
fi
fi
if [ "$PGID" -ne 0 ]; then
if getent group $PGID >/dev/null 2>&1; then
group_name=$(getent group $PGID | cut -d: -f1)
else
addgroup --gid $PGID --force-badname "nonRootGroup"
fi
fi
rm -rf /config/hls
mkdir -p /config/Cache
mkdir -p /config/Logs
mkdir -p /config/PlayLists/EPG
mkdir -p /config/PlayLists/M3U
mkdir -p /config/HLS
mkdir -p $PGDATA
rename_directory /config/settings /config/Settings
rename_directory /config/backups /config/Backups
# Change ownership of the /app directory
if [ "$PUID" -ne 0 ] || [ "$PGID" -ne 0 ]; then
echo "Changing ownership of /app to ${PUID:-0}:${PGID:-0}"
chown -R ${PUID:-0}:${PGID:-0} /app
echo "Changing ownership of /config to ${PUID:-0}:${PGID:-0}"
find /config -mindepth 1 -maxdepth 1 -type d -not -path '/config/tv-logos' -not -path '/config/DB' -exec chown -R ${PUID:-0}:${PGID:-0} {} \;
fi
chown ${PUID:-0}:${PGID:-0} '/config/tv-logos' 2>/dev/null
# Pretty printing the configuration
echo "Configuration:"
echo "--------------"
echo "PGADMIN:"
echo " Platform Type: $PGADMIN_PLATFORM_TYPE"
echo " Setup Email: $PGADMIN_SETUP_EMAIL"
echo " Setup Password: ********" #$PGADMIN_SETUP_PASSWORD"
echo "POSTGRES:"
echo " User: $POSTGRES_USER"
echo " Password: ********" #$POSTGRES_PASSWORD"
echo " DB Name: $POSTGRES_DB"
echo " Host: $POSTGRES_HOST"
echo " Is DB Local: $POSTGRES_ISLOCAL"
echo " Data Directory: $PGDATA"
echo " Set Perms: $POSTGRES_SET_PERMS"
echo "OS:"
echo " User: ${PUID:-0} Group: ${PGID:-0}"
echo " User: postgres Group: postgres"
echo " UID: $(id -u postgres) GID: $(id -g postgres)"
if [ "$POSTGRES_ISLOCAL" -eq 1 ] && [ "$POSTGRES_SET_PERMS" -eq 1 ]; then
chown -R postgres:postgres "$PGDATA"
fi
if [ "$POSTGRES_ISLOCAL" -eq 1 ]; then
# Start the database
/usr/local/bin/docker-entrypoint.sh postgres &
fi
wait_for_postgres $POSTGRES_HOST "5432" 20 5
if [ $? -eq 0 ]; then
# PostgreSQL is ready, you can proceed with your tasks
echo "Postgres is up"
# Check if any file is ready for restore and run restore.sh if so
if check_files_ready_for_restore; then
# Print a warning message about the restoration process
echo "WARNING: You are about to restore the database. This operation cannot be undone."
echo "The restoration process will begin in 10 seconds. Press Ctrl+C to cancel."
# Pause for 10 seconds to give the user a chance to cancel
sleep 10
echo "Initiating restoration process..."
/usr/local/bin/restore.sh
else
echo "No files ready for restoration."
# Perform migration update
perform_migration_update
fi
else
# PostgreSQL is not ready after max_attempts, handle the error
echo "Error: PostgreSQL is not ready."
exit 1
fi
mkdir -p $BACKUP_DIR
mkdir -p $RESTORE_DIR
moveFilesAndDeleteDir /config/DB/Backup $BACKUP_DIR
chown ${PUID:-0}:${PGID:-0} $BACKUP_DIR
chown ${PUID:-0}:${PGID:-0} $RESTORE_DIR
chmod 777 $BACKUP_DIR
chmod 777 $RESTORE_DIR
if [ "$PUID" -ne 0 ]; then
if usermod -aG postgres "$user_name"; then
echo "User $user_name added to group postgres successfully."
chmod -R 775 $PGDATA
else
echo "Failed to add user $user_name to group postgres."
fi
fi
# Execute the main application as the specified user
if [ "$PUID" -ne 0 ] && [ "$PGID" -ne 0 ]; then
echo "Running as $user_name:$group_name"
exec gosu $user_name:$group_name "$@"
elif [ "$PUID" -ne 0 ]; then
echo "Running as $user_name"
exec gosu $user_name "$@"
elif [ "$PGID" -ne 0 ]; then
echo "Running as :$group_name"
exec gosu :$group_name "$@"
else
echo "Running as root"
exec "$@"
fi