Skip to content

Commit

Permalink
Merge pull request #17 from startersclan/docs/add-example-docker-comp…
Browse files Browse the repository at this point in the history
…ose.example.yml-for-production-deployments

Docs: Add example `docker-compose.example.yml` for production deployments
  • Loading branch information
leojonathanoh authored Oct 31, 2023
2 parents 424b418 + 879ad12 commit 2e667ef
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 4 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ a PHP frontend.

## Usage (docker)

> The tag convention is `<version>-<service>` or `<version>-<sha>-<service>`. For instance, for release `v1.2.3` on sha `0abcdef`:
>
> - `web` image tags: `1-web`, `1.2-web`, `1.2.3-web`, `1-0abcdef-web`, `1.2-0abcdef-web`, `1.2.3-0abcdef-web`
> - `daemon` image tags: `1-daemon`, `1.2-daemon`, `1.2.3-daemon`, `1-0abcdef-daemon`, `1.2-0abcdef-daemon`, `1.2.3-0abcdef-daemon`
`web` image (See [./web/config.php](./web/config.php) for supported environment variables):

```sh
Expand All @@ -47,10 +52,13 @@ docker run --rm -it -e DB_ADDR=db -e DB_NAME=hlstatsxce -e DB_USER=hlstatsxce -e
docker run --rm -it -p 27500:27500/udp startersclan/hlstatsx-community-edition:1.8.0-daemon --db-host=db:3306 --db-name=hlstatsxce --db-username=hlstatsxce --db-password=hlstatsxce #--help
```

The tag convention is `<version>-<service>` or `<version>-<sha>-<service>`. For instance, for release `v1.2.3` on sha `0abcdef`:
To deploy using Docker Compose:

```sh
docker compose -f docker-compose.example.yml up
```

- `web` image tags: `1-web`, `1.2-web`, `1.2.3-web`, `1-0abcdef-web`, `1.2-0abcdef-web`, `1.2.3-0abcdef-web`
- `daemon` image tags: `1-daemon`, `1.2-daemon`, `1.2.3-daemon`, `1-0abcdef-daemon`, `1.2-0abcdef-daemon`, `1.2.3-0abcdef-daemon`
- [install.sql](./sql/install.sql) is mounted in `mysql` container which automatically installs the DB only on the first time. If you prefer not to mount `install.sql`, you may manually install the DB by logging into PHPMyAdmin and importing the `install.sql` there.

### Upgrading (docker)

Expand Down
211 changes: 211 additions & 0 deletions docker-compose.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
version: '2.2'
services:
# 1. Counter-Strike 1.6 gameserver sends UDP logs to source-udp-forwarder
# See: https://github.com/startersclan/docker-sourceservers
cstrike:
image: goldsourceservers/cstrike:latest
volumes:
- dns-volume:/dns:ro
ports:
- 27015:27015/udp
networks:
- default
stdin_open: true
tty: true
stop_signal: SIGKILL
depends_on:
- source-udp-forwarder
entrypoint:
- /bin/bash
command:
- -c
- |
set -eu
exec hlds_linux -console -noipx -secure -game cstrike +map de_dust2 +maxplayers 32 +sv_lan 0 +ip 0.0.0.0 +port 27015 +rcon_password password +log on +logaddress_add "$$( cat /dns/source-udp-forwarder )" 26999
# 2. source-udp-forwarder proxy forwards gameserver logs to the daemon
# See: https://github.com/startersclan/source-udp-forwarder
source-udp-forwarder:
image: startersclan/source-udp-forwarder:latest
environment:
- UDP_LISTEN_ADDR=:26999
- UDP_FORWARD_ADDR=daemon:27500
- FORWARD_PROXY_KEY=somedaemonsecret # The daemon's proxy_key secret
- FORWARD_GAMESERVER_IP=192.168.1.100 # The gameserver's IP as registered in the HLStatsX:CE database
- FORWARD_GAMESERVER_PORT=27015 # The gameserver's IP as registered in the HLStatsX:CE database
- LOG_LEVEL=INFO
- LOG_FORMAT=txt
volumes:
- dns-volume:/dns
networks:
- default
depends_on:
- daemon
entrypoint:
- /bin/sh
command:
- -c
- |
set -eu
echo "Outputting my IP address"
ip addr show eth0 | grep 'inet ' | awk '{print $$2}' | cut -d '/' -f1 | tee /dns/source-udp-forwarder
exec /source-udp-forwarder
# 3. HLStatsX:CE perl daemon accepts the gameserver logs. Gameserver Logs are parsed and stats are recorded
# The daemon's proxy_key secret can only be setup in the HLStatsX:CE Web Admin Panel Settings under 'Proxy Settings' section
daemon:
image: startersclan/hlstatsx-community-edition:1.8.0-daemon
ports:
- 27500:27500/udp # For external servers to send logs to the daemon
networks:
- default
command:
- --ip=0.0.0.0
- --port=27500
- --db-host=db:3306
- --db-name=hlstatsxce
- --db-username=hlstatsxce
- --db-password=hlstatsxce
- --nodns-resolveip
- --debug
# - --debug
# - --help

# Cron - awards
awards:
image: startersclan/hlstatsx-community-edition:1.8.0-daemon
stop_signal: SIGKILL
entrypoint:
- /bin/sh
command:
- -c
- |
set -eu
echo "Creating /awards.sh"
cat - > /awards.sh <<'EOF'
#!/bin/sh
set -eu
cd /scripts
perl hlstats-awards.pl --db-host=db:3306 --db-name=hlstatsxce --db-username=hlstatsxce --db-password=hlstatsxce #--help
EOF
chmod +x /awards.sh
# Run at 00:00 daily. To customize your cron schedule, use https://crontab.guru
echo "Creating crontab"
crontab - <<'EOF'
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 0 * * * /awards.sh > /proc/1/fd/1 2>/proc/1/fd/2
EOF
crontab -l
echo "Running cron"
cron -f
# 4. HLStatsX:CE DB
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=root # Username 'root', password 'root'
- MYSQL_USER=hlstatsxce
- MYSQL_PASSWORD=hlstatsxce
- MYSQL_DATABASE=hlstatsxce
volumes:
- db-volume:/var/lib/mysql
- ./sql/install.sql:/docker-entrypoint-initdb.d/install.sql:ro # This seeds the DB only on the first time
networks:
- default

# 5. HLStatsX:CE web
# Available at http://localhost:8081
# Admin Panel username: admin, password: 123456
web:
image: startersclan/hlstatsx-community-edition:1.8.0-web
volumes:
- games-volume:/web/hlstatsimg/games # Stateful games volume, which also contains heatmaps
environment:
- DB_ADDR=db
- DB_NAME=hlstatsxce
- DB_USER=hlstatsxce
- DB_PASS=hlstatsxce
- DB_DEBUG=0
ports:
- 8081:80
- 9000
networks:
- default
depends_on:
- init-container

# Cron - Heatmaps
heatmaps:
image: startersclan/hlstatsx-community-edition:1.8.0-web
volumes:
- games-volume:/web/hlstatsimg/games # Stateful games volume, which also contains heatmaps
environment:
- DB_HOST=db
- DB_NAME=hlstatsxce
- DB_USER=hlstatsxce
- DB_PASS=hlstatsxce
- HLXCE_WEB=/web
- HUD_URL=http://www.hlxcommunity.com
- OUTPUT_SIZE=medium
- DEBUG=1
depends_on:
- init-container
working_dir: /heatmaps
stop_signal: SIGKILL
entrypoint:
- /bin/sh
command:
- -c
- |
set -eu
# Run at 00:00 daily. To customize your cron schedule, use https://crontab.guru
echo "Creating crontab"
crontab - <<'EOF'
0 0 * * * php /heatmaps/generate.php > /proc/1/fd/1 2>/proc/1/fd/2
EOF
crontab -l
echo "Running crond"
exec crond -f
# PHPMyAdmin to manage DB
# Available at http://localhost:8083
phpmyadmin:
image: phpmyadmin:5.2
environment:
- PMA_HOST=db
ports:
- 8083:80
networks:
- default

# Init container to set permissions in mounted folders and volumes
init-container:
image: alpine:latest
volumes:
- db-volume:/var/lib/mysql
networks:
- default
entrypoint:
- /bin/sh
command:
- -c
- |
set -eu
echo "Granting db write permissions"
chown -R 999:999 /var/lib/mysql
networks:
default:

volumes:
dns-volume:
db-volume:
games-volume:
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ services:
- MYSQL_DATABASE=hlstatsxce
volumes:
- db-volume:/var/lib/mysql
- ./sql/install.sql:/docker-entrypoint-initdb.d/install.sql:ro
- ./sql/install.sql:/docker-entrypoint-initdb.d/install.sql:ro # This seeds the DB only on the first time
networks:
- default

Expand Down
2 changes: 2 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ fi

VERSION=$( echo "$TAG" | sed 's/^v//' )
VERSION_PREV=$( echo "$TAG_PREV" | sed 's/^v//' )
VERSION_PREV_REGEX=$( echo "$VERSION_PREV" | sed 's/\./\\./g' ) # '1.0.0' -> '1\.0\.0'
DBVERSION_PREV=$( ls web/updater | grep -E '^[0-9]+\.php$' | sort -n | tail -n1 | cut -d '.' -f1 )
DBVERSION=$(( $DBVERSION_PREV + 1 ))

# Bump version in docs, .php, and .sql files
sed -i "s/$VERSION_PREV/$VERSION/" README.md
sed -i "s/$VERSION_PREV/$VERSION/" docker-compose.example.yml
sed -i "s/^SET @DBVERSION=.*/SET @DBVERSION=\"$DBVERSION\";/" sql/install.sql
sed -i "s/^SET @VERSION=.*/SET @VERSION=\"$VERSION\";/" sql/install.sql
echo "Creating web/updater/$DBVERSION.php"
Expand Down

0 comments on commit 2e667ef

Please sign in to comment.