This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user