argus/src/log/fluent-bit/build/start-fluent-bit.sh

97 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
echo "[INFO] Starting Fluent Bit setup in Ubuntu container (offline-first)..."
export DEBIAN_FRONTEND=noninteractive
# Stage bundle to /tmp (read-only mount under /private)
echo "[INFO] Staging fluent-bit bundle..."
rm -rf /tmp/flb && mkdir -p /tmp/flb
cp -r /private/etc /tmp/flb/
mkdir -p /tmp/flb/packages
cp -r /private/packages/* /tmp/flb/packages/ 2>/dev/null || true
# Helper: check and install a local deb if not already satisfied
ensure_lib() {
local soname="$1"; shift
local pattern="$1"; shift
if ldconfig -p 2>/dev/null | grep -q "$soname"; then
echo "[OK] $soname already present"
return 0
fi
local deb="$(ls /tmp/flb/packages/$pattern 2>/dev/null | head -n1 || true)"
if [[ -n "$deb" ]]; then
echo "[INFO] Installing local dependency: $(basename "$deb")"
dpkg -i "$deb" >/dev/null 2>&1 || true
else
echo "[WARN] Local deb for $soname not found (pattern=$pattern)"
fi
if ! ldconfig -p 2>/dev/null | grep -q "$soname"; then
echo "[WARN] $soname still missing after local install; attempting apt fallback"
apt-get update -qq || true
case "$soname" in
libpq.so.5) apt-get install -y -qq libpq5 || true ;;
libyaml-0.so.2) apt-get install -y -qq libyaml-0-2 || true ;;
esac
fi
ldconfig 2>/dev/null || true
}
# Offline-first: satisfy runtime deps from local debs, fallback to apt only if necessary
ensure_lib "libpq.so.5" "libpq5_*_amd64.deb"
ensure_lib "libyaml-0.so.2" "libyaml-0-2_*_amd64.deb"
ensure_lib "libsasl2.so.2" "libsasl2-2_*_amd64.deb"
ensure_lib "libldap-2.5.so.0" "libldap-2.5-0_*_amd64.deb"
# Install fluent-bit main package from local bundle
FLB_DEB="$(ls /tmp/flb/packages/fluent-bit_*_amd64.deb 2>/dev/null | head -n1 || true)"
if [[ -z "$FLB_DEB" ]]; then
echo "[ERROR] fluent-bit deb not found under /private/packages" >&2
exit 1
fi
echo "[INFO] Installing Fluent Bit: $(basename "$FLB_DEB")"
dpkg -i "$FLB_DEB" >/dev/null 2>&1 || true
# If dpkg reported unresolved dependencies, try apt -f only as last resort
if ! command -v /opt/fluent-bit/bin/fluent-bit >/dev/null 2>&1; then
echo "[WARN] fluent-bit binary missing after dpkg; attempting apt --fix-broken"
apt-get install -f -y -qq || true
fi
# Ensure runtime library dependencies are satisfied (libsasl2, libldap are required via libpq/curl)
MISSING=$(ldd /opt/fluent-bit/bin/fluent-bit 2>/dev/null | awk '/not found/{print $1}' | xargs -r echo || true)
if [[ -n "$MISSING" ]]; then
echo "[WARN] missing shared libs: $MISSING"
apt-get update -qq || true
apt-get install -y -qq libsasl2-2 libldap-2.5-0 || true
apt-get install -f -y -qq || true
fi
echo "[INFO] Fluent Bit version:"
/opt/fluent-bit/bin/fluent-bit --version || { echo "[ERROR] fluent-bit not installed or libraries missing" >&2; exit 1; }
# Place configuration
mkdir -p /etc/fluent-bit
cp -r /tmp/flb/etc/* /etc/fluent-bit/
# Create logs/buffers dirs
mkdir -p /logs/train /logs/infer /buffers
chmod 755 /logs/train /logs/infer /buffers
# Wait for Elasticsearch via bash /dev/tcp to avoid curl dependency
echo "[INFO] Waiting for Elasticsearch to be ready (tcp ${ES_HOST}:${ES_PORT})..."
for i in $(seq 1 120); do
if exec 3<>/dev/tcp/${ES_HOST}/${ES_PORT}; then
exec 3<&- 3>&-
echo "[INFO] Elasticsearch is ready"
break
fi
[[ $i -eq 120 ]] && { echo "[ERROR] ES not reachable" >&2; exit 1; }
sleep 1
done
echo "[INFO] Starting Fluent Bit with configuration from /etc/fluent-bit/"
echo "[INFO] Command: /opt/fluent-bit/bin/fluent-bit --config=/etc/fluent-bit/fluent-bit.conf"
exec /opt/fluent-bit/bin/fluent-bit --config=/etc/fluent-bit/fluent-bit.conf