Update scripts/dccd2.sh

This commit is contained in:
Matt Reeves 2024-05-04 02:19:19 +00:00
parent ba460428fc
commit 3a7f681107

View file

@ -27,7 +27,7 @@ send_notification() {
update_compose_files() { update_compose_files() {
local dir="$1" local dir="$1"
local folder_pattern="$2" # Added parameter for folder pattern local folder_pattern="$2"
cd "$dir" || { log_message "ERROR: Directory doesn't exist, exiting..."; send_notification "Script Error" "Failed to update compose files: Directory doesn't exist"; exit 127; } cd "$dir" || { log_message "ERROR: Directory doesn't exist, exiting..."; send_notification "Script Error" "Failed to update compose files: Directory doesn't exist"; exit 127; }
@ -62,10 +62,10 @@ update_compose_files() {
# Check if the local hash matches the remote hash # Check if the local hash matches the remote hash
if [ "$local_hash" != "$remote_hash" ]; then if [ "$local_hash" != "$remote_hash" ]; then
log_message "STATE: Hashes don't match, updating..." log_message "STATE: Hashes don't match, checking for relevant changes..."
# Get list of changed files # Get the list of changed files
changed_files=$(git diff --name-only HEAD@{1} HEAD) changed_files=$(git diff --name-only "origin/$REMOTE_BRANCH")
# Loop through directories matching the specified pattern # Loop through directories matching the specified pattern
find "$dir" -type d -name "$folder_pattern" | while IFS= read -r folder; do find "$dir" -type d -name "$folder_pattern" | while IFS= read -r folder; do
@ -77,31 +77,28 @@ update_compose_files() {
continue continue
fi fi
# Check if docker-compose.yml was changed in this directory # Check if the docker-compose.yml file in this folder has changed
if echo "$changed_files" | grep -q "$folder/docker-compose.yml"; then compose_file_changed=false
# Pull any changes in the Git repository for file in $changed_files; do
if ! git pull --quiet origin "$REMOTE_BRANCH"; then if [[ "$file" == "$folder/docker-compose.yml" ]]; then
log_message "ERROR: Unable to pull changes from the remote repository (the server may be offline or unreachable)" compose_file_changed=true
send_notification "Script Error" "Failed to update compose files: Unable to pull changes from the remote repository (the server may be offline or unreachable)" break
exit 1
fi fi
done
# Check if the docker-compose.yml file was modified after the pull if $compose_file_changed; then
if git diff --quiet --exit-code "$folder/docker-compose.yml"; then # Go into the directory
log_message "INFO: No changes in $folder/docker-compose.yml" cd "$folder" || { log_message "ERROR: Failed to enter directory $folder"; send_notification "Script Error" "Failed to update compose files: Failed to enter directory $folder"; continue; }
else
# Go into the directory
cd "$folder" || { log_message "ERROR: Failed to enter directory $folder"; send_notification "Script Error" "Failed to update compose files: Failed to enter directory $folder"; continue; }
# Redeploy compose file in this directory # Redeploy compose file in this directory
log_message "STATE: Redeploying compose file in directory: $folder" log_message "STATE: Redeploying compose file in directory: $folder"
docker compose up -d --quiet-pull docker compose up -d --quiet-pull
# Go back to the original directory # Go back to the original directory
cd "$dir" || { log_message "ERROR: Failed to return to directory $dir"; send_notification "Script Error" "Failed to update compose files: Failed to return to directory $dir"; exit 1; } cd "$dir" || { log_message "ERROR: Failed to return to directory $dir"; send_notification "Script Error" "Failed to update compose files: Failed to return to directory $dir"; exit 1; }
fi else
log_message "INFO: No changes detected in docker-compose.yml for $folder, skipping..."
fi fi
done done
else else
log_message "STATE: Hashes match, so nothing to do" log_message "STATE: Hashes match, so nothing to do"
@ -116,7 +113,6 @@ update_compose_files() {
log_message "STATE: Done!" log_message "STATE: Done!"
} }
usage() { usage() {
printf " printf "
Usage: $0 [OPTIONS] Usage: $0 [OPTIONS]
@ -136,7 +132,10 @@ usage() {
exit 1 exit 1
} }
########################################
# Options # Options
########################################
while getopts ":b:d:hl:px:f:" opt; do while getopts ":b:d:hl:px:f:" opt; do
case "$opt" in case "$opt" in
b) b)
@ -171,7 +170,10 @@ while getopts ":b:d:hl:px:f:" opt; do
esac esac
done done
########################################
# Script starts here # Script starts here
########################################
touch "$LOG_FILE" touch "$LOG_FILE"
{ {
echo "########################################" echo "########################################"