churn-v2/install.sh
kjuulh 38a0a9fba4
All checks were successful
continuous-integration/drone/push Build is passing
feat: agent is already setup
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-11-30 11:44:51 +01:00

88 lines
2.1 KiB
Bash

#!/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"
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc/${APP_NAME}"
CHURN_DISCOVERY="https://churn.internal.prod.kjuulh.app"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# 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}"
# 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}"
# 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=nobody
Group=nogroup
ExecStart=${INSTALL_DIR}/${BINARY_NAME} agent start
Restart=always
RestartSec=10
Environment=RUST_LOG=info
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and enable service
echo "Configuring systemd service..."
systemctl daemon-reload
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