|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# PeerClaw systemd installation script. |
| 4 | +# Usage: sudo ./install.sh [path-to-binary] |
| 5 | +# |
| 6 | +# This script: |
| 7 | +# 1. Creates the peerclaw system user and group |
| 8 | +# 2. Installs the binary to /usr/local/bin/ |
| 9 | +# 3. Sets up configuration and data directories |
| 10 | +# 4. Installs and enables the systemd unit |
| 11 | +# |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +BINARY="${1:-../../bin/peerclawd}" |
| 15 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 16 | + |
| 17 | +# Colors |
| 18 | +RED='\033[0;31m' |
| 19 | +GREEN='\033[0;32m' |
| 20 | +YELLOW='\033[1;33m' |
| 21 | +NC='\033[0m' |
| 22 | + |
| 23 | +info() { echo -e "${GREEN}[INFO]${NC} $*"; } |
| 24 | +warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } |
| 25 | +error() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; } |
| 26 | + |
| 27 | +# Must run as root. |
| 28 | +if [[ $EUID -ne 0 ]]; then |
| 29 | + error "This script must be run as root (use sudo)" |
| 30 | +fi |
| 31 | + |
| 32 | +# Check binary exists. |
| 33 | +if [[ ! -f "$BINARY" ]]; then |
| 34 | + error "Binary not found: $BINARY\n Build first with: make build" |
| 35 | +fi |
| 36 | + |
| 37 | +info "Installing PeerClaw..." |
| 38 | + |
| 39 | +# 1. Create system user. |
| 40 | +if ! id -u peerclaw &>/dev/null; then |
| 41 | + useradd --system --no-create-home --shell /usr/sbin/nologin --user-group peerclaw |
| 42 | + info "Created system user: peerclaw" |
| 43 | +else |
| 44 | + info "System user peerclaw already exists" |
| 45 | +fi |
| 46 | + |
| 47 | +# 2. Install binary. |
| 48 | +install -m 0755 "$BINARY" /usr/local/bin/peerclawd |
| 49 | +info "Installed binary to /usr/local/bin/peerclawd" |
| 50 | + |
| 51 | +# 3. Create directories. |
| 52 | +install -d -m 0755 -o peerclaw -g peerclaw /var/lib/peerclaw |
| 53 | +install -d -m 0755 -o peerclaw -g peerclaw /var/log/peerclaw |
| 54 | +install -d -m 0750 -o root -g peerclaw /etc/peerclaw |
| 55 | +info "Created directories: /var/lib/peerclaw, /var/log/peerclaw, /etc/peerclaw" |
| 56 | + |
| 57 | +# 4. Install config (don't overwrite existing). |
| 58 | +if [[ ! -f /etc/peerclaw/config.yaml ]]; then |
| 59 | + install -m 0640 -o root -g peerclaw "$SCRIPT_DIR/../../configs/peerclaw.production.yaml" /etc/peerclaw/config.yaml |
| 60 | + info "Installed config to /etc/peerclaw/config.yaml" |
| 61 | +else |
| 62 | + warn "Config /etc/peerclaw/config.yaml already exists, skipping" |
| 63 | +fi |
| 64 | + |
| 65 | +if [[ ! -f /etc/peerclaw/peerclaw.env ]]; then |
| 66 | + install -m 0640 -o root -g peerclaw "$SCRIPT_DIR/peerclaw.env" /etc/peerclaw/peerclaw.env |
| 67 | + info "Installed env file to /etc/peerclaw/peerclaw.env" |
| 68 | + warn "Edit /etc/peerclaw/peerclaw.env and set JWT_SECRET before starting" |
| 69 | +else |
| 70 | + warn "Env file /etc/peerclaw/peerclaw.env already exists, skipping" |
| 71 | +fi |
| 72 | + |
| 73 | +# 5. Install systemd unit. |
| 74 | +install -m 0644 "$SCRIPT_DIR/peerclawd.service" /etc/systemd/system/peerclawd.service |
| 75 | +systemctl daemon-reload |
| 76 | +info "Installed systemd unit: peerclawd.service" |
| 77 | + |
| 78 | +# 6. Enable (but don't start yet). |
| 79 | +systemctl enable peerclawd.service |
| 80 | +info "Enabled peerclawd.service" |
| 81 | + |
| 82 | +echo "" |
| 83 | +info "Installation complete!" |
| 84 | +echo "" |
| 85 | +echo " Next steps:" |
| 86 | +echo " 1. Edit /etc/peerclaw/peerclaw.env and set JWT_SECRET:" |
| 87 | +echo " sudo nano /etc/peerclaw/peerclaw.env" |
| 88 | +echo "" |
| 89 | +echo " 2. Review /etc/peerclaw/config.yaml for your environment:" |
| 90 | +echo " sudo nano /etc/peerclaw/config.yaml" |
| 91 | +echo "" |
| 92 | +echo " 3. Start the service:" |
| 93 | +echo " sudo systemctl start peerclawd" |
| 94 | +echo "" |
| 95 | +echo " 4. Check status:" |
| 96 | +echo " sudo systemctl status peerclawd" |
| 97 | +echo " sudo journalctl -u peerclawd -f" |
| 98 | +echo "" |
0 commit comments