feat: añadido archivos básicos para crear el template y los equipos en proxmox
This commit is contained in:
143
teams.sh
Executable file
143
teams.sh
Executable file
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env bash
|
||||
# Se ejecuta en: host Proxmox, después de template.sh.
|
||||
# Uso: ./teams.sh [n] (equipos 1..n; por defecto CT_COUNT)
|
||||
# Clona un CT por equipo, lo aísla con el firewall de PVE y le da una URL
|
||||
# secreta. Después publica las rutas de nginx en el proxy y deja en state/ los
|
||||
# QR y los papelitos para imprimir. Se puede repetir: salta los CT que ya
|
||||
# existen, y un equipo conserva su token aunque se recree su CT.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
# shellcheck source=SCRIPTDIR/common.sh
|
||||
source ./common.sh
|
||||
|
||||
TOKENS=state/tokens.tsv
|
||||
count=${1:-$CT_COUNT}
|
||||
[[ $count =~ ^[1-9][0-9]*$ ]] || die "uso: ./teams.sh [n] (n: número de equipos, por defecto $CT_COUNT)"
|
||||
|
||||
token_of() {
|
||||
awk -F '\t' -v id="$1" '$1 == id { print $2 }' "$TOKENS"
|
||||
}
|
||||
|
||||
create_team() {
|
||||
local n=$1 id=$((CT_ID_BASE + $1)) token
|
||||
|
||||
pct clone "$TEMPLATE_ID" "$id" --hostname "eq$n"
|
||||
pct set "$id" --cores 1 --cpulimit "$CT_CPULIMIT" --memory "$CT_MEMORY_MB" --swap 0 \
|
||||
--onboot 1 \
|
||||
--net0 "name=eth0,bridge=$INT_BRIDGE,ip=$SUBNET.$id/24,rate=$CT_RATE_MBYTES,firewall=1"
|
||||
echo "lxc.cgroup2.pids.max: $CT_PIDS_MAX" >> "/etc/pve/lxc/$id.conf"
|
||||
|
||||
# Solo el proxy puede llegar a ttyd, y no sale nada
|
||||
cat > "/etc/pve/firewall/$id.fw" <<EOF
|
||||
[OPTIONS]
|
||||
enable: 1
|
||||
policy_in: DROP
|
||||
policy_out: DROP
|
||||
ipfilter: 1
|
||||
|
||||
[RULES]
|
||||
IN ACCEPT -source $PROXY_INT_IP -p tcp -dport $TTYD_PORT
|
||||
EOF
|
||||
|
||||
pct start "$id"
|
||||
# systemctl dentro del CT solo responde cuando su systemd ya ha arrancado
|
||||
wait_for 30 pct exec "$id" -- systemctl is-active --quiet multi-user.target ||
|
||||
die "el CT $id no ha terminado de arrancar"
|
||||
|
||||
token=$(token_of "$id")
|
||||
if [[ -z $token ]]; then
|
||||
token=$(openssl rand -hex 4)
|
||||
printf '%s\t%s\n' "$id" "$token" >> "$TOKENS"
|
||||
fi
|
||||
printf 'TTYD_PORT=%s\nTTYD_BASE=/t/%s\n' "$TTYD_PORT" "$token" |
|
||||
pct exec "$id" -- tee /etc/default/ttyd >/dev/null
|
||||
pct exec "$id" -- systemctl restart ttyd
|
||||
pct snapshot "$id" clean
|
||||
}
|
||||
|
||||
# El token de la ruta es la única credencial. proxy_pass sin URI reenvía
|
||||
# /t/<token>/ tal cual, que es la ruta base que espera ttyd
|
||||
write_routes() {
|
||||
local id token
|
||||
while IFS=$'\t' read -r id token; do
|
||||
cat <<EOF
|
||||
location /t/$token/ {
|
||||
proxy_pass http://$SUBNET.$id:$TTYD_PORT;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_read_timeout 2h;
|
||||
}
|
||||
EOF
|
||||
done < "$TOKENS"
|
||||
}
|
||||
|
||||
# Los QR van incrustados para que el HTML se pueda imprimir por sí solo
|
||||
write_slips() {
|
||||
local id token n url qr
|
||||
cat <<'EOF'
|
||||
<!doctype html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Taller de bash: papelitos</title>
|
||||
<style>
|
||||
@page { size: A4; margin: 10mm; }
|
||||
body { margin: 0; font-family: sans-serif; display: flex; flex-wrap: wrap; }
|
||||
.slip { box-sizing: border-box; width: 50%; height: 90mm; padding: 6mm;
|
||||
border: 1px dashed #999; text-align: center; break-inside: avoid; }
|
||||
h1 { margin: 0 0 3mm; font-size: 22pt; }
|
||||
img { width: 45mm; height: 45mm; image-rendering: pixelated; }
|
||||
.url { font: 11pt monospace; word-break: break-all; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
EOF
|
||||
while IFS=$'\t' read -r id token; do
|
||||
n=$((id - CT_ID_BASE))
|
||||
url="https://$DOMAIN/t/$token/"
|
||||
qr=state/qr/eq$n.png
|
||||
qrencode -s 10 -m 2 -o "$qr" "$url"
|
||||
cat <<EOF
|
||||
<div class="slip">
|
||||
<h1>Equipo $n</h1>
|
||||
<img src="data:image/png;base64,$(base64 -w 0 "$qr")" alt="">
|
||||
<p class="url">$url</p>
|
||||
<p>Primer comando: <code>less LEEME</code></p>
|
||||
</div>
|
||||
EOF
|
||||
done < <(sort -n "$TOKENS")
|
||||
echo '</body>'
|
||||
echo '</html>'
|
||||
}
|
||||
|
||||
grep -qs '^template: 1' "/etc/pve/lxc/$TEMPLATE_ID.conf" ||
|
||||
die "no existe la plantilla $TEMPLATE_ID: ejecuta antes ./template.sh"
|
||||
command -v qrencode >/dev/null || die "falta qrencode: apt install qrencode"
|
||||
mkdir -p state/qr
|
||||
touch "$TOKENS"
|
||||
|
||||
for ((n = 1; n <= count; n++)); do
|
||||
id=$((CT_ID_BASE + n))
|
||||
conf=/etc/pve/lxc/$id.conf
|
||||
if [[ ! -e $conf ]]; then
|
||||
echo "eq$n: creando el CT $id"
|
||||
create_team "$n"
|
||||
continue
|
||||
fi
|
||||
echo "eq$n: el CT $id ya existe, se salta"
|
||||
grep -q '^\[clean\]' "$conf" ||
|
||||
echo " aviso: el CT $id no tiene snapshot 'clean', quedó a medias: pct destroy $id --purge y vuelve a ejecutar" >&2
|
||||
[[ -n $(token_of "$id") ]] ||
|
||||
echo " aviso: el CT $id no tiene token en $TOKENS, se queda sin ruta" >&2
|
||||
done
|
||||
|
||||
write_routes > state/taller-routes.conf
|
||||
pct push "$PROXY_ID" state/taller-routes.conf /etc/nginx/taller-routes.conf
|
||||
pct exec "$PROXY_ID" -- nginx -tq
|
||||
pct exec "$PROXY_ID" -- systemctl reload nginx
|
||||
# Un CT recreado conserva su IP pero no su MAC: borra del proxy las entradas ARP obsoletas
|
||||
pct exec "$PROXY_ID" -- ip neigh flush to "$SUBNET.0/24"
|
||||
|
||||
write_slips > state/papelitos.html
|
||||
echo "rutas publicadas; imprime state/papelitos.html"
|
||||
Reference in New Issue
Block a user