#!/usr/bin/env bash set -euo pipefail HOST="" REMOTE_ROOT="/opt/lingniu-go-native" usage() { cat <<'USAGE' Usage: scripts/install-identity-writer-service.sh --host root@115.29.187.205 [--remote-root DIR] Prepares the native identity-writer service and switches Gateway registration persistence to Kafka delegation. The existing Gateway binary ignores the new environment switch, so this is safe to run before staging the first release that contains identity-writer. USAGE } while [[ $# -gt 0 ]]; do case "$1" in --host) HOST="${2:-}" shift 2 ;; --remote-root) REMOTE_ROOT="${2:-}" shift 2 ;; -h|--help) usage exit 0 ;; *) echo "unknown argument: $1" >&2 usage >&2 exit 2 ;; esac done if [[ -z "$HOST" ]]; then echo "--host is required" >&2 exit 2 fi ssh "$HOST" "REMOTE_ROOT='$REMOTE_ROOT' bash -s" <<'REMOTE' set -euo pipefail env_dir="${REMOTE_ROOT}/env" gateway_env="${env_dir}/gateway.env" identity_env="${env_dir}/identity-writer.env" unit="/etc/systemd/system/lingniu-go-identity-writer.service" mkdir -p "${env_dir}" cat >"${identity_env}" <<'ENV' KAFKA_GROUP=go-identity-writer KAFKA_TOPIC=vehicle.raw.go.jt808.v1 KAFKA_START_OFFSET=last HEALTH_ADDR=127.0.0.1:20217 MYSQL_ENSURE_SCHEMA=true MYSQL_MAX_OPEN_CONNS=8 MYSQL_MAX_IDLE_CONNS=4 MYSQL_CONN_MAX_LIFETIME_SECONDS=300 IDENTITY_WRITER_BATCH_SIZE=500 IDENTITY_WRITER_BATCH_WAIT_MS=20 IDENTITY_WRITER_RETRY_DELAY_MS=1000 JT808_REGISTRATION_LOCATION_TOUCH_INTERVAL_SECONDS=600 LOCAL_TZ=Asia/Shanghai ENV chmod 0640 "${identity_env}" touch "${gateway_env}" if grep -q '^JT808_REGISTRATION_GATEWAY_WRITES_ENABLED=' "${gateway_env}"; then sed -i 's/^JT808_REGISTRATION_GATEWAY_WRITES_ENABLED=.*/JT808_REGISTRATION_GATEWAY_WRITES_ENABLED=false/' "${gateway_env}" else printf '\nJT808_REGISTRATION_GATEWAY_WRITES_ENABLED=false\n' >>"${gateway_env}" fi cat >"${unit}" <