|
| 1 | +# Deployment Guide |
| 2 | + |
| 3 | +This document explains how to deploy the internal-bot to a production environment. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The deployment process uses: |
| 8 | +- Docker for containerization |
| 9 | +- Docker Compose for container orchestration |
| 10 | +- Ansible for automation |
| 11 | +- Nginx for web server and SSL termination |
| 12 | + |
| 13 | +The application is deployed as several containers: |
| 14 | +- Django web app (handles webhooks) |
| 15 | +- Discord bot (sends messages to Discord) |
| 16 | +- Background worker (processes tasks) |
| 17 | +- PostgreSQL database |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- A server running Ubuntu |
| 22 | +- SSH access to the server |
| 23 | +- Domain name pointing to the server |
| 24 | +- uv installed on your local machine (it will automatically download and install ansible, if you run it from the `make deploy/*` targets. |
| 25 | + |
| 26 | +## Deployment Process |
| 27 | + |
| 28 | +The deployment is done in three stages using Ansible playbooks: |
| 29 | + |
| 30 | +### 1. Server Setup |
| 31 | + |
| 32 | +```bash |
| 33 | +make deploy/provision |
| 34 | +``` |
| 35 | + |
| 36 | +This runs the first two playbooks: |
| 37 | +- `01_setup.yml`: Sets up server, installs Docker, creates users (nginx_user and intbot_user) |
| 38 | +- `02_nginx.yml`: Configures Nginx with SSL certificates |
| 39 | + |
| 40 | +### 2. Application Deployment |
| 41 | + |
| 42 | +```bash |
| 43 | +make deploy/app |
| 44 | +``` |
| 45 | + |
| 46 | +This runs the `03_app.yml` playbook which: |
| 47 | +- Builds Docker images |
| 48 | +- Sets up environment variables |
| 49 | +- Creates Docker Compose configuration |
| 50 | +- Runs database migrations |
| 51 | +- Starts all services |
| 52 | + |
| 53 | +## Ansible Templates and Separate User Environments |
| 54 | + |
| 55 | +The deployment uses a separated approach with different users for different responsibilities: |
| 56 | + |
| 57 | +### Separate Users and Docker Compose Files |
| 58 | + |
| 59 | +1. **Nginx Environment** (managed by `nginx_user`): |
| 60 | + - Uses its own Docker Compose file generated from `docker-compose.nginx.yml.j2` |
| 61 | + - Handles SSL termination and proxying |
| 62 | + - Has access to port 80/443 for web traffic |
| 63 | + |
| 64 | +2. **Application Environment** (managed by `intbot_user`): |
| 65 | + - Uses its own Docker Compose file generated from `docker-compose.app.yml.j2` |
| 66 | + - Runs Django app, Discord bot, worker, and database |
| 67 | + - Doesn't need direct public internet access |
| 68 | + |
| 69 | +Both environments are connected via a shared Docker network called "shared_with_nginx_network". This architecture provides several benefits: |
| 70 | +- **Security**: Each component runs with minimal required permissions |
| 71 | +- **Access Control**: Different teams can have access to different parts (some only to app, some to both) |
| 72 | +- **Separation of Concerns**: Nginx configuration changes don't affect the application |
| 73 | +- **Maintenance**: Either component can be updated independently |
| 74 | + |
| 75 | +### Custom Makefiles for Each Environment |
| 76 | + |
| 77 | +Ansible generates two specialized Makefiles: |
| 78 | + |
| 79 | +1. **Nginx Makefile** (`Makefile.nginx.j2`): |
| 80 | + - Focused on SSL certificate management |
| 81 | + - Key targets: |
| 82 | + - `certbot/init-staging`: Set up staging certificates (for testing) |
| 83 | + - `certbot/upgrade-to-prod`: Upgrade to production certificates |
| 84 | + - `certbot/renew`: Renew existing certificates |
| 85 | + - `certbot/force-reissue-PROD-certificate`: Force reissue production certificates |
| 86 | + |
| 87 | +2. **Application Makefile** (`Makefile.app.j2`): |
| 88 | + - Focused on application management |
| 89 | + - All commands use the `prod/` prefix |
| 90 | + - Key targets: |
| 91 | + - `prod/migrate`: Run database migrations |
| 92 | + - `prod/shell`: Access Django shell |
| 93 | + - `prod/db_shell`: Access database shell |
| 94 | + - `prod/manage`: Run Django management commands |
| 95 | + - `logs`: View application logs |
| 96 | + |
| 97 | +## Version Control |
| 98 | + |
| 99 | +Deployments are tied to specific Git commits: |
| 100 | + |
| 101 | +```bash |
| 102 | +# Deploy specific version |
| 103 | +make deploy/app V=abcd1234 |
| 104 | +``` |
| 105 | + |
| 106 | +If no version is specified, the current Git commit hash is used. |
| 107 | + |
| 108 | +## Environment Variables |
| 109 | + |
| 110 | +The application needs these environment variables in `intbot.env`: |
| 111 | + |
| 112 | +- `DJANGO_SECRET_KEY`: Secret key for Django |
| 113 | +- `DJANGO_ALLOWED_HOSTS`: Comma-separated list of allowed hosts |
| 114 | +- `DATABASE_URL`: PostgreSQL connection string |
| 115 | +- `DISCORD_BOT_TOKEN`: Discord bot authentication token |
| 116 | +- `DISCORD_GUILD_ID`: Discord server ID |
| 117 | +- `GITHUB_WEBHOOK_SECRET`: Secret for GitHub webhook verification |
| 118 | +- `GITHUB_TOKEN`: GitHub API token |
| 119 | +- `ZAMMAD_WEBHOOK_SECRET`: Secret for Zammad webhook verification |
| 120 | + |
| 121 | +An example file is available at `deploy/templates/app/intbot.env.example`. |
| 122 | + |
| 123 | +## Monitoring |
| 124 | + |
| 125 | +- Logs can be viewed with `docker compose logs` |
| 126 | +- The Django admin interface is available at `/admin/` |
| 127 | +- Server monitoring should be set up separately (not included) |
| 128 | + |
| 129 | +## Troubleshooting |
| 130 | + |
| 131 | +Common issues: |
| 132 | +- **Webhook verification failures**: Check secret keys in environment variables |
| 133 | +- **Database connection errors**: Verify DATABASE_URL is correct |
| 134 | +- **Discord messages not being sent**: Check DISCORD_BOT_TOKEN and permissions |
| 135 | + |
| 136 | +For more detailed logs: |
| 137 | +```bash |
| 138 | +docker compose logs -f bot |
| 139 | +docker compose logs -f web |
| 140 | +docker compose logs -f worker |
| 141 | +``` |
| 142 | + |
| 143 | +Or even simpler if you want to see all of them at once |
| 144 | +```bash |
| 145 | +make logs |
| 146 | +``` |
0 commit comments