[talos] add node recognization

This commit is contained in:
2026-04-20 10:52:13 +02:00
parent c95cd8b81c
commit 49ce1af3b6

61
bin/talos Normal file → Executable file
View File

@@ -1,27 +1,18 @@
#!/usr/bin/env bash #!/usr/bin/env bash
### Usage ###
# talos context [optional-nodename] command
# Example:
# talos home dashboard
# talos home dashboard
# talos home talos-nuc-1 dashboard
# If wrong context or missing this script will output which contexts are available
CONFIG_FILE="$HOME/.talos/config" CONFIG_FILE="$HOME/.talos/config"
COMMAND="$(basename "$0")"
# Check yq # Check yq
if ! command -v yq &>/dev/null; then if ! command -v yq &>/dev/null; then
echo "Error: yq (Go version v4+) not found." echo "Error: yq (Go version v4+) not found."
exit 1 exit 1
fi fi
# Read all contexts
mapfile -t CONTEXTS < <(yq e '.contexts | keys | .[]' "$CONFIG_FILE") mapfile -t CONTEXTS < <(yq e '.contexts | keys | .[]' "$CONFIG_FILE")
# Helper: check if value exists in array contains() {
function contains() {
local seeking=$1; shift local seeking=$1; shift
for element; do for element; do
[[ "$element" == "$seeking" ]] && return 0 [[ "$element" == "$seeking" ]] && return 0
@@ -29,10 +20,39 @@ function contains() {
return 1 return 1
} }
print_help() {
cat <<EOF
Usage:
$COMMAND <context> [node] <command>
Description:
Wrapper for talosctl with context and optional node selection.
Examples:
$COMMAND home dashboard
$COMMAND home talos-nuc-1 dashboard
$COMMAND zl-erm talos-geekom-1 kubeconfig
Arguments:
context Required. Must match a configured Talos context.
node Optional. Node name or short name.
command Any talosctl command.
Commands:
help, -h, --help Show this help message
EOF
}
# Help handling
if [[ "$1" == "help" || "$1" == "-h" || "$1" == "--help" ]]; then
print_help
exit 0
fi
# Require context # Require context
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Error: Context is required." echo "Error: Context is required."
echo "Usage: talos <context> [node] <command>" print_help
exit 1 exit 1
fi fi
@@ -47,23 +67,30 @@ fi
CONTEXT="$1" CONTEXT="$1"
shift shift
# Load nodes for context
mapfile -t NODES < <(yq e ".contexts.${CONTEXT}.nodes[]" "$CONFIG_FILE") mapfile -t NODES < <(yq e ".contexts.${CONTEXT}.nodes[]" "$CONFIG_FILE")
NODE="" NODE=""
NODE_INPUT=""
# Check if next arg is a node
if [ -n "$1" ]; then if [ -n "$1" ]; then
NODE_INPUT="$1"
for n in "${NODES[@]}"; do for n in "${NODES[@]}"; do
if [[ "$n" == "$1" || "${n%%.*}" == "$1" ]]; then if [[ "$n" == "$NODE_INPUT" || "${n%%.*}" == "$NODE_INPUT" ]]; then
NODE="$n" NODE="$n"
shift shift
break break
fi fi
done done
if [ -n "$NODE_INPUT" ] && [ -z "$NODE" ]; then
echo "Error: Node '$NODE_INPUT' not found in context '$CONTEXT'"
echo "Available nodes:"
printf ' - %s\n' "${NODES[@]}"
exit 1
fi
fi fi
# Execute
if [ -n "$NODE" ]; then if [ -n "$NODE" ]; then
talosctl --context "$CONTEXT" -n "$NODE" -e "$NODE" "$@" talosctl --context "$CONTEXT" -n "$NODE" -e "$NODE" "$@"
else else