From 05109681735e43d6f7be7169dfa3d358be580681 Mon Sep 17 00:00:00 2001 From: pasindu Date: Mon, 27 May 2024 17:53:18 +0530 Subject: [PATCH] Remove jq usage --- Dockerfile | 3 --- entrypoint.sh | 45 +++++++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/Dockerfile b/Dockerfile index ee195c6..ba86611 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,9 +3,6 @@ FROM docker.elastic.co/beats/filebeat:8.2.0 # Switch to root user USER root -#install jq to the container -RUN apt-get update && apt-get install -y jq - COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh diff --git a/entrypoint.sh b/entrypoint.sh index a13a7ae..d859a04 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,35 +1,32 @@ #!/bin/bash -set -e -# Path to the ECS metadata file -METADATA_FILE=$(cat "$ECS_CONTAINER_METADATA_FILE") +# Check if ECS_CONTAINER_METADATA_FILE is set +if [ -z "$ECS_CONTAINER_METADATA_FILE" ]; then + echo "ECS_CONTAINER_METADATA_FILE is not set or the server is running on local deployment environment" +fi -if [ -f "$METADATA_FILE" ]; then - # Extract HostPrivateIPv4Address from the metadata file - HOST_PRIVATE_IP=$(jq -r '.HostPrivateIPv4Address' < "$METADATA_FILE") - if [ "$HOST_PRIVATE_IP" != "null" ]; then - echo "Host Private IP: $HOST_PRIVATE_IP" - export HOST_PRIVATE_IP +# Extract HostPrivateIPv4Address from the metadata file +HOST_PRIVATE_IP=$(cat $ECS_CONTAINER_METADATA_FILE | grep -oP '(?<=HostPrivateIPv4Address": ")[^"]+') - # Define the input template file and output file - TEMPLATE_FILE="filebeat.template.yml" - OUTPUT_FILE="filebeat.yml" +if [ "$HOST_PRIVATE_IP" != "null" ]; then + echo "Host Private IP: $HOST_PRIVATE_IP" - mkdir -p /opt/filebeat-configs/${HOST_PRIVATE_IP} + # Define the input template file and output file + TEMPLATE_FILE="filebeat.template.yml" + OUTPUT_FILE="filebeat.yml" - # Replace the placeholder with the actual node name and write to the output file - sed "s/{{HOST_PRIVATE_IP}}/${HOST_PRIVATE_IP}/g" "/opt/filebeat-configs/$TEMPLATE_FILE" > "/opt/filebeat-configs/${HOST_PRIVATE_IP}/$OUTPUT_FILE" - echo "Filebeat configuration file created: $OUTPUT_FILE" + mkdir -p /opt/filebeat-configs/${HOST_PRIVATE_IP} - # Create symlinks - rm -rf /usr/share/filebeat/elk - ln -sf "/opt/logs/${HOST_PRIVATE_IP}/elk" /usr/share/filebeat/elk - ln -sf "/opt/filebeat-configs/${HOST_PRIVATE_IP}/filebeat.yml" /usr/share/filebeat/filebeat.yml - else - echo "HostPrivateIPv4Address not found in metadata file" - fi + # Replace the placeholder with the actual node name and write to the output file + sed "s/{{HOST_PRIVATE_IP}}/${HOST_PRIVATE_IP}/g" "/opt/filebeat-configs/$TEMPLATE_FILE" > "/opt/filebeat-configs/${HOST_PRIVATE_IP}/$OUTPUT_FILE" + echo "Filebeat configuration file created: $OUTPUT_FILE" + + # Create symlinks + rm -rf /usr/share/filebeat/elk + ln -sf "/opt/logs/${HOST_PRIVATE_IP}/elk" /usr/share/filebeat/elk + ln -sf "/opt/filebeat-configs/${HOST_PRIVATE_IP}/filebeat.yml" /usr/share/filebeat/filebeat.yml else - echo "Metadata file does not exist" + echo "HostPrivateIPv4Address not found" fi # Run the original filebeat entrypoint