feat: add install script
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
79a8a34499
commit
b48b1ec886
110
install.sh
110
install.sh
@ -1,97 +1,71 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Configuration variables
|
# Configuration
|
||||||
GITEA_HOST="https://git.front.kjuulh.io"
|
APP_NAME="churn"
|
||||||
REPO_OWNER="kjuulh"
|
APP_VERSION="latest" # or specify a version
|
||||||
REPO_NAME="churn-v2"
|
S3_BUCKET="rust-artifacts"
|
||||||
BINARY_NAME="churn"
|
BINARY_NAME="churn"
|
||||||
SERVICE_NAME="churn-agent"
|
SERVICE_NAME="${APP_NAME}.service"
|
||||||
SERVICE_USER="churn"
|
INSTALL_DIR="/usr/local/bin"
|
||||||
RELEASE_TAG="latest" # or specific version like "v1.0.0"
|
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
|
# Check if running as root
|
||||||
if [ "$EUID" -ne 0 ]; then
|
if [ "$EUID" -ne 0 ]; then
|
||||||
echo "Please run as root"
|
echo -e "${RED}Please run as root${NC}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create service user if it doesn't exist
|
# Create necessary directories
|
||||||
if ! id "$SERVICE_USER" &>/dev/null; then
|
echo "Creating directories..."
|
||||||
useradd -r -s /bin/false "$SERVICE_USER"
|
mkdir -p "${INSTALL_DIR}"
|
||||||
fi
|
mkdir -p "${CONFIG_DIR}"
|
||||||
|
|
||||||
# Function to get latest release if RELEASE_TAG is "latest"
|
# Download binary from S3
|
||||||
get_latest_release() {
|
echo "Downloading binary..."
|
||||||
curl -s "https://$GITEA_HOST/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/latest" | \
|
curl -L "https://api-minio.front.kjuulh.io/${S3_BUCKET}/releases/${APP_NAME}/${APP_VERSION}/${BINARY_NAME}" -o "${INSTALL_DIR}/${BINARY_NAME}"
|
||||||
grep '"tag_name":' | \
|
|
||||||
sed -E 's/.*"([^"]+)".*/\1/'
|
|
||||||
}
|
|
||||||
|
|
||||||
# Determine the actual release tag
|
# Make binary executable
|
||||||
if [ "$RELEASE_TAG" = "latest" ]; then
|
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
|
||||||
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"
|
|
||||||
|
|
||||||
# Create systemd service file
|
# Create systemd service file
|
||||||
cat > "/etc/systemd/system/$SERVICE_NAME.service" << EOF
|
echo "Creating systemd service..."
|
||||||
|
cat > "/etc/systemd/system/${SERVICE_NAME}" << EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=$SERVICE_NAME Service
|
Description=${APP_NAME} service
|
||||||
After=network.target
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
User=$SERVICE_USER
|
User=nobody
|
||||||
ExecStart=/usr/local/bin/$BINARY_NAME
|
Group=nogroup
|
||||||
|
ExecStart=${INSTALL_DIR}/${BINARY_NAME}
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=5
|
RestartSec=10
|
||||||
|
Environment=RUST_LOG=info
|
||||||
# Security hardening options
|
|
||||||
ProtectSystem=strict
|
|
||||||
ProtectHome=true
|
|
||||||
NoNewPrivileges=true
|
|
||||||
ReadWritePaths=/var/log/$SERVICE_NAME
|
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
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
|
# Reload systemd and enable service
|
||||||
|
echo "Configuring systemd service..."
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl enable "$SERVICE_NAME"
|
systemctl enable "${SERVICE_NAME}"
|
||||||
systemctl start "$SERVICE_NAME"
|
systemctl start "${SERVICE_NAME}"
|
||||||
|
|
||||||
# Clean up
|
# Check service status
|
||||||
cd
|
if systemctl is-active --quiet "${SERVICE_NAME}"; then
|
||||||
rm -rf "$TMP_DIR"
|
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
|
|
||||||
"
|
|
||||||
|
Loading…
Reference in New Issue
Block a user