Spaces:
No application file
No application file
Upload init_script
Browse files- init_script +86 -0
init_script
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Script: Installing Base sys and CLI Dev Toolkit
|
| 3 |
+
# Description: Performs various system operations and logs the results
|
| 4 |
+
# Author: Hector Agosto 2024
|
| 5 |
+
# Version: 0.0.1b5
|
| 6 |
+
# License under Apache 2
|
| 7 |
+
|
| 8 |
+
# Check for required commands
|
| 9 |
+
for cmd in jq curl; do
|
| 10 |
+
command -v "$cmd" &> /dev/null || { echo "Error: $cmd is missing. Please install it."; exit 1; }
|
| 11 |
+
done
|
| 12 |
+
|
| 13 |
+
# Configuration
|
| 14 |
+
log_file="script_execution_$(date +%Y%m%d_%H%M%S).log"
|
| 15 |
+
step_definitions=(
|
| 16 |
+
'{ "step": "Fetching system kernel info", "command": "uname -a", "description": "Retrieves the current system kernel information" }'
|
| 17 |
+
'{ "step": "Updating package lists", "command": "sudo apt update", "description": "Updates the list of available software packages" }'
|
| 18 |
+
'{ "step": "Upgrading packages", "command": "sudo apt upgrade -y", "description": "Upgrading installed software packages to the latest versions" }'
|
| 19 |
+
'{ "step": "Downloading remote file", "command": "curl -f https://example.com/remote-file -o remote-file.txt", "description": "Downloads a file from a given URL and saves it locally" }'
|
| 20 |
+
'{ "step": "Reading log file", "command": "cat info.log", "description": "Reads and displays the contents of a specified log file" }'
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Utility functions
|
| 24 |
+
print_color() { echo -e "\033[${1}m${2}\033[0m"; }
|
| 25 |
+
clear_lines() { for ((i=0; i<$1; i++)); do tput cuu1; tput el; done; }
|
| 26 |
+
|
| 27 |
+
# Logging function
|
| 28 |
+
log() {
|
| 29 |
+
local step="$1"
|
| 30 |
+
local command="$2"
|
| 31 |
+
local status="$3"
|
| 32 |
+
local details="$4"
|
| 33 |
+
echo "$(date '+%Y-%m-%d %H:%M:%S') | $step | $command | $status ${details:+| $details}" >> /tmp/"$log_file"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Initialize log file
|
| 37 |
+
{
|
| 38 |
+
echo "Script Execution Log"
|
| 39 |
+
echo "===================="
|
| 40 |
+
echo "Date: $(date)"
|
| 41 |
+
echo "User: $(whoami)"
|
| 42 |
+
echo "Hostname: $(hostname)"
|
| 43 |
+
echo "===================="
|
| 44 |
+
} > /tmp/"$log_file"
|
| 45 |
+
|
| 46 |
+
# Execute a single step
|
| 47 |
+
execute_step() {
|
| 48 |
+
local step=$(echo "$1" | jq -r '.step')
|
| 49 |
+
local command=$(echo "$1" | jq -r '.command')
|
| 50 |
+
local description=$(echo "$1" | jq -r '.description')
|
| 51 |
+
local temp_output=$(mktemp)
|
| 52 |
+
local status
|
| 53 |
+
|
| 54 |
+
echo -n "$step (${description})... "
|
| 55 |
+
print_color 33 "[INFO]"
|
| 56 |
+
|
| 57 |
+
if sh -c "$command" > "$temp_output" 2>&1; then
|
| 58 |
+
echo; cat "$temp_output"; sleep 2
|
| 59 |
+
clear_lines $(($(wc -l < "$temp_output") + 1))
|
| 60 |
+
echo -n "$step... "
|
| 61 |
+
print_color 32 "Success"
|
| 62 |
+
status="Success"
|
| 63 |
+
else
|
| 64 |
+
echo; cat "$temp_output"
|
| 65 |
+
print_color 31 "Failed"
|
| 66 |
+
status="Failed"
|
| 67 |
+
local retry
|
| 68 |
+
read -p "Retry this step? (y/n): " retry
|
| 69 |
+
if [[ "$retry" =~ ^[Yy]$ ]]; then
|
| 70 |
+
execute_step "$1"
|
| 71 |
+
status="Success"
|
| 72 |
+
return
|
| 73 |
+
fi
|
| 74 |
+
fi
|
| 75 |
+
|
| 76 |
+
log "$step" "$command" "$status" "$(cat $temp_output)"
|
| 77 |
+
rm "$temp_output"
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
# Main execution
|
| 81 |
+
for step in "${step_definitions[@]}"; do
|
| 82 |
+
execute_step "$step"
|
| 83 |
+
echo "----------------------------------------"
|
| 84 |
+
done
|
| 85 |
+
|
| 86 |
+
print_color 32 "\nAll done! Check the log: /tmp/$log_file\n"
|