mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 01:10:12 -08:00
feat: Add base HTML template and implement dashboard, logs, and service views
- Created a base HTML template for consistent layout across pages. - Developed a dashboard page to display backup service metrics and statuses. - Implemented a log viewer for detailed log file inspection. - Added error handling page for better user experience during failures. - Introduced service detail page to show specific service metrics and actions. - Enhanced log filtering and viewing capabilities. - Integrated auto-refresh functionality for real-time updates on metrics. - Created integration and unit test scripts for backup metrics functionality.
This commit is contained in:
@@ -6,6 +6,18 @@
|
||||
|
||||
set -e
|
||||
|
||||
# Load the unified backup metrics library
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
LIB_DIR="$SCRIPT_DIR/lib"
|
||||
if [[ -f "$LIB_DIR/unified-backup-metrics.sh" ]]; then
|
||||
# shellcheck source=lib/unified-backup-metrics.sh
|
||||
source "$LIB_DIR/unified-backup-metrics.sh"
|
||||
METRICS_ENABLED=true
|
||||
else
|
||||
echo "Warning: Unified backup metrics library not found at $LIB_DIR/unified-backup-metrics.sh"
|
||||
METRICS_ENABLED=false
|
||||
fi
|
||||
|
||||
# Colors for output
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[0;33m'
|
||||
@@ -70,7 +82,7 @@ find_env_files() {
|
||||
local base_dir="$1"
|
||||
|
||||
if [ ! -d "$base_dir" ]; then
|
||||
echo -e "${YELLOW}Warning: Docker directory $base_dir does not exist${NC}"
|
||||
echo -e "${YELLOW}Warning: Docker directory $base_dir does not exist${NC}" >&2
|
||||
return 0
|
||||
fi
|
||||
|
||||
@@ -227,6 +239,20 @@ EOF
|
||||
log "Backup repository initialized at $BACKUP_DIR"
|
||||
}
|
||||
|
||||
# Cleanup function for metrics finalization
|
||||
cleanup() {
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
if [[ -n "$1" && "$1" == "error" ]]; then
|
||||
metrics_backup_complete "failed" "Backup failed during execution"
|
||||
else
|
||||
metrics_backup_complete "success" "Environment files backup completed successfully"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Set up cleanup trap
|
||||
trap 'cleanup error' ERR
|
||||
|
||||
# Load configuration
|
||||
load_config() {
|
||||
local config_file="$BACKUP_DIR/.env-backup-config"
|
||||
@@ -244,9 +270,18 @@ backup_env_files() {
|
||||
|
||||
echo -e "${YELLOW}Starting .env files backup...${NC}"
|
||||
|
||||
# Initialize metrics if enabled
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_backup_start "env-files" "$DOCKER_DIR" "$BACKUP_DIR"
|
||||
metrics_status_update "initializing" "Preparing environment files backup"
|
||||
fi
|
||||
|
||||
# Check if backup directory exists
|
||||
if [ ! -d "$BACKUP_DIR" ]; then
|
||||
echo -e "${RED}Backup directory not found. Run with --init first.${NC}"
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_backup_complete "failed" "Backup directory not found"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -259,11 +294,21 @@ backup_env_files() {
|
||||
local backup_count=0
|
||||
local unchanged_count=0
|
||||
|
||||
# Update metrics for scanning phase
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "scanning" "Scanning for environment files"
|
||||
fi
|
||||
|
||||
# Process each .env file using a temp file to avoid subshell issues
|
||||
local temp_file
|
||||
temp_file=$(mktemp)
|
||||
find_env_files "$DOCKER_DIR" > "$temp_file"
|
||||
|
||||
# Update metrics for copying phase
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "copying" "Backing up environment files"
|
||||
fi
|
||||
|
||||
while IFS= read -r env_file; do
|
||||
if [ -n "$env_file" ]; then
|
||||
# Determine relative path and backup location
|
||||
@@ -291,9 +336,24 @@ backup_env_files() {
|
||||
|
||||
if [ "$needs_backup" = "true" ]; then
|
||||
# Copy the file
|
||||
cp "$env_file" "$backup_path"
|
||||
echo -e "${GREEN}✓ Backed up: $rel_path${NC}"
|
||||
backup_count=$((backup_count + 1))
|
||||
if cp "$env_file" "$backup_path"; then
|
||||
echo -e "${GREEN}✓ Backed up: $rel_path${NC}"
|
||||
backup_count=$((backup_count + 1))
|
||||
|
||||
# Track file completion in metrics
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
local file_size
|
||||
file_size=$(stat -c%s "$env_file" 2>/dev/null || echo "0")
|
||||
metrics_file_backup_complete "$rel_path" "$file_size" "copied"
|
||||
fi
|
||||
else
|
||||
echo -e "${RED}✗ Failed to backup: $rel_path${NC}"
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
local file_size
|
||||
file_size=$(stat -c%s "$env_file" 2>/dev/null || echo "0")
|
||||
metrics_file_backup_complete "$rel_path" "$file_size" "failed"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Also create a reference docker-compose.yml if it exists
|
||||
local compose_file
|
||||
@@ -306,6 +366,13 @@ backup_env_files() {
|
||||
fi
|
||||
else
|
||||
echo -e "${YELLOW}- Unchanged: $rel_path${NC}"
|
||||
|
||||
# Track unchanged file in metrics
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
local file_size
|
||||
file_size=$(stat -c%s "$env_file" 2>/dev/null || echo "0")
|
||||
metrics_file_backup_complete "$rel_path" "$file_size" "unchanged"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done < "$temp_file"
|
||||
@@ -315,9 +382,18 @@ backup_env_files() {
|
||||
|
||||
if [ "$dry_run" = "true" ]; then
|
||||
echo -e "${BLUE}Dry run completed. No files were actually backed up.${NC}"
|
||||
# Update metrics for dry run completion
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "completed" "Dry run completed successfully"
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Update metrics for committing phase
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "committing" "Committing changes to repository"
|
||||
fi
|
||||
|
||||
# Update README with backup information
|
||||
sed -i "/^## Last Backup/,$ d" README.md
|
||||
cat >> README.md << EOF
|
||||
@@ -347,22 +423,42 @@ EOF
|
||||
|
||||
echo -e "${GREEN}Changes committed to local repository${NC}"
|
||||
|
||||
# Update metrics for pushing phase
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "pushing" "Pushing changes to remote repository"
|
||||
fi
|
||||
|
||||
# Push to remote if configured
|
||||
if git remote get-url origin >/dev/null 2>&1; then
|
||||
echo -e "${YELLOW}Pushing to remote repository...${NC}"
|
||||
if git push origin main 2>/dev/null || git push origin master 2>/dev/null; then
|
||||
echo -e "${GREEN}✓ Successfully pushed to remote repository${NC}"
|
||||
log "Backup completed and pushed to remote - $backup_count files backed up, $unchanged_count unchanged"
|
||||
|
||||
# Update metrics for successful push
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "completed" "Backup completed and pushed to remote"
|
||||
fi
|
||||
else
|
||||
echo -e "${YELLOW}Warning: Could not push to remote repository${NC}"
|
||||
echo "You may need to:"
|
||||
echo "1. Create the repository in Gitea first"
|
||||
echo "2. Set up authentication (SSH key or token)"
|
||||
log "Backup completed locally but failed to push to remote - $backup_count files backed up"
|
||||
|
||||
# Update metrics for push failure
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "completed_with_warnings" "Backup completed but failed to push to remote"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo -e "${YELLOW}No remote repository configured${NC}"
|
||||
log "Backup completed locally - $backup_count files backed up, $unchanged_count unchanged"
|
||||
|
||||
# Update metrics for local-only backup
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "completed" "Backup completed locally (no remote configured)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -371,12 +467,23 @@ EOF
|
||||
echo " - Files backed up: $backup_count"
|
||||
echo " - Files unchanged: $unchanged_count"
|
||||
echo " - Backup location: $BACKUP_DIR"
|
||||
|
||||
# Finalize metrics
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
cleanup
|
||||
fi
|
||||
}
|
||||
|
||||
# Restore .env files
|
||||
restore_env_files() {
|
||||
echo -e "${YELLOW}Starting .env files restore...${NC}"
|
||||
|
||||
# Initialize metrics if enabled
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_backup_start "env-files-restore" "$BACKUP_DIR" "$DOCKER_DIR"
|
||||
metrics_status_update "initializing" "Preparing environment files restore"
|
||||
fi
|
||||
|
||||
if [ ! -d "$BACKUP_DIR" ]; then
|
||||
echo -e "${RED}Backup directory not found at $BACKUP_DIR${NC}"
|
||||
echo "Either run --init first or clone your backup repository to this location."
|
||||
@@ -386,6 +493,11 @@ restore_env_files() {
|
||||
cd "$BACKUP_DIR"
|
||||
load_config
|
||||
|
||||
# Update metrics for pulling phase
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "pulling" "Pulling latest changes from remote"
|
||||
fi
|
||||
|
||||
# Pull latest changes if remote is configured
|
||||
if git remote get-url origin >/dev/null 2>&1; then
|
||||
echo -e "${YELLOW}Pulling latest changes from remote...${NC}"
|
||||
@@ -395,6 +507,11 @@ restore_env_files() {
|
||||
local restore_count=0
|
||||
local error_count=0
|
||||
|
||||
# Update metrics for restoring phase
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
metrics_status_update "restoring" "Restoring environment files"
|
||||
fi
|
||||
|
||||
# Use a temp file to avoid subshell issues
|
||||
local temp_file
|
||||
temp_file=$(mktemp)
|
||||
@@ -434,9 +551,23 @@ restore_env_files() {
|
||||
if cp "$backup_file" "$target_file"; then
|
||||
echo -e "${GREEN}✓ Restored: $rel_path${NC}"
|
||||
restore_count=$((restore_count + 1))
|
||||
|
||||
# Track file restoration in metrics
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
local file_size
|
||||
file_size=$(stat -c%s "$target_file" 2>/dev/null || echo "0")
|
||||
metrics_file_backup_complete "$rel_path" "$file_size" "restored"
|
||||
fi
|
||||
else
|
||||
echo -e "${RED}✗ Failed to restore: $rel_path${NC}"
|
||||
error_count=$((error_count + 1))
|
||||
|
||||
# Track failed restoration in metrics
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
local file_size
|
||||
file_size=$(stat -c%s "$backup_file" 2>/dev/null || echo "0")
|
||||
metrics_file_backup_complete "$rel_path" "$file_size" "restore_failed"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done < "$temp_file"
|
||||
@@ -450,6 +581,15 @@ restore_env_files() {
|
||||
echo " - Errors: $error_count"
|
||||
|
||||
log "Restore completed - $restore_count files restored, $error_count errors"
|
||||
|
||||
# Finalize metrics for restore
|
||||
if [[ "$METRICS_ENABLED" == "true" ]]; then
|
||||
if [[ $error_count -gt 0 ]]; then
|
||||
metrics_backup_complete "completed_with_errors" "Restore completed with $error_count errors"
|
||||
else
|
||||
metrics_backup_complete "success" "Environment files restore completed successfully"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Main function
|
||||
|
||||
Reference in New Issue
Block a user