This commit is contained in:
@@ -40,23 +40,25 @@ 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"
|
||||
if [ -z "$TELEPORT_LABELS" ]; then
|
||||
echo "No TELEPORT_LABELS environment variable set, skipping label configuration"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ ! -f "$CONFIG_FILE" ]; then
|
||||
echo "Error: Teleport config not found at $CONFIG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Create a backup
|
||||
cp "$CONFIG_FILE" "$CONFIG_FILE.backup"
|
||||
echo "Adding Teleport labels from TELEPORT_LABELS environment variable..."
|
||||
|
||||
# Build the new ssh_service section
|
||||
# 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:"
|
||||
|
||||
# Parse TELEPORT_LABELS and add each label
|
||||
IFS=',' read -ra LABELS <<< "$TELEPORT_LABELS"
|
||||
for label in "${LABELS[@]}"; do
|
||||
# Trim whitespace
|
||||
@@ -80,13 +82,18 @@ add_teleport_labels() {
|
||||
|
||||
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..."
|
||||
|
||||
Reference in New Issue
Block a user