fix(post-install): identity_network écrit réellement hostname + /etc/hosts + IP statique

Le template SJ-8 était un stub (echo STATIC_TARGET sans écriture). Désormais :
- /etc/hostname + hostnamectl, ligne 127.0.1.1 <fqdn> <host> dans /etc/hosts
- IP statique via drop-in /etc/network/interfaces.d/<iface>.cfg (ifupdown),
  neutralisation awk de la strophe DHCP existante + source interfaces.d
- sauvegardes horodatées avant écriture ; réseau appliqué AU REBOOT (ne coupe
  jamais SSH en live) ; FILE_MODIFIED émis après écriture réelle

Cible Debian/ifupdown (netinstall). syntaxe sh -n validée sur rendu.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-06 18:47:44 +02:00
parent d1b0290e3b
commit aaf1b4988d
+62 -10
View File
@@ -1,24 +1,76 @@
#!/bin/sh
# Identité + réseau : hostname, domaine, IP statique. Sauvegarde avant modif.
# Ne coupe jamais la connexion sans stratégie de reconnexion planifiée côté webapp.
# Identité + réseau : hostname, /etc/hosts, IP statique (ifupdown drop-in).
# Le changement d'IP s'applique AU REBOOT (on ne coupe jamais SSH en live).
# Sauvegardes horodatées avant toute écriture. Échec contrôlé.
export LC_ALL=C
HOST="{{newHostname}}"
DOMAIN="{{domain}}"
IFACE="{{interfaceName}}"
ADDR="{{staticAddress}}"
GW="{{gateway}}"
DNS="{{dnsNameservers}}"
echo "===SU:CUSTOM_IDENTITY==="
cp -a /etc/hosts "/etc/hosts.su.bak.$(date +%s)" 2>/dev/null && echo "FILE_MODIFIED=/etc/hosts"
cp -a /etc/network/interfaces "/etc/network/interfaces.su.bak.$(date +%s)" 2>/dev/null && echo "FILE_MODIFIED=/etc/network/interfaces"
# --- Sauvegardes ---
TS=$(date +%s)
cp -a /etc/hosts "/etc/hosts.su.bak.${TS}" 2>/dev/null
[ -f /etc/network/interfaces ] && cp -a /etc/network/interfaces "/etc/network/interfaces.su.bak.${TS}" 2>/dev/null
[ -f /etc/hostname ] && cp -a /etc/hostname "/etc/hostname.su.bak.${TS}" 2>/dev/null
echo "OLD_ENDPOINT={{dhcpEndpoint}}"
if hostnamectl set-hostname "{{newHostname}}" 2>&1; then
echo "HOSTNAME_SET={{newHostname}}"
# --- Hostname (immédiat, ne coupe pas SSH) ---
if hostnamectl set-hostname "$HOST" 2>/dev/null || { printf '%s\n' "$HOST" > /etc/hostname; }; then
printf '%s\n' "$HOST" > /etc/hostname
echo "HOSTNAME_SET=$HOST"
echo "FILE_MODIFIED=/etc/hostname"
else
echo "ERR=hostname_failed"
fi
if ip link show "{{interfaceName}}" >/dev/null 2>&1; then
echo "IFACE_OK={{interfaceName}}"
# Rendu détaillé de /etc/network/interfaces renvoyé à la tâche 4 ; ici on valide la cible.
echo "STATIC_TARGET={{staticAddress}} gw {{gateway}} dns {{dnsNameservers}}"
# --- /etc/hosts : ligne 127.0.1.1 <fqdn> <host> ---
FQDN="$HOST"
[ -n "$DOMAIN" ] && FQDN="$HOST.$DOMAIN"
if grep -qE '^127\.0\.1\.1' /etc/hosts 2>/dev/null; then
sed -i -E "s|^127\.0\.1\.1.*|127.0.1.1\t${FQDN} ${HOST}|" /etc/hosts && echo "FILE_MODIFIED=/etc/hosts"
else
printf '127.0.1.1\t%s %s\n' "$FQDN" "$HOST" >> /etc/hosts && echo "FILE_MODIFIED=/etc/hosts"
fi
# --- IP statique (ifupdown drop-in, appliqué au reboot) ---
if ip link show "$IFACE" >/dev/null 2>&1; then
echo "IFACE_OK=$IFACE"
mkdir -p /etc/network/interfaces.d
# S'assure que le fichier principal source le répertoire interfaces.d.
if [ -f /etc/network/interfaces ] && ! grep -qE '^[[:space:]]*source(-directory)?[[:space:]]+/etc/network/interfaces\.d' /etc/network/interfaces; then
printf '\nsource /etc/network/interfaces.d/*\n' >> /etc/network/interfaces
fi
# Neutralise (commente) toute strophe existante de l'interface dans le fichier principal.
if [ -f /etc/network/interfaces ]; then
awk -v IFACE="$IFACE" '
$0 ~ "^[[:space:]]*(auto|allow-hotplug)[[:space:]]+" IFACE "([[:space:]]|$)" { print "#SU# " $0; next }
$0 ~ "^[[:space:]]*iface[[:space:]]+" IFACE "([[:space:]]|$)" { inblk=1; print "#SU# " $0; next }
inblk==1 && $0 ~ /^[[:space:]]+[^[:space:]]/ { print "#SU# " $0; next }
inblk==1 { inblk=0 }
{ print }
' /etc/network/interfaces > /etc/network/interfaces.su.tmp && cat /etc/network/interfaces.su.tmp > /etc/network/interfaces && rm -f /etc/network/interfaces.su.tmp
fi
# Écrit la configuration statique en drop-in.
{
echo "auto $IFACE"
echo "iface $IFACE inet static"
echo " address $ADDR"
echo " gateway $GW"
[ -n "$DNS" ] && echo " dns-nameservers $DNS"
} > "/etc/network/interfaces.d/${IFACE}.cfg"
echo "FILE_MODIFIED=/etc/network/interfaces.d/${IFACE}.cfg"
echo "STATIC_TARGET=$ADDR gw $GW dns $DNS"
else
echo "ERR=interface_not_found"
fi
echo "NEW_ENDPOINT={{reconnectHost}}"
echo "RECONNECT_REQUIRED=1"
echo "NETWORK_APPLIES_ON=reboot"
{{#rebootAfterInstall}}echo "REBOOT_REQUESTED=1"{{/rebootAfterInstall}}
echo "===SU:EXIT=0==="