diff --git a/Dockers/remote-node-container/entrypoint.sh b/Dockers/remote-node-container/entrypoint.sh index c97db2b..8c06b61 100644 --- a/Dockers/remote-node-container/entrypoint.sh +++ b/Dockers/remote-node-container/entrypoint.sh @@ -55,10 +55,52 @@ add_teleport_labels() { echo "Adding Teleport labels from TELEPORT_LABELS environment variable..." - # Build the new ssh_service section (we'll either replace or append this) + # Extract existing labels from the config file + declare -A EXISTING_LABELS + IN_LABELS_SECTION=0 + IN_SSH_SERVICE=0 + while IFS= read -r line; do + # Detect when we enter the ssh_service section + if [[ "$line" =~ ^[[:space:]]*ssh_service:[[:space:]]*$ ]]; then + IN_SSH_SERVICE=1 + continue + fi + # Detect when we enter the labels section + if [ "$IN_SSH_SERVICE" -eq 1 ] && [[ "$line" =~ ^[[:space:]]*labels:[[:space:]]*$ ]]; then + IN_LABELS_SECTION=1 + continue + fi + # Detect when we leave the ssh_service section (next top-level key) + if [ "$IN_SSH_SERVICE" -eq 1 ] && [[ "$line" =~ ^[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*:[[:space:]]* ]] && [ "$IN_LABELS_SECTION" -eq 0 ]; then + IN_SSH_SERVICE=0 + fi + # Detect when we leave the labels section (next key at same or higher indentation) + if [ "$IN_LABELS_SECTION" -eq 1 ] && [[ "$line" =~ ^[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*:[[:space:]]* ]]; then + # Check if this is a key at the same or less indentation as labels: + if [[ ! "$line" =~ ^[[:space:]]{4,} ]]; then + IN_LABELS_SECTION=0 + IN_SSH_SERVICE=0 + fi + fi + # Extract label key:value pairs + if [ "$IN_LABELS_SECTION" -eq 1 ]; then + if [[ "$line" =~ ^[[:space:]]+([^:]+):[[:space:]]*(.+)$ ]]; then + existing_key=$(echo "${BASH_REMATCH[1]}" | xargs) + existing_value=$(echo "${BASH_REMATCH[2]}" | xargs) + EXISTING_LABELS["$existing_key"]="$existing_value" + fi + fi + done < "$CONFIG_FILE" + + # Build the new ssh_service section with existing and new labels NEW_SSH_SECTION="ssh_service:" NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" labels:" + # First, add existing labels that are not being replaced + for existing_key in "${!EXISTING_LABELS[@]}"; do + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $existing_key: ${EXISTING_LABELS[$existing_key]}" + done + IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS" for label in "${LABELS[@]}"; do # Trim whitespace @@ -70,8 +112,13 @@ add_teleport_labels() { 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" + # Only add if the label key doesn't already exist + if [ -z "${EXISTING_LABELS[$key]}" ]; then + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $key: $value" + echo "Added label: $key = $value" + else + echo "Label '$key' already exists with value '${EXISTING_LABELS[$key]}', skipping" + fi else echo "Warning: Invalid label format '$label'. Expected format: key:value" fi @@ -85,7 +132,7 @@ add_teleport_labels() { # 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..." + echo "Found existing ssh_service section, updating labels..." awk -v new_section="$NEW_SSH_SECTION" ' BEGIN { in_ssh_service = 0 @@ -118,7 +165,6 @@ add_teleport_labels() { } - # Optional Teleport setup if [ -n "$TELEPORT_VERSION" ] && [ -n "$TELEPORT_URL" ] && [ -n "$TELEPORT_TOKEN" ]; then echo "Teleport configuration detected. Setting up Teleport..." diff --git a/Dockers/remote-react-container/entrypoint.sh b/Dockers/remote-react-container/entrypoint.sh index 1876040..f6c07c4 100644 --- a/Dockers/remote-react-container/entrypoint.sh +++ b/Dockers/remote-react-container/entrypoint.sh @@ -109,8 +109,6 @@ CONFIG_FILE="/etc/teleport.yaml" # Edit the teleport.yaml file to update the nodename sed -i "s/^ nodename:.*/ nodename: $HOSTNAME/" "$CONFIG_FILE" -# Function to add labels to teleport.yaml -# Function to add labels to teleport.yaml add_teleport_labels() { if [ -n "$TELEPORT_LABELS" ]; then echo "Adding Teleport labels from TELEPORT_LABELS environment variable..." @@ -124,11 +122,39 @@ add_teleport_labels() { # Create a backup cp "$CONFIG_FILE" "$CONFIG_FILE.backup" - # Build the new ssh_service section + # Extract existing labels from the config file + declare -A EXISTING_LABELS + IN_LABELS_SECTION=0 + while IFS= read -r line; do + # Detect when we enter the labels section + if [[ "$line" =~ ^[[:space:]]*labels:[[:space:]]*$ ]]; then + IN_LABELS_SECTION=1 + continue + fi + # Detect when we leave the labels section (next top-level key) + if [ "$IN_LABELS_SECTION" -eq 1 ] && [[ "$line" =~ ^[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*:[[:space:]]* ]]; then + IN_LABELS_SECTION=0 + fi + # Extract label key:value pairs + if [ "$IN_LABELS_SECTION" -eq 1 ]; then + if [[ "$line" =~ ^[[:space:]]+([^:]+):[[:space:]]*(.+)$ ]]; then + existing_key=$(echo "${BASH_REMATCH[1]}" | xargs) + existing_value=$(echo "${BASH_REMATCH[2]}" | xargs) + EXISTING_LABELS["$existing_key"]="$existing_value" + fi + fi + done < "$CONFIG_FILE" + + # Build the new ssh_service section with existing and new labels NEW_SSH_SECTION="ssh_service:" NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" labels:" - # Parse TELEPORT_LABELS and add each label + # First, add existing labels that are not being replaced + for existing_key in "${!EXISTING_LABELS[@]}"; do + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $existing_key: ${EXISTING_LABELS[$existing_key]}" + done + + # Parse TELEPORT_LABELS and add only new labels IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS" for label in "${LABELS[@]}"; do # Trim whitespace @@ -140,8 +166,13 @@ add_teleport_labels() { 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" + # Only add if the label key doesn't already exist + if [ -z "${EXISTING_LABELS[$key]}" ]; then + NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $key: $value" + echo "Added label: $key = $value" + else + echo "Label '$key' already exists with value '${EXISTING_LABELS[$key]}', skipping" + fi else echo "Warning: Invalid label format '$label'. Expected format: key:value" fi @@ -180,7 +211,7 @@ add_teleport_labels() { # Replace the original file mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" - echo "Teleport labels added successfully" + echo "Teleport labels processed successfully" else echo "No TELEPORT_LABELS environment variable set, skipping label configuration" fi