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