From fa4808f34d36295a9774100c3264053eaafd55f1 Mon Sep 17 00:00:00 2001 From: xavier Date: Thu, 30 Apr 2026 14:00:48 +0200 Subject: [PATCH] Added the console mode (default to "console") and dry run options. --- nixos-infra/scripts/create-lxc-nixos.sh | 67 +++++++++++++++++-------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/nixos-infra/scripts/create-lxc-nixos.sh b/nixos-infra/scripts/create-lxc-nixos.sh index 716652f..813696b 100644 --- a/nixos-infra/scripts/create-lxc-nixos.sh +++ b/nixos-infra/scripts/create-lxc-nixos.sh @@ -5,32 +5,37 @@ set -euo pipefail # Check if docopts is installed (for Bash) if ! command -v docopts &> /dev/null; then echo "โŒ Error: 'docopts' is required for Bash." >&2 - echo "Install it with: wget https://raw.githubusercontent.com/docopt/docopts/master/docopts && chmod +x docopts && sudo mv docopts /usr/local/bin/" >&2 + echo "See https://github.com/docopt/docopts to install it." >&2 exit 1 fi # --- Usage and Documentation --- -usage="Usage: +usage="Create and configure an LXC container on a remote Proxmox VE 9 server. + +Usage: $0 [options] Options: -h, --help Show this message. - -t, --template TEMPLATE LXC template (e.g., local:vztmpl/nixos-unstable-amd64-default_20260428_0830-rootfs.tar.gz). - -r, --rootfs-size SIZE Root filesystem size (e.g., 8G). + -t, --template TEMPLATE LXC template (e.g. local:vztmpl/nixos-unstable). + -r, --rootfs-size SIZE Root filesystem size (e.g. 8G). -c, --cores CORES Number of CPU cores. -m, --memory MEMORY RAM in MiB. -s, --swap SWAP Swap in MiB. -p, --password PASSWORD Root password for the container. - -b, --bridge BRIDGE Network bridge (e.g., vmbr0). - -v, --vlan VLAN VLAN tag (e.g., tag=10). + -b, --bridge BRIDGE Network bridge (e.g. vmbr0). + -v, --vlan VLAN VLAN tag (e.g. tag=10). -d, --domain DOMAIN DNS domain. -u, --unprivileged UNPRIV Unprivileged container (0 or 1). - -i, --ip IP Static IP (e.g., 192.168.1.100/24). - --pve-host HOST Proxmox host (e.g., pve). + -i, --ip IP Static IP (e.g. 192.168.1.100/24). + -C, --cmode CMODE Console mode (console or tty). Default: console. + -T, --tags TAGS Tags for the container (optional). + --pve-host HOST Proxmox host (e.g. pve). --pve-user USER Proxmox user (default: admin). --pve-port PORT SSH port for Proxmox (default: 22). --pve-password PASSWORD Password for SSH authentication on Proxmox. - --pve-ssh-key KEY SSH key file for authentication (e.g., ~/.ssh/id_admin). + --pve-ssh-key KEY SSH key file for authentication. + --dry-run Simulate container creation without execution. " # --- Default Parameters (Environment Variables) --- @@ -40,9 +45,10 @@ PVE_USER="${PVE_USER:-admin}" PVE_PORT="${PVE_PORT:-22}" PVE_PASSWORD="${PVE_PASSWORD:-}" PVE_SSH_KEY="${PVE_SSH_KEY:-}" +DRY_RUN="${DRY_RUN:-false}" # LXC Container -TEMPLATE="${TEMPLATE:-local:vztmpl/nixos-unstable-amd64-default_20260428_0830-rootfs.tar.gz}" +TEMPLATE="${TEMPLATE:-local:vztmpl/nixos-unstable-amd64-default_20260428}" ROOTFS_SIZE="${ROOTFS_SIZE:-8G}" CORES="${CORES:-2}" MEMORY="${MEMORY:-2048}" @@ -53,6 +59,8 @@ VLAN="${VLAN:-}" DOMAIN="${DOMAIN:-lagraula.fr}" UNPRIVILEGED="${UNPRIVILEGED:-0}" IP="${IP:-}" +CMODE="${CMODE:-console}" +TAGS="${TAGS:-}" # --- Parse Arguments with docopts (Lowest Priority) --- args=$(docopts -h "$usage" : "$@") @@ -66,7 +74,7 @@ if [ -z "$SHORT_NAME" ]; then exit 1 fi -# --- Override with /etc/nixos-infra/hosts/ (Medium Priority) --- +# --- Override with /etc/nixos-infra/hosts/ (Medium Priority) if [ -f "/etc/nixos-infra/hosts/$SHORT_NAME" ]; then echo "๐Ÿ“„ Applying parameters from /etc/nixos-infra/hosts/$SHORT_NAME..." set -a @@ -96,7 +104,8 @@ fi if [ -z "$TEMPLATE" ] || [ -z "$ROOTFS_SIZE" ] || [ -z "$CORES" ] || \ [ -z "$MEMORY" ] || [ -z "$SWAP" ] || [ -z "$PASSWORD" ] || \ [ -z "$BRIDGE" ] || [ -z "$DOMAIN" ] || [ -z "$UNPRIVILEGED" ] || \ - [ -z "$PVE_HOST" ] || [ -z "$PVE_USER" ] || [ -z "$PVE_PORT" ]; then + [ -z "$CMODE" ] || [ -z "$PVE_HOST" ] || [ -z "$PVE_USER" ] || \ + [ -z "$PVE_PORT" ]; then echo "โŒ Error: One or more critical parameters are missing." >&2 exit 1 fi @@ -104,7 +113,7 @@ fi # Authentication Validation if [ -z "$PVE_PASSWORD" ]; then if [ -z "$PVE_SSH_KEY" ]; then - echo "โŒ Error: No authentication parameter (password or SSH key) is defined." >&2 + echo "โŒ Error: No authentication parameter is defined." >&2 exit 1 elif [ ! -f "$PVE_SSH_KEY" ]; then echo "โŒ Error: SSH key file '$PVE_SSH_KEY' does not exist." >&2 @@ -115,12 +124,11 @@ fi # --- SSH Connection to Proxmox Server --- run_proxmox() { local ssh_cmd="ssh -p $PVE_PORT" - # Priority to SSH key if it is provided and exists if [ -n "$PVE_SSH_KEY" ] && [ -f "$PVE_SSH_KEY" ]; then ssh_cmd="$ssh_cmd -i $PVE_SSH_KEY" else - # 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 $ssh_cmd "$PVE_USER@$PVE_HOST" "$1" } @@ -136,10 +144,27 @@ fi # --- Container Creation --- 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+') -if [ -z "$LXC_ID" ]; then - echo "โŒ Error: Failed to create the container." >&2 - exit 1 +CREATE_CMD="pct create $ROOTFS_SIZE $TEMPLATE --cores $CORES \ +--memory $MEMORY --swap $SWAP --hostname $SHORT_NAME.$DOMAIN \ +--password $PASSWORD --unprivileged $UNPRIVILEGED --net0 $NET_OPTS \ +--onboot 1 --cmode $CMODE" +if [ -n "$TAGS" ]; then + CREATE_CMD="$CREATE_CMD --tags $TAGS" fi -echo "โœ… LXC container $SHORT_NAME created successfully (ID: $LXC_ID)." \ No newline at end of file +# Display the command (with password masked) +DISPLAY_CMD=$(echo "$CREATE_CMD" | + sed "s/--password [^ ]*/--password \*\*\*\*\*/g") +echo "๐Ÿ”ง Command to execute on $PVE_HOST: $DISPLAY_CMD" + +# Execute or simulate +if [ "$DRY_RUN" = "true" ]; then + echo "๐Ÿงช Dry run: Skipping actual execution." +else + LXC_ID=$(run_proxmox "$CREATE_CMD" | grep -oP '\d+') + if [ -z "$LXC_ID" ]; then + echo "โŒ Error: Failed to create the container." >&2 + exit 1 + fi + echo "โœ… LXC container $SHORT_NAME created successfully (ID: $LXC_ID)." +fi \ No newline at end of file