This commit is contained in:
@@ -40,53 +40,60 @@ fi
|
|||||||
PACKAGE_MANAGER=${PACKAGE_MANAGER:-npm}
|
PACKAGE_MANAGER=${PACKAGE_MANAGER:-npm}
|
||||||
|
|
||||||
add_teleport_labels() {
|
add_teleport_labels() {
|
||||||
if [ -n "$TELEPORT_LABELS" ]; then
|
# Default config path if not provided
|
||||||
echo "Adding Teleport labels from TELEPORT_LABELS environment variable..."
|
CONFIG_FILE=${CONFIG_FILE:-/etc/teleport.yaml}
|
||||||
|
|
||||||
# Check if ssh_service section exists
|
if [ -z "$TELEPORT_LABELS" ]; then
|
||||||
if ! grep -q "ssh_service:" "$CONFIG_FILE"; then
|
echo "No TELEPORT_LABELS environment variable set, skipping label configuration"
|
||||||
echo "Error: ssh_service section not found in teleport.yaml"
|
return 0
|
||||||
return 1
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
# Create a backup
|
if [ ! -f "$CONFIG_FILE" ]; then
|
||||||
cp "$CONFIG_FILE" "$CONFIG_FILE.backup"
|
echo "Error: Teleport config not found at $CONFIG_FILE"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Build the new ssh_service section
|
echo "Adding Teleport labels from TELEPORT_LABELS environment variable..."
|
||||||
NEW_SSH_SECTION="ssh_service:"
|
|
||||||
NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" labels:"
|
|
||||||
|
|
||||||
# Parse TELEPORT_LABELS and add each label
|
# Build the new ssh_service section (we'll either replace or append this)
|
||||||
IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS"
|
NEW_SSH_SECTION="ssh_service:"
|
||||||
for label in "${LABELS[@]}"; do
|
NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" labels:"
|
||||||
# Trim whitespace
|
|
||||||
label=$(echo "$label" | xargs)
|
|
||||||
|
|
||||||
# Check if label contains colon
|
IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS"
|
||||||
if [[ "$label" == *":"* ]]; then
|
for label in "${LABELS[@]}"; do
|
||||||
key=$(echo "$label" | cut -d':' -f1 | xargs)
|
# Trim whitespace
|
||||||
value=$(echo "$label" | cut -d':' -f2- | xargs)
|
label=$(echo "$label" | xargs)
|
||||||
|
|
||||||
if [ -n "$key" ] && [ -n "$value" ]; then
|
# Check if label contains colon
|
||||||
NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $key: $value"
|
if [[ "$label" == *":"* ]]; then
|
||||||
echo "Added label: $key = $value"
|
key=$(echo "$label" | cut -d':' -f1 | xargs)
|
||||||
else
|
value=$(echo "$label" | cut -d':' -f2- | xargs)
|
||||||
echo "Warning: Invalid label format '$label'. Expected format: key:value"
|
|
||||||
fi
|
if [ -n "$key" ] && [ -n "$value" ]; then
|
||||||
|
NEW_SSH_SECTION="$NEW_SSH_SECTION"$'\n'" $key: $value"
|
||||||
|
echo "Added label: $key = $value"
|
||||||
else
|
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
|
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" '
|
awk -v new_section="$NEW_SSH_SECTION" '
|
||||||
BEGIN {
|
BEGIN {
|
||||||
in_ssh_service = 0
|
in_ssh_service = 0
|
||||||
section_replaced = 0
|
section_replaced = 0
|
||||||
}
|
}
|
||||||
/^ssh_service:/ {
|
/^[[:space:]]*ssh_service:/ {
|
||||||
in_ssh_service = 1
|
in_ssh_service = 1
|
||||||
if (!section_replaced) {
|
if (!section_replaced) {
|
||||||
print new_section
|
print new_section
|
||||||
@@ -94,27 +101,26 @@ add_teleport_labels() {
|
|||||||
}
|
}
|
||||||
next
|
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 = 0
|
||||||
}
|
}
|
||||||
in_ssh_service {
|
in_ssh_service {
|
||||||
next
|
next
|
||||||
}
|
}
|
||||||
{
|
{ print $0 }
|
||||||
print $0
|
' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
|
||||||
}
|
echo "Teleport labels updated successfully"
|
||||||
' "$CONFIG_FILE" > "$CONFIG_FILE.tmp"
|
|
||||||
|
|
||||||
# Replace the original file
|
|
||||||
mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
|
|
||||||
|
|
||||||
echo "Teleport labels added successfully"
|
|
||||||
else
|
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
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Optional Teleport setup
|
# Optional Teleport setup
|
||||||
if [ -n "$TELEPORT_VERSION" ] && [ -n "$TELEPORT_URL" ] && [ -n "$TELEPORT_TOKEN" ]; then
|
if [ -n "$TELEPORT_VERSION" ] && [ -n "$TELEPORT_URL" ] && [ -n "$TELEPORT_TOKEN" ]; then
|
||||||
echo "Teleport configuration detected. Setting up Teleport..."
|
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
|
# Get the hostname of the machine
|
||||||
HOSTNAME=$(hostname)
|
HOSTNAME=$(hostname)
|
||||||
|
|
||||||
|
add_teleport_labels
|
||||||
# Update nodename in teleport config
|
# Update nodename in teleport config
|
||||||
sed -i "s/^ nodename:.*/ nodename: $HOSTNAME/" /etc/teleport.yaml
|
sed -i "s/^ nodename:.*/ nodename: $HOSTNAME/" /etc/teleport.yaml
|
||||||
add_teleport_labels
|
|
||||||
|
|
||||||
# Start Teleport in the background
|
# Start Teleport in the background
|
||||||
echo "Starting Teleport service..."
|
echo "Starting Teleport service..."
|
||||||
|
|||||||
Reference in New Issue
Block a user