From 1c68a4f6510c60ba7ed6a5b91cc1bae5d865928e Mon Sep 17 00:00:00 2001 From: antomfdez Date: Wed, 27 Nov 2024 15:35:59 +0100 Subject: [PATCH 1/3] Add colorized output and improved error handling for script execution --- daily_updates.sh | 108 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 22 deletions(-) mode change 100644 => 100755 daily_updates.sh diff --git a/daily_updates.sh b/daily_updates.sh old mode 100644 new mode 100755 index 733ce66..2c4c343 --- a/daily_updates.sh +++ b/daily_updates.sh @@ -1,61 +1,125 @@ #!/usr/bin/env bash reset && clear -if [ "$EUID" -ne 0 ] - then +# Function to display error messages with failure reason and not exit +log_error() { + local msg="$1" + local cmd="$2" tput bold - echo "Please run as root" + echo -e "\033[31m[ ERROR ]: $msg \033[0m" # Red color for errors tput sgr0 - exit + echo "$(date) - ERROR: $msg. Command: $cmd" >> "$LOG_FILE" +} + +# Function to display success messages +log_success() { + local msg="$1" + local cmd="$2" + tput bold + echo -e "\033[32m[ SUCCESS ]: $msg \033[0m" # Green color for success + tput sgr0 + echo "$(date) - SUCCESS: $msg. Command: $cmd" >> "$LOG_FILE" +} + +# Ensure script is run as root +if [ "$EUID" -ne 0 ]; then + tput bold + echo "Please run as root." + tput sgr0 + exit 1 fi +# Log file to capture script execution details +LOG_FILE="/var/log/port_update.log" +echo "$(date) - Starting script execution" > "$LOG_FILE" + +# Function to run commands with logging and error checking without exiting +run_command() { + local cmd="$1" + echo "Running: $cmd" + echo "$(date) - Running: $cmd" >> "$LOG_FILE" + + # Execute the command, capturing both stdout and stderr + output=$($cmd 2>&1) + result=$? + + # Print output to terminal and log it + echo "$output" + echo "$output" >> "$LOG_FILE" + + # If the command succeeded, log as success + if [ $result -eq 0 ]; then + log_success "$cmd was successful" "$cmd" + else + log_error "Command failed with error" "$cmd" + log_error "Error output: $output" "$cmd" + fi +} + +# Function to mark directories as safe for Git +mark_safe_directory() { + local dir="$1" + run_command "git config --global --add safe.directory $dir" +} + +# Mark the important directories as safe +tput bold +echo "[ Marking Git directories as safe... ]" +tput sgr0 + +mark_safe_directory "/usr/ports" +mark_safe_directory "/usr/src" +# Add any additional directories you need to mark as safe: +# mark_safe_directory "/path/to/other/repository" + +# Update /usr/ports git tree (split into cd and git pull for better error handling) tput bold -echo echo "[ Updating /usr/ports git tree... ]" -echo tput sgr0 -cd /usr/ports && git pull +run_command "cd /usr/ports" +run_command "git pull" +# Update /usr/src git tree (split into cd and git pull for better error handling) tput bold -echo echo "[ Updating /usr/src git tree... ]" -echo tput sgr0 -cd /usr/src && git pull +run_command "cd /usr/src" +run_command "git pull" +# Update FreeBSD pkg repositories tput bold -echo echo "[ Updating FreeBSD pkg repos... ]" -echo tput sgr0 -pkg update -echo +run_command "pkg update" +# Clean port distfiles tput bold echo "[ Cleaning port distfiles... ]" -echo tput sgr0 -/usr/local/sbin/portmaster -y --clean-distfiles +run_command "/usr/local/sbin/portmaster -y --clean-distfiles" +# Clean stale port packages tput bold echo "[ Cleaning stale port packages... ]" -echo tput sgr0 -/usr/local/sbin/portmaster -y --clean-packages +run_command "/usr/local/sbin/portmaster -y --clean-packages" +# Check for stale entries in /var/db/ports tput bold echo "[ Checking for stale entries in /var/db/ports... ]" -echo tput sgr0 -/usr/local/sbin/portmaster -v --check-port-dbdir +run_command "/usr/local/sbin/portmaster -v --check-port-dbdir" +# Check for portmaster upgrades tput bold echo "[ Checking portmaster for any upgrades... ]" -echo tput sgr0 -/usr/local/sbin/portmaster -adyG +run_command "/usr/local/sbin/portmaster -adyG" tput bold echo "[ All done! ]" echo tput sgr0 + +# End of script logging +echo "$(date) - Script execution completed." >> "$LOG_FILE" From f9ee1a4464dda4bd6c12d97bd713f369cc8f1d33 Mon Sep 17 00:00:00 2001 From: antomfdez Date: Fri, 29 Nov 2024 18:41:03 +0100 Subject: [PATCH 2/3] Fixed GIT_DISCOVERY_ACROSS_FILESYSTEM err --- daily_updates.sh | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/daily_updates.sh b/daily_updates.sh index 2c4c343..19ed86a 100755 --- a/daily_updates.sh +++ b/daily_updates.sh @@ -69,22 +69,18 @@ tput sgr0 mark_safe_directory "/usr/ports" mark_safe_directory "/usr/src" -# Add any additional directories you need to mark as safe: -# mark_safe_directory "/path/to/other/repository" -# Update /usr/ports git tree (split into cd and git pull for better error handling) +# Update /usr/ports git tree tput bold echo "[ Updating /usr/ports git tree... ]" tput sgr0 -run_command "cd /usr/ports" -run_command "git pull" +run_command "cd /usr/ports && git pull" -# Update /usr/src git tree (split into cd and git pull for better error handling) +# Update /usr/src git tree tput bold echo "[ Updating /usr/src git tree... ]" tput sgr0 -run_command "cd /usr/src" -run_command "git pull" +run_command "cd /usr/src && git pull" # Update FreeBSD pkg repositories tput bold @@ -122,4 +118,4 @@ echo tput sgr0 # End of script logging -echo "$(date) - Script execution completed." >> "$LOG_FILE" +echo "$(date) - Script execution completed." >> "$LOG_FILE" \ No newline at end of file From d009360e15ddc28a553a591cfed361960a3b9235 Mon Sep 17 00:00:00 2001 From: antomfdez Date: Sat, 30 Nov 2024 20:36:57 +0100 Subject: [PATCH 3/3] Changed some cmd formating giving err --- daily_updates.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/daily_updates.sh b/daily_updates.sh index 19ed86a..5d163b4 100755 --- a/daily_updates.sh +++ b/daily_updates.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash -reset && clear # Function to display error messages with failure reason and not exit log_error() { @@ -38,9 +37,9 @@ run_command() { local cmd="$1" echo "Running: $cmd" echo "$(date) - Running: $cmd" >> "$LOG_FILE" - - # Execute the command, capturing both stdout and stderr - output=$($cmd 2>&1) + + # Execute the command using eval to handle complex commands + output=$(eval "$cmd" 2>&1) result=$? # Print output to terminal and log it @@ -74,13 +73,13 @@ mark_safe_directory "/usr/src" tput bold echo "[ Updating /usr/ports git tree... ]" tput sgr0 -run_command "cd /usr/ports && git pull" +run_command "(cd /usr/ports && git pull)" # Update /usr/src git tree tput bold echo "[ Updating /usr/src git tree... ]" tput sgr0 -run_command "cd /usr/src && git pull" +run_command "(cd /usr/src && git pull)" # Update FreeBSD pkg repositories tput bold