Rewrite the README and setup guide, and fix the example Compose setup
The README describes what Kafkatorio does now (the live map, production and research pages), that this is a Factorio 2.0 fork, and how the services fit together. The guide is rewritten as numbered steps, from building the images to a public website: configuration, creating the server's token locally (instead of typing the secret into jwt.io), connecting the Factorio server's log, sending the existing map, exporting icons and names, and the reverse proxy. Also: the Unraid setup, updating, and troubleshooting. It warns that the mod portal's kafkatorio-events is the original, for Factorio 1.1, with overlapping version numbers, so players must install the mod zip by hand. The example docker-compose.yml lost all data when containers were recreated (e.g. on every update), and exposed Kafka, which has no authentication, on all interfaces. It's now based on the Unraid setup: Kafka data, map tiles, state and icons are kept next to it, only the website is exposed, and the optional Factorio server (--profile factorio) starts after the syslog server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c766794d9c
commit
a6be36df2e
4 changed files with 447 additions and 228 deletions
361
docs/guide.md
361
docs/guide.md
|
|
@ -1,106 +1,309 @@
|
|||
# Running Kafkatorio
|
||||
|
||||
### Building the project
|
||||
This guide sets up Kafkatorio for a Factorio server, from building it to a public website.
|
||||
|
||||
#### Requirements
|
||||
- [What you need](#what-you-need)
|
||||
- [1. Build the Docker images and the mod](#1-build-the-docker-images-and-the-mod)
|
||||
- [2. Configure Kafkatorio](#2-configure-kafkatorio)
|
||||
- [3. Start Kafkatorio](#3-start-kafkatorio)
|
||||
- [4. Connect the Factorio server](#4-connect-the-factorio-server)
|
||||
- [5. Send the existing map](#5-send-the-existing-map)
|
||||
- [6. Icons and names](#6-icons-and-names)
|
||||
- [7. Put it on the internet](#7-put-it-on-the-internet)
|
||||
- [Unraid](#unraid)
|
||||
- [Updating](#updating)
|
||||
- [Troubleshooting](#troubleshooting)
|
||||
- [Developing](#developing)
|
||||
|
||||
* Docker
|
||||
* Docker Compose
|
||||
* Java 11
|
||||
## What you need
|
||||
|
||||
Kafkatorio uses [Gradle](https://docs.gradle.org/current/userguide/getting_started.html) to
|
||||
build the project, and Docker Compose to create images.
|
||||
* **A Factorio 2.0 server running in Docker**, for example
|
||||
[factoriotools/factorio](https://hub.docker.com/r/factoriotools/factorio). Kafkatorio reads
|
||||
the server's log through Docker's syslog log driver. The example setup can run the Factorio
|
||||
server for you.
|
||||
* **A Docker host for Kafkatorio**, ideally the same one as the Factorio server. It needs about
|
||||
5 GB of memory while the existing map is sent (step 5), and much less after that. (The memory
|
||||
limits are the `-Xmx` options in `docker-compose.yml`.)
|
||||
* **A machine to build on**, with Docker, Git and a JDK (11 or newer). There are no published
|
||||
images, so you build them yourself. Gradle downloads everything else (including Node.js, for the
|
||||
mod).
|
||||
|
||||
* `./gradlew build` - build and test the project
|
||||
* `./gradlew runKafkatorio` - run locally (if Docker is running, a Kafka instance will start)
|
||||
* `./gradlew dockerComposeBuild` - build, test, and create Kafkatorio Docker images
|
||||
| Port | Service | Expose? |
|
||||
|--------------|---------------------------------------------|--------------------------------------------|
|
||||
| `12081` | the website (`web-map`) | yes, through a reverse proxy (step 7) |
|
||||
| `12035` | receives the Factorio server's log (syslog) | only to the Factorio server's Docker host |
|
||||
| `18090` | Kafka UI, for debugging | no - Kafka has no authentication |
|
||||
| `34197/udp` | the Factorio game (if the example runs it) | yes, for players |
|
||||
|
||||
### Server set up
|
||||
## 1. Build the Docker images and the mod
|
||||
|
||||
> See [./kafkatorio-platform/docker-compose.yml](kafkatorio-platform/docker-compose.yml) for a
|
||||
> complete example
|
||||
```shell
|
||||
git clone <this repository> kafkatorio
|
||||
cd kafkatorio
|
||||
|
||||
1. Copy the [kafkatorio-platform](./kafkatorio-platform) directory to your computer
|
||||
2. Rename
|
||||
[`example.secret.kafkatorio-config.yml`](./kafkatorio-platform/example.secret.kafkatorio-config.yml)
|
||||
to `.secret.kafkatorio-config.yml`
|
||||
./gradlew \
|
||||
:modules:infra-kafka-cluster:dockerComposeBuild \
|
||||
:modules:events-processors:dockerComposeBuild \
|
||||
:modules:events-server-syslog:dockerComposeBuild \
|
||||
:modules:events-server-web:dockerComposeBuild \
|
||||
:modules:web-map:dockerComposeBuild \
|
||||
:modules:events-mod:distZip
|
||||
```
|
||||
|
||||
In it, define both
|
||||
* a JWT secret password (a long alphanumeric string is best, you won't need to type this
|
||||
password manually)
|
||||
* a Factorio server, with a short-ID and a description.
|
||||
3. Rename [`example.env`](./kafkatorio-platform/example.env) to `.env`
|
||||
4. Create a JWT for your Factorio server, by going to https://jwt.io/
|
||||
This creates five images, tagged `localhost/dev.adamko.kafkatorio/<name>:<version>`
|
||||
(`kafka-kraft`, `events-processors`, `events-server-syslog`, `events-server-web`, `web-map`), and
|
||||
the mod, `modules/events-mod/build/distributions/kafkatorio-events_<version>.zip`. The version is
|
||||
the `version` in [`build.gradle.kts`](../build.gradle.kts).
|
||||
|
||||
* In 'PAYLOAD: DATA', set `sub` to your Factorio server's short ID.
|
||||
```json
|
||||
{
|
||||
"sub": "<your server short ID>",
|
||||
"iat": ...
|
||||
}
|
||||
```
|
||||
(`iat`, 'issued-at-time', should update automatically to be the current time.)
|
||||
* Replace `your-256-bit-secret` with your JWT secret password
|
||||
If you build on a different machine from the Docker host, copy the images over (no registry needed):
|
||||
|
||||
Copy the 'Encoded' token (it should start with `ey`).
|
||||
```shell
|
||||
docker save $(docker images --format '{{.Repository}}:{{.Tag}}' | grep dev.adamko.kafkatorio) \
|
||||
| gzip | ssh <docker host> 'gunzip | docker load'
|
||||
```
|
||||
|
||||
In the `.env` file, set `KAFKATORIO_TOKEN` to equal the token.
|
||||
5. In `kafkatorio-platform`, start the docker containers
|
||||
## 2. Configure Kafkatorio
|
||||
|
||||
```terminal
|
||||
docker-compose up -d; docker-compose logs -f
|
||||
Copy the [`kafkatorio-platform`](./kafkatorio-platform) directory to the Docker host. All
|
||||
Kafkatorio's data is kept in it. In that directory:
|
||||
|
||||
1. Copy [`example.env`](./kafkatorio-platform/example.env) to `.env`, and check
|
||||
`KAFKATORIO_VERSION` matches the images you built.
|
||||
|
||||
2. Copy [`example.secret.kafkatorio-config.yml`](./kafkatorio-platform/example.secret.kafkatorio-config.yml)
|
||||
to `.secret.kafkatorio-config.yml`, and set
|
||||
|
||||
* `jwt-secret`: a long random password, used to sign the Factorio server's token. You won't
|
||||
need to type it anywhere. For example, `openssl rand -hex 32`.
|
||||
* `kafkatorio-servers`: your Factorio server. The key is the server's *token name* (put in
|
||||
the token, below), and the value is its *ID*, which is used in the website's URLs
|
||||
(`/servers/<ID>/`). They can be the same, e.g. `my-server: "my-server"`.
|
||||
|
||||
3. Create a Kafka cluster ID, so Kafka's data survives image rebuilds:
|
||||
|
||||
```shell
|
||||
docker run --rm --entrypoint ./bin/kafka-storage.sh \
|
||||
localhost/dev.adamko.kafkatorio/kafka-kraft:<version> random-uuid > config/cluster_id
|
||||
```
|
||||
|
||||
### Unraid set up
|
||||
### Create a token for the Factorio server
|
||||
|
||||
> See [./kafkatorio-platform/unraid/docker-compose.yml](kafkatorio-platform/unraid/docker-compose.yml)
|
||||
The Factorio server's log is tagged with a token (a JWT), so Kafkatorio knows which server it's
|
||||
from, and ignores logs from anyone else. Create it with the `jwt-secret` and the server's token
|
||||
name (`sub`):
|
||||
|
||||
This runs the Kafkatorio services on the same Unraid server as the Factorio server. The Docker
|
||||
syslog log driver runs in the Docker daemon on the host, so the syslog port only needs to listen
|
||||
on `127.0.0.1`, and Kafka isn't exposed at all.
|
||||
```shell
|
||||
python3 -c 'import base64,hashlib,hmac,json,sys,time
|
||||
b=lambda d: base64.urlsafe_b64encode(d).rstrip(b"=").decode()
|
||||
j=lambda o: b(json.dumps(o,separators=(",",":")).encode())
|
||||
m=j({"alg":"HS256","typ":"JWT"})+"."+j({"sub":sys.argv[2],"iat":int(time.time())})
|
||||
print(m+"."+b(hmac.new(sys.argv[1].encode(),m.encode(),hashlib.sha256).digest()))' \
|
||||
'<jwt-secret>' '<token name>'
|
||||
```
|
||||
|
||||
(Or use [jwt.io](https://jwt.io/), with algorithm HS256, the payload
|
||||
`{"sub": "<token name>", "iat": <current unix time>}`, and the `jwt-secret` as the secret. But
|
||||
then you're typing the secret into a website.)
|
||||
|
||||
The token starts with `ey`. Put it in `.env` as `KAFKATORIO_TOKEN`.
|
||||
|
||||
## 3. Start Kafkatorio
|
||||
|
||||
```shell
|
||||
docker compose up -d
|
||||
docker compose logs -f
|
||||
```
|
||||
|
||||
The website is on `http://<docker host>:12081`. It's empty until the Factorio server sends
|
||||
something.
|
||||
|
||||
## 4. Connect the Factorio server
|
||||
|
||||
### Install the mod
|
||||
|
||||
Put `kafkatorio-events_<version>.zip` in the Factorio server's `mods` directory (for the example
|
||||
server, `factorio-server/mods/`), and restart it.
|
||||
|
||||
Players need the same mod, with the same version, to join, so give them the zip too (it goes in
|
||||
their Factorio `mods` directory). Don't let them download it from the mod portal: the portal's
|
||||
`kafkatorio-events` is the original project, for Factorio 1.1, and its version numbers overlap
|
||||
with this one.
|
||||
|
||||
### Forward the server's log to Kafkatorio
|
||||
|
||||
**If the example runs your Factorio server**, this is already set up. Start it with:
|
||||
|
||||
```shell
|
||||
docker compose --profile factorio up -d
|
||||
```
|
||||
|
||||
**If you already have a Factorio container**, add these options to it, then recreate it:
|
||||
|
||||
```shell
|
||||
--log-driver=syslog \
|
||||
--log-opt syslog-address=tcp://127.0.0.1:12035 \
|
||||
--log-opt syslog-format=rfc5424 \
|
||||
--log-opt mode=non-blocking \
|
||||
--log-opt tag=<KAFKATORIO_TOKEN>
|
||||
```
|
||||
|
||||
or in a Compose file:
|
||||
|
||||
```yaml
|
||||
logging:
|
||||
driver: syslog
|
||||
options:
|
||||
syslog-address: "tcp://127.0.0.1:12035"
|
||||
syslog-format: "rfc5424"
|
||||
mode: "non-blocking"
|
||||
tag: "<KAFKATORIO_TOKEN>"
|
||||
```
|
||||
|
||||
The log driver runs in the Docker daemon, not in the container, so `127.0.0.1` is the Docker
|
||||
host. If the Factorio server runs on another host, use this host's address, and in
|
||||
`docker-compose.yml` change `127.0.0.1:12035:9002` to `12035:9002` (and firewall it so only the
|
||||
Factorio host can connect).
|
||||
|
||||
Docker won't start a container if its syslog server can't be reached, so Kafkatorio must be
|
||||
running before the Factorio server starts. After a reboot, delay the Factorio server's start by
|
||||
about 30 seconds.
|
||||
|
||||
To check it's working, `docker compose logs -f events-server-syslog` while someone plays. If the
|
||||
token wasn't signed with the `jwt-secret`, you'll see `Could not decode JWT` there.
|
||||
|
||||
## 5. Send the existing map
|
||||
|
||||
New chunks, buildings and research are sent as they happen. To send what's already there, join the
|
||||
game and run, in the chat console:
|
||||
|
||||
```
|
||||
/kafkatorio prototypes
|
||||
/kafkatorio chunks 10
|
||||
```
|
||||
|
||||
* `prototypes` sends the tile and entity types. Run it first, and again after changing mods.
|
||||
* `chunks <radius>` sends the terrain, resources and buildings of every chunk within `<radius>`
|
||||
chunks of your position (on every surface). Each chunk is 32 tiles. Start small, and increase it
|
||||
until the whole map is on the website. Large radiuses (e.g. `chunks 30`) take minutes to process,
|
||||
and need the most memory.
|
||||
|
||||
Research is sent every minute. Production statistics are sent every minute, but only while the
|
||||
game is running: if the server pauses when nobody's online (`auto_pause`), there are no statistics
|
||||
then.
|
||||
|
||||
## 6. Icons and names
|
||||
|
||||
The building icons on the map, the production page and the research page need icons, building
|
||||
sizes, in-game names and the technology tree. They're exported from a Factorio client with the
|
||||
same mods as the server (they're not in this repository, since they belong to Wube and the mod
|
||||
authors). Without them, the map has no building icons, and those pages show internal names, or
|
||||
nothing.
|
||||
|
||||
On a computer with Factorio, with the game closed:
|
||||
|
||||
```shell
|
||||
factorio --dump-data
|
||||
factorio --dump-prototype-locale
|
||||
factorio --dump-icon-sprites
|
||||
python3 docs/kafkatorio-platform/building-icons/export_building_icons.py ~/.factorio/script-output ./icons
|
||||
scp -r ./icons <docker host>:<kafkatorio-platform directory>/
|
||||
```
|
||||
|
||||
* With Steam, run `SteamAppId=427520 factorio --dump-icon-sprites`, or Steam restarts the game
|
||||
normally instead.
|
||||
* `factorio` is the game's executable, e.g. `~/.steam/steam/steamapps/common/Factorio/bin/x64/factorio`.
|
||||
`~/.factorio/script-output` is the Linux location; on Windows it's `%APPDATA%\Factorio\script-output`.
|
||||
* If [Pillow](https://pypi.org/project/pillow/) is installed, the (large) technology icons are
|
||||
shrunk.
|
||||
|
||||
The website uses them straight away, but browsers keep icons for up to a day, so reload with
|
||||
Ctrl+Shift+R. Export them again when the mods change.
|
||||
|
||||
## 7. Put it on the internet
|
||||
|
||||
Point a reverse proxy (Nginx Proxy Manager, Caddy, Traefik, ...) at port `12081`, and **enable
|
||||
WebSocket support** - otherwise the website loads, but players and other live updates don't move.
|
||||
The website, the API (`/kafkatorio/`) and the WebSocket (`/kafkatorio/ws`) are all on that one
|
||||
port.
|
||||
|
||||
Don't expose the other ports. Kafka and the Kafka UI have no authentication.
|
||||
|
||||
## Unraid
|
||||
|
||||
[`kafkatorio-platform/unraid`](./kafkatorio-platform/unraid) has a setup for running Kafkatorio
|
||||
on the same Unraid server as the Factorio server. It's the same as the example above, but keeps
|
||||
the data in `appdata`, and doesn't run a Factorio server.
|
||||
|
||||
1. Install the *Compose Manager* plugin (or *Compose Manager Plus*) from the Apps tab.
|
||||
2. Build the images locally, and copy them to Unraid (no registry needed):
|
||||
|
||||
```terminal
|
||||
./gradlew dockerComposeBuild
|
||||
docker save $(docker images --format '{{.Repository}}:{{.Tag}}' | grep dev.adamko.kafkatorio) \
|
||||
| gzip | ssh root@<unraid> 'gunzip | docker load'
|
||||
```
|
||||
3. On Unraid, create `/mnt/user/appdata/kafkatorio-platform/` containing
|
||||
* `.secret.kafkatorio-config.yml` (see above)
|
||||
* `config/kafka-server.properties` (copy from [./kafkatorio-platform/config](kafkatorio-platform/config))
|
||||
* `config/cluster_id` - a fixed Kafka cluster ID, so the Kafka data survives image rebuilds.
|
||||
Generate one with `docker run --rm --entrypoint ./bin/kafka-storage.sh <kafka-kraft image> random-uuid`.
|
||||
4. Create a Compose Manager stack, with the Unraid `docker-compose.yml`, and
|
||||
[`example.env`](kafkatorio-platform/unraid/example.env) as its `.env`. Start the stack.
|
||||
5. Edit the Factorio container, enable 'Advanced View', and set 'Extra Parameters' to
|
||||
2. Build the images, and copy them to Unraid ([step 1](#1-build-the-docker-images-and-the-mod)).
|
||||
3. Create `/mnt/user/appdata/kafkatorio-platform/` containing
|
||||
* `.secret.kafkatorio-config.yml` ([step 2](#2-configure-kafkatorio))
|
||||
* `config/kafka-server.properties` (copy from [`kafkatorio-platform/config`](./kafkatorio-platform/config))
|
||||
* `config/cluster_id` ([step 2](#2-configure-kafkatorio))
|
||||
4. Create a Compose Manager stack, with the Unraid
|
||||
[`docker-compose.yml`](./kafkatorio-platform/unraid/docker-compose.yml), and
|
||||
[`example.env`](./kafkatorio-platform/unraid/example.env) as its `.env`.
|
||||
Set `KAFKA_DATA_DIR` to a direct disk or pool path (e.g. `/mnt/cache/...` or `/mnt/disk1/...`),
|
||||
not `/mnt/user/...`: Unraid's FUSE layer is slow for Kafka. Start the stack.
|
||||
5. Edit the Factorio container, enable *Advanced View*, and set *Extra Parameters* to
|
||||
|
||||
```
|
||||
--log-driver=syslog --log-opt syslog-address=tcp://127.0.0.1:12035 --log-opt syslog-format=rfc5424 --log-opt mode=non-blocking --log-opt tag=<KAFKATORIO_TOKEN>
|
||||
```
|
||||
|
||||
Docker won't start a container if its TCP syslog server isn't reachable, so on the Docker tab
|
||||
give the Factorio container an autostart 'wait' of ~30 seconds, so the Kafkatorio services start
|
||||
first.
|
||||
6. The web map is on port `12081`. It also proxies `/kafkatorio/` to the events server, so a
|
||||
reverse proxy (e.g. Nginx Proxy Manager) only needs to forward to that one port - make sure
|
||||
WebSocket support is enabled.
|
||||
7. In Factorio, run `/kafkatorio prototypes` and then `/kafkatorio chunks 10` to send the existing
|
||||
map.
|
||||
8. Optional: icons and names. The web map's 'Building icons' layer, the research page and the
|
||||
production page use icons, building sizes, the technology tree and in-game names, exported from
|
||||
a Factorio client that has the same mods as the server (they're not in this repo, since they
|
||||
belong to Wube and the mod authors). Without them, those pages show less, or nothing. With the
|
||||
game closed, run
|
||||
On the Docker tab, give the Factorio container an autostart *wait* of about 30 seconds, so
|
||||
Kafkatorio starts first.
|
||||
6. Continue from [step 4](#4-connect-the-factorio-server) (installing the mod). Put the icons in
|
||||
`/mnt/user/appdata/kafkatorio-platform/icons`.
|
||||
|
||||
```terminal
|
||||
factorio --dump-data
|
||||
factorio --dump-prototype-locale
|
||||
SteamAppId=427520 factorio --dump-icon-sprites # SteamAppId stops Steam relaunching the game
|
||||
python3 docs/kafkatorio-platform/building-icons/export_building_icons.py ~/.factorio/script-output ./icons
|
||||
scp -r ./icons root@<unraid>:/mnt/user/appdata/kafkatorio-platform/
|
||||
```
|
||||
## Updating
|
||||
|
||||
Re-export them when the mods change. If [Pillow](https://pypi.org/project/pillow/) is installed,
|
||||
the (large) technology icons are shrunk.
|
||||
1. Pull the changes, and rebuild the images and the mod ([step 1](#1-build-the-docker-images-and-the-mod)).
|
||||
If the version in `build.gradle.kts` changed, update `KAFKATORIO_VERSION` in `.env`.
|
||||
2. `docker compose up -d` recreates the containers with the new images. The map and other data are
|
||||
kept.
|
||||
3. If the mod changed, replace it on the server and give players the new zip. Factorio clients and
|
||||
the server must have exactly the same mod. Close the game before replacing the zip in your own
|
||||
`mods` directory.
|
||||
|
||||
Restart all the Kafkatorio services together when updating, not only some of them:
|
||||
`events-server-syslog` reads and re-writes every packet, so it drops anything it doesn't know
|
||||
about yet.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**The website shows "Loading servers…" forever, or a new page shows the landing page.** Reload
|
||||
without the cache (Ctrl+Shift+R). Older versions of the website could be cached by browsers after
|
||||
an update.
|
||||
|
||||
**The website loads, but nothing moves.** Check WebSocket support is enabled in the reverse proxy
|
||||
([step 7](#7-put-it-on-the-internet)).
|
||||
|
||||
**Nothing appears on the map.**
|
||||
* Check the server's log reaches Kafkatorio: `docker compose logs events-server-syslog`.
|
||||
`Could not decode JWT` means the token wasn't signed with the `jwt-secret`.
|
||||
* Log lines that Kafkatorio rejects go to the `kafkatorio.src.server-log.dlq` topic, with the
|
||||
reason (look at it in the Kafka UI). `no server ID for token` means the token name (`sub`) isn't
|
||||
in `kafkatorio-servers`.
|
||||
* Run `/kafkatorio prototypes` and `/kafkatorio chunks 5` in game ([step 5](#5-send-the-existing-map)).
|
||||
* After a large `chunks` command, processing takes minutes. The Kafka UI (`http://localhost:18090`
|
||||
on the Docker host) shows the topics, and how far behind the processors are.
|
||||
|
||||
**The Factorio container won't start: `failed to initialize logging driver`.** Kafkatorio's
|
||||
syslog server isn't running yet. Start Kafkatorio first, wait a few seconds, then start the
|
||||
Factorio container.
|
||||
|
||||
**Building icons, names, or the technology tree are missing.** Export the icons and names
|
||||
([step 6](#6-icons-and-names)) with the same mods as the server.
|
||||
|
||||
## Developing
|
||||
|
||||
* `./gradlew build` builds and tests everything.
|
||||
* [`modules/modules.md`](../modules/modules.md) shows how the modules depend on each other.
|
||||
* The packets are defined in [`events-library`](../modules/events-library). Changing them
|
||||
regenerates the mod's TypeScript types
|
||||
(`modules/events-mod/src/main/typescript/generated/kafkatorio-schema.ts`).
|
||||
* For the website, `./gradlew :modules:web-map:browserDevelopmentRun --continuous` serves it on
|
||||
`http://localhost:3000`. It forwards API requests to `events-server-web` on `localhost:12080`, so
|
||||
add `ports: [ "12080:80" ]` to `events-server-web` in your local `docker-compose.yml`. (Icons
|
||||
aren't forwarded.)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue