Skip to content

Refactor chr_install.sh with improved error handling and UX - #16

Merged
gpas45 merged 1 commit into
mainfrom
claude/gifted-archimedes-ujs7a4
Jun 12, 2026
Merged

gpas45 merged 1 commit into
mainfrom
claude/gifted-archimedes-ujs7a4

Conversation

@gpas45

@gpas45 gpas45 commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Summary

Significantly refactored the MikroTik CHR installation script to improve robustness, user experience, and code maintainability. The script now includes proper error handling, automatic RouterOS version detection, and better validation throughout the deployment process.

Key Changes

  • Error Handling: Added set -euo pipefail and a die() helper function for consistent error reporting and early exit on failures
  • Cleanup on Failure: Implemented trap-based cleanup to remove incomplete image directories if the script fails mid-execution
  • Version Detection: Added fetch_channel() function to automatically look up current Long-term and Stable RouterOS releases from MikroTik, with sensible defaults
  • Input Validation: Added validation for version format, VM ID (must be numeric), and image size (must be non-negative)
  • Improved User Prompts: Enhanced prompts with context (showing available versions, suggesting defaults) and using read -r for safer input handling
  • Better Variable Management: Converted to uppercase for constants (e.g., TEMP_DIR, MIKROTIK_DL) and improved quoting throughout for safety
  • Code Organization: Restructured script into logical sections with clear comments (temp dir, version selection, download, VM ID, image creation, VM creation)
  • Disk Naming: Changed disk filename from vm-$vmID-disk-1.qcow2 to vm-$vmID-disk-0.qcow2 for consistency with Proxmox conventions
  • Documentation: Added comprehensive header comment explaining script purpose and capabilities
  • Configurable Defaults: Added VM configuration variables (VM_MEMORY, VM_SOCKETS, VM_CORES, VM_BRIDGE, VM_STORAGE) at the top for easy customization

Notable Implementation Details

  • The script now gracefully handles network failures when fetching version information and allows manual version entry as fallback
  • All external commands are checked for success with proper error messages
  • Temporary and image directories are created with -p flag to handle existing directories safely
  • The cleanup trap ensures partial deployments don't leave orphaned directories

https://claude.ai/code/session_016rtazH8tiWw4fqCuLcEPFC

…rsions

- Quote all variables; validate version, VM ID and image size inputs
- Use exit 1 (via die helper) for error paths instead of exit 0
- Derive Proxmox node name from hostname instead of hardcoding 'pve'
- Check results of wget/unzip/qemu-img/qm and clean up on failure (trap)
- Fetch current Long-term and Stable RouterOS releases and offer as default
- Use conventional disk-0 naming and configurable VM defaults
@gpas45
gpas45 merged commit 97e0ad2 into main Jun 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants