Update auto_server_setup.sh

This commit is contained in:
Marco Gallegos
2025-05-18 12:12:38 -06:00
committed by GitHub
parent ebcc3379a7
commit eaf675c339

View File

@@ -5,25 +5,23 @@
# Samba share • Oh-My-Zsh (plugins + alias) # Samba share • Oh-My-Zsh (plugins + alias)
# Utilidades básicas (SSH, net-tools, htop, ufw, fail2ban, avahi) # Utilidades básicas (SSH, net-tools, htop, ufw, fail2ban, avahi)
# Visual: barra de progreso con emojis 🚀🛠️ # Visual: barra de progreso con emojis 🚀🛠️
# Todo el output se registra en /var/log/auto_server_setup.log # Todos los logs en /var/log/auto_server_setup.log
set -euo pipefail set -euo pipefail
# Workaround para evitar errores de "unbound variable" en funciones
width=30
# Logfile setup # Logfile setup
LOGFILE="/var/log/auto_server_setup.log" debug_log="/var/log/auto_server_setup.log"
mkdir -p "$(dirname "$LOGFILE")" mkdir -p "$(dirname "$debug_log")"
: > "$LOGFILE" : > "$debug_log"
exec > >(tee -a "$LOGFILE") 2>&1 exec > >(tee -a "$debug_log") 2>&1
LOG() { echo -e "\033[1;32m▶ $*\033[0m"; } LOG() { echo -e "\033[1;32m▶ $*\033[0m"; }
# Check ports function
# Función para verificar puertos libres
check_ports() { check_ports() {
local svc="$1"; shift local name="$1"; shift
for port in "$@"; do for port in "$@"; do
if ss -tulpn | grep -q ":$port "; then if ss -tulpn | grep -q ":${port} "; then
LOG "⚠️ Puertos en uso para $svc (puerto $port), omitiendo $svc" LOG "⚠️ $name: puerto $port en uso, se omitirá este servicio"
return 1 return 1
fi fi
done done
@@ -31,41 +29,46 @@ check_ports() {
} }
############################################################################## ##############################################################################
# Barra de progreso estética # # Barra de progreso estética #
############################################################################## ##############################################################################
STEPS_TOTAL=17 STEPS_TOTAL=17
STEP_NOW=0 STEP_NOW=0
bar() { bar() {
clear clear
local width=30 filled=$(( STEP_NOW * width / STEPS_TOTAL )) empty=$(( width - filled )) gauge local width=30
gauge="$(printf '%0.s🟩' $(seq 1 $filled))$(printf '%0.s⬜' $(seq 1 $empty))" local filled empty gauge
printf "\n %s %3d%% [ %s ]\n" "$gauge" $(( STEP_NOW * 100 / STEPS_TOTAL )) "$1" filled=$(( STEP_NOW * width / STEPS_TOTAL ))
empty=$(( width - filled ))
gauge="$(printf '🟩%.0s' $(seq 1 $filled))$(printf '⬜%.0s' $(seq 1 $empty))"
printf "\n %s %3d%% [ %s ]\n" "$gauge" "${STEP_NOW}*100/$STEPS_TOTAL" "$1"
}
next() {
STEP_NOW=$(( STEP_NOW + 1 ))
bar "$1"
} }
next() { STEP_NOW=$(( STEP_NOW + 1 )); bar "$1"; }
############################################################################## ##############################################################################
# 0. Root check # # 0. Root check #
############################################################################## ##############################################################################
next "🔑 Verificando root" next "🔑 Verificando permisos root"
[[ $(id -u) -eq 0 ]] || { echo "⚠️ Run as root or sudo." >&2; exit 1; } [[ $(id -u) -eq 0 ]] || { echo "⚠️ Ejecuta este script como root o con sudo" >&2; exit 1; }
############################################################################## ##############################################################################
# 1. Wi-Fi Configuration # # 1. Wi-Fi Configuration #
############################################################################## ##############################################################################
next "📶 Configuración Wi-Fi" next "📶 Configuración Wi-Fi"
if lspci | grep -i wireless >/dev/null || lsusb | grep -i wireless >/dev/null; then if lspci | grep -qi wireless || lsusb | grep -qi wireless; then
LOG "Adaptador Wi-Fi detectado" LOG "Adaptador Wi-Fi detectado"
apt update && apt install -y wpasupplicant wireless-tools apt update && apt install -y wpasupplicant wireless-tools
read -rp "➤ SSID de la red Wi-Fi: " WIFI_SSID read -rp "➤ SSID Wi-Fi: " WIFI_SSID
read -rsp "➤ Contraseña Wi-Fi: " WIFI_PASS; echo read -rsp "➤ Contraseña Wi-Fi: " WIFI_PASS; echo
cat > /etc/wpa_supplicant/wpa_supplicant.conf <<EOF cat > /etc/wpa_supplicant/wpa_supplicant.conf <<EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1 update_config=1
country=MX country=MX
network={ network={
ssid="${WIFI_SSID}" ssid="${WIFI_SSID}"
psk="${WIFI_PASS}" psk="${WIFI_PASS}"
} }
EOF EOF
chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
@@ -74,7 +77,7 @@ EOF
dhclient -v wlan0 || dhclient -v wlp2s0 || true dhclient -v wlan0 || dhclient -v wlp2s0 || true
LOG "Wi-Fi configurado" LOG "Wi-Fi configurado"
else else
LOG "No se detectó adaptador Wi-Fi; omitiendo configuración" LOG "No se detectó adaptador Wi-Fi, omitiendo"
fi fi
############################################################################## ##############################################################################
@@ -87,24 +90,24 @@ clear && neofetch
############################################################################## ##############################################################################
# 3. Hostname & local domain # # 3. Hostname & local domain #
############################################################################## ##############################################################################
next "🖥️ Hostname & dominio" next "🖥️ Ajuste de hostname y dominio"
DEFAULT_HOST="$(hostname)" default_host="$(hostname)"
read -rp "➤ New hostname [${DEFAULT_HOST}]: " NEW_HOST read -rp "➤ Nuevo hostname [${default_host}]: " NEW_HOST
NEW_HOST="${NEW_HOST:-$DEFAULT_HOST}" NEW_HOST="${NEW_HOST:-$default_host}"
echo "$NEW_HOST" >/etc/hostname echo "$NEW_HOST" >/etc/hostname
sed -i "s/127.0.1.1.*/127.0.1.1\t$NEW_HOST/" /etc/hosts || true sed -i "s/127.0.1.1.*/127.0.1.1\t$NEW_HOST/" /etc/hosts || true
hostname "$NEW_HOST" hostname "$NEW_HOST"
read -rp "➤ Configure local domain? (e.g., server.local) [Y/n]: " DOM read -rp "➤ Configurar dominio local (ej. server.local)? [Y/n]: " CONF_DOMAIN
if [[ ${DOM,,} =~ ^y ]]; then if [[ ${CONF_DOMAIN,,} =~ ^y ]]; then
read -rp "➤ Domain: " LOCAL_DOMAIN read -rp "➤ Nombre de dominio: " LOCAL_DOMAIN
echo "127.0.0.1\t$LOCAL_DOMAIN" >>/etc/hosts echo "127.0.0.1\t$LOCAL_DOMAIN" >>/etc/hosts
LOG "Dominio local configurado: $LOCAL_DOMAIN" LOG "Dominio local: $LOCAL_DOMAIN"
fi fi
############################################################################## ##############################################################################
# 4. Basic utilities # # 4. Basic utilities #
############################################################################## ##############################################################################
next "🛠️ Instalando utilidades" next "🛠️ Instalando utilidades básicas"
apt update && apt -y upgrade apt update && apt -y upgrade
apt install -y \ apt install -y \
openssh-server net-tools htop curl wget gnupg2 ca-certificates lsb-release \ openssh-server net-tools htop curl wget gnupg2 ca-certificates lsb-release \
@@ -115,13 +118,15 @@ apt install -y \
############################################################################## ##############################################################################
next "🐳 Instalando Docker" next "🐳 Instalando Docker"
apt install -y apt-transport-https software-properties-common apt install -y apt-transport-https software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor \
-o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" >/etc/apt/sources.list.d/docker.list https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
>/etc/apt/sources.list.d/docker.list
apt update && apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin apt update && apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
systemctl enable docker && systemctl start docker systemctl enable docker && systemctl start docker
groupadd docker || true groupadd docker || true
read -rp "➤ User for Docker group [${SUDO_USER:-$USER}]: " DU read -rp "➤ Usuario Docker (sin sudo) [${SUDO_USER:-$USER}]: " DU
DOCKER_USER="${DU:-${SUDO_USER:-$USER}}" DOCKER_USER="${DU:-${SUDO_USER:-$USER}}"
usermod -aG docker "$DOCKER_USER" usermod -aG docker "$DOCKER_USER"
@@ -131,7 +136,7 @@ usermod -aG docker "$DOCKER_USER"
next "🔧 Desplegando Portainer" next "🔧 Desplegando Portainer"
if check_ports "Portainer" 9443; then if check_ports "Portainer" 9443; then
docker volume create portainer_data docker volume create portainer_data
docker run -d --name portainer \ docker run -d --name portainer \
--restart=always -p 9443:9443 \ --restart=always -p 9443:9443 \
-v /var/run/docker.sock:/var/run/docker.sock \ -v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data portainer/portainer-ce:latest -v portainer_data:/data portainer/portainer-ce:latest
@@ -198,17 +203,17 @@ if check_ports "CasaOS" 80 443; then
curl -fsSL https://get.casaos.io | bash curl -fsSL https://get.casaos.io | bash
LOG "CasaOS: http://$NEW_HOST" LOG "CasaOS: http://$NEW_HOST"
else else
LOG "Salteando CasaOS: puertos 80/443 en uso" LOG "CasaOS omitido: puertos 80/443 en uso"
fi fi
############################################################################## ##############################################################################
# 12. Samba share # # 12. Samba share #
############################################################################## ##############################################################################
next "📁 Configurando Samba" next "📁 Configurando Samba"
read -rp "➤ Folder to share (full path): " SMB_DIR read -rp "➤ Carpeta a compartir (ruta completa): " SMB_DIR
mkdir -p "$SMB_DIR" mkdir -p "$SMB_DIR"
read -rp "➤ Samba user: " SMB_USER read -rp "➤ Usuario Samba: " SMB_USER
read -srp "➤ Samba password: " SMB_PASS; echo read -srp "➤ Contraseña Samba: " SMB_PASS; echo
adduser --gecos "" --disabled-password "$SMB_USER" adduser --gecos "" --disabled-password "$SMB_USER"
echo "$SMB_USER:$SMB_PASS" | chpasswd echo "$SMB_USER:$SMB_PASS" | chpasswd
(echo "$SMB_PASS"; echo "$SMB_PASS") | smbpasswd -s -a "$SMB_USER" (echo "$SMB_PASS"; echo "$SMB_PASS") | smbpasswd -s -a "$SMB_USER"
@@ -248,7 +253,7 @@ systemctl enable fail2ban && systemctl start fail2ban
############################################################################## ##############################################################################
next "✅ Resumen" next "✅ Resumen"
echo echo
LOG "Access your services:" LOG "Accede a tus servicios:"
echo " - Portainer → https://$NEW_HOST:9443" echo " - Portainer → https://$NEW_HOST:9443"
echo " - CapRover → http://$NEW_HOST:3000" echo " - CapRover → http://$NEW_HOST:3000"
echo " - NPM → http://$NEW_HOST:81" echo " - NPM → http://$NEW_HOST:81"
@@ -260,9 +265,9 @@ echo " - Samba → //$NEW_HOST/$SMB_USER"
############################################################################## ##############################################################################
# 16. Reboot if desired # # 16. Reboot if desired #
############################################################################## ##############################################################################
read -rp "🔄 Reboot now? [y/N]: " REBOOT read -rp "🔄 Reiniciar ahora? [y/N]: " REBOOT
if [[ ${REBOOT,,} == y ]]; then if [[ ${REBOOT,,} == y ]]; then
reboot reboot
else else
LOG "Setup complete. Reboot manually to apply all changes." LOG "Script finalizado. Reinicia manualmente para aplicar todo."
fi fi