#!/usr/bin/env bash set -e # Configuration APP_NAME="churn" APP_VERSION="latest" # or specify a version S3_BUCKET="rust-artifacts" BINARY_NAME="churn" SERVICE_NAME="${APP_NAME}.service" SERVICE_UPDATE_NAME="${APP_NAME}-update.service" TIMER_UPDATE_NAME="${APP_NAME}-update.timer" INSTALL_DIR="/usr/local/bin" CONFIG_DIR="/etc/${APP_NAME}" CHURN_DISCOVERY="https://churn.prod.kjuulh.app" LOG="/var/log/churn-install.log" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' # No Color exec > >(tee -i ${LOG}) exec 2>&1 echo "Starting churn install $(date)" # Check if running as root if [ "$EUID" -ne 0 ]; then echo -e "${RED}Please run as root${NC}" exit 1 fi # Create necessary directories echo "Creating directories..." mkdir -p "${INSTALL_DIR}" mkdir -p "${CONFIG_DIR}" if systemctl is-active --quiet churn.service; then echo "Stopping existing churn service..." systemctl stop churn.service fi # Download binary from S3 echo "Downloading binary..." curl -L -s "https://api-minio.front.kjuulh.io/${S3_BUCKET}/releases/${APP_NAME}/${APP_VERSION}/${BINARY_NAME}" -o "${INSTALL_DIR}/${BINARY_NAME}" # Make binary executable chmod +x "${INSTALL_DIR}/${BINARY_NAME}" echo "Starting churn agent setup..." set +e res=$(churn agent setup --discovery "${CHURN_DISCOVERY}" 2>&1) set -e exit_code=$? if [[ $exit_code != 0 ]]; then if [[ "$res" != *"config file already exists"* ]] && [[ "$res" != *"already exists"* ]]; then echo "Error detected: $res" exit 1 else echo "Ignoring setup 'agent is already setup'" fi fi # Create systemd service file echo "Creating systemd service..." cat > "/etc/systemd/system/${SERVICE_NAME}" << EOF [Unit] Description=${APP_NAME} service After=network.target [Service] Type=simple User=root Group=root ExecStart=${INSTALL_DIR}/${BINARY_NAME} agent start Restart=always RestartSec=10 Environment=RUST_LOG=h2=warn,hyper=warn,churn=debug,warn [Install] WantedBy=multi-user.target EOF echo "Creating churn update service..." cat > "/etc/systemd/system/${SERVICE_UPDATE_NAME}" < "/etc/systemd/system/${TIMER_UPDATE_NAME}" <