#!/usr/bin/env bash # GitKS bare-metal installation script # # Installs the gitks binary, systemd service, environment config, # logrotate config, and creates the service user and data directory. # # Usage: # sudo ./install.sh # install from local build # sudo ./install.sh /path/to/gitks # install from prebuilt binary # sudo GITKS_USER=custom ./install.sh # use a different service user set -euo pipefail GITKS_USER="${GITKS_USER:-gitks}" GITKS_GROUP="${GITKS_GROUP:-gitks}" BINARY_SRC="${1:-target/release/gitks}" INSTALL_PREFIX="${INSTALL_PREFIX:-/usr/local}" DATA_DIR="${DATA_DIR:-/data/repos}" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' log() { echo -e "${GREEN}[INFO]${NC} $*"; } warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } err() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; } # ── Preflight checks ────────────────────────────────────────────── [[ $EUID -eq 0 ]] || err "This script must be run as root (use sudo)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" command -v git >/dev/null 2>&1 || { warn "git not found in PATH. Installing git..." if command -v apt-get >/dev/null 2>&1; then apt-get update -qq && apt-get install -y -qq git elif command -v yum >/dev/null 2>&1; then yum install -y git elif command -v dnf >/dev/null 2>&1; then dnf install -y git else err "Cannot install git automatically. Please install git first." fi } # ── Create service user ─────────────────────────────────────────── if id "$GITKS_USER" &>/dev/null; then log "User '$GITKS_USER' already exists" else log "Creating system user '$GITKS_USER'" useradd --system --user-group --home-dir /var/lib/gitks \ --shell /usr/sbin/nologin --comment "GitKS service" "$GITKS_USER" fi # ── Install binary ──────────────────────────────────────────────── BINARY_DST="${INSTALL_PREFIX}/bin/gitks" if [[ -f "$BINARY_SRC" ]]; then log "Installing gitks binary: $BINARY_SRC → $BINARY_DST" install -o root -g root -m 0755 "$BINARY_SRC" "$BINARY_DST" elif [[ -f "$PROJECT_DIR/$BINARY_SRC" ]]; then log "Installing gitks binary: $PROJECT_DIR/$BINARY_SRC → $BINARY_DST" install -o root -g root -m 0755 "$PROJECT_DIR/$BINARY_SRC" "$BINARY_DST" else warn "Binary not found at '$BINARY_SRC'. Skipping binary install." warn "Build first: cargo build --release" warn "Or specify path: sudo ./install.sh /path/to/gitks" fi # ── Create directories ──────────────────────────────────────────── log "Creating directories..." install -d -o "$GITKS_USER" -g "$GITKS_GROUP" -m 0750 /var/lib/gitks install -d -o "$GITKS_USER" -g "$GITKS_GROUP" -m 0750 /var/log/gitks install -d -o root -g root -m 0755 /etc/gitks [[ -d "$DATA_DIR" ]] || { log "Creating repo data directory: $DATA_DIR" mkdir -p "$DATA_DIR" chown "$GITKS_USER:$GITKS_GROUP" "$DATA_DIR" } # ── Install config ──────────────────────────────────────────────── CONF_SRC="${PROJECT_DIR}/etc/gitks.env" if [[ -f /etc/gitks/gitks.env ]]; then warn "/etc/gitks/gitks.env already exists — skipping (preserving existing config)" else log "Installing environment config..." install -o root -g "$GITKS_GROUP" -m 0640 "$CONF_SRC" /etc/gitks/gitks.env log "Edit /etc/gitks/gitks.env to configure your deployment" fi # ── Install systemd service ─────────────────────────────────────── log "Installing systemd service..." install -o root -g root -m 0644 \ "${PROJECT_DIR}/etc/systemd/gitks.service" \ /etc/systemd/system/gitks.service # ── Install logrotate config ────────────────────────────────────── LOG_ROTATE_SRC="${PROJECT_DIR}/etc/logrotate.d/gitks" if [[ -d /etc/logrotate.d ]]; then if [[ -f /etc/logrotate.d/gitks ]]; then warn "/etc/logrotate.d/gitks already exists — skipping" else log "Installing logrotate config..." install -o root -g root -m 0644 "$LOG_ROTATE_SRC" /etc/logrotate.d/gitks fi fi # ── Reload systemd & enable ─────────────────────────────────────── log "Reloading systemd..." systemctl daemon-reload log "Enabling gitks to start on boot..." systemctl enable gitks.service # ── Done ────────────────────────────────────────────────────────── echo "" log "Installation complete!" echo "" echo " Next steps:" echo " 1. Edit config: vim /etc/gitks/gitks.env" echo " 2. Start service: systemctl start gitks" echo " 3. Check status: systemctl status gitks" echo " 4. View logs: journalctl -fu gitks" echo "" echo " Cluster setup (multi-node):" echo " - Set STORAGE_NAME uniquely per node" echo " - Set GITKS_CLUSTER_HOSTNAME to each node's hostname" echo " - Set GITKS_ADVERTISE_ADDR to each node's reachable IP:port" echo " - Point GITKS_ETCD_ENDPOINTS to your etcd cluster" echo ""