A website for running the aiarena.net infrastructure.
-
Clone the project
You can use the IDE way of doing it, or do it manually:
git clone https://github.com/aiarena/aiarena-web/
-
Make sure you have
uvinstalled -
Set up your virtual environment
In Pycharm, you just need to create a new local interpreter (make sure to select
uvas the interpreter type and match the python version to the one currently set in pyproject.toml). Otherwise, you can do:uv venv
After this is done, you'll want to run
uv sync
to install python deps, and
cd aiarena/frontend-spa && npm install
to install javascript deps.
-
Configuring environment variables
You'll want to set environment variable
DJANGO_ENVIRONMENTtoDEVELOPMENT. There are several ways to do this - one way is to prefix every command with the variable:DJANGO_ENVIRONMENT=DEVELOPMENT uv run manage.py runserver -
Set up Postgres and Redis
There is a docker-compose.yml file that's configured to run correct versions of Postgres and Redis. It already ensures that there is a correct user / database created in the Postgres DB. If you want to manually connect to those, look up the credentials in the compose file.
The
DATABASESsetting and Redis-related settings in default.py will automatically point to those databases, but if you wish to use a custom database config, you can override those settings in local.py. This file is gitignored.
For Pycharm users, there's a ready-made
Databasesrun config that launches the docker-compose file, as well as aProjectcompound run config that launches everything needed for development. -
Populate the DB
You can go two routes here - either populate it with seed data (if you don't want to worry about protecting production data on a local machine, or don't have access to AWS), or just restore the production backup locally (better for reproducing performance issues, simpler, but requires AWS access).
- Seed data - run
uv run manage.py migrateto apply the database migrations, thenuv run manage.py seedand optionallyuv run manage.py generatestats - Restore backup - run
uv run run.py restore-backup --s3. That will download the latest production backup, which will already have the migrations applied.
- Seed data - run
-
Launching the Website
If the enviornment is not activated - activate it:
source .venv/bin/activateIn the terminal, run:
docker compose up
In another terminal run:
uv run manage.py graphql_schema uv run manage.py runserver
In another terminal - navigate to aiarena-web/aiarena/frontend-spa
cd aiarena/frontend-spathen run:
npm run start_relay
In yet another terminal - navigate to aiarena-web/aiarena/frontend-spa
cd aiarena/frontend-spathen run:
npm run dev
AI Arena should now be available in your browser at
http://127.0.0.1:8000/If you used seed data in the previous step, you can log into the website using the accounts below:
Admin user: username - devadmin, password - x.
Regular user: username - devuser1, password - x.
Otherwise (if you restored the production backup), you can log in with the same username and password that you use in aiarena.net.
-
Install pre-commit hooks
Whenever you're pushing code to production, we'll run linters and make sure your code is well-formatted. To get faster feedback on this, you can install pre-commit hooks that will run the exact same checks when you're trying to commit.
uv run pre-commit install
After creating the venv with UV. navigate to ./venv/bin/activate and add the env export just before "deactivate()":
```shell
export DJANGO_ENVIRONMENT=DEVELOPMENT
```
Then reactivate the environment with source.
This is needed to restore production backups, or do any other work with production environment.
-
Make sure you have an AWS user with
SuperPowerUsersgroup -
Configure your AWS credentials:
Get your
Access key IDandSecret access keyinIAMsection of AWS Console and save them on your machine underaiarenaprofile:- go to https://315513665747.signin.aws.amazon.com/console
- in the IAM find your user, go to
Security credentialsand create an access key if you don't have one - use the credentials to configure AWS:
$ aws configure --profile aiarena AWS Access Key ID [None]: ****** AWS Secret Access Key [None]: ****** Default region name [None]: eu-central-1 Default output format [None]:
Pre-requisites:
- Install local development tools and configure your AWS credentials, as described above;
- Install AWS Session Manager plugin.
After that you should be able to use the uv run.py production-one-off-task command.
It will spin up a new task with no production traffic routed to it, and with custom CPU and memory values.
By default, the task will be killed in 24 hours to make sure it's not consuming money after it's finished. You can use the --lifetime-hours flag to override this behaviour if you need to run something really long.
Also, by default, the task is killed early if you disconnect from the ssh session. Use the --dont-kill-on-disconnect flag to disable this behaviour, if you want it running in the background.
Pre-requisites:
- Install local development tools and configure your AWS credentials, as described above;
- Install AWS Session Manager plugin.
After that you should be able to use the uv run.py production-attach-to-task command. It will help you find an existing production container, and connect you to it.
You can also specify a task ID with --task-id <task id here> flag, if you already have one (for example, if you created a one-off task).
This is the way we do Infrastructure-as-Code. All the AWS resources we have are in index-default.yaml template file. Make some changes to it, and then generate a CloudFormation template based on it:
uv run.py cloudformation
The template would be saved to cloudformation-default.yaml in the project root
directory. Next, use this template to update the stack in CloudFormation
section of AWS Console:
- Select
aiarenaand clickUpdate - Choose
Replace current template - Choose
Upload a template file, and select thecloudformation-default.yamlfile that you generated in the previous step. Then click next. - Update the variables if you want to, then click next until you get to the last screen.
- On the last screen please wait for the
Change set previewto be generated, it can take a minute. Take a look at the preview, and make sure it makes sense. - Click
Submitand watch the infrastructure change
We're running the application code inside of Fargate containers, so to deploy a new version to prod, we need to build a new Docker image, and then update the Fargate containers to use it.
Here's how the deployment process works:
- Changes are pushed to the
mainbranch - GitHub Actions starts running tests/linters and in parallel runs
uv run.py prepare-images, which builds an image and pushes it to Elastic Container Registry with a tag likebuild-111-amd64 - If all tests/linters passed,
uv run.py ecsruns next:- It makes a
latestalias to the image that we built earlier - Then, it runs
manage.py migrate - Finally, it triggers a rolling update to all the Fargate containers, that will use the
latestimage
- It makes a
- As a final step,
uv run.py monitor-ecsruns. It watches the rolling update, and makes sure all the services are replaced, and running. This step can fail if the containers are failing to start for some reason.
Core project functionality
Web API endpoints and functionality.
This root api folder contains views for public use.
API endpoints specifically for use by the arenaclients to obtain new matches and report results.
API endpoints specifically for use by the livestream player to obtain a curated list of match replays to feature.
Django template website frontend
React frontend for the profile dashboard
GraphQL API used by the React frontend
A module for linking website users to their patreon counterparts.
Copyright (c) 2019
Licensed under the GPLv3 license.