Translated to english for an international audience.
This commit is contained in:
@@ -1,47 +1,47 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
# --- Dépendances ---
|
# --- Dependencies ---
|
||||||
# Vérifier que docopts est installé (pour Bash)
|
# Check if docopts is installed (for Bash)
|
||||||
if ! command -v docopts &> /dev/null; then
|
if ! command -v docopts &> /dev/null; then
|
||||||
echo "❌ Erreur : 'docopts' est requis pour Bash." >&2
|
echo "❌ Error: 'docopts' is required for Bash." >&2
|
||||||
echo "Installez-le avec : wget https://raw.githubusercontent.com/docopt/docopts/master/docopts && chmod +x docopts && sudo mv docopts /usr/local/bin/" >&2
|
echo "Install it with: wget https://raw.githubusercontent.com/docopt/docopts/master/docopts && chmod +x docopts && sudo mv docopts /usr/local/bin/" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Usage et documentation ---
|
# --- Usage and Documentation ---
|
||||||
usage="Usage:
|
usage="Usage:
|
||||||
$0 <nom_court> [options]
|
$0 <short_name> [options]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h, --help Affiche ce message.
|
-h, --help Show this message.
|
||||||
-t, --template TEMPLATE Template LXC (ex: local:vztmpl/nixos-unstable-amd64-default_20260428_0830-rootfs.tar.gz).
|
-t, --template TEMPLATE LXC template (e.g., local:vztmpl/nixos-unstable-amd64-default_20260428_0830-rootfs.tar.gz).
|
||||||
-r, --rootfs-size SIZE Taille du stockage racine (ex: 8G).
|
-r, --rootfs-size SIZE Root filesystem size (e.g., 8G).
|
||||||
-c, --cores CORES Nombre de cœurs CPU.
|
-c, --cores CORES Number of CPU cores.
|
||||||
-m, --memory MEMORY RAM en Mo.
|
-m, --memory MEMORY RAM in MiB.
|
||||||
-s, --swap SWAP Swap en Mo.
|
-s, --swap SWAP Swap in MiB.
|
||||||
-p, --password PASSWORD Mot de passe root du conteneur.
|
-p, --password PASSWORD Root password for the container.
|
||||||
-b, --bridge BRIDGE Bridge réseau (ex: vmbr0).
|
-b, --bridge BRIDGE Network bridge (e.g., vmbr0).
|
||||||
-v, --vlan VLAN VLAN (ex: tag=10).
|
-v, --vlan VLAN VLAN tag (e.g., tag=10).
|
||||||
-d, --domain DOMAIN Domaine DNS.
|
-d, --domain DOMAIN DNS domain.
|
||||||
-u, --unprivileged UNPRIV Conteneur non privilégié (0 ou 1).
|
-u, --unprivileged UNPRIV Unprivileged container (0 or 1).
|
||||||
-i, --ip IP IP statique (ex: 192.168.1.100/24).
|
-i, --ip IP Static IP (e.g., 192.168.1.100/24).
|
||||||
--pve-host HOST Hôte Proxmox (ex: pve).
|
--pve-host HOST Proxmox host (e.g., pve).
|
||||||
--pve-user USER Utilisateur Proxmox (ex: admin).
|
--pve-user USER Proxmox user (default: admin).
|
||||||
--pve-port PORT Port SSH Proxmox (ex: 22).
|
--pve-port PORT SSH port for Proxmox (default: 22).
|
||||||
--pve-password PASSWORD Mot de passe pour l'authentification SSH sur Proxmox.
|
--pve-password PASSWORD Password for SSH authentication on Proxmox.
|
||||||
--pve-ssh-key KEY Fichier de clé SSH pour l'authentification (ex: ~/.ssh/id_admin).
|
--pve-ssh-key KEY SSH key file for authentication (e.g., ~/.ssh/id_admin).
|
||||||
"
|
"
|
||||||
|
|
||||||
# --- Paramètres par défaut (variables d'environnement) ---
|
# --- Default Parameters (Environment Variables) ---
|
||||||
# Serveur Proxmox
|
# Proxmox Server
|
||||||
PVE_HOST="${PVE_HOST:-}"
|
PVE_HOST="${PVE_HOST:-}"
|
||||||
PVE_USER="${PVE_USER:-admin}"
|
PVE_USER="${PVE_USER:-admin}"
|
||||||
PVE_PORT="${PVE_PORT:-22}"
|
PVE_PORT="${PVE_PORT:-22}"
|
||||||
PVE_PASSWORD="${PVE_PASSWORD:-}"
|
PVE_PASSWORD="${PVE_PASSWORD:-}"
|
||||||
PVE_SSH_KEY="${PVE_SSH_KEY:-}"
|
PVE_SSH_KEY="${PVE_SSH_KEY:-}"
|
||||||
|
|
||||||
# Conteneur LXC
|
# LXC Container
|
||||||
TEMPLATE="${TEMPLATE:-local:vztmpl/nixos-unstable-amd64-default_20260428_0830-rootfs.tar.gz}"
|
TEMPLATE="${TEMPLATE:-local:vztmpl/nixos-unstable-amd64-default_20260428_0830-rootfs.tar.gz}"
|
||||||
ROOTFS_SIZE="${ROOTFS_SIZE:-8G}"
|
ROOTFS_SIZE="${ROOTFS_SIZE:-8G}"
|
||||||
CORES="${CORES:-2}"
|
CORES="${CORES:-2}"
|
||||||
@@ -54,78 +54,78 @@ DOMAIN="${DOMAIN:-lagraula.fr}"
|
|||||||
UNPRIVILEGED="${UNPRIVILEGED:-0}"
|
UNPRIVILEGED="${UNPRIVILEGED:-0}"
|
||||||
IP="${IP:-}"
|
IP="${IP:-}"
|
||||||
|
|
||||||
# --- Parsing des arguments avec docopts (priorité la plus basse) ---
|
# --- Parse Arguments with docopts (Lowest Priority) ---
|
||||||
args=$(docopts -h "$usage" : "$@")
|
args=$(docopts -h "$usage" : "$@")
|
||||||
eval "$args"
|
eval "$args"
|
||||||
|
|
||||||
# Nom court de la machine (paramètre obligatoire)
|
# Short name of the machine (mandatory parameter)
|
||||||
SHORT_NAME="${argv[0]:-}"
|
SHORT_NAME="${argv[0]:-}"
|
||||||
if [ -z "$SHORT_NAME" ]; then
|
if [ -z "$SHORT_NAME" ]; then
|
||||||
echo "❌ Erreur : Le nom court de la machine est obligatoire." >&2
|
echo "❌ Error: The short name of the machine is required." >&2
|
||||||
echo "$usage" >&2
|
echo "$usage" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Surcharge par /etc/nixos-infra/hosts/<nom_court> (priorité moyenne) ---
|
# --- Override with /etc/nixos-infra/hosts/<short_name> (Medium Priority) ---
|
||||||
if [ -f "/etc/nixos-infra/hosts/$SHORT_NAME" ]; then
|
if [ -f "/etc/nixos-infra/hosts/$SHORT_NAME" ]; then
|
||||||
echo "📄 Application des paramètres depuis /etc/nixos-infra/hosts/$SHORT_NAME..."
|
echo "📄 Applying parameters from /etc/nixos-infra/hosts/$SHORT_NAME..."
|
||||||
set -a
|
set -a
|
||||||
source "/etc/nixos-infra/hosts/$SHORT_NAME"
|
source "/etc/nixos-infra/hosts/$SHORT_NAME"
|
||||||
set +a
|
set +a
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Surcharge par ./<nom_court> (priorité moyenne) ---
|
# --- Override with ./<short_name> (Medium Priority) ---
|
||||||
if [ -f "./$SHORT_NAME" ]; then
|
if [ -f "./$SHORT_NAME" ]; then
|
||||||
echo "📄 Application des paramètres depuis ./$SHORT_NAME..."
|
echo "📄 Applying parameters from ./$SHORT_NAME..."
|
||||||
set -a
|
set -a
|
||||||
source "./$SHORT_NAME"
|
source "./$SHORT_NAME"
|
||||||
set +a
|
set +a
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Application des arguments de la ligne de commande (priorité la plus haute) ---
|
# --- Apply Command-Line Arguments (Highest Priority) ---
|
||||||
eval "$args"
|
eval "$args"
|
||||||
|
|
||||||
# --- Gestion de la clé SSH par défaut ---
|
# --- SSH Key Default Logic ---
|
||||||
if [ "$PVE_SSH_KEY" = "default" ]; then
|
if [ "$PVE_SSH_KEY" = "default" ]; then
|
||||||
PVE_SSH_KEY="${HOME}/.ssh/id_${PVE_USER}"
|
PVE_SSH_KEY="${HOME}/.ssh/id_${PVE_USER}"
|
||||||
elif [ -z "$PVE_SSH_KEY" ] && [ -z "$PVE_PASSWORD" ]; then
|
elif [ -z "$PVE_SSH_KEY" ] && [ -z "$PVE_PASSWORD" ]; then
|
||||||
PVE_SSH_KEY="${HOME}/.ssh/id_${PVE_USER}"
|
PVE_SSH_KEY="${HOME}/.ssh/id_${PVE_USER}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Vérification des paramètres critiques ---
|
# --- Critical Parameters Validation ---
|
||||||
if [ -z "$TEMPLATE" ] || [ -z "$ROOTFS_SIZE" ] || [ -z "$CORES" ] || \
|
if [ -z "$TEMPLATE" ] || [ -z "$ROOTFS_SIZE" ] || [ -z "$CORES" ] || \
|
||||||
[ -z "$MEMORY" ] || [ -z "$SWAP" ] || [ -z "$PASSWORD" ] || \
|
[ -z "$MEMORY" ] || [ -z "$SWAP" ] || [ -z "$PASSWORD" ] || \
|
||||||
[ -z "$BRIDGE" ] || [ -z "$DOMAIN" ] || [ -z "$UNPRIVILEGED" ] || \
|
[ -z "$BRIDGE" ] || [ -z "$DOMAIN" ] || [ -z "$UNPRIVILEGED" ] || \
|
||||||
[ -z "$PVE_HOST" ] || [ -z "$PVE_USER" ] || [ -z "$PVE_PORT" ]; then
|
[ -z "$PVE_HOST" ] || [ -z "$PVE_USER" ] || [ -z "$PVE_PORT" ]; then
|
||||||
echo "❌ Erreur : Un ou plusieurs paramètres critiques sont manquants." >&2
|
echo "❌ Error: One or more critical parameters are missing." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Vérification de l'authentification
|
# Authentication Validation
|
||||||
if [ -z "$PVE_PASSWORD" ]; then
|
if [ -z "$PVE_PASSWORD" ]; then
|
||||||
if [ -z "$PVE_SSH_KEY" ]; then
|
if [ -z "$PVE_SSH_KEY" ]; then
|
||||||
echo "❌ Erreur : Aucun paramètre d'authentification (mot de passe ou clé SSH) n'est défini." >&2
|
echo "❌ Error: No authentication parameter (password or SSH key) is defined." >&2
|
||||||
exit 1
|
exit 1
|
||||||
elif [ ! -f "$PVE_SSH_KEY" ]; then
|
elif [ ! -f "$PVE_SSH_KEY" ]; then
|
||||||
echo "❌ Erreur : Le fichier de clé SSH '$PVE_SSH_KEY' n'existe pas." >&2
|
echo "❌ Error: SSH key file '$PVE_SSH_KEY' does not exist." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Connexion SSH au serveur Proxmox ---
|
# --- SSH Connection to Proxmox Server ---
|
||||||
run_proxmox() {
|
run_proxmox() {
|
||||||
local ssh_cmd="ssh -p $PVE_PORT"
|
local ssh_cmd="ssh -p $PVE_PORT"
|
||||||
# Priorité à la clé SSH si elle est fournie et existe
|
# Priority to SSH key if it is provided and exists
|
||||||
if [ -n "$PVE_SSH_KEY" ] && [ -f "$PVE_SSH_KEY" ]; then
|
if [ -n "$PVE_SSH_KEY" ] && [ -f "$PVE_SSH_KEY" ]; then
|
||||||
ssh_cmd="$ssh_cmd -i $PVE_SSH_KEY"
|
ssh_cmd="$ssh_cmd -i $PVE_SSH_KEY"
|
||||||
else
|
else
|
||||||
# Utiliser le mot de passe si la clé SSH n'est pas disponible
|
# Use password if SSH key is not available
|
||||||
ssh_cmd="$ssh_cmd -o PreferredAuthentications=password -o StrictHostKeyChecking=no"
|
ssh_cmd="$ssh_cmd -o PreferredAuthentications=password -o StrictHostKeyChecking=no"
|
||||||
fi
|
fi
|
||||||
$ssh_cmd "$PVE_USER@$PVE_HOST" "$1"
|
$ssh_cmd "$PVE_USER@$PVE_HOST" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Construction des options réseau ---
|
# --- Network Options Construction ---
|
||||||
NET_OPTS="name=eth0,bridge=$BRIDGE"
|
NET_OPTS="name=eth0,bridge=$BRIDGE"
|
||||||
if [ -n "$VLAN" ]; then
|
if [ -n "$VLAN" ]; then
|
||||||
NET_OPTS="$NET_OPTS,$VLAN"
|
NET_OPTS="$NET_OPTS,$VLAN"
|
||||||
@@ -134,12 +134,12 @@ if [ -n "$IP" ]; then
|
|||||||
NET_OPTS="$NET_OPTS,ip=$IP"
|
NET_OPTS="$NET_OPTS,ip=$IP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Création du conteneur ---
|
# --- Container Creation ---
|
||||||
echo "🚀 Création du conteneur LXC $SHORT_NAME sur $PVE_HOST..."
|
echo "🚀 Creating LXC container $SHORT_NAME on $PVE_HOST..."
|
||||||
LXC_ID=$(run_proxmox "pct create $ROOTFS_SIZE $TEMPLATE --cores $CORES --memory $MEMORY --swap $SWAP --hostname $SHORT_NAME.$DOMAIN --password $PASSWORD --unprivileged $UNPRIVILEGED --net0 $NET_OPTS --onboot 1" | grep -oP '\d+')
|
LXC_ID=$(run_proxmox "pct create $ROOTFS_SIZE $TEMPLATE --cores $CORES --memory $MEMORY --swap $SWAP --hostname $SHORT_NAME.$DOMAIN --password $PASSWORD --unprivileged $UNPRIVILEGED --net0 $NET_OPTS --onboot 1" | grep -oP '\d+')
|
||||||
if [ -z "$LXC_ID" ]; then
|
if [ -z "$LXC_ID" ]; then
|
||||||
echo "❌ Erreur : Échec de la création du conteneur." >&2
|
echo "❌ Error: Failed to create the container." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "✅ Conteneur LXC $SHORT_NAME créé avec succès (ID: $LXC_ID)."
|
echo "✅ LXC container $SHORT_NAME created successfully (ID: $LXC_ID)."
|
||||||
Reference in New Issue
Block a user