Deployment Guide
Overview
This guide covers deploying CodeAnvil on your own infrastructure. CodeAnvil consists of:
- anvil-server - The SSH server that handles git operations and CodeAnvil commands
- anvil - The CLI client used by developers
- anvil-agent - The build agent for CI/CD pipelines
Requirements
Server Requirements
- Linux server (Ubuntu 22.04+ or similar)
- Git installed and accessible
- OpenSSH client for build agent communication
- At least 512MB RAM
- Storage for git repositories and database
Network Requirements
- SSH port (default: 2222) accessible to users
- SSH port accessible to build agents (if running separately)
Building from Source
bash
# Install Rust if not already installed
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Clone the repository
git clone ssh://git@codeanvil.io:2222/~/anvil
cd anvil
# Build release binaries
cargo build --release
# Binaries will be in target/release/
# - anvil-server
# - anvil (CLI)
# - anvil-agent
Install Binaries
bash
# Copy binaries to /usr/local/bin sudo cp target/release/anvil-server /usr/local/bin/ sudo cp target/release/anvil /usr/local/bin/ sudo cp target/release/anvil-agent /usr/local/bin/ # Make executable sudo chmod +x /usr/local/bin/anvil*
Server Setup
Create Data Directory
bash
# Create directory for repositories sudo mkdir -p /var/lib/codeanvil/repos sudo chown codeanvil:codeanvil /var/lib/codeanvil/repos # Create directory for database sudo mkdir -p /var/lib/codeanvil/data sudo chown codeanvil:codeanvil /var/lib/codeanvil/data
Create User
bash
# Create a dedicated user for CodeAnvil sudo useradd -r -s /bin/false -d /var/lib/codeanvil codeanvil sudo chown -R codeanvil:codeanvil /var/lib/codeanvil
Systemd Service
Create a systemd service file for automatic startup and management:
Server Service
Create /etc/systemd/system/anvil-server.service:
ini
[Unit] Description=CodeAnvil SSH Server After=network.target [Service] Type=simple User=codeanvil Group=codeanvil WorkingDirectory=/var/lib/codeanvil Environment="ANVIL_DB=/var/lib/codeanvil/data/anvil.db" Environment="REPO_STORAGE_PATH=/var/lib/codeanvil/repos" Environment="ANVIL_LISTEN=0.0.0.0:2222" # Mailgun configuration (optional) # Environment="ANVIL_MAILGUN_API_KEY=your-api-key" # Environment="ANVIL_MAILGUN_DOMAIN=mg.example.com" # Environment="ANVIL_MAIL_FROM=CodeAnvil" ExecStart=/usr/local/bin/anvil-server Restart=on-failure RestartSec=5 # Security settings NoNewPrivileges=true ProtectSystem=strict ProtectHome=true ReadWritePaths=/var/lib/codeanvil [Install] WantedBy=multi-user.target
Enable and Start
bash
# Reload systemd sudo systemctl daemon-reload # Enable service to start on boot sudo systemctl enable anvil-server # Start the service sudo systemctl start anvil-server # Check status sudo systemctl status anvil-server # View logs sudo journalctl -u anvil-server -f
Reverse Proxy (Optional)
If you want to use a custom domain with SSH, you can use nginx for TCP proxying:
nginx
# In nginx.conf or a stream config file
stream {
upstream anvil_ssh {
server 127.0.0.1:2222;
}
server {
listen 22;
proxy_pass anvil_ssh;
}
}
Note: This requires nginx to be configured with stream module.
Make sure to configure SELinux or firewall rules appropriately.
Build Agent Setup
The build agent can run on the same server or on separate machines.
Agent Service
Create /etc/systemd/system/anvil-agent.service:
ini
[Unit] Description=CodeAnvil Build Agent After=network.target [Service] Type=simple User=codeanvil Group=codeanvil WorkingDirectory=/var/lib/codeanvil-agent ExecStart=/usr/local/bin/anvil-agent start Restart=on-failure RestartSec=10 # Security settings NoNewPrivileges=true ProtectSystem=strict ProtectHome=true ReadWritePaths=/var/lib/codeanvil-agent /tmp [Install] WantedBy=multi-user.target
Register the Agent
bash
# First, create agent registration on server (as admin) anvil agent register "ci-agent-1" # Copy the registration code and SSH key to the agent machine # Then configure the agent anvil-agent configure --host your-server.com --port 2222 # Start the agent sudo systemctl start anvil-agent
Environment Variables
Server Variables
| Variable | Default | Description |
|---|---|---|
ANVIL_DB |
anvil.db |
Path to SQLite database file |
ANVIL_LISTEN |
127.0.0.1:2222 |
Address and port to listen on |
REPO_STORAGE_PATH |
./repos |
Directory for git repositories |
ANVIL_MAILGUN_API_KEY |
- | Mailgun API key for email notifications |
ANVIL_MAILGUN_DOMAIN |
- | Mailgun domain |
ANVIL_MAIL_FROM |
CodeAnvil |
From address for emails |
ANVIL_AI_API_KEY |
- | API key for AI code review |
ANVIL_AI_API_ENDPOINT |
zhipu API | AI API endpoint URL |
Backup & Recovery
What to Backup
/var/lib/codeanvil/data/anvil.db- The SQLite database/var/lib/codeanvil/repos/- All git repositories
Backup Script
bash
#!/bin/bash # /usr/local/bin/backup-codeanvil.sh BACKUP_DIR="/backup/codeanvil" DATE=$(date +%Y%m%d_%H%M%S) # Create backup directory mkdir -p "$BACKUP_DIR" # Backup database cp /var/lib/codeanvil/data/anvil.db "$BACKUP_DIR/anvil_$DATE.db" # Backup repositories (using tar for efficiency) tar -czf "$BACKUP_DIR/repos_$DATE.tar.gz" -C /var/lib/codeanvil repos/ # Keep only last 7 days of backups find "$BACKUP_DIR" -name "*.db" -mtime +7 -delete find "$BACKUP_DIR" -name "*.tar.gz" -mtime +7 -delete echo "Backup completed: $DATE"
Restore
bash
# Stop the server sudo systemctl stop anvil-server # Restore database cp /backup/codeanvil/anvil_YYYYMMDD_HHMMSS.db /var/lib/codeanvil/data/anvil.db # Restore repositories tar -xzf /backup/codeanvil/repos_YYYYMMDD_HHMMSS.tar.gz -C /var/lib/codeanvil/ # Fix permissions sudo chown -R codeanvil:codeanvil /var/lib/codeanvil # Start the server sudo systemctl start anvil-server