From d5df19d30f8f8d2144ab37dbc1e7a3bb24c76666 Mon Sep 17 00:00:00 2001 From: Aaron van Meerten Date: Wed, 20 Mar 2024 12:22:08 -0500 Subject: [PATCH] jigasi: autoscaler sidecar support (#1738) * jigasi: autoscaler sidecar support * jigasi: remove local address --- jigasi.yml | 11 +++++++ jigasi/Dockerfile | 2 +- .../rootfs/defaults/autoscaler-sidecar.config | 18 +++++++++++ jigasi/rootfs/etc/cont-init.d/10-config | 32 +++++++++++++++++++ .../etc/services.d/50-autoscaler-sidecar/run | 10 ++++++ jigasi/rootfs/etc/services.d/jigasi/run | 5 ++- 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 jigasi/rootfs/defaults/autoscaler-sidecar.config create mode 100644 jigasi/rootfs/etc/services.d/50-autoscaler-sidecar/run diff --git a/jigasi.yml b/jigasi.yml index 210d591..3ad1d3f 100644 --- a/jigasi.yml +++ b/jigasi.yml @@ -11,6 +11,16 @@ services: - ${CONFIG}/jigasi:/config:Z - ${CONFIG}/transcripts:/tmp/transcripts:Z environment: + - AUTOSCALER_SIDECAR_KEY_FILE + - AUTOSCALER_SIDECAR_KEY_ID + - AUTOSCALER_SIDECAR_GROUP_NAME + - AUTOSCALER_SIDECAR_HOST_ID + - AUTOSCALER_SIDECAR_INSTANCE_ID + - AUTOSCALER_SIDECAR_PORT + - AUTOSCALER_SIDECAR_REGION + - AUTOSCALER_SIDECAR_SHUTDOWN_POLLING_INTERVAL + - AUTOSCALER_SIDECAR_STATS_POLLING_INTERVAL + - AUTOSCALER_URL - ENABLE_AUTH - ENABLE_GUESTS - XMPP_AUTH_DOMAIN @@ -22,6 +32,7 @@ services: - XMPP_DOMAIN - PUBLIC_URL - JIGASI_DISABLE_SIP + - JIGASI_LOG_FILE - JIGASI_SIP_URI - JIGASI_SIP_PASSWORD - JIGASI_SIP_SERVER diff --git a/jigasi/Dockerfile b/jigasi/Dockerfile index 5aea861..0a2ac1f 100644 --- a/jigasi/Dockerfile +++ b/jigasi/Dockerfile @@ -11,7 +11,7 @@ LABEL org.opencontainers.image.documentation="https://jitsi.github.io/handbook/" ENV GOOGLE_APPLICATION_CREDENTIALS /config/key.json RUN apt-dpkg-wrap apt-get update && \ - apt-dpkg-wrap apt-get install -y jigasi jq && \ + apt-dpkg-wrap apt-get install -y jigasi jq jitsi-autoscaler-sidecar && \ apt-cleanup COPY rootfs/ / diff --git a/jigasi/rootfs/defaults/autoscaler-sidecar.config b/jigasi/rootfs/defaults/autoscaler-sidecar.config new file mode 100644 index 0000000..ca68cff --- /dev/null +++ b/jigasi/rootfs/defaults/autoscaler-sidecar.config @@ -0,0 +1,18 @@ +{{ $SHUTDOWN_POLLING_INTERVAL := .Env.AUTOSCALER_SIDECAR_SHUTDOWN_POLLING_INTERVAL | default "60" -}} +{{ $STATS_POLLING_INTERVAL := .Env.AUTOSCALER_SIDECAR_STATS_POLLING_INTERVAL | default "30" -}} +{{ $JIGASI_STATS_PORT := .Env.JIGASI_STATS_PORT | default "8788" -}} +export SHUTDOWN_POLLING_INTERVAL={{ $SHUTDOWN_POLLING_INTERVAL }} +export STATS_POLLING_INTERVAL={{ $STATS_POLLING_INTERVAL }} +export PORT={{ .Env.AUTOSCALER_SIDECAR_PORT }} +export GRACEFUL_SHUTDOWN_SCRIPT="/usr/share/jigasi/graceful_shutdown.sh" +export TERMINATE_SCRIPT="/opt/jitsi/shutdown.sh" +export ENABLE_REPORT_STATS=true +export POLLING_URL="{{ .Env.AUTOSCALER_URL }}/sidecar/poll" +export STATUS_URL="{{ .Env.AUTOSCALER_URL }}/sidecar/status" +export STATS_RETRIEVE_URL="http://localhost:{{ $JIGASI_STATS_PORT }}/about/stats" +export STATS_REPORT_URL="{{ .Env.AUTOSCALER_URL }}/sidecar/stats" +export ASAP_SIGNING_KEY_FILE="{{ .Env.AUTOSCALER_SIDECAR_KEY_FILE }}" +export ASAP_JWT_KID="{{ .Env.AUTOSCALER_SIDECAR_KEY_ID }}" +export INSTANCE_TYPE="jigasi" +export INSTANCE_ID="{{ .Env.AUTOSCALER_SIDECAR_INSTANCE_ID }}" +export INSTANCE_METADATA='{"environment":"{{ .Env.XMPP_ENV_NAME }}","region":"{{ .Env.AUTOSCALER_SIDECAR_REGION }}","group":"{{ .Env.AUTOSCALER_SIDECAR_GROUP_NAME }}","name":"{{ .Env.JIGASI_INSTANCE_ID }}","version":"{{ .Env.JIGASI_VERSION }}","privateIp":"{{ .Env.LOCAL_ADDRESS }}","hostId":"{{ .Env.AUTOSCALER_SIDECAR_HOST_ID }}"}' diff --git a/jigasi/rootfs/etc/cont-init.d/10-config b/jigasi/rootfs/etc/cont-init.d/10-config index fcadba6..c158cb7 100644 --- a/jigasi/rootfs/etc/cont-init.d/10-config +++ b/jigasi/rootfs/etc/cont-init.d/10-config @@ -13,6 +13,38 @@ if [[ "$JIGASI_XMPP_PASSWORD" == "$OLD_JIGASI_XMPP_PASSWORD" ]]; then exit 1 fi +# set random jigasi nickname for the instance if is not set +[ -z "${JIGASI_INSTANCE_ID}" ] && export JIGASI_INSTANCE_ID="jigasi-$(date +%N)" + +# check for AUTOSCALER_URL, AUTOSCALER_SIDECAR_KEY_FILE and AUTOSCALER_SIDECAR_KEY_ID as indicator that sidecar should be enabled +if [ -n "$AUTOSCALER_URL" ]; then + if [ -z "$AUTOSCALER_SIDECAR_KEY_FILE" ]; then + export AUTOSCALER_SIDECAR_KEY_FILE="/etc/jitsi/autoscaler-sidecar/asap.pem" + fi + if [ -z "$AUTOSCALER_SIDECAR_KEY_ID" ]; then + # assume key id is equal to the base real path of the key file minus .pem + export AUTOSCALER_SIDECAR_KEY_ID="$(basename "$(realpath "$AUTOSCALER_SIDECAR_KEY_FILE")" | tr -d '.pem')" + fi + + if [ -f "$AUTOSCALER_SIDECAR_KEY_FILE" ]; then + echo "AUTOSCALER_URL found, enabling autoscaler sidecar" + + export JIGASI_VERSION="$(dpkg -s jigasi | grep Version | awk '{print $2}' | sed 's/..$//')" + + [ -z "$AUTOSCALER_SIDECAR_PORT" ] && export AUTOSCALER_SIDECAR_PORT="6000" + [ -z "$AUTOSCALER_SIDECAR_INSTANCE_ID" ] && export AUTOSCALER_SIDECAR_INSTANCE_ID="$JIGASI_INSTANCE_ID" + [ -z "$AUTOSCALER_SIDECAR_REGION" ] && export AUTOSCALER_SIDECAR_REGION="docker" + [ -z "$AUTOSCALER_SIDECAR_GROUP_NAME" ] && export AUTOSCALER_SIDECAR_GROUP_NAME="docker-jigasi" + + mkdir -p /etc/jitsi/autoscaler-sidecar + tpl /defaults/autoscaler-sidecar.config > /etc/jitsi/autoscaler-sidecar/config + else + echo "No key file at $AUTOSCALER_SIDECAR_KEY_FILE, leaving autoscaler sidecar disabled" + fi +else + echo "No AUTOSCALER_URL defined, leaving autoscaler sidecar disabled" +fi + tpl /defaults/logging.properties > /config/logging.properties tpl /defaults/sip-communicator.properties > /config/sip-communicator.properties diff --git a/jigasi/rootfs/etc/services.d/50-autoscaler-sidecar/run b/jigasi/rootfs/etc/services.d/50-autoscaler-sidecar/run new file mode 100644 index 0000000..22f7750 --- /dev/null +++ b/jigasi/rootfs/etc/services.d/50-autoscaler-sidecar/run @@ -0,0 +1,10 @@ +#!/usr/bin/with-contenv bash + +if [[ -n "$AUTOSCALER_URL" ]] && [[ -f "/etc/jitsi/autoscaler-sidecar/config" ]]; then + DAEMON="/usr/bin/node /usr/share/jitsi-autoscaler-sidecar/app.js" + exec s6-setuidgid autoscaler-sidecar /bin/bash -c ". /etc/jitsi/autoscaler-sidecar/config && exec $DAEMON" +else + # if autoscaler-sidecar should not be started, + # prevent s6 from restarting this script again and again + s6-svc -O /var/run/s6/services/50-autoscaler-sidecar +fi diff --git a/jigasi/rootfs/etc/services.d/jigasi/run b/jigasi/rootfs/etc/services.d/jigasi/run index 8b9eb5d..9c7f054 100644 --- a/jigasi/rootfs/etc/services.d/jigasi/run +++ b/jigasi/rootfs/etc/services.d/jigasi/run @@ -5,4 +5,7 @@ JAVA_SYS_PROPS="-Djava.util.logging.config.file=/config/logging.properties" DAEMON=/usr/share/jigasi/jigasi.sh DAEMON_OPTS="--nocomponent=true --configdir=/ --configdirname=config --min-port=${JIGASI_PORT_MIN:-20000} --max-port=${JIGASI_PORT_MAX:-20050}" -exec s6-setuidgid jigasi /bin/bash -c "JAVA_SYS_PROPS=\"$JAVA_SYS_PROPS\" exec $DAEMON $DAEMON_OPTS" +JIGASI_CMD="JAVA_SYS_PROPS=\"$JAVA_SYS_PROPS\" exec $DAEMON $DAEMON_OPTS" +[ -n "$JIGASI_LOG_FILE" ] && JIGASI_CMD="$JIGASI_CMD 2>&1 | tee $JIGASI_LOG_FILE" + +exec s6-setuidgid jigasi /bin/bash -c "$JIGASI_CMD"