feat(installer/cli): Add OS detection and git auto-installation support to install.sh and improve service setup prompt in CLI

This commit is contained in:
Philipp Kunz 2025-03-25 11:25:03 +00:00
parent c7db209da7
commit bd5b85f6b0
4 changed files with 105 additions and 6 deletions

View File

@ -1,5 +1,12 @@
# Changelog # Changelog
## 2025-03-25 - 2.3.0 - feat(installer/cli)
Add OS detection and git auto-installation support to install.sh and improve service setup prompt in CLI
- Implemented helper functions in install.sh to detect OS type and automatically install git if missing
- Prompt user for git installation if not present before cloning the repository
- Enhanced CLI service setup flow to offer starting the NUPST service immediately after installation
## 2025-03-25 - 2.2.0 - feat(cli) ## 2025-03-25 - 2.2.0 - feat(cli)
Add 'config' command to display current configuration and update CLI help Add 'config' command to display current configuration and update CLI help

View File

@ -17,6 +17,78 @@ if [ ! -t 0 ]; then
PIPED=1 PIPED=1
fi fi
# Helper function to detect OS type
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si | tr '[:upper:]' '[:lower:]')
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
OS=$DISTRIB_ID
elif [ -f /etc/debian_version ]; then
OS="debian"
elif [ -f /etc/redhat-release ]; then
if grep -q "CentOS" /etc/redhat-release; then
OS="centos"
elif grep -q "Fedora" /etc/redhat-release; then
OS="fedora"
else
OS="rhel"
fi
else
OS=$(uname -s)
fi
echo $OS
}
# Helper function to install git
install_git() {
OS=$(detect_os)
echo "Detected OS: $OS"
case "$OS" in
ubuntu|debian|pop|mint|elementary|kali|zorin)
echo "Installing git using apt..."
apt-get update && apt-get install -y git
;;
fedora|rhel|centos|almalinux|rocky)
echo "Installing git using dnf/yum..."
if command -v dnf &> /dev/null; then
dnf install -y git
else
yum install -y git
fi
;;
arch|manjaro|endeavouros|garuda)
echo "Installing git using pacman..."
pacman -Sy --noconfirm git
;;
opensuse*|suse|sles)
echo "Installing git using zypper..."
zypper install -y git
;;
alpine)
echo "Installing git using apk..."
apk add git
;;
*)
echo "Unsupported OS: $OS"
echo "Please install git manually and run the installer again."
exit 1
;;
esac
# Check if git was installed successfully
if ! command -v git &> /dev/null; then
echo "Failed to install git. Please install git manually and run the installer again."
exit 1
fi
echo "Git installed successfully."
}
# Define installation directory # Define installation directory
INSTALL_DIR="/opt/nupst" INSTALL_DIR="/opt/nupst"
REPO_URL="https://code.foss.global/serve.zone/nupst.git" REPO_URL="https://code.foss.global/serve.zone/nupst.git"
@ -26,8 +98,15 @@ if [ $PIPED -eq 1 ]; then
# Check if git is installed # Check if git is installed
if ! command -v git &> /dev/null; then if ! command -v git &> /dev/null; then
echo "Git is required but not installed. Please install git first." echo "Git is required but not installed."
exit 1 read -p "Would you like to install git now? (Y/n): " install_git_prompt
if [[ "$install_git_prompt" =~ ^[Nn]$ ]]; then
echo "Git installation skipped. Please install git manually and run the installer again."
exit 1
else
install_git
fi
fi fi
# Check if installation directory exists # Check if installation directory exists

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@serve.zone/nupst', name: '@serve.zone/nupst',
version: '2.2.0', version: '2.3.0',
description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices'
} }

View File

@ -834,7 +834,7 @@ Options:
} }
/** /**
* Optionally enable systemd service * Optionally enable and start systemd service
* @param prompt Function to prompt for user input * @param prompt Function to prompt for user input
*/ */
private async optionallyEnableService(prompt: (question: string) => Promise<string>): Promise<void> { private async optionallyEnableService(prompt: (question: string) => Promise<string>): Promise<void> {
@ -843,8 +843,21 @@ Options:
} else { } else {
const setupService = await prompt('Would you like to enable NUPST as a system service? (y/N): '); const setupService = await prompt('Would you like to enable NUPST as a system service? (y/N): ');
if (setupService.toLowerCase() === 'y') { if (setupService.toLowerCase() === 'y') {
await this.nupst.getSystemd().install(); try {
console.log('Service installed. Use "nupst start" to start the service.'); await this.nupst.getSystemd().install();
console.log('Service installed and enabled to start on boot.');
// Ask if the user wants to start the service now
const startService = await prompt('Would you like to start the NUPST service now? (Y/n): ');
if (startService.toLowerCase() !== 'n') {
await this.nupst.getSystemd().start();
console.log('NUPST service started successfully.');
} else {
console.log('Service not started. Use "nupst start" to start the service manually.');
}
} catch (error) {
console.error(`Failed to setup service: ${error.message}`);
}
} }
} }
} }