proxmox-node-config/manage-lxc-vm-notes.sh

109 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
set -e
update_notes() {
local vmid="$1"
local type="$2"
echo "[INFO] Procesando $type $vmid..."
# Detectar OS
os=$(pct exec "$vmid" -- cat /etc/os-release 2>/dev/null | grep '^ID=' | cut -d= -f2 | tr -d '"')
[ -z "$os" ] && echo "[WARN] No se pudo detectar OS para $type $vmid. Saltando." && return
echo "[INFO] Sistema operativo detectado: $os"
# Comandos por OS
case "$os" in
alpine)
update_cmd="apk update && apk upgrade --no-cache"
install_base_pkgs="apk add --no-cache docker openssh"
# Comprobar si tailscale está instalado
check_tailscale_installed=$(pct exec "$vmid" -- sh -c "which tailscale 2>/dev/null || echo notfound")
if [[ "$check_tailscale_installed" == "notfound" ]]; then
install_tailscale="apk add --no-cache tailscale"
else
install_tailscale="echo '[INFO] tailscale ya está instalado'"
fi
enable_docker="rc-update add docker && rc-service docker start"
enable_ssh="rc-update add sshd && rc-service sshd start"
enable_tailscale_check='if [ -e /etc/init.d/tailscaled ]; then rc-update add tailscaled; rc-service tailscaled start; fi'
check_tailscale_status='if [ -e /etc/init.d/tailscaled ]; then rc-service tailscaled status; else echo notinstalled; fi'
;;
ubuntu)
update_cmd="apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y"
install_base_pkgs="apt-get install -y docker.io openssh-server"
check_tailscale_installed=$(pct exec "$vmid" -- sh -c "which tailscale 2>/dev/null || echo notfound")
if [[ "$check_tailscale_installed" == "notfound" ]]; then
install_tailscale="apt-get install -y tailscale"
else
install_tailscale="echo '[INFO] tailscale ya está instalado'"
fi
enable_docker="systemctl enable docker && systemctl start docker"
enable_ssh="systemctl enable ssh && systemctl start ssh"
enable_tailscale_check="systemctl enable tailscaled && systemctl start tailscaled"
check_tailscale_status="systemctl is-active tailscaled"
;;
*)
echo "[WARN] OS no soportado: $os. Saltando $vmid."
return
;;
esac
# Ejecutar comandos
pct exec "$vmid" -- sh -c "$update_cmd"
pct exec "$vmid" -- sh -c "$install_base_pkgs || true"
pct exec "$vmid" -- sh -c "$install_tailscale || true"
pct exec "$vmid" -- sh -c "$enable_docker"
pct exec "$vmid" -- sh -c "$enable_ssh"
pct exec "$vmid" -- sh -c "$enable_tailscale_check"
# Estado tailscale
tailscale_status=$(pct exec "$vmid" -- sh -c "$check_tailscale_status" 2>&1)
if echo "$tailscale_status" | grep -q "running"; then
tailscale_note="tailscale UP"
elif echo "$tailscale_status" | grep -q "notinstalled"; then
tailscale_note="tailscale NOT INSTALLED"
else
tailscale_note="tailscale DOWN"
fi
# Obtener contenedores Docker
docker_info=$(pct exec "$vmid" -- sh -c "docker ps --format '{{.Names}} : {{.Image}}'" 2>/dev/null || echo "Sin contenedores")
if [[ -z "$docker_info" ]]; then
docker_info="Sin contenedores"
fi
# Formar nota
full_note=$(printf "# Notes:\n%s\n%s\n" "$tailscale_note" "$docker_info")
# Añadir al archivo de configuración del contenedor
conf_path="/etc/pve/lxc/${vmid}.conf"
# Eliminar notas anteriores si existen
sed -i '/^# Notes:/,$d' "$conf_path"
echo -e "$full_note" >> "$conf_path"
echo "[INFO] Notas actualizadas en $conf_path:"
echo "$full_note"
}
echo "[INFO] Iniciando gestión automática de LXC..."
# Iterar sobre LXC activos
for vmid in $(pct list | awk 'NR>1 {print $1}'); do
update_notes "$vmid" "LXC"
done
# VMs (sin integración por ahora)
for vmid in $(qm list | awk 'NR>1 && $2 == "running" {print $1}'); do
echo "[INFO] Saltando VM $vmid (se requiere integración SSH para gestionar desde el host)"
done
echo "[INFO] Script completado."