101 lines
2.6 KiB
Bash
Executable File
101 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Logger Function
|
|
log() {
|
|
local message="$1"
|
|
local type="$2"
|
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
|
local color
|
|
local endcolor="\033[0m"
|
|
|
|
case "$type" in
|
|
"info") color="\033[38;5;79m" ;;
|
|
"success") color="\033[1;32m" ;;
|
|
"error") color="\033[1;31m" ;;
|
|
*) color="\033[1;34m" ;;
|
|
esac
|
|
|
|
echo -e "${color}${timestamp} - ${message}${endcolor}"
|
|
}
|
|
|
|
# Error handler function
|
|
handle_error() {
|
|
local exit_code=$1
|
|
local error_message="$2"
|
|
log "Error: $error_message (Exit Code: $exit_code)" "error"
|
|
exit $exit_code
|
|
}
|
|
|
|
# Function to check for command availability
|
|
command_exists() {
|
|
command -v "$1" &> /dev/null
|
|
}
|
|
|
|
check_os() {
|
|
if ! [ -f "/etc/redhat-release" ]; then
|
|
echo "Error: This script is only supported on RHEL-based systems."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to install the script prerequisites
|
|
install_pre_reqs() {
|
|
log "Installing pre-requisites" "info"
|
|
|
|
# Run 'yum update'
|
|
if ! yum update -y; then
|
|
handle_error "$?" "Failed to run 'yum update'"
|
|
fi
|
|
|
|
# Install required packages
|
|
if ! yum install -y curl ca-certificates gnupg2; then
|
|
handle_error "$?" "Failed to install required packages"
|
|
fi
|
|
|
|
# Create directory for keyrings
|
|
if ! mkdir -p /etc/pki/rpm-gpg; then
|
|
handle_error "$?" "Failed to create /etc/pki/rpm-gpg directory"
|
|
fi
|
|
|
|
# Remove old keyring if exists
|
|
rm -f /etc/pki/rpm-gpg/nodesource.gpg || true
|
|
|
|
# Download and import the NodeSource GPG key
|
|
if ! curl -fsSL https://rpm.nodesource.com/pub/el/NODESOURCE-GPG-SIGNING-KEY-EL | gpg --dearmor -o /etc/pki/rpm-gpg/nodesource.gpg; then
|
|
handle_error "$?" "Failed to download and import the NodeSource GPG key"
|
|
fi
|
|
}
|
|
|
|
# Function to configure the Node.js repository for RHEL
|
|
configure_repo() {
|
|
local node_version=$1
|
|
|
|
# Create the Nodesource repo file
|
|
cat <<EOF > /etc/yum.repos.d/nodesource.repo
|
|
[nodesource]
|
|
name=Node.js Packages for Enterprise Linux
|
|
baseurl=https://rpm.nodesource.com/pub_$(echo $node_version | tr -d 'x').x/el/\$releasever/\$basearch
|
|
enabled=1
|
|
gpgcheck=1
|
|
gpgkey=file:///etc/pki/rpm-gpg/nodesource.gpg
|
|
EOF
|
|
|
|
# Run 'yum clean all' and 'yum makecache' to refresh the repository
|
|
if ! yum clean all && yum makecache; then
|
|
handle_error "$?" "Failed to refresh repositories"
|
|
else
|
|
log "Repository configured successfully." "success"
|
|
log "To install Node.js, run: yum install nodejs -y" "info"
|
|
fi
|
|
}
|
|
|
|
# Define Node.js version
|
|
NODE_VERSION="20.x"
|
|
|
|
# Check OS
|
|
check_os
|
|
|
|
# Main execution
|
|
install_pre_reqs || handle_error $? "Failed installing pre-requisites"
|
|
configure_repo "$NODE_VERSION" || handle_error $? "Failed configuring repository"
|