#!/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 ───────────────────────────────────────────────── # Skip if already running from the install directory if [ "$SCRIPT_DIR" != "$INSTALL_DIR" ]; then for item in *.py *.txt *.sh *.md .env.example systemd; do [ -e "$SCRIPT_DIR/$item" ] && cp -r "$SCRIPT_DIR/$item" "$INSTALL_DIR/" done fi # ── 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 ───────────────────────────────────────── # python3 is pre-installed on Ubuntu but python3-venv is a separate package # that is frequently absent — install it unconditionally (apt is idempotent) echo "Ensuring python3-venv and pip are available..." apt-get install -y python3-venv python3-pip # Check for pip specifically — a previous failed run may have left an incomplete venv if [ ! -f "$INSTALL_DIR/venv/bin/pip" ]; then rm -rf "$INSTALL_DIR/venv" python3 -m venv "$INSTALL_DIR/venv" || { echo "ERROR: 'python3 -m venv' failed. Try: apt-get install python3-venv" exit 1 } 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"