Compare commits

..

2 Commits

2 changed files with 104 additions and 51 deletions
+22
View File
@@ -0,0 +1,22 @@
bridge=
cmode=
cores=
domain=
dry_run=false
help=false
ip=
memory=
password=
pve_host=
pve_password=
pve_port=
pve_ssh_key=
pve_user=
rootfs_size=
ssh_public_keys=
swap=
tags=
template=
unprivileged=
vlan=
short_name='2'
+76 -45
View File
@@ -14,6 +14,7 @@ usage="Create and configure an LXC container on a remote Proxmox VE 9 server.
Usage:
$0 <short_name> [options]
$0 -h|--help
Options:
-h, --help Show this message.
@@ -34,9 +35,13 @@ Options:
--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.
--dry-run Simulate container creation without execution.
Optional configuration files:
/etc/nixos-infra/hosts/config
\${XDG_CONFIG_HOME}/nixos-infra/hosts/config
./config
"
# --- Default Parameters (Environment Variables) ---
@@ -44,7 +49,6 @@ Options:
PVE_HOST="${PVE_HOST:-}"
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}"
@@ -64,64 +68,92 @@ CMODE="${CMODE:-console}"
TAGS="${TAGS:-}"
SSH_PUBLIC_KEYS="${SSH_PUBLIC_KEYS:-}"
# --- Parse Arguments with docopts (Lowest Priority) ---
# --- Parse Arguments with docopts (Highest priority) ---
# set +e is to prevent set -e from eating the error message from docopts.
# This code is up here to prevent useless error messages to be printed
# in case the "-h" or "--help" argument is used.
set +e
args=$(docopts -h "$usage" : "$@")
eval "$args"
set -e
# Short name of the machine (mandatory parameter)
SHORT_NAME="${argv[0]:-}"
if [ -z "$SHORT_NAME" ]; then
echo "❌ Error: The short name of the machine is required." >&2
echo "$usage" >&2
exit 1
fi
# --- Override with /etc/nixos-infra/hosts/<short_name> (Medium Priority)
if [ -f "/etc/nixos-infra/hosts/$SHORT_NAME" ]; then
echo "📄 Applying parameters from /etc/nixos-infra/hosts/$SHORT_NAME..."
# --- Apply Configuration Files (by increasing priority) ---
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
CONFIG_FILES=(\
"/etc/nixos-infra/hosts/config" \
"$XDG_CONFIG_HOME/nixos-infra/hosts/config" \
"./config")
for conffile in ${CONFIG_FILES[*]}; do
if [ -f "$conffile" ]; then
echo "📄 Applying parameters from $conffile..."
set -a
source "/etc/nixos-infra/hosts/$SHORT_NAME"
source "$conffile"
set +a
else
echo "$conffile not found."
fi
done
# --- Override with ./<short_name> (Medium Priority) ---
if [ -f "./$SHORT_NAME" ]; then
echo "📄 Applying parameters from ./$SHORT_NAME..."
set -a
source "./$SHORT_NAME"
set +a
fi
# Proxmox Server
PVE_HOST="${pve_host:-$PVE_HOST}"
PVE_USER="${pve_user:-$PVE_USER}"
PVE_PORT="${pve_port:-$PVE_PORT}"
PVE_SSH_KEY="${pve_ssh_key:-$PVE_SSH_KEY}"
DRY_RUN="${dry_run:-$DRY_RUN}"
# --- Apply Command-Line Arguments (Highest Priority) ---
eval "$args"
# LXC Container
TEMPLATE="${template:-$TEMPLATE}"
ROOTFS_SIZE="${rootfs_size:-$ROOTFS_SIZE}"
CORES="${cores:-$CORES}"
MEMORY="${memory:-$MEMORY}"
SWAP="${swap:-$SWAP}"
PASSWORD="${password:-$PASSWORD}"
BRIDGE="${bridge:-$BRIDGE}"
VLAN="${vlan:-$VLAN}"
DOMAIN="${domain:-$DOMAIN}"
UNPRIVILEGED="${unprivileged:-$UNPRIVILEGED}"
IP="${ip:-$IP}"
CMODE="${cmode:-$CMODE}"
TAGS="${tags:-$TAGS}"
SSH_PUBLIC_KEYS="${ssh_public_keys:-$SSH_PUBLIC_KEYS}"
# --- SSH Key Default Logic ---
if [ "$PVE_SSH_KEY" = "default" ]; then
PVE_SSH_KEY="${HOME}/.ssh/id_${PVE_USER}"
elif [ -z "$PVE_SSH_KEY" ] && [ -z "$PVE_PASSWORD" ]; then
PVE_SSH_KEY="${HOME}/.ssh/id_${PVE_USER}"
fi
# --- Critical Parameters Validation ---
if [ -z "$TEMPLATE" ] || [ -z "$ROOTFS_SIZE" ] || [ -z "$CORES" ] || \
[ -z "$MEMORY" ] || [ -z "$SWAP" ] || [ -z "$PASSWORD" ] || \
[ -z "$BRIDGE" ] || [ -z "$DOMAIN" ] || [ -z "$UNPRIVILEGED" ] || \
[ -z "$CMODE" ] || [ -z "$SSH_PUBLIC_KEYS" ] || \
[ -z "$PVE_HOST" ] || [ -z "$PVE_USER" ] || [ -z "$PVE_PORT" ]; then
echo "❌ Error: One or more critical parameters are missing." >&2
mandatory_params=(
"TEMPLATE" \
"ROOTFS_SIZE" \
"CORES" \
"MEMORY" \
"SWAP" \
"PASSWORD" \
"BRIDGE" \
"DOMAIN" \
"UNPRIVILEGED" \
"CMODE" \
"SSH_PUBLIC_KEYS" \
"PVE_HOST" \
"PVE_USER" \
"PVE_PORT"
)
missing_params=()
for param in ${mandatory_params[*]}; do
if [ -z "${!param}" ]; then missing_params+=("$param"); fi
done
if [ ${#missing_params[@]} -gt 0 ]; then
echo "❌ Error: The following necessary parameters are missing: ${missing_params[*]}" >&2
echo "❌ Error: Plesase provide them through one the proposed config file or the command line." >&2
exit 1
fi
# Authentication Validation
if [ -z "$PVE_PASSWORD" ]; then
if [ -z "$PVE_SSH_KEY" ]; then
echo "❌ Error: No authentication parameter is defined." >&2
exit 1
elif [ ! -f "$PVE_SSH_KEY" ]; then
if [ ! -f "$PVE_SSH_KEY" ]; then
echo "❌ Error: SSH key file '$PVE_SSH_KEY' does not exist." >&2
exit 1
fi
fi
# --- SSH Connection to Proxmox Server ---
run_proxmox() {
@@ -129,8 +161,7 @@ run_proxmox() {
if [ -n "$PVE_SSH_KEY" ] && [ -f "$PVE_SSH_KEY" ]; then
ssh_cmd="$ssh_cmd -i $PVE_SSH_KEY "
else
ssh_cmd="$ssh_cmd -o PreferredAuthentications=password \
-o StrictHostKeyChecking=no"
ssh_cmd="$ssh_cmd -o PreferredAuthentications=password "
fi
$ssh_cmd "$PVE_USER@$PVE_HOST" "$1"
}
@@ -143,13 +174,13 @@ fi
if [ -n "$IP" ]; then
NET_OPTS="$NET_OPTS,ip=$IP"
fi
set -x
# --- Container Creation ---
echo "🚀 Creating LXC container $SHORT_NAME on $PVE_HOST..."
echo "🚀 Creating LXC container $short_name on $PVE_HOST..."
CREATE_CMD="pct create $ROOTFS_SIZE $TEMPLATE --cores $CORES \
--memory $MEMORY --swap $SWAP --hostname $SHORT_NAME.$DOMAIN \
--memory $MEMORY --swap $SWAP --hostname $short_name.$DOMAIN \
--password $PASSWORD --unprivileged $UNPRIVILEGED --net0 $NET_OPTS \
--onboot 1 --cmode $CMODE --ssh-public-keys $SSH_PUBLIC_KEYS"
--onboot 0 --cmode $CMODE --ssh-public-keys $SSH_PUBLIC_KEYS"
if [ -n "$TAGS" ]; then
CREATE_CMD="$CREATE_CMD --tags $TAGS"
fi
@@ -168,5 +199,5 @@ else
echo "❌ Error: Failed to create the container." >&2
exit 1
fi
echo "✅ LXC container $SHORT_NAME created successfully (ID: $LXC_ID)."
echo "✅ LXC container $short_name created successfully (ID: $LXC_ID)."
fi