Files
bash-101-resources/template.sh

75 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Se ejecuta en: host Proxmox.
# Uso: ./template.sh
# Construye la plantilla (TEMPLATE_ID) de la que se clonan los CT de equipo:
# paquetes, usuario alumno, servicio ttyd, los ficheros de files/ y los retos.
# Si la plantilla ya existe no hace nada; para rehacerla, antes pct destroy.
set -euo pipefail
cd "$(dirname "$0")"
# shellcheck source=SCRIPTDIR/common.sh
source ./common.sh
GEN_LOG=retos/1-logs/gen-access-log.sh
in_ct() {
pct exec "$TEMPLATE_ID" -- "$@"
}
conf=/etc/pve/lxc/$TEMPLATE_ID.conf
if [[ -e $conf ]]; then
grep -q '^template: 1' "$conf" ||
die "el CT $TEMPLATE_ID existe pero no es plantilla (¿quedó a medias?): pct destroy $TEMPLATE_ID"
echo "la plantilla $TEMPLATE_ID ya existe, no hay nada que hacer"
exit 0
fi
[[ -f $GEN_LOG ]] || die "no existe $GEN_LOG"
ttyd_bin=$(mktemp)
trap 'rm -f "$ttyd_bin"' EXIT
curl -fsSL -o "$ttyd_bin" "https://github.com/tsl0922/ttyd/releases/download/$TTYD_VERSION/ttyd.x86_64"
sha256sum --check --quiet <<< "$TTYD_SHA256 $ttyd_bin"
pveam update
os_image=$(pveam available --section system |
awk '$2 ~ /^debian-13-standard_.*_amd64\./ { print $2 }' | sort -V | tail -n 1)
[[ -n $os_image ]] || die "no hay imagen de Debian 13 en 'pveam available'"
# Sin grep -q: con pipefail, si grep sale antes de tiempo, pveam puede fallar por SIGPIPE
pveam list "$IMAGE_STORAGE" | grep -F "$os_image" >/dev/null ||
pveam download "$IMAGE_STORAGE" "$os_image"
# Sin privilegios; nesting es lo que recomienda Proxmox para systemd >= 254 en el CT
pct create "$TEMPLATE_ID" "$IMAGE_STORAGE:vztmpl/$os_image" \
--hostname taller-template \
--unprivileged 1 \
--features nesting=1 \
--rootfs "$CT_STORAGE:$CT_DISK_GB" \
--net0 "name=eth0,bridge=$EXT_BRIDGE,ip=dhcp"
pct start "$TEMPLATE_ID"
wait_for 60 in_ct getent hosts deb.debian.org >/dev/null ||
die "el CT $TEMPLATE_ID no tiene red"
in_ct apt-get update
in_ct env DEBIAN_FRONTEND=noninteractive apt-get install -y bash-completion shellcheck tmux \
vim nano less man-db gawk jq inotify-tools procps cron tree
in_ct apt-get clean
in_ct useradd --create-home --shell /bin/bash alumno
pct push "$TEMPLATE_ID" "$ttyd_bin" /usr/local/bin/ttyd --perms 755
pct push "$TEMPLATE_ID" files/ttyd.service /etc/systemd/system/ttyd.service
in_ct systemctl enable ttyd
pct push "$TEMPLATE_ID" files/LEEME /home/alumno/LEEME --user alumno --group alumno
# Readline ignora /etc/inputrc si existe ~/.inputrc; este lo incluye
pct push "$TEMPLATE_ID" files/inputrc /home/alumno/.inputrc --user alumno --group alumno
# El generador no entra en el CT: leyéndolo se sabría la respuesta
in_ct mkdir /home/alumno/retos
tar -C retos --exclude=gen-access-log.sh -cf - . | in_ct tar -C /home/alumno/retos -xf -
in_ct bash -c 'bash -s > /home/alumno/retos/1-logs/access.log' < "$GEN_LOG"
in_ct chown -R alumno:alumno /home/alumno/retos
pct shutdown "$TEMPLATE_ID"
pct set "$TEMPLATE_ID" --delete net0
pct template "$TEMPLATE_ID"
echo "plantilla $TEMPLATE_ID lista"