2024-11-30 11:27:55 +01:00
|
|
|
#!/usr/bin/env bash
|
2024-11-24 00:53:43 +01:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2024-11-30 11:27:55 +01:00
|
|
|
# Configuration
|
|
|
|
APP_NAME="churn"
|
|
|
|
APP_VERSION="latest" # or specify a version
|
|
|
|
S3_BUCKET="rust-artifacts"
|
2024-11-24 00:53:43 +01:00
|
|
|
BINARY_NAME="churn"
|
2024-11-30 11:27:55 +01:00
|
|
|
SERVICE_NAME="${APP_NAME}.service"
|
|
|
|
INSTALL_DIR="/usr/local/bin"
|
|
|
|
CONFIG_DIR="/etc/${APP_NAME}"
|
2024-11-30 11:48:26 +01:00
|
|
|
CHURN_DISCOVERY="https://churn.prod.kjuulh.app"
|
2024-11-30 11:27:55 +01:00
|
|
|
|
|
|
|
# Colors for output
|
|
|
|
RED='\033[0;31m'
|
|
|
|
GREEN='\033[0;32m'
|
|
|
|
NC='\033[0m' # No Color
|
2024-11-24 00:53:43 +01:00
|
|
|
|
|
|
|
# Check if running as root
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
2024-11-30 11:27:55 +01:00
|
|
|
echo -e "${RED}Please run as root${NC}"
|
2024-11-24 00:53:43 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-11-30 11:27:55 +01:00
|
|
|
# Create necessary directories
|
|
|
|
echo "Creating directories..."
|
|
|
|
mkdir -p "${INSTALL_DIR}"
|
|
|
|
mkdir -p "${CONFIG_DIR}"
|
2024-11-24 00:53:43 +01:00
|
|
|
|
2024-11-30 11:27:55 +01:00
|
|
|
# Download binary from S3
|
|
|
|
echo "Downloading binary..."
|
|
|
|
curl -L "https://api-minio.front.kjuulh.io/${S3_BUCKET}/releases/${APP_NAME}/${APP_VERSION}/${BINARY_NAME}" -o "${INSTALL_DIR}/${BINARY_NAME}"
|
2024-11-24 00:53:43 +01:00
|
|
|
|
2024-11-30 11:27:55 +01:00
|
|
|
# Make binary executable
|
|
|
|
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
|
2024-11-24 00:53:43 +01:00
|
|
|
|
2024-11-30 11:42:46 +01:00
|
|
|
echo "Starting churn agent setup..."
|
2024-11-30 11:43:45 +01:00
|
|
|
set +e
|
2024-11-30 11:37:28 +01:00
|
|
|
res=$(churn agent setup --discovery "${CHURN_DISCOVERY}" 2>&1)
|
2024-11-30 11:43:45 +01:00
|
|
|
set -e
|
2024-11-30 11:35:24 +01:00
|
|
|
exit_code=$?
|
2024-11-30 11:40:05 +01:00
|
|
|
|
2024-11-30 11:35:24 +01:00
|
|
|
if [[ $exit_code != 0 ]]; then
|
2024-11-30 11:42:46 +01:00
|
|
|
if [[ "$res" != *"config file already exists"* ]] && [[ "$res" != *"already exists"* ]]; then
|
|
|
|
echo "Error detected: $res"
|
2024-11-30 11:35:24 +01:00
|
|
|
exit 1
|
2024-11-30 11:42:46 +01:00
|
|
|
else
|
2024-11-30 11:44:51 +01:00
|
|
|
echo "Ignoring setup 'agent is already setup'"
|
2024-11-30 11:35:24 +01:00
|
|
|
fi
|
|
|
|
fi
|
2024-11-30 11:32:41 +01:00
|
|
|
|
2024-11-24 00:53:43 +01:00
|
|
|
# Create systemd service file
|
2024-11-30 11:27:55 +01:00
|
|
|
echo "Creating systemd service..."
|
|
|
|
cat > "/etc/systemd/system/${SERVICE_NAME}" << EOF
|
2024-11-24 00:53:43 +01:00
|
|
|
[Unit]
|
2024-11-30 11:27:55 +01:00
|
|
|
Description=${APP_NAME} service
|
2024-11-24 00:53:43 +01:00
|
|
|
After=network.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Type=simple
|
2024-11-30 11:45:49 +01:00
|
|
|
User=root
|
|
|
|
Group=root
|
2024-11-30 11:33:21 +01:00
|
|
|
ExecStart=${INSTALL_DIR}/${BINARY_NAME} agent start
|
2024-11-24 00:53:43 +01:00
|
|
|
Restart=always
|
2024-11-30 11:27:55 +01:00
|
|
|
RestartSec=10
|
|
|
|
Environment=RUST_LOG=info
|
2024-11-24 00:53:43 +01:00
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Reload systemd and enable service
|
2024-11-30 11:27:55 +01:00
|
|
|
echo "Configuring systemd service..."
|
2024-11-24 00:53:43 +01:00
|
|
|
systemctl daemon-reload
|
2024-11-30 11:27:55 +01:00
|
|
|
systemctl enable "${SERVICE_NAME}"
|
|
|
|
systemctl start "${SERVICE_NAME}"
|
|
|
|
|
|
|
|
# Check service status
|
|
|
|
if systemctl is-active --quiet "${SERVICE_NAME}"; then
|
|
|
|
echo -e "${GREEN}Installation successful! ${APP_NAME} is running.${NC}"
|
|
|
|
echo "You can check the status with: systemctl status ${SERVICE_NAME}"
|
|
|
|
else
|
|
|
|
echo -e "${RED}Installation completed but service failed to start. Check logs with: journalctl -u ${SERVICE_NAME}${NC}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2024-11-24 00:53:43 +01:00
|
|
|
|