From b48b1ec8861fab631d34d681a20b2c3a8684b09a Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 30 Nov 2024 11:27:55 +0100 Subject: [PATCH] feat: add install script Signed-off-by: kjuulh --- install.sh | 110 ++++++++++++++++++++--------------------------------- 1 file changed, 42 insertions(+), 68 deletions(-) diff --git a/install.sh b/install.sh index 84c5e27..25d595f 100644 --- a/install.sh +++ b/install.sh @@ -1,97 +1,71 @@ -#!/bin/bash +#!/usr/bin/env bash set -e -# Configuration variables -GITEA_HOST="https://git.front.kjuulh.io" -REPO_OWNER="kjuulh" -REPO_NAME="churn-v2" +# Configuration +APP_NAME="churn" +APP_VERSION="latest" # or specify a version +S3_BUCKET="rust-artifacts" BINARY_NAME="churn" -SERVICE_NAME="churn-agent" -SERVICE_USER="churn" -RELEASE_TAG="latest" # or specific version like "v1.0.0" +SERVICE_NAME="${APP_NAME}.service" +INSTALL_DIR="/usr/local/bin" +CONFIG_DIR="/etc/${APP_NAME}" + +# 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 "Please run as root" + echo -e "${RED}Please run as root${NC}" exit 1 fi -# Create service user if it doesn't exist -if ! id "$SERVICE_USER" &>/dev/null; then - useradd -r -s /bin/false "$SERVICE_USER" -fi +# Create necessary directories +echo "Creating directories..." +mkdir -p "${INSTALL_DIR}" +mkdir -p "${CONFIG_DIR}" -# Function to get latest release if RELEASE_TAG is "latest" -get_latest_release() { - curl -s "https://$GITEA_HOST/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/latest" | \ - grep '"tag_name":' | \ - sed -E 's/.*"([^"]+)".*/\1/' -} +# 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}" -# Determine the actual release tag -if [ "$RELEASE_TAG" = "latest" ]; then - RELEASE_TAG=$(get_latest_release) -fi - -echo "Installing $BINARY_NAME version $RELEASE_TAG..." - -# Download and install binary -TMP_DIR=$(mktemp -d) -cd "$TMP_DIR" - -# Download binary from Gitea -curl -L -o "$BINARY_NAME" \ - "https://$GITEA_HOST/$REPO_OWNER/$REPO_NAME/releases/download/$RELEASE_TAG/$BINARY_NAME" - -# Make binary executable and move to appropriate location -chmod +x "$BINARY_NAME" -mv "$BINARY_NAME" "/usr/local/bin/$BINARY_NAME" +# Make binary executable +chmod +x "${INSTALL_DIR}/${BINARY_NAME}" # Create systemd service file -cat > "/etc/systemd/system/$SERVICE_NAME.service" << EOF +echo "Creating systemd service..." +cat > "/etc/systemd/system/${SERVICE_NAME}" << EOF [Unit] -Description=$SERVICE_NAME Service +Description=${APP_NAME} service After=network.target [Service] Type=simple -User=$SERVICE_USER -ExecStart=/usr/local/bin/$BINARY_NAME +User=nobody +Group=nogroup +ExecStart=${INSTALL_DIR}/${BINARY_NAME} Restart=always -RestartSec=5 - -# Security hardening options -ProtectSystem=strict -ProtectHome=true -NoNewPrivileges=true -ReadWritePaths=/var/log/$SERVICE_NAME +RestartSec=10 +Environment=RUST_LOG=info [Install] WantedBy=multi-user.target EOF -# Create log directory if logging is needed -mkdir -p "/var/log/$SERVICE_NAME" -chown "$SERVICE_USER:$SERVICE_USER" "/var/log/$SERVICE_NAME" - # Reload systemd and enable service +echo "Configuring systemd service..." systemctl daemon-reload -systemctl enable "$SERVICE_NAME" -systemctl start "$SERVICE_NAME" +systemctl enable "${SERVICE_NAME}" +systemctl start "${SERVICE_NAME}" -# Clean up -cd -rm -rf "$TMP_DIR" +# 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 -echo "Installation complete! Service status:" -systemctl status "$SERVICE_NAME" - -# Provide some helpful commands -echo " -Useful commands: -- Check status: systemctl status $SERVICE_NAME -- View logs: journalctl -u $SERVICE_NAME -- Restart service: systemctl restart $SERVICE_NAME -- Stop service: systemctl stop $SERVICE_NAME -"