137 lines
4.4 KiB
Bash
137 lines
4.4 KiB
Bash
#!/usr/bin/env bash
|
|
# safe/install-nodesource.sh
|
|
# Replaces: curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
|
|
#
|
|
# What this does:
|
|
# 1. Installs prerequisites (apt-transport-https, ca-certificates, curl, gnupg)
|
|
# 2. Downloads and imports NodeSource GPG key
|
|
# 3. Creates apt sources file for Node.js 22.x nodistro suite
|
|
# 4. Runs apt update
|
|
# 5. Installs nodejs package
|
|
#
|
|
# No pipe to bash. Each step is explicit and auditable.
|
|
set -euo pipefail
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
CYAN='\033[0;36m'
|
|
BOLD='\033[1m'
|
|
NC='\033[0m'
|
|
|
|
NODE_VERSION="${1:-22.x}" # Default: Node.js 22.x
|
|
ARCH="${2:-$(dpkg --print-architecture)}"
|
|
|
|
echo ""
|
|
echo -e "${BOLD}=== Safe NodeSource Node.js ${NODE_VERSION} setup ===${NC}"
|
|
echo " This script sets up the NodeSource apt repository manually."
|
|
echo " No curl|bash piping. Every step is shown."
|
|
echo ""
|
|
|
|
# Pre-flight
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo -e "${YELLOW}[WARN]${NC} Not running as root. Some steps may fail."
|
|
echo " Run with sudo or as root."
|
|
fi
|
|
|
|
if [ ! -f /etc/debian_version ]; then
|
|
echo -e "${RED}[FAIL]${NC} This script is only supported on Debian-based systems."
|
|
echo " /etc/debian_version not found."
|
|
exit 1
|
|
fi
|
|
|
|
if [ "$ARCH" != "amd64" ] && [ "$ARCH" != "arm64" ]; then
|
|
echo -e "${RED}[FAIL]${NC} Unsupported architecture: $ARCH"
|
|
echo " Only amd64 and arm64 are supported by NodeSource."
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "${CYAN}[INFO]${NC} Target: Node.js $NODE_VERSION on $ARCH"
|
|
echo ""
|
|
|
|
# ── Step 1: Install prerequisites ──────────────────────────
|
|
echo -e "${BOLD}[1/5] Installing prerequisites${NC}"
|
|
echo "----------------------------------------"
|
|
echo " apt-get update"
|
|
apt-get update -qq
|
|
|
|
echo " apt-get install -y apt-transport-https ca-certificates curl gnupg"
|
|
apt-get install -y apt-transport-https ca-certificates curl gnupg
|
|
|
|
# ── Step 2: Import GPG key ──────────────────────────────
|
|
echo ""
|
|
echo -e "${BOLD}[2/5] Importing NodeSource GPG key${NC}"
|
|
echo "----------------------------------------"
|
|
GPG_KEY_URL="https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key"
|
|
GPG_KEY_FILE="/tmp/nodesource-repo.gpg.key"
|
|
GPG_KEYRING="/usr/share/keyrings/nodesource.gpg"
|
|
|
|
echo " Download: $GPG_KEY_URL"
|
|
echo " -> $GPG_KEY_FILE"
|
|
curl -fsSL "$GPG_KEY_URL" -o "$GPG_KEY_FILE"
|
|
|
|
echo " Import: gpg --dearmor -o $GPG_KEYRING"
|
|
gpg --dearmor -o "$GPG_KEYRING" "$GPG_KEY_FILE"
|
|
chmod 644 "$GPG_KEYRING"
|
|
|
|
rm -f "$GPG_KEY_FILE"
|
|
echo -e "${GREEN}[OK]${NC} GPG key imported to $GPG_KEYRING"
|
|
|
|
# ── Step 3: Create apt sources file ─────────────────────
|
|
echo ""
|
|
echo -e "${BOLD}[3/5] Creating apt sources file${NC}"
|
|
echo "----------------------------------------"
|
|
|
|
# Convert NODE_VERSION like "22.x" to nodistro suite name
|
|
# NodeSource uses "nodistro" suite, not the actual version string
|
|
nodistro_version=$(echo "$NODE_VERSION" | sed 's/\.x$//') # "22.x" -> "22"
|
|
NODE_SUITE="nodistro"
|
|
|
|
SOURCES_FILE="/etc/apt/sources.list.d/nodesource.sources"
|
|
echo " File: $SOURCES_FILE"
|
|
echo ""
|
|
echo " Content:"
|
|
cat << SOURCES_EOF | tee "$SOURCES_FILE"
|
|
Types: deb
|
|
URIs: https://deb.nodesource.com/node_${nodistro_version}
|
|
Suites: ${NODE_SUITE}
|
|
Components: main
|
|
Architectures: ${ARCH}
|
|
Signed-By: ${GPG_KEYRING}
|
|
SOURCES_EOF
|
|
|
|
echo ""
|
|
echo -e "${GREEN}[OK]${NC} Sources file created"
|
|
|
|
# ── Step 4: apt update ─────────────────────────────────
|
|
echo ""
|
|
echo -e "${BOLD}[4/5] Refreshing package lists${NC}"
|
|
echo "----------------------------------------"
|
|
echo " apt-get update"
|
|
apt-get update -qq
|
|
echo -e "${GREEN}[OK]${NC} Package lists refreshed"
|
|
|
|
# ── Step 5: Install Node.js ─────────────────────────────
|
|
echo ""
|
|
echo -e "${BOLD}[5/5] Installing Node.js${NC}"
|
|
echo "----------------------------------------"
|
|
echo " apt-get install -y nodejs"
|
|
apt-get install -y nodejs
|
|
|
|
# Verify
|
|
NODE_VER=$(node --version 2>/dev/null || echo "not found")
|
|
NPM_VER=$(npm --version 2>/dev/null || echo "not found")
|
|
|
|
if [ "$NODE_VER" != "not found" ]; then
|
|
echo ""
|
|
echo -e "${GREEN}[OK]${NC} Node.js installed:"
|
|
echo " Node: $NODE_VER"
|
|
echo " npm: $NPM_VER"
|
|
else
|
|
echo -e "${RED}[FAIL]${NC} Node.js installation failed"
|
|
exit 1
|
|
fi
|
|
|
|
echo ""
|
|
echo -e "${GREEN}Done.${NC} Node.js $NODE_VER is ready."
|