Files
mev-beta/orig/AUTO_UPDATE_GUIDE.md
Administrator c54c569f30 refactor: move all remaining files to orig/ directory
Completed clean root directory structure:
- Root now contains only: .git, .env, docs/, orig/
- Moved all remaining files and directories to orig/:
  - Config files (.claude, .dockerignore, .drone.yml, etc.)
  - All .env variants (except active .env)
  - Git config (.gitconfig, .github, .gitignore, etc.)
  - Tool configs (.golangci.yml, .revive.toml, etc.)
  - Documentation (*.md files, @prompts)
  - Build files (Dockerfiles, Makefile, go.mod, go.sum)
  - Docker compose files
  - All source directories (scripts, tests, tools, etc.)
  - Runtime directories (logs, monitoring, reports)
  - Dependency files (node_modules, lib, cache)
  - Special files (--delete)

- Removed empty runtime directories (bin/, data/)

V2 structure is now clean:
- docs/planning/ - V2 planning documents
- orig/ - Complete V1 codebase preserved
- .env - Active environment config (not in git)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 10:53:05 +01:00

9.0 KiB

MEV Bot - Auto-Update Guide

This guide explains how to set up automatic updates for the MEV Bot. When the master branch is updated, your production bot will automatically pull changes, rebuild, and restart.

Quick Setup

# For auto-updates with systemd timer (checks every 5 minutes)
sudo ./scripts/setup-auto-update.sh

# Or without systemd timer (manual updates only)
./scripts/setup-auto-update.sh

This installs:

  • Git hooks for auto-rebuild after git pull
  • Systemd timer for periodic update checks (if run with sudo)
  • Logging infrastructure

Update Methods

The auto-update system supports three methods:

Automatically checks for updates every 5 minutes.

Setup:

sudo ./scripts/setup-auto-update.sh

Manage the timer:

# Check status
sudo systemctl status mev-bot-auto-update.timer

# View logs
sudo journalctl -u mev-bot-auto-update -f

# Stop timer
sudo systemctl stop mev-bot-auto-update.timer

# Start timer
sudo systemctl start mev-bot-auto-update.timer

# Disable auto-updates
sudo systemctl disable mev-bot-auto-update.timer

# See when next check will run
systemctl list-timers mev-bot-auto-update.timer

Change update frequency:

Edit /etc/systemd/system/mev-bot-auto-update.timer:

[Timer]
# Check every 10 minutes instead of 5
OnUnitActiveSec=10min

Then reload:

sudo systemctl daemon-reload
sudo systemctl restart mev-bot-auto-update.timer

2. Manual Updates with Auto-Rebuild

Git hooks automatically rebuild and restart after pulling updates.

Usage:

# Just pull - hooks handle the rest
git pull

# Or use the auto-update script
./scripts/auto-update.sh

The post-merge hook will:

  • Rebuild the Docker image
  • Restart the container
  • Show you the logs
  • Log everything to logs/auto-update.log

3. Webhook-Triggered Updates

Get instant updates when you push to GitHub/GitLab.

Setup webhook receiver:

# Start the webhook receiver
./scripts/webhook-receiver.sh

# Or run in background
nohup ./scripts/webhook-receiver.sh > logs/webhook.log 2>&1 &

Configure GitHub webhook:

  1. Go to your repository → Settings → Webhooks → Add webhook
  2. Set Payload URL: http://your-server:9000/webhook
  3. Content type: application/json
  4. Secret: (optional, configure in script)
  5. Events: Just the push event
  6. Branch filter: master

Configure GitLab webhook:

  1. Go to your repository → Settings → Webhooks
  2. Set URL: http://your-server:9000/webhook
  3. Secret token: (optional, configure in script)
  4. Trigger: Push events
  5. Branch filter: master

How It Works

Update Flow

Remote Update Detected
        ↓
    git fetch
        ↓
Compare local vs remote commits
        ↓
    git pull
        ↓
Post-merge hook triggers
        ↓
  Docker rebuild
        ↓
Container restart
        ↓
   Verification
        ↓
 Logs & notification

What Gets Updated

  • Application code
  • Dependencies (go.mod)
  • Docker image
  • Configuration (if changed)
  • .env file (preserved)
  • Runtime data
  • Logs

Safety Features

The auto-update system includes safety checks:

  1. Uncommitted changes: Won't update if you have local changes
  2. Branch check: Only updates on master branch
  3. Build verification: Ensures Docker build succeeds
  4. Container health: Verifies container starts properly
  5. Rollback capability: Previous image remains available
  6. Detailed logging: All actions logged to logs/auto-update.log

Monitoring Auto-Updates

View Update Logs

# Live tail
tail -f logs/auto-update.log

# Last 50 lines
tail -50 logs/auto-update.log

# Search for specific updates
grep "Update Complete" logs/auto-update.log

# See what commits were deployed
grep "Updated from" logs/auto-update.log

Check Last Update

# Using git
git log -1 --pretty=format:"%h - %an, %ar : %s"

# Using logs
tail -20 logs/auto-update.log | grep "Updated to"

# Container restart time
docker inspect mev-bot-production | grep StartedAt

Systemd Status

# Timer status
systemctl status mev-bot-auto-update.timer

# Service status
systemctl status mev-bot-auto-update.service

# Recent logs
journalctl -u mev-bot-auto-update -n 50

# Follow logs
journalctl -u mev-bot-auto-update -f

Notifications

Slack Notifications

Add to your .env:

WEBHOOK_URL="https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK"

Discord Notifications

Add to your .env:

WEBHOOK_URL="https://discord.com/api/webhooks/YOUR/DISCORD/WEBHOOK"

Custom Notifications

Edit scripts/auto-update.sh and modify the notification section:

if command -v curl &> /dev/null && [ -n "$WEBHOOK_URL" ]; then
    # Custom notification logic here
    curl -X POST "$WEBHOOK_URL" ...
fi

Troubleshooting

Updates Not Happening

Check timer is running:

systemctl status mev-bot-auto-update.timer
systemctl list-timers | grep mev-bot

Check for errors:

journalctl -u mev-bot-auto-update -n 50
tail -50 logs/auto-update.log

Verify git access:

git fetch origin
git status

Build Failures

Check build logs:

tail -100 logs/auto-update.log | grep -A 20 "ERROR"

Try manual build:

docker compose build

Check disk space:

df -h
docker system df

Uncommitted Changes Blocking Updates

# See what's uncommitted
git status

# Stash changes
git stash save "Local changes before auto-update"

# Or commit them
git add .
git commit -m "Local production changes"

# Or reset (careful!)
git reset --hard origin/master

Rollback to Previous Version

# See available images
docker images | grep mev-bot

# Stop current container
docker compose down

# Use previous image (if available)
docker tag mev-bot:latest mev-bot:backup
# Then restore from backup or rebuild specific commit

# Or rollback via git
git log --oneline -10  # Find commit to rollback to
git reset --hard <commit-hash>
docker compose up -d --build

Best Practices

1. Test Updates in Staging First

# On staging server
git pull
docker compose up -d --build
# Run tests, verify functionality
# Then push to production (auto-updates will handle it)

2. Monitor After Updates

# Watch logs for 5 minutes after update
docker compose logs -f mev-bot

# Check metrics
curl http://localhost:9090/metrics

# Verify trading activity
grep "opportunity detected" logs/auto-update.log

3. Backup Before Major Updates

# Backup database/state
cp -r data/ backup/data-$(date +%Y%m%d-%H%M%S)

# Tag current version
git tag -a v$(date +%Y%m%d-%H%M%S) -m "Pre-update backup"

4. Use Feature Branches

# Develop on feature branches
git checkout -b feature/new-strategy

# Test thoroughly
# Merge to master only when ready
git checkout master
git merge feature/new-strategy
git push origin master

# Production auto-updates within 5 minutes

5. Schedule Maintenance Windows

For major updates, temporarily disable auto-updates:

# Disable timer
sudo systemctl stop mev-bot-auto-update.timer

# Perform manual update with monitoring
./scripts/auto-update.sh

# Re-enable timer
sudo systemctl start mev-bot-auto-update.timer

Advanced Configuration

Custom Update Frequency

Create a custom timer:

# Edit /etc/systemd/system/mev-bot-auto-update.timer
[Timer]
OnBootSec=5min
OnUnitActiveSec=1min  # Check every minute (aggressive)
# or
OnCalendar=*:0/15     # Check every 15 minutes
# or
OnCalendar=hourly     # Check every hour

Update Specific Branches

Edit /etc/systemd/system/mev-bot-auto-update.service:

[Service]
Environment="GIT_BRANCH=production"
Environment="GIT_REMOTE=origin"

Pre/Post Update Hooks

Edit scripts/auto-update.sh to add custom logic:

# Before update
pre_update_hook() {
    # Custom logic (e.g., backup, notifications)
}

# After update
post_update_hook() {
    # Custom logic (e.g., warmup, health checks)
}

Security Considerations

  1. Use SSH keys for git - Avoid storing credentials
  2. Validate webhook signatures - Prevent unauthorized updates
  3. Limit network access - Firewall webhook receiver port
  4. Monitor update logs - Detect suspicious activity
  5. Use signed commits - Verify update authenticity

Uninstall Auto-Updates

# Remove systemd timer
sudo systemctl stop mev-bot-auto-update.timer
sudo systemctl disable mev-bot-auto-update.timer
sudo rm /etc/systemd/system/mev-bot-auto-update.*
sudo systemctl daemon-reload

# Remove git hooks
rm .git/hooks/post-merge

# Stop webhook receiver
pkill -f webhook-receiver.sh

Support

If you encounter issues:

  1. Check logs: tail -100 logs/auto-update.log
  2. Verify git status: git status
  3. Test manual update: ./scripts/auto-update.sh
  4. Check systemd: journalctl -u mev-bot-auto-update -n 50

Your MEV bot now auto-updates whenever master branch changes! 🎉