4.7 KiB
CyberPanel Installation Scripts Refactoring
Date: January 5, 2025
Overview
This document outlines the refactoring work performed on CyberPanel's installation scripts to consolidate common functionality and improve code maintainability.
Files Modified
/install/install.py/install/installCyberPanel.py/install/install_utils.py(newly created)
Refactoring Summary
1. Created Shared Utility Module
Created install_utils.py in the /install/ directory to house common functions used by both install.py and installCyberPanel.py.
2. Functions Moved to install_utils.py
System Detection Functions
-
FetchCloudLinuxAlmaVersionVersion()- Detects CloudLinux and AlmaLinux versions
- Previously duplicated in both files with identical implementations
-
get_Ubuntu_release()- Gets Ubuntu release version from
/etc/lsb-release - Added parameters to handle minor differences between implementations
- Gets Ubuntu release version from
-
get_distro()- Detects Linux distribution (Ubuntu, CentOS, CentOS 8, OpenEuler)
- Only existed in
install.py, now shared
Output and Logging
stdOut(message, log=0, do_exit=0, code=os.EX_OK)- Standard output function with timestamps
- Enhanced with color support:
- 🔴 Red: Errors, failures, critical issues
- 🟡 Yellow: Warnings and alerts
- 🟢 Green: Success messages
- 🔵 Blue: Running/processing operations
- 🟣 Purple: Default/general messages
- Colors automatically disabled when output is piped
Package Management
-
get_package_install_command(distro, package_name, options="")- Returns appropriate install command for the distribution
- Handles apt-get, yum, and dnf package managers
-
get_package_remove_command(distro, package_name)- Returns appropriate remove command for the distribution
Command Execution
-
resFailed(distro, res)- Checks if command execution failed based on return code
-
call(command, distro, bracket, message, log=0, do_exit=0, code=os.EX_OK, shell=False)- Executes shell commands with retry logic (3 attempts)
- Provides consistent error handling and logging
Password Generation
-
char_setdictionary- Character sets for password generation (kept for backward compatibility)
-
generate_pass(length=14)- Generates cryptographically secure passwords
- Uses
secretsmodule instead ofrandomfor better security
-
generate_random_string(length=32, include_special=False)- Flexible string generation with optional special characters
LiteSpeed Management
format_restart_litespeed_command(server_root_path)- Formats the LiteSpeed restart command
3. Distribution Constants
Moved distribution constants to install_utils.py:
ubuntu = 0
centos = 1
cent8 = 2
openeuler = 3
4. Key Improvements
Security Enhancements
- Password generation now uses
secretsmodule for cryptographic security - Removed usage of
random.choice()for password generation
Code Quality
- Eliminated code duplication across files
- Single source of truth for common functionality
- Consistent error handling and logging
Maintainability
- Centralized utility functions in one location
- Easier to update and maintain shared functionality
- Better organization of code
Visual Improvements
- Added color-coded output for better readability
- Automatic detection of terminal capabilities
- Clear visual distinction between error, warning, success, and info messages
5. Backward Compatibility
All changes maintain backward compatibility:
- Function signatures preserved where possible
- Wrapper functions created in original locations when needed
installCyberPanel.pycontinues to useinstall.preFlightsChecks.callthrough import
6. Testing
Created test scripts to verify functionality:
- Color output testing
- Password generation verification
- Ensured all functions work as expected
Benefits
- Reduced Code Duplication: Common functions now exist in single location
- Improved Security: Cryptographically secure password generation
- Better User Experience: Color-coded output for easier reading
- Easier Maintenance: Changes to common functions only need to be made once
- Consistent Behavior: Both installation scripts now use identical implementations
Future Recommendations
- Consider moving more shared functionality to
install_utils.py - Add unit tests for utility functions
- Consider creating additional utility modules for specific domains (e.g.,
network_utils.py,database_utils.py) - Document function parameters and return values more thoroughly
- Consider adding configuration file support for installation parameters