OverviewFilesCommitsIssuesDocsReleasesPeers

hyperident-gateway / install-service.sh

140 lines · bash ref:
#!/bin/bash
# Install hyperident-gateway as a systemd service

set -e

echo "🔧 Hyperident Gateway - Service Installation"
echo "============================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "❌ Error: This script must be run as root"
    echo "   Run: sudo ./install-service.sh"
    exit 1
fi

# Get the actual user (in case running via sudo)
REAL_USER=${SUDO_USER:-$USER}
PROJECT_DIR=$(pwd)

echo "Installing service for user: $REAL_USER"
echo "Project directory: $PROJECT_DIR"
echo ""

# Find bare runtime
export NVM_DIR="/home/$REAL_USER/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Switch to node 20 context
export PATH="$NVM_DIR/versions/node/v20.11.0/bin:$PATH"

BARE_WRAPPER=$(which bare 2>/dev/null || echo "")

if [ -z "$BARE_WRAPPER" ]; then
    echo "❌ Error: bare not found"
    echo "   Install with: npm install -g bare (as user $REAL_USER)"
    exit 1
fi

BARE_DIR=$(dirname "$(dirname "$BARE_WRAPPER")")/lib/node_modules/bare
BARE_PATH="$BARE_DIR/node_modules/bare-runtime-linux-x64/bin/bare"

if [ ! -f "$BARE_PATH" ]; then
    echo "❌ Error: Could not find bare runtime binary"
    echo "   Expected at: $BARE_PATH"
    exit 1
fi

echo "Found bare runtime at: $BARE_PATH"
echo ""

# Create config directory if it doesn't exist
if [ ! -d "$PROJECT_DIR/config" ]; then
    mkdir -p "$PROJECT_DIR/config"
    chown $REAL_USER:$REAL_USER "$PROJECT_DIR/config"
fi

# Copy example config if config doesn't exist
if [ ! -f "$PROJECT_DIR/config/gateway-config.json" ]; then
    if [ -f "$PROJECT_DIR/config/gateway-config.example.json" ]; then
        echo "Creating default config from example..."
        cp "$PROJECT_DIR/config/gateway-config.example.json" "$PROJECT_DIR/config/gateway-config.json"
        chown $REAL_USER:$REAL_USER "$PROJECT_DIR/config/gateway-config.json"
        echo "⚠️  Please edit config/gateway-config.json before starting the service"
        echo ""
    fi
fi

# Create systemd service file with correct paths
echo "Creating systemd service file..."
cat > /etc/systemd/system/hyperident-gateway.service <<EOF
[Unit]
Description=Hyperident P2P Gateway
Documentation=https://github.com/hyperident-gateway
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=$PROJECT_DIR

ExecStart=$BARE_PATH $PROJECT_DIR/gateway.js --config $PROJECT_DIR/config/gateway-config.json

Restart=on-failure
RestartSec=5s
StartLimitInterval=60s
StartLimitBurst=3

StandardOutput=journal
StandardError=journal
SyslogIdentifier=hyperident-gateway

AmbientCapabilities=CAP_NET_ADMIN
SecureBits=keep-caps

LimitNOFILE=65536
LimitNPROC=4096

Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
EOF

echo "✅ Service file created: /etc/systemd/system/hyperident-gateway.service"
echo ""

# Reload systemd
echo "Reloading systemd daemon..."
systemctl daemon-reload
echo "✅ Systemd reloaded"
echo ""

echo "============================================="
echo "✅ Installation complete!"
echo "============================================="
echo ""
echo "Next steps:"
echo ""
echo "1. Configure the gateway:"
echo "   Edit: $PROJECT_DIR/config/gateway-config.json"
echo ""
echo "2. Start the service:"
echo "   sudo systemctl start hyperident-gateway"
echo ""
echo "3. Check status:"
echo "   sudo systemctl status hyperident-gateway"
echo ""
echo "4. View logs:"
echo "   sudo journalctl -u hyperident-gateway -f"
echo ""
echo "5. Enable auto-start on boot:"
echo "   sudo systemctl enable hyperident-gateway"
echo ""
echo "6. Stop the service:"
echo "   sudo systemctl stop hyperident-gateway"
echo ""