Skip to content

Commit 9191f4e

Browse files
authored
Updates
1 parent 6bcaa9b commit 9191f4e

9 files changed

Lines changed: 273 additions & 194 deletions

File tree

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ All notable changes to EngineScript will be documented in this file.
44

55
Changes are organized by date, with the most recent changes listed first.
66

7+
## 2025-07-01
8+
9+
### 🔧 CODE QUALITY
10+
- **Shell Scripts**: Standardized shebang line in `scripts/functions/alias/alias-debug.sh` to use `#!/usr/bin/env bash` for consistency across all shell scripts
11+
- **Function Deduplication**: Created shared functions library at `scripts/functions/shared/enginescript-common.sh` to consolidate duplicated functions
12+
- Consolidated `debug_pause()` and `print_last_errors()` functions from `scripts/install/enginescript-install.sh` and `scripts/install/nginx/nginx-install.sh`
13+
- Consolidated `restart_service()`, `restart_php_fpm()`, and `clear_cache()` functions from `scripts/functions/alias/alias-cache.sh` and `scripts/functions/alias/alias-restart.sh`
14+
- Updated `scripts/install/enginescript-install.sh`, `scripts/install/nginx/nginx-install.sh`, `scripts/install/tools/tools-install.sh`, `scripts/functions/alias/alias-cache.sh`, and `scripts/functions/alias/alias-restart.sh` to source the shared library
15+
- Removed duplicate function definitions from individual scripts, improving maintainability and consistency
16+
17+
### 🐛 BUG FIXES
18+
- **Timing Issues**: Fixed timing issues in `scripts/functions/vhost/vhost-export.sh`
19+
- Added `set -e` and `set -o pipefail` for proper error handling
20+
- Changed all command execution to use immediate error checking instead of checking `$?` after the fact
21+
- Improved error checking for `cd` commands and file operations
22+
- Enhanced cleanup operations with `|| true` to prevent secondary errors
23+
- Fixed race conditions between database export, compression, and file archiving operations
24+
- **Silent Error Handling**: Fixed silent error handling in `scripts/functions/alias/alias-debug.sh`
25+
- Added `set -o pipefail` for proper pipeline error handling
26+
- Enhanced all command substitutions with error checking and fallback values
27+
- Added comprehensive error checking for system information gathering (CPU, memory, disk, network)
28+
- Improved hostname, network interface, and port detection with proper error handling
29+
- Added fallback values ("unknown") for failed system information commands
30+
- Enhanced website status checking with proper curl error handling
31+
32+
---
33+
734
## 2025-06-29
835

936
### 🚀 ENHANCEMENTS

scripts/functions/alias/alias-cache.sh

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
source /usr/local/bin/enginescript/enginescript-variables.txt
1212
source /home/EngineScript/enginescript-install-options.txt
1313

14-
14+
# Source shared functions library
15+
source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.sh
1516

1617
#----------------------------------------------------------------------------------
1718
# Start Main Script
@@ -67,27 +68,6 @@ clear_redis_cache() {
6768
}
6869
}
6970

70-
# Function to restart a service
71-
restart_service() {
72-
local service_name=$1
73-
echo "Restarting ${service_name}"
74-
service "${service_name}" restart || {
75-
echo "Error: Failed to restart ${service_name}"
76-
}
77-
}
78-
79-
# Function to restart PHP-FPM service
80-
restart_php_fpm() {
81-
local php_versions=("8.1" "8.2" "8.3" "8.4")
82-
for version in "${php_versions[@]}"; do
83-
if systemctl is-active --quiet "php${version}-fpm"; then
84-
restart_service "php${version}-fpm"
85-
return
86-
fi
87-
done
88-
echo "Error: No active PHP-FPM service found."
89-
}
90-
9171
# Clear caches
9272
clear_transients
9373
clear_nginx_cache

scripts/functions/alias/alias-debug.sh

Lines changed: 133 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env bash
22

3+
# Add proper error handling for critical operations
4+
set -o pipefail
5+
36
# Source EngineScript variables
47
source /usr/local/bin/enginescript/enginescript-variables.txt
58
source /home/EngineScript/enginescript-install-options.txt
69

710
# Create temp file with date stamp
8-
DEBUG_FILE="/tmp/enginescript-debug-$(date +%Y%m%d-%H%M%S).md"
11+
DEBUG_FILE="/tmp/enginescript-debug-$(date +%Y%m%d-%H%M%S 2>/dev/null || echo "unknown").md"
912

1013
# ANSI color codes
1114
BOLD="\e[1m"
@@ -32,40 +35,116 @@ debug_print() {
3235

3336
# Get server info from alias-server-info.sh functions
3437
get_server_info() {
35-
# System Info
36-
BIT_TYPE=$(getconf LONG_BIT)
37-
CPU_COUNT=$(nproc)
38-
SERVER_MEMORY_TOTAL_100=$(free -m | awk '/^Mem:/{print $2}')
39-
IP_ADDRESS=$(hostname -I | awk '{print $1}')
40-
UBUNTU_TYPE=$(lsb_release -i | cut -f2)
41-
UBUNTU_VERSION=$(lsb_release -r | cut -f2)
42-
UBUNTU_CODENAME=$(lsb_release -c | cut -f2)
38+
# System Info with error checking
39+
if ! BIT_TYPE=$(getconf LONG_BIT 2>/dev/null); then
40+
BIT_TYPE="unknown"
41+
debug_print "WARNING: Failed to retrieve system architecture." "WARNING: Failed to retrieve system architecture."
42+
fi
43+
44+
if ! CPU_COUNT=$(nproc 2>/dev/null); then
45+
CPU_COUNT="unknown"
46+
debug_print "WARNING: Failed to retrieve CPU count." "WARNING: Failed to retrieve CPU count."
47+
fi
4348

44-
# CPU Info
45-
if ! CPU_INFO=$(lscpu | grep -E "^Model name:" | cut -d":" -f2 | xargs); then
46-
debug_print "ERROR: Failed to retrieve CPU model information." "ERROR: Failed to retrieve CPU model information."
49+
if ! SERVER_MEMORY_TOTAL_100=$(free -m 2>/dev/null | awk '/^Mem:/{print $2}'); then
50+
SERVER_MEMORY_TOTAL_100="unknown"
51+
debug_print "WARNING: Failed to retrieve total memory." "WARNING: Failed to retrieve total memory."
4752
fi
48-
CPU_CORES=$(nproc)
49-
CPU_FREQ=$(lscpu | grep -E "^CPU MHz:" | cut -d":" -f2 | xargs)
5053

51-
# Memory Info
52-
TOTAL_RAM=$(free -h | awk '/^Mem:/ {print $2}')
53-
USED_RAM=$(free -h | awk '/^Mem:/ {print $3}')
54-
FREE_RAM=$(free -h | awk '/^Mem:/ {print $4}')
55-
SWAP_TOTAL=$(free -h | awk '/^Swap:/ {print $2}')
56-
SWAP_USED=$(free -h | awk '/^Swap:/ {print $3}')
54+
if ! IP_ADDRESS=$(hostname -I 2>/dev/null | awk '{print $1}'); then
55+
IP_ADDRESS="unknown"
56+
debug_print "WARNING: Failed to retrieve IP address." "WARNING: Failed to retrieve IP address."
57+
fi
5758

58-
# Disk Info
59-
ROOT_TOTAL=$(df -h / | awk 'NR==2 {print $2}')
60-
ROOT_USED=$(df -h / | awk 'NR==2 {print $3}')
61-
ROOT_FREE=$(df -h / | awk 'NR==2 {print $4}')
62-
ROOT_PCENT=$(df -h / | awk 'NR==2 {print $5}')
59+
if ! UBUNTU_TYPE=$(lsb_release -i 2>/dev/null | cut -f2); then
60+
UBUNTU_TYPE="unknown"
61+
debug_print "WARNING: Failed to retrieve Ubuntu type." "WARNING: Failed to retrieve Ubuntu type."
62+
fi
6363

64-
# Load Average
65-
LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}' | xargs)
64+
if ! UBUNTU_VERSION=$(lsb_release -r 2>/dev/null | cut -f2); then
65+
UBUNTU_VERSION="unknown"
66+
debug_print "WARNING: Failed to retrieve Ubuntu version." "WARNING: Failed to retrieve Ubuntu version."
67+
fi
68+
69+
if ! UBUNTU_CODENAME=$(lsb_release -c 2>/dev/null | cut -f2); then
70+
UBUNTU_CODENAME="unknown"
71+
debug_print "WARNING: Failed to retrieve Ubuntu codename." "WARNING: Failed to retrieve Ubuntu codename."
72+
fi
73+
74+
# CPU Info with enhanced error checking
75+
if ! CPU_INFO=$(lscpu 2>/dev/null | grep -E "^Model name:" | cut -d":" -f2 | xargs); then
76+
CPU_INFO="unknown"
77+
debug_print "WARNING: Failed to retrieve CPU model information." "WARNING: Failed to retrieve CPU model information."
78+
fi
79+
80+
if ! CPU_CORES=$(nproc 2>/dev/null); then
81+
CPU_CORES="unknown"
82+
debug_print "WARNING: Failed to retrieve CPU core count." "WARNING: Failed to retrieve CPU core count."
83+
fi
84+
85+
if ! CPU_FREQ=$(lscpu 2>/dev/null | grep -E "^CPU MHz:" | cut -d":" -f2 | xargs); then
86+
CPU_FREQ="unknown"
87+
debug_print "WARNING: Failed to retrieve CPU frequency." "WARNING: Failed to retrieve CPU frequency."
88+
fi
89+
90+
# Memory Info with error checking
91+
if ! TOTAL_RAM=$(free -h 2>/dev/null | awk '/^Mem:/ {print $2}'); then
92+
TOTAL_RAM="unknown"
93+
debug_print "WARNING: Failed to retrieve total RAM." "WARNING: Failed to retrieve total RAM."
94+
fi
95+
96+
if ! USED_RAM=$(free -h 2>/dev/null | awk '/^Mem:/ {print $3}'); then
97+
USED_RAM="unknown"
98+
debug_print "WARNING: Failed to retrieve used RAM." "WARNING: Failed to retrieve used RAM."
99+
fi
100+
101+
if ! FREE_RAM=$(free -h 2>/dev/null | awk '/^Mem:/ {print $4}'); then
102+
FREE_RAM="unknown"
103+
debug_print "WARNING: Failed to retrieve free RAM." "WARNING: Failed to retrieve free RAM."
104+
fi
66105

67-
# Network Info
68-
NETWORK_INFO=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1')
106+
if ! SWAP_TOTAL=$(free -h 2>/dev/null | awk '/^Swap:/ {print $2}'); then
107+
SWAP_TOTAL="unknown"
108+
debug_print "WARNING: Failed to retrieve total swap." "WARNING: Failed to retrieve total swap."
109+
fi
110+
111+
if ! SWAP_USED=$(free -h 2>/dev/null | awk '/^Swap:/ {print $3}'); then
112+
SWAP_USED="unknown"
113+
debug_print "WARNING: Failed to retrieve used swap." "WARNING: Failed to retrieve used swap."
114+
fi
115+
116+
# Disk Info with error checking
117+
if ! ROOT_TOTAL=$(df -h / 2>/dev/null | awk 'NR==2 {print $2}'); then
118+
ROOT_TOTAL="unknown"
119+
debug_print "WARNING: Failed to retrieve root disk total." "WARNING: Failed to retrieve root disk total."
120+
fi
121+
122+
if ! ROOT_USED=$(df -h / 2>/dev/null | awk 'NR==2 {print $3}'); then
123+
ROOT_USED="unknown"
124+
debug_print "WARNING: Failed to retrieve root disk used." "WARNING: Failed to retrieve root disk used."
125+
fi
126+
127+
if ! ROOT_FREE=$(df -h / 2>/dev/null | awk 'NR==2 {print $4}'); then
128+
ROOT_FREE="unknown"
129+
debug_print "WARNING: Failed to retrieve root disk free." "WARNING: Failed to retrieve root disk free."
130+
fi
131+
132+
if ! ROOT_PCENT=$(df -h / 2>/dev/null | awk 'NR==2 {print $5}'); then
133+
ROOT_PCENT="unknown"
134+
debug_print "WARNING: Failed to retrieve root disk percentage." "WARNING: Failed to retrieve root disk percentage."
135+
fi
136+
137+
# Load Average with error checking
138+
if ! LOAD_AVG=$(uptime 2>/dev/null | awk -F'load average:' '{print $2}' | xargs); then
139+
LOAD_AVG="unknown"
140+
debug_print "WARNING: Failed to retrieve load average." "WARNING: Failed to retrieve load average."
141+
fi
142+
143+
# Network Info with error checking
144+
if ! NETWORK_INFO=$(ip -4 addr show 2>/dev/null | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1'); then
145+
NETWORK_INFO="unknown"
146+
debug_print "WARNING: Failed to retrieve network information." "WARNING: Failed to retrieve network information."
147+
fi
69148
}
70149

71150
# Start debug report
@@ -87,7 +166,8 @@ debug_print "Ubuntu Version | ${UBUNTU_TYPE} ${UBUNTU_VERSION} (${UBUNTU_CODENAM
87166
debug_print "\n## System Information\n" "\n## System Information\n"
88167
debug_print "| Component | Details |" "| Component | Details |"
89168
debug_print "|-----------|----------|" "|-----------|----------|"
90-
debug_print "Hostname | $(hostname)" "| Hostname | \`$(hostname)\` |"
169+
HOSTNAME_VAL=$(hostname 2>/dev/null || echo "unknown")
170+
debug_print "Hostname | ${HOSTNAME_VAL}" "| Hostname | \`${HOSTNAME_VAL}\` |"
91171
debug_print "IP Address | ${IP_ADDRESS}" "| IP Address | \`${IP_ADDRESS}\` |"
92172
debug_print "CPU Model | ${CPU_INFO}" "| CPU Model | \`${CPU_INFO}\` |"
93173
debug_print "CPU Cores | ${CPU_CORES}" "| CPU Cores | \`${CPU_CORES}\` |"
@@ -97,17 +177,22 @@ debug_print "\n## Network Information\n" "\n## Network Information\n"
97177
debug_print "### IP Addresses\n" "### IP Addresses\n"
98178
debug_print "| Interface | IP Address |" "| Interface | IP Address |"
99179
debug_print "|-----------|------------|" "|-----------|------------|"
100-
ip -4 addr show | grep inet | while read -r line; do
101-
IFACE=$(echo "$line" | awk '{print $NF}')
102-
IP=$(echo "$line" | awk '{print $2}' | cut -d/ -f1)
103-
debug_print "${IFACE} | ${IP}" "| ${IFACE} | \`${IP}\` |"
104-
done
180+
if ip -4 addr show 2>/dev/null | grep inet >/dev/null 2>&1; then
181+
ip -4 addr show 2>/dev/null | grep inet | while read -r line; do
182+
if IFACE=$(echo "$line" | awk '{print $NF}' 2>/dev/null) && IP=$(echo "$line" | awk '{print $2}' | cut -d/ -f1 2>/dev/null); then
183+
debug_print "${IFACE} | ${IP}" "| ${IFACE} | \`${IP}\` |"
184+
fi
185+
done
186+
else
187+
debug_print "Unknown | Unable to retrieve" "| Unknown | \`Unable to retrieve\` |"
188+
fi
105189

106190
debug_print "\n\n"
107191

108192
debug_print "\n### Active Ports\n" "### Active Ports\n"
109193
debug_print "\`\`\`" "\`\`\`"
110-
debug_print "$(netstat -tuln | grep LISTEN)" "$(netstat -tuln | grep LISTEN)"
194+
NETSTAT_OUTPUT=$(netstat -tuln 2>/dev/null | grep LISTEN || echo "Unable to retrieve port information")
195+
debug_print "${NETSTAT_OUTPUT}" "${NETSTAT_OUTPUT}"
111196
debug_print "\`\`\`\n" "\`\`\`\n"
112197

113198
# Memory Information
@@ -230,16 +315,23 @@ debug_print "|--------|---------|" "|--------|---------|"
230315
first_domain=""
231316

232317
while IFS= read -r site; do
233-
# Get full domain name
234-
domain=$(basename "$(dirname "$site")")
318+
# Get full domain name with error checking
319+
if ! site_dir=$(dirname "$site" 2>/dev/null); then
320+
continue # Skip if we can't get the directory
321+
fi
322+
if ! domain=$(basename "$site_dir" 2>/dev/null); then
323+
continue # Skip if we can't get the domain name
324+
fi
235325

236326
# Store first domain encountered
237-
if [ -z "$first_domain" ]; then
327+
if [[ -z "$first_domain" ]]; then
238328
first_domain="$domain"
239329
fi
240330

241-
# Get HTTP status
242-
curl_result=$(curl -sL -w "%{http_code}" "https://$domain" -o /dev/null 2>/dev/null)
331+
# Get HTTP status with error handling
332+
if ! curl_result=$(curl -sL -w "%{http_code}" "https://$domain" -o /dev/null 2>/dev/null); then
333+
curl_result="000" # Default to connection failed
334+
fi
243335

244336
# Set status emoji
245337
case $curl_result in

scripts/functions/alias/alias-restart.sh

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,11 @@
1111
source /usr/local/bin/enginescript/enginescript-variables.txt
1212
source /home/EngineScript/enginescript-install-options.txt
1313

14+
# Source shared functions library
15+
source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.sh
1416

15-
16-
#----------------------------------------------------------------------------------
1717
# Start Main Script
1818

19-
# Function to clear cache
20-
clear_cache() {
21-
local cache_path="$1"
22-
echo "Clearing ${cache_path} Cache"
23-
rm -rf "${cache_path}"/* || {
24-
echo "Error: Failed to clear ${cache_path} cache."
25-
}
26-
}
27-
28-
# Function to restart a service
29-
restart_service() {
30-
local service_name="$1"
31-
echo "Restarting ${service_name}"
32-
service "${service_name}" restart || {
33-
echo "Error: Failed to restart ${service_name}."
34-
}
35-
}
36-
37-
# Function to restart PHP-FPM service
38-
restart_php_fpm() {
39-
local php_versions=("8.1" "8.2" "8.3" "8.4")
40-
for version in "${php_versions[@]}"; do
41-
if systemctl is-active --quiet "php${version}-fpm"; then
42-
restart_service "php${version}-fpm"
43-
return
44-
fi
45-
done
46-
echo "Error: No active PHP-FPM service found."
47-
}
48-
4919
echo -e "\nRestarting Services\n\n"
5020

5121
clear_cache "/var/cache/nginx"

0 commit comments

Comments
 (0)