Files
crypto-trader/install.sh
2026-05-03 23:37:55 -04:00

82 lines
3.7 KiB
Bash

#!/usr/bin/env bash
# Installs crypto-trader as a systemd service on Ubuntu/Debian.
# Must be run as root (or with sudo): sudo bash install.sh
#
# What this does:
# 1. Copies the project to /opt/crypto-trader
# 2. Creates a dedicated system user 'crypto-trader'
# 3. Sets up a Python virtual environment
# 4. Installs and enables the systemd service + timer
set -euo pipefail
INSTALL_DIR="/opt/crypto-trader"
SERVICE_USER="crypto-trader"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: Run this with sudo: sudo bash install.sh"
exit 1
fi
echo "Installing crypto-trader to $INSTALL_DIR ..."
# ── 1. Create install directory ───────────────────────────────────────────
mkdir -p "$INSTALL_DIR"
# ── 2. Copy project files ─────────────────────────────────────────────────
rsync -a --exclude='.git' --exclude='venv' --exclude='__pycache__' \
"$SCRIPT_DIR/" "$INSTALL_DIR/"
# ── 3. Create dedicated system user ──────────────────────────────────────
if ! id -u "$SERVICE_USER" &>/dev/null; then
useradd --system --no-create-home --shell /usr/sbin/nologin "$SERVICE_USER"
echo "Created system user: $SERVICE_USER"
fi
# ── 4. Set up .env (API keys) ─────────────────────────────────────────────
if [ ! -f "$INSTALL_DIR/.env" ]; then
cp "$INSTALL_DIR/.env.example" "$INSTALL_DIR/.env"
chmod 600 "$INSTALL_DIR/.env"
chown "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/.env"
echo ""
echo ">>> Created $INSTALL_DIR/.env — add your Kraken API keys before starting:"
echo " sudo nano $INSTALL_DIR/.env"
echo ""
fi
# ── 5. Python virtual environment ─────────────────────────────────────────
if ! command -v python3 &>/dev/null; then
echo "Installing python3-venv ..."
apt-get install -y python3-venv python3-pip
fi
if [ ! -d "$INSTALL_DIR/venv" ]; then
python3 -m venv "$INSTALL_DIR/venv"
fi
"$INSTALL_DIR/venv/bin/pip" install --upgrade pip -q
"$INSTALL_DIR/venv/bin/pip" install -r "$INSTALL_DIR/requirements.txt" -q
echo "Python dependencies installed"
# ── 6. Ownership ─────────────────────────────────────────────────────────
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR"
# positions.json and bot.log need to be writable by the service user
touch "$INSTALL_DIR/positions.json" "$INSTALL_DIR/bot.log" 2>/dev/null || true
chown "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/positions.json" "$INSTALL_DIR/bot.log" 2>/dev/null || true
# ── 7. Systemd units ──────────────────────────────────────────────────────
cp "$INSTALL_DIR/systemd/crypto-trader.service" /etc/systemd/system/
cp "$INSTALL_DIR/systemd/crypto-trader.timer" /etc/systemd/system/
systemctl daemon-reload
systemctl enable crypto-trader.timer
echo ""
echo "Installation complete."
echo ""
echo "Next steps:"
echo " 1. Edit API keys: sudo nano $INSTALL_DIR/.env"
echo " 2. Test paper mode: sudo -u $SERVICE_USER $INSTALL_DIR/venv/bin/python $INSTALL_DIR/bot.py"
echo " 3. Start the timer: sudo systemctl start crypto-trader.timer"
echo " 4. View logs: journalctl -u crypto-trader.service -f"
echo " 5. Check schedule: systemctl list-timers crypto-trader.timer"