Spaces:
No application file
No application file
| # Script: Installing Base sys and CLI Dev Toolkit | |
| # Description: Performs various system operations and logs the results | |
| # Author: Hector Agosto 2024 | |
| # Version: 0.0.1b5 | |
| # License under Apache 2 | |
| # Check for required commands | |
| for cmd in jq curl; do | |
| command -v "$cmd" &> /dev/null || { echo "Error: $cmd is missing. Please install it."; exit 1; } | |
| done | |
| # Configuration | |
| log_file="script_execution_$(date +%Y%m%d_%H%M%S).log" | |
| step_definitions=( | |
| '{ "step": "Fetching system kernel info", "command": "uname -a", "description": "Retrieves the current system kernel information" }' | |
| '{ "step": "Updating package lists", "command": "sudo apt update", "description": "Updates the list of available software packages" }' | |
| '{ "step": "Upgrading packages", "command": "sudo apt upgrade -y", "description": "Upgrading installed software packages to the latest versions" }' | |
| '{ "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" }' | |
| '{ "step": "Reading log file", "command": "cat info.log", "description": "Reads and displays the contents of a specified log file" }' | |
| ) | |
| # Utility functions | |
| print_color() { echo -e "\033[${1}m${2}\033[0m"; } | |
| clear_lines() { for ((i=0; i<$1; i++)); do tput cuu1; tput el; done; } | |
| # Logging function | |
| log() { | |
| local step="$1" | |
| local command="$2" | |
| local status="$3" | |
| local details="$4" | |
| echo "$(date '+%Y-%m-%d %H:%M:%S') | $step | $command | $status ${details:+| $details}" >> /tmp/"$log_file" | |
| } | |
| # Initialize log file | |
| { | |
| echo "Script Execution Log" | |
| echo "====================" | |
| echo "Date: $(date)" | |
| echo "User: $(whoami)" | |
| echo "Hostname: $(hostname)" | |
| echo "====================" | |
| } > /tmp/"$log_file" | |
| # Execute a single step | |
| execute_step() { | |
| local step=$(echo "$1" | jq -r '.step') | |
| local command=$(echo "$1" | jq -r '.command') | |
| local description=$(echo "$1" | jq -r '.description') | |
| local temp_output=$(mktemp) | |
| local status | |
| echo -n "$step (${description})... " | |
| print_color 33 "[INFO]" | |
| if sh -c "$command" > "$temp_output" 2>&1; then | |
| echo; cat "$temp_output"; sleep 2 | |
| clear_lines $(($(wc -l < "$temp_output") + 1)) | |
| echo -n "$step... " | |
| print_color 32 "Success" | |
| status="Success" | |
| else | |
| echo; cat "$temp_output" | |
| print_color 31 "Failed" | |
| status="Failed" | |
| local retry | |
| read -p "Retry this step? (y/n): " retry | |
| if [[ "$retry" =~ ^[Yy]$ ]]; then | |
| execute_step "$1" | |
| status="Success" | |
| return | |
| fi | |
| fi | |
| log "$step" "$command" "$status" "$(cat $temp_output)" | |
| rm "$temp_output" | |
| } | |
| # Main execution | |
| for step in "${step_definitions[@]}"; do | |
| execute_step "$step" | |
| echo "----------------------------------------" | |
| done | |
| print_color 32 "\nAll done! Check the log: /tmp/$log_file\n" | |