From 50ab2576b927df8893491b1d3d8b3a28d4aaf91e Mon Sep 17 00:00:00 2001 From: Bram Date: Wed, 22 Oct 2025 22:42:07 +0200 Subject: [PATCH] lets give it a shot --- Dockers/remote-node-container/entrypoint.sh | 94 +++++++++++---------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/Dockers/remote-node-container/entrypoint.sh b/Dockers/remote-node-container/entrypoint.sh index 2f0347a..76268fb 100644 --- a/Dockers/remote-node-container/entrypoint.sh +++ b/Dockers/remote-node-container/entrypoint.sh @@ -40,53 +40,60 @@ fi PACKAGE_MANAGER=${PACKAGE_MANAGER:-npm} add_teleport_labels() { - if [ -n "$TELEPORT_LABELS" ]; then - echo "Adding Teleport labels from TELEPORT_LABELS environment variable..." + # Default config path if not provided + CONFIG_FILE=${CONFIG_FILE:-/etc/teleport.yaml} - # Check if ssh_service section exists - if ! grep -q "ssh_service:" "$CONFIG_FILE"; then - echo "Error: ssh_service section not found in teleport.yaml" - return 1 - fi + if [ -z "$TELEPORT_LABELS" ]; then + echo "No TELEPORT_LABELS environment variable set, skipping label configuration" + return 0 + fi - # Create a backup - cp "$CONFIG_FILE" "$CONFIG_FILE.backup" + if [ ! -f "$CONFIG_FILE" ]; then + echo "Error: Teleport config not found at $CONFIG_FILE" + return 1 + fi - # Build the new ssh_service section - NEW_SSH_SECTION="ssh_service:" - NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" labels:" + echo "Adding Teleport labels from TELEPORT_LABELS environment variable..." - # Parse TELEPORT_LABELS and add each label - IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS" - for label in "${LABELS[@]}"; do - # Trim whitespace - label=$(echo "$label" | xargs) + # Build the new ssh_service section (we'll either replace or append this) + NEW_SSH_SECTION="ssh_service:" + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" labels:" - # Check if label contains colon - if [[ "$label" == *":"* ]]; then - key=$(echo "$label" | cut -d':' -f1 | xargs) - value=$(echo "$label" | cut -d':' -f2- | xargs) + IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS" + for label in "${LABELS[@]}"; do + # Trim whitespace + label=$(echo "$label" | xargs) - if [ -n "$key" ] && [ -n "$value" ]; then - NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $key: $value" - echo "Added label: $key = $value" - else - echo "Warning: Invalid label format '$label'. Expected format: key:value" - fi + # Check if label contains colon + if [[ "$label" == *":"* ]]; then + key=$(echo "$label" | cut -d':' -f1 | xargs) + value=$(echo "$label" | cut -d':' -f2- | xargs) + + if [ -n "$key" ] && [ -n "$value" ]; then + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $key: $value" + echo "Added label: $key = $value" else - echo "Warning: Label '$label' does not contain colon separator. Expected format: key:value" + echo "Warning: Invalid label format '$label'. Expected format: key:value" fi - done + else + echo "Warning: Label '$label' does not contain colon separator. Expected format: key:value" + fi + done - NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" enabled: \"yes\"" + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" enabled: \"yes\"" - # Replace the ssh_service section + # Create a backup + cp "$CONFIG_FILE" "$CONFIG_FILE.backup" + + # If ssh_service exists, replace it. Otherwise, append to the end. + if grep -qE '^[[:space:]]*ssh_service:' "$CONFIG_FILE"; then + echo "Found existing ssh_service section, replacing it with new labels..." awk -v new_section="$NEW_SSH_SECTION" ' BEGIN { in_ssh_service = 0 section_replaced = 0 } - /^ssh_service:/ { + /^[[:space:]]*ssh_service:/ { in_ssh_service = 1 if (!section_replaced) { print new_section @@ -94,27 +101,26 @@ add_teleport_labels() { } next } - in_ssh_service && /^[a-zA-Z_][a-zA-Z0-9_]*:/ { + in_ssh_service && /^[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*:/ { in_ssh_service = 0 } in_ssh_service { next } - { - print $0 - } - ' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" - - # Replace the original file - mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" - - echo "Teleport labels added successfully" + { print $0 } + ' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" + echo "Teleport labels updated successfully" else - echo "No TELEPORT_LABELS environment variable set, skipping label configuration" + echo "ssh_service section not found; appending to bottom of $CONFIG_FILE..." + # Ensure file ends with a newline before appending + tail -c1 "$CONFIG_FILE" | read -r _ || echo >> "$CONFIG_FILE" + printf "%s\n" "$NEW_SSH_SECTION" >> "$CONFIG_FILE" + echo "Teleport ssh_service section appended successfully" fi } + # Optional Teleport setup if [ -n "$TELEPORT_VERSION" ] && [ -n "$TELEPORT_URL" ] && [ -n "$TELEPORT_TOKEN" ]; then echo "Teleport configuration detected. Setting up Teleport..." @@ -139,9 +145,9 @@ if [ -n "$TELEPORT_VERSION" ] && [ -n "$TELEPORT_URL" ] && [ -n "$TELEPORT_TOKEN # Get the hostname of the machine HOSTNAME=$(hostname) + add_teleport_labels # Update nodename in teleport config sed -i "s/^ nodename:.*/ nodename: $HOSTNAME/" /etc/teleport.yaml - add_teleport_labels # Start Teleport in the background echo "Starting Teleport service..."