Compare commits

..
45 Commits
Author SHA1 Message Date
Cooldude2606 bd41801c8d Prepare 2.1.1 2026-11-20 16:36:01 +00:00
Cooldude2606 34deacb881 Merge pull request #1230 from bbassie/feature/ctl-output-json
Add --output json to clusterioctl
2026-11-20 15:22:38 +00:00
Cooldude2606 464baabf96 Merge pull request #1220 from clusterio/player-sync-rejected-upload
Download at the next join when the controller did not store an upload
2026-11-19 20:54:28 +00:00
Cooldude2606andClaude Opus 5.5 dd20054151 Add requiredMods to plugin declarations (#1229)
* Add requiredMods to plugin declarations

Plugins can list the Factorio mods they need under requiredMods. When an
instance starts, the host warns in the instance log about each loaded
plugin whose required mods are missing or disabled in the mod pack.
Subspace Storage declares the subspace_storage mod.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Refuse to start without required mods and download them

requiredMods entries are now Factorio dependency strings, and a mod
listed twice gives a version range. The plugin loader validates them.

The host refuses to start an instance whose mod pack is missing a
required mod of a loaded plugin, has it disabled or has it outside the
version range.

On start the controller resolves the required mods of its plugins and
their dependencies for the default mod pack's Factorio version and
downloads the ones missing from the mod store. The resolver and portal
download move from the ControlConnection handlers into Controller
methods so both paths share them.

Subspace Storage requires subspace_storage >= 1.99.0, and the
integration tests build a stub of that mod for the test mod pack.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Add a Factorio 1.1 variant to the subspace_storage test stub

The 1.1 test jobs failed because the test mod pack asked for
subspace_storage 2.3.11, which the stub did not build. The real mod
publishes 2.3.11 for 1.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 20:07:40 +00:00
bbassieandClaude Opus 5.5 dbedc4bc08 Add a Factorio 1.1 variant to the subspace_storage test stub
The 1.1 test jobs failed because the test mod pack asked for
subspace_storage 2.3.11, which the stub did not build. The real mod
publishes 2.3.11 for 1.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 20:02:31 +00:00
bbassieandClaude Opus 5.5 3aefcb062f Unit test mod-pack show warnings and export assets
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 16:26:37 +00:00
bbassieandClaude Opus 5.5 c5ad1953ad Unit test mod show and user show output
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 16:20:21 +00:00
bbassieandClaude Opus 5.5 f52821d1cd Cover ctl output code that only ran in child processes
CI coverage does not measure the ctl processes the integration tests start, so cover the Ctl log and debug event handlers, start-all, stop-all and pickFields with unit tests, and run mod show, user list flags, parse-exchange-string and log follow in process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 16:14:58 +00:00
bbassieandClaude Opus 5.5 f49c633e77 Cover ctl output paths and stop deleting fields before printing
user show, mod list, mod search and mod-pack list deleted fields off the received objects to trim the table, which made their toJSON throw in wire mode. They now pick the fields for the human table only and print the whole objects in json and wire mode.

Add integration cases for config edit on the controller, a host and an instance, user show with instance stats, log query and host pending in human mode, mod-pack show with mods and settings, and wire output of the trimmed lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 16:05:08 +00:00
bbassieandClaude Opus 5.5 23187859d6 Address second review of ctl --output
CommandOutput.error takes a human reporter like result, so command errors log their message in human mode and print JSON in json and wire mode. table takes a map applied to each row in human mode, which note list, host plugin list, user list and save list now use. Rename streaming to streamingLog, and print startup errors with errorToJson directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:56:48 +00:00
bbassieandClaude Opus 5.5 bf33f4f52e Refuse to start without required mods and download them
requiredMods entries are now Factorio dependency strings, and a mod
listed twice gives a version range. The plugin loader validates them.

The host refuses to start an instance whose mod pack is missing a
required mod of a loaded plugin, has it disabled or has it outside the
version range.

On start the controller resolves the required mods of its plugins and
their dependencies for the default mod pack's Factorio version and
downloads the ones missing from the mod store. The resolver and portal
download move from the ControlConnection handlers into Controller
methods so both paths share them.

Subspace Storage requires subspace_storage >= 1.99.0, and the
integration tests build a stub of that mod for the test mod pack.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:19:05 +00:00
bbassieandClaude Opus 5.5 a845b7ac52 Add requiredMods to plugin declarations
Plugins can list the Factorio mods they need under requiredMods. When an
instance starts, the host warns in the instance log about each loaded
plugin whose required mods are missing or disabled in the mod pack.
Subspace Storage declares the subspace_storage mod.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:07:06 +00:00
bbassieandClaude Opus 5.5 8550df69eb Address review of ctl --output
Add a wire output format that prints the toJSON format sent over the link. Commands without a result print {"success": true}, or "Command ran successfully" in human mode. The missing URL check throws a StartupError, unexpected command errors are rethrown to the startup handler, and startup errors log as fatal. user list, note list and host plugin list print the objects they received instead of their table rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 16:48:40 +00:00
Bastiaan eefb4be44b Merge pull request #1231 from bbassie/fix/link-test-system-info
Answer system info requests in the link integration test
2026-11-18 14:29:13 +00:00
Cooldude2606 b8e96989af Merge pull request #1228 from bbassie/feature/arm-headless
Support the ARM64 Linux headless build of Factorio
2026-11-18 13:33:28 +00:00
Cooldude2606 e814309750 Merge pull request #1227 from bbassie/fix/machine-details-on-start
Collect system info on start and when a host connects
2026-11-18 13:22:55 +00:00
Cooldude2606 d42d9d3743 Merge pull request #1226 from bbassie/ci/changelog-check
Check the changelog section of pull request descriptions in CI
2026-11-18 13:12:39 +00:00
bbassieandClaude Opus 5.5 10beffe167 Add --output json to clusterioctl
Commands print their results through a CommandOutput on the ctl link.
In json mode lists print as arrays of objects, other results as an
object, commands without a result print {} and failures print
{ error: ClassName, message } with exit code 1. log follow and
debug dump-ws print JSON lines.

Human output now goes through the shared asTable printer in every
command, results that were logged to stderr (role create, save and mod
upload/download) print to stdout, and config edit failures are logged
instead of printed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 13:07:18 +00:00
bbassieandClaude Opus 5.5 c2e08540d0 Quote the expected text in changelog check messages
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 13:05:29 +00:00
bbassieandClaude Opus 5.5 62f17a20af Take platform and arch in the lib headless helpers
hasFactorioHeadless(version, platform, arch) replaces the host's
hasArm64Headless, and factorioHeadlessUrl takes the platform too and
throws when Factorio has no headless build for it, which for now is
every platform except Linux. headlessUrl is marked deprecated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 11:15:40 +00:00
bbassieandClaude Opus 5.5 8374e4e3cd Start the autosave and system metrics intervals together
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 11:15:18 +00:00
bbassieandClaude Opus 5.5 5aaea1f213 Support the ARM64 Linux headless build of Factorio
Factorio publishes a linux-arm64 headless build from 2.1.18. On ARM64
Linux the host now downloads that build, refuses versions older than
2.1.18 that are not already installed, and finds the binary in bin/arm64
or bin/x64, whichever is present.

The host builds the download URL from the version instead of using
headlessUrl from the controller, so the version list on the wire stays
the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 11:14:54 +00:00
Cooldude2606 35f9609865 Merge pull request #1225 from bbassie/chore/drop-maintenance-node
Test only the oldest and latest supported Node.js in CI
2026-11-18 11:11:16 +00:00
Cooldude2606andClaude Opus 5.5 710bf8b636 Only print test output for failing tests (#1224)
* Only print test output for failing tests

A spec reporter subclass buffers console output, including winston's
ConsoleTransport and the integration child process lines, and prints it
above the failure line when a test or hook fails. VERBOSE_TEST streams
everything as before, and CI sets it on debug re-runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Replace SILENT_TEST and VERBOSE_TEST with TEST_OUTPUT

The reporter now handles all three modes: verbose prints everything,
normal prints output from failing tests and hooks, silent prints only
the results. The integration spawn helper and setup_logging no longer
filter output themselves.

npm install output in plugin install now goes through the logger
instead of inherited stdio, so it follows the log level and is
captured in tests like the rest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 17:07:47 +00:00
bbassieandClaude Opus 5.5 211180a801 Type check Node.js code against ES2024
Node.js 22 supports all of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:13:55 +00:00
bbassieandClaude Opus 5.5 bb6ce06e5a Test only the oldest and latest supported Node.js in CI
The middle version adds two jobs without catching anything the top and
bottom of the range would miss. The host whitelist watcher now uses
Set.difference, which every supported Node.js has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:05:57 +00:00
bbassieandClaude Opus 5.5 6363f15cb9 Replace SILENT_TEST and VERBOSE_TEST with TEST_OUTPUT
The reporter now handles all three modes: verbose prints everything,
normal prints output from failing tests and hooks, silent prints only
the results. The integration spawn helper and setup_logging no longer
filter output themselves.

npm install output in plugin install now goes through the logger
instead of inherited stdio, so it follows the log level and is
captured in tests like the rest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:31:45 +00:00
bbassieandClaude Opus 5.5 838a43428b Only print test output for failing tests
A spec reporter subclass buffers console output, including winston's
ConsoleTransport and the integration child process lines, and prints it
above the failure line when a test or hook fails. VERBOSE_TEST streams
everything as before, and CI sets it on debug re-runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:27:29 +00:00
Daniel 5cf14eb15b Fix plugin list and header layout on mobile (#1223) 2026-11-17 12:14:36 +00:00
Bastiaan adc92fbdb0 Merge pull request #1222 from bbassie/chore/mocha-12
Bump mocha to 12
2026-11-17 12:14:08 +00:00
bbassieandClaude Opus 5.5 dbe56638f9 Collect system info on start and when a host connects
The controller polled system info only on the system metrics interval, so
restart required and machine details stayed empty for up to one interval
after the controller started or a host connected. Collect once right away
and then on every interval. Resumed sessions are skipped since the host
process kept running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:12:11 +00:00
bbassieandClaude Opus 5.5 cd7c850a16 Match the CI lint style in the changelog check
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:09:53 +00:00
bbassieandClaude Opus 5.5 3d1ca99027 Check the changelog section of pull request descriptions in CI
The check runs the same parser as scripts/pr_to_changelog.mts, so a
description that passes CI is one the release script accepts. Checks
that need the GitHub API (issue references existing, pointing at this
pull request) still only run at release time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:08:06 +00:00
89f966bb68 Keep quick bar slots whose record is not in the blueprint library yet (#1221)
* Keep quick bar slots whose record is not in the blueprint library yet

A save gets a player's blueprint library from the client some seconds
after the join. The data of a player new to the save is applied before
that, so quick bar slots pointing at library records were cleared, and
the next upload stored the quick bar without them.

The entries are now kept. They are set once the record shows up, and an
upload before that stores them again as they came.

Fixes #1219

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JyNnVzRW4jPWaoWTxCyhM7

* Leave missing quick bar records alone once the quick bar is not synced

An instance whose quick bar sync was turned off while a record entry was
pending still set the slot when the library arrived.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JyNnVzRW4jPWaoWTxCyhM7

---------

Co-authored-by: ILLISIS <jayson.harris01@gmail.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 12:06:03 +00:00
BastiaanandClaude Opus 5.5 42e2e0284b Document the real user import and export commands (#1217)
The managing a cluster guide listed whitelist-bulk-import,
admin-bulk-import and banned-bulk-import, which don't exist. Describe
user import, restore and export with the JSON formats they read and
write. Also reword the README known issue now that the installer no
longer offers server_select and discord_bridge.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 11:11:45 +00:00
BastiaanandClaude Opus 5.5 c2c7edf1b7 Default to the newest Base Game pack on new clusters (#1216)
A new cluster gets the default mod packs but controller.default_mod_pack_id
stays null, so the first Create save or Start fails with "Default mod pack
not set on controller". When seeding the packs, also point the default at
the newest Base Game pack, unless an admin already set one. Clusters that
already have mod packs are left alone. The config is written with the
mod packs on the next autosave or shutdown.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 11:03:22 +00:00
BastiaanandClaude Opus 5.5 09d79d775f Fix small web interface nits from the RC smoke test (#1215)
Fix the "Permamently" typo in the delete instance confirm.

The Search Mod Portal dialog now opens on the default mod pack's
Factorio version, or the newest known version when there is no default
pack. It used to always start on 2.0. A version the user picks still
sticks while the page is open.

The mod portal cache key used request.hide_deprecated directly, and the
web interface never sends it, so the controller logged "2.0-undefined".
A missing value now counts as false, the same default
fetchAllModsFromPortal already uses, so "2.1" and "2.1-false" share a
cache entry.

Closes #1209

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:57:32 +00:00
BastiaanandClaude Opus 5.5 c6bc81b0df Confirm controller and host restarts in the web UI (#1214)
Restart on the Controller and host pages sent the request on click. A
host restart stops every running instance on it, and they stay down
unless instance.auto_start is set, so one misclick takes servers
offline. Both now open a confirm dialog first, and the host one names
the running instances it will stop.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:51:34 +00:00
BastiaanandClaude Opus 5.5 8acb54985c Print Set values in ctl tables as lists (#1213)
user list, user show and role list passed UserDetails and Role objects
straight to as-table, whose default String() turns the roleIds,
instances and permissions Sets into "[object Set]". Move the as-table
setup into command_ops.ts with a cell printer that joins Sets with
", ", and use it for the user and role commands.

The integration tests for role list and user list now capture the
output and check it.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:44:39 +00:00
BastiaanandClaude Opus 5.5 ec2bc6504b Exit run scripts with the code that stopped the restart loop (#1212)
run-controller.sh and run-host.sh ran `exit $?` after the `[[ ]]` test,
so they exited with the test's status (0) even when the process exited
with 8 to ask not to be restarted. Save the status before testing it.

The .cmd scripts now pass 8 to `exit /b` explicitly instead of relying on
a bare `exit /b` to carry the errorlevel through.

Closes #1208.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:44:17 +00:00
Bastiaan 154ebe294a Merge pull request #1211 from bbassie/fix/pairing-restart-pending
Don't flag a restart when a new host pairs
2026-11-17 10:32:44 +00:00
ILLISISandClaude Fable 5.2 a0c42225a4 Leave missing quick bar records alone once the quick bar is not synced
An instance whose quick bar sync was turned off while a record entry was
pending still set the slot when the library arrived.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JyNnVzRW4jPWaoWTxCyhM7
2026-11-14 14:57:44 +11:00
ILLISISandClaude Fable 5.2 a959ff25a4 Reject an upload for a player who is back, and take an empty reply as stored
A player rejoining before the controller's reply was skipped by
reject_upload, and the join then kept the copy the controller refused.
The rejection now counts while the join still waits for its acquire
reply, which the controller sends after it.

A controller from before the upload response replies with nothing. That
failed validation on a newer host, so its uploads were never confirmed.
It is taken as stored, as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JyNnVzRW4jPWaoWTxCyhM7
2026-11-14 14:57:30 +11:00
ILLISISandClaude Fable 5.2 0b0e2fd377 Keep quick bar slots whose record is not in the blueprint library yet
A save gets a player's blueprint library from the client some seconds
after the join. The data of a player new to the save is applied before
that, so quick bar slots pointing at library records were cleared, and
the next upload stored the quick bar without them.

The entries are now kept. They are set once the record shows up, and an
upload before that stores them again as they came.

Fixes #1219

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JyNnVzRW4jPWaoWTxCyhM7
2026-11-14 13:24:10 +11:00
ILLISISandClaude Fable 5.2 b6bc118297 Download at the next join when the controller did not store an upload
The controller gave an upload it did not store the same empty reply as
one it stored, and the instance confirmed either to the save. An instance
restarting after a crash uploads the players it had online, and when
another instance had since uploaded the same generation the save took its
own copy for the stored one: the next join kept it, and the leave after
that stored it over the newer data.

The reply now says whether the upload was stored. One that was not marks
the save's copy outdated, so it is not uploaded again and the next join
downloads.

Fixes #1218

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JyNnVzRW4jPWaoWTxCyhM7
2026-11-14 13:20:31 +11:00
93 changed files with 2727 additions and 754 deletions

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
name: Changelog
on:
pull_request:
types: [opened, edited, synchronize, reopened]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
check:
# Bot pull requests do not fill in the template.
if: github.event.pull_request.user.type != 'Bot'
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v7
with:
sparse-checkout: scripts
- uses: actions/setup-node@v7
with:
node-version: 24.x
- name: Check changelog in pull request description
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: node scripts/check_pr_changelog.mts
+9 -9
View File
@@ -29,9 +29,9 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
# To stay within the concurrent job limit, every Factorio version is # To stay within the concurrent job limit, every Factorio version is
# tested on the latest Node.js, and every other supported Node.js # tested on the latest Node.js, and the oldest supported Node.js only
# version only with the latest Factorio. Coverage is performed on the # with the latest Factorio. Versions in between are not tested.
# oldest supported Node.js. # Coverage is performed on the oldest supported Node.js.
# Node.js release schedule: https://nodejs.org/en/about/releases/ # Node.js release schedule: https://nodejs.org/en/about/releases/
# Supported factorio versions are latest stable and one minor version before stable (even if from a previous major) # Supported factorio versions are latest stable and one minor version before stable (even if from a previous major)
node-version: [26.x] node-version: [26.x]
@@ -42,12 +42,6 @@ jobs:
tests: [ctl, other] tests: [ctl, other]
include: include:
- node-version: 24.x
factorio-version: 2.1.17
tests: ctl
- node-version: 24.x
factorio-version: 2.1.17
tests: other
- node-version: 22.x - node-version: 22.x
factorio-version: 2.1.17 factorio-version: 2.1.17
tests: ctl tests: ctl
@@ -67,9 +61,15 @@ jobs:
run: wget -q -O factorio.tar.gz https://www.factorio.com/get-download/${{ matrix.factorio-version }}/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz run: wget -q -O factorio.tar.gz https://www.factorio.com/get-download/${{ matrix.factorio-version }}/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz
- name: Run tests - name: Run tests
if: ${{ !matrix.coverage }} if: ${{ !matrix.coverage }}
env:
# Re-run with debug logging to see output from passing tests
TEST_OUTPUT: ${{ runner.debug == '1' && 'verbose' || 'normal' }}
run: pnpm test --grep "^(Integration of Clusterio (clusterioctl|plugin system)|Clusterio Instance) " ${{ matrix.tests == 'other' && '--invert' || '' }} run: pnpm test --grep "^(Integration of Clusterio (clusterioctl|plugin system)|Clusterio Instance) " ${{ matrix.tests == 'other' && '--invert' || '' }}
- name: Run coverage - name: Run coverage
if: ${{ matrix.coverage }} if: ${{ matrix.coverage }}
env:
# Re-run with debug logging to see output from passing tests
TEST_OUTPUT: ${{ runner.debug == '1' && 'verbose' || 'normal' }}
run: pnpm run ci-cover --grep "^(Integration of Clusterio (clusterioctl|plugin system)|Clusterio Instance) " ${{ matrix.tests == 'other' && '--invert' || '' }} run: pnpm run ci-cover --grep "^(Integration of Clusterio (clusterioctl|plugin system)|Clusterio Instance) " ${{ matrix.tests == 'other' && '--invert' || '' }}
- name: Upload coverage to Codecov - name: Upload coverage to Codecov
if: ${{ matrix.coverage }} if: ${{ matrix.coverage }}
+43
View File
@@ -31,6 +31,49 @@ Many thanks to the following for contributing to this release:
[@username](https://github.com/username) [@username](https://github.com/username)
--> -->
## Version 2.1.1
### Features
- Hosts on ARM64 Linux download and run the ARM64 headless build of Factorio, available from Factorio 2.1.18. [#1228](<https://github.com/clusterio/clusterio/pull/1228>)
- Added `requiredMods` to plugin declarations. Instances refuse to start when their mod pack lacks a mod a loaded plugin requires, and the controller downloads missing required mods and their dependencies from the mod portal on start. Subspace Storage requires the subspace_storage mod. [#535](<https://github.com/clusterio/clusterio/issues/535>)
- Added `--output json` and `--output wire` to clusterioctl for machine readable results and errors. [#1230](<https://github.com/clusterio/clusterio/pull/1230>)
### Fixes
- Fixed a newly paired host reporting that it needs a restart. [#1204](<https://github.com/clusterio/clusterio/issues/1204>)
- Fixed the installer's run-controller and run-host scripts exiting with 0 when the controller or host exited with 8 to stop the restart loop. [#1208](<https://github.com/clusterio/clusterio/issues/1208>)
- Fixed clusterioctl user list, user show and role list printing [object Set] for roles, instances and permissions. [#1205](<https://github.com/clusterio/clusterio/issues/1205>)
- Fixed typo in the delete instance confirmation. [#1209](<https://github.com/clusterio/clusterio/issues/1209>)
- The Search Mod Portal dialog now opens on the default mod pack's Factorio version instead of always 2.0. [#1209](<https://github.com/clusterio/clusterio/issues/1209>)
- Fixed the mod portal cache key logging "undefined" when hide_deprecated was not sent. [#1209](<https://github.com/clusterio/clusterio/issues/1209>)
- Fixed player_sync losing quick bar slots of blueprint library records when a player joins an instance for the first time. [#1219](<https://github.com/clusterio/clusterio/issues/1219>)
- Fixed cramped plugin rows and wrapping header branding on mobile screens. [#1223](<https://github.com/clusterio/clusterio/pull/1223>)
- Fixed player_sync keeping an instance's old copy of a player after the controller refused its upload, which let an instance restarted after a crash overwrite newer player data. [#1218](<https://github.com/clusterio/clusterio/issues/1218>)
### Changes
- Restarting the controller or a host from the web interface now asks for confirmation, and for hosts lists the running instances that will be stopped. [#1207](<https://github.com/clusterio/clusterio/issues/1207>)
- New clusters default controller.default_mod_pack_id to the newest Base Game mod pack, so instances can create saves and start without setting it first. [#1203](<https://github.com/clusterio/clusterio/issues/1203>)
- Output from npm during `plugin install` now goes through the logger. [#1224](<https://github.com/clusterio/clusterio/pull/1224>)
- The controller collects system info, including restart required, right after it starts and when a host connects instead of waiting for the first system metrics interval. [#1227](<https://github.com/clusterio/clusterio/pull/1227>)
- clusterioctl prints all tables with the same formatter and prints command results to stdout instead of the log. [#1230](<https://github.com/clusterio/clusterio/pull/1230>)
- clusterioctl prints "Command ran successfully" for commands that have no other output. [#1230](<https://github.com/clusterio/clusterio/pull/1230>)
### Meta
- Added a CI check that pull request descriptions have a valid changelog section. [#1226](<https://github.com/clusterio/clusterio/pull/1226>)
### Contributors
Many thanks to the following for contributing to this release:
[@bbassie](<https://github.com/bbassie>), [@ILLISIS](<https://github.com/ILLISIS>), [@Danielv123](<https://github.com/Danielv123>)
### Statistics
Commits: 44 Pull Requests: 19 Files Changed: 81 Additions: +2,676 Deletions: -739
## Version 2.1.0 ## Version 2.1.0
### Features ### Features
+3 -1
View File
@@ -11,7 +11,9 @@ RUN apt update \
&& apt install -y wget \ && apt install -y wget \
&& mkdir /clusterio && mkdir /clusterio
WORKDIR /clusterio WORKDIR /clusterio
RUN wget -q -O factorio.tar.gz https://www.factorio.com/get-download/latest/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz ARG TARGETARCH
RUN if [ "$TARGETARCH" = "arm64" ]; then build=linux-arm64; else build=linux64; fi \
&& wget -q -O factorio.tar.gz https://www.factorio.com/get-download/latest/headless/$build && tar -xf factorio.tar.gz && rm factorio.tar.gz
# Copy project files into the container # Copy project files into the container
COPY . . COPY . .
+5 -1
View File
@@ -129,6 +129,10 @@ Check out all our of decisions for [node, factorio, and operation support](/docs
3. Optionally copy the generated systemd service files in `systemd` folder to `/etc/systemd/system/`. 3. Optionally copy the generated systemd service files in `systemd` folder to `/etc/systemd/system/`.
Hosts on Linux download the headless build of Factorio as needed.
On ARM64 (aarch64) Linux this only works for Factorio 2.1.18 and later, which is the first version Factorio published an ARM64 headless build for.
Older versions have to be installed manually, for example the x64 build run through an emulator.
**Ubuntu with Docker** **Ubuntu with Docker**
@@ -306,7 +310,7 @@ If you want to use a different user for the command line interface, you can gene
- `@hornwitser/server_select@2.0.1` fails to load with Clusterio 2.0 because its entrypoints omit the required `.js` extension. - `@hornwitser/server_select@2.0.1` fails to load with Clusterio 2.0 because its entrypoints omit the required `.js` extension.
`@hornwitser/discord_bridge@0.2.5` is not automatically discovered because it lacks the `clusterio-plugin` keyword. `@hornwitser/discord_bridge@0.2.5` is not automatically discovered because it lacks the `clusterio-plugin` keyword.
The Clusterio 2.0.2 installer still offers these versions; leave them unchecked until compatible plugin releases are available. The installer no longer offers them, but installs made with the 2.0.2 installer or earlier may still have them.
If already installed, stop the controller and host, run `npm uninstall @hornwitser/server_select @hornwitser/discord_bridge` in each affected installation directory, and restart them to continue with the core plugins. If already installed, stop the controller and host, run `npm uninstall @hornwitser/server_select @hornwitser/discord_bridge` in each affected installation directory, and restart them to continue with the core plugins.
See [#1102](https://github.com/clusterio/clusterio/issues/1102). See [#1102](https://github.com/clusterio/clusterio/issues/1102).
- After updating Factorio you may get the error `Error: Expected empty response but got "Cannot execute command. Error: [string "clusterio_private.update_instance(..."]:1: attempt to index global 'clusterio_private' (a nil value)?` on instance start. - After updating Factorio you may get the error `Error: Expected empty response but got "Cannot execute command. Error: [string "clusterio_private.update_instance(..."]:1: attempt to index global 'clusterio_private' (a nil value)?` on instance start.
+3 -1
View File
@@ -284,6 +284,7 @@ Defaults to true.
ID of the mod pack to use for instances that don't have factorio.mod_pack_id set. ID of the mod pack to use for instances that don't have factorio.mod_pack_id set.
Instances without a mod pack of their own fail to start if this is null. Instances without a mod pack of their own fail to start if this is null.
When the controller adds the default mod packs to a new cluster it sets this to the newest Base Game pack if it is null.
Defaults to null. Defaults to null.
@@ -591,7 +592,8 @@ Defaults to "latest".
### factorio.executable_path ### factorio.executable_path
Path relative to the Factorio installation directory of the executable to run. Path relative to the Factorio installation directory of the executable to run.
If null the path is auto detected, which is `bin/x64/factorio` on Linux and Windows and `MacOS/factorio` on macOS. If null the path is auto detected, which is `MacOS/factorio` on macOS and otherwise `bin/x64/factorio` or `bin/arm64/factorio`, whichever exists.
When both exist the one matching the CPU is used.
Only needed in special setups. Only needed in special setups.
Defaults to null. Defaults to null.
+3
View File
@@ -197,6 +197,9 @@ Use `pnpm run fast-test` to skip tests marked as slow, or `pnpm run no-external-
These correspond to setting the `FAST_TEST` and `NO_EXTERNAL_TEST` environment variables and can be combined. These correspond to setting the `FAST_TEST` and `NO_EXTERNAL_TEST` environment variables and can be combined.
For example, `FAST_TEST=y NO_EXTERNAL_TEST=y pnpm test` combines both exclusions. For example, `FAST_TEST=y NO_EXTERNAL_TEST=y pnpm test` combines both exclusions.
These flags still run the remaining integration setup and tests; neither produces an isolated unit-test run. These flags still run the remaining integration setup and tests; neither produces an isolated unit-test run.
Console and log output is only printed for tests and hooks that fail, just above the failure.
Set `TEST_OUTPUT=verbose` to print all of it as it happens, or `TEST_OUTPUT=silent` (`pnpm run silent-test`) to print only the results.
In CI, re-running a job with debug logging enabled gives verbose output.
When writing slow tests call `slowTest(this)`, and for external API tests call `externalTest(this)`; both are exported from `test/common.js`. When writing slow tests call `slowTest(this)`, and for external API tests call `externalTest(this)`; both are exported from `test/common.js`.
For tests that need a Factorio installation, call `requiresFactorio(this)` in the test or pass the suite to `requiresFactorio`, imported from `test/integration/index.js`. For tests that need a Factorio installation, call `requiresFactorio(this)` in the test or pass the suite to `requiresFactorio`, imported from `test/integration/index.js`.
`test/common.js` also provides JSON round-trip/schema helpers, and `test/mock.js` provides shared mocks. `test/common.js` also provides JSON round-trip/schema helpers, and `test/mock.js` provides shared mocks.
+42 -16
View File
@@ -8,6 +8,24 @@ Before `clusterioctl` can be used it needs to be configured for the cluster it w
The easiest way to do this is to run `npx clusteriocontroller bootstrap create-ctl-config <username>` on the controller, which creates the necessary `config-ctl.json` for managing the cluster as the given user. The easiest way to do this is to run `npx clusteriocontroller bootstrap create-ctl-config <username>` on the controller, which creates the necessary `config-ctl.json` for managing the cluster as the given user.
## Output Format
ctl> --output json <command>
Print the result of the command as JSON on stdout instead of text meant for reading.
Lists are printed as an array of objects, other results as an object, and commands that have no result print `{"success": true}`.
If the command fails it prints `{"error": "<ClassName>", "message": "<message>"}` and exits with a non-zero exit code.
Log messages are written to stderr in all formats.
`--output wire` prints the same results in the format they are sent between the controller, hosts and clusterioctl, which uses snake_case names like `is_admin`.
The `json` format uses the property names shown in the tables of the human format.
`log follow` and `debug dump-ws` keep running and print one JSON object per line as entries arrive.
`instance start-all` and `instance stop-all` print an array with an entry per instance, which has an `error` property if that instance failed to start or stop.
The default is `--output human`, which prints `Command ran successfully` for commands that have no result.
## Hosts ## Hosts
### List hosts ### List hosts
@@ -209,31 +227,39 @@ Note: This bans the user from logging in to Factorio servers in the cluster, it
### Bulk importing admin, whitelist and ban lists ### Bulk importing admin, whitelist and ban lists
Whitelists and admin lists can be bulk imported with the following commands: Admin lists, whitelists and banlists can be bulk imported with the following command:
ctl> user whitelist-bulk-import <file> [--create] ctl> user import <file> [--admins|--whitelist|--bans|--users]
ctl> user admin-bulk-import <file> [--create]
The file should simply be newline seperated names: The admin list and whitelist files should be a JSON array of names, the same format as Factorio's server-adminlist.json and server-whitelist.json:
joe ["joe", "bob", "allan"]
bob
allan
The `--create` option will create the user if they do not exist, and then set the relevent value. The banlist file uses the format of Factorio's server-banlist.json, where each entry is either a name or an object with a username and reason:
Banlists can be bulk imported with the following command: [
"joe",
{"username": "bob", "reason": "stole materials"},
{"username": "allan", "reason": "rude language"}
]
ctl> user banned-bulk-import <file> [--create] The `--users` option imports a file written by `user export`, which holds the admin, whitelist and ban status of every user.
If no option is given the type is guessed from the file, files with an `export_version` field are treated as user exports, otherwise the file name must contain ban, admin or whitelist.
Users that do not exist are created.
Importing only adds to the existing lists, it never removes anyone from them.
The file should be names, followed by a ban reason: To replace a list instead, use restore:
joe greifed copper mines ctl> user restore <file> [--admins|--whitelist|--bans|--users] [--no-backup]
bob stole materials
allan rude language
The first string on the line is used as the username, the others are used as the ban reason. This takes the same files as import, but removes everyone not in the file from the list.
The `--create` option will create the user if they do not exist, then ban them. The previous list is written to `<type>-backup.json` in the current directory unless `--no-backup` is passed.
The current lists can be exported with the following command:
ctl> user export <file> [--admins|--whitelist|--bans|--users]
Defaults to exporting users.
### Set Cluster Roles ### Set Cluster Roles
+37
View File
@@ -151,6 +151,10 @@ The following properties are recognized:
Array of features the plugin needs from the instances it is loaded on, any of `"SavePatching"`, `"ScriptCommands"` and `"LuaUdp"`. Array of features the plugin needs from the instances it is loaded on, any of `"SavePatching"`, `"ScriptCommands"` and `"LuaUdp"`.
See [Handling Invalid Configuration](#handling-invalid-configuration) See [Handling Invalid Configuration](#handling-invalid-configuration)
**requiredMods**:
Array of Factorio mods the plugin needs in the mod pack of the instances it is loaded on, written as Factorio dependency strings.
See [Handling Invalid Configuration](#handling-invalid-configuration)
**messages**: **messages**:
Array of request and event classes for this plugin. Array of request and event classes for this plugin.
See guide for [defining link messages](#defining-link-messages) below. See guide for [defining link messages](#defining-link-messages) below.
@@ -321,6 +325,25 @@ export const plugin = {
Enabling the plugin on an instance that has the feature turned off then fails config validation, so the mistake is caught when the config is edited instead of when the instance starts. Enabling the plugin on an instance that has the feature turned off then fails config validation, so the mistake is caught when the config is edited instead of when the instance starts.
Instances that have the feature turned off get the plugin disabled by default when it is added. Instances that have the feature turned off get the plugin disabled by default when it is added.
If the plugin talks to a Factorio mod, list the mod under `requiredMods` using the same format as `dependencies` in a mod's info.json, without a prefix.
List a mod twice to give a version range:
```js
export const plugin = {
name: "foo_frobber",
// ...
requiredMods: ["frobber >= 1.2.0", "frobber < 2.0.0"],
};
```
An instance with the plugin loaded refuses to start if its mod pack is missing one of the mods, has it disabled, or has a version outside the range.
Leave out the dependencies of the required mods. Factorio checks those itself.
When the controller starts it downloads required mods it does not have, and their dependencies, from the mod portal for the Factorio version of the default mod pack.
This needs `controller.factorio_username` and `controller.factorio_token` to be set.
The mods still have to be added to the mod packs by hand.
Forks of a required mod are not supported.
Other constraints on config fields are expressed with a `validator` on the field definition, which throws when the value is not acceptable. Other constraints on config fields are expressed with a `validator` on the field definition, which throws when the value is not acceptable.
Use `dependsOn` when the check involves other fields so that the validator runs when those change too. Use `dependsOn` when the check involves other fields so that the validator runs when those change too.
See [Configuration System](config-system.md) for details. See [Configuration System](config-system.md) for details.
@@ -972,6 +995,20 @@ For a command the `definition` is the arguments to pass to [yargs.command](http:
The `handler` is an async function that's invoked when the command is executed and it's passed the parsed command line arguments and a reference to the `Ctl` class of clusterioctl. The `handler` is an async function that's invoked when the command is executed and it's passed the parsed command line arguments and a reference to the `Ctl` class of clusterioctl.
Note that messages sent from clusterioctl needs to have `"control"` as a part of `src` for it to be accepted by the controller, see [Defining Link Messages](#defining-link-messages) for how to define the messages that can be sent to the controller. Note that messages sent from clusterioctl needs to have `"control"` as a part of `src` for it to be accepted by the controller, see [Defining Link Messages](#defining-link-messages) for how to define the messages that can be sent to the controller.
Commands print their results through `ctl.output` so that `--output json` works for them:
```js
handler: async function(args, ctl) {
const result = await ctl.sendTo("controller", new messages.FrobnicateRequest(args.type));
ctl.output.result(result, () => console.log(`Frobnicated ${result.count} foos`));
},
```
`ctl.output.result(data, human)` prints `data` as JSON in json and wire mode and calls `human` otherwise.
`ctl.output.table(rows, map)` prints an array of objects as a JSON array, or as a table in human mode with `map` applied to each row first.
`ctl.output.human` is true when the output is meant for reading.
Commands that print nothing get `Command ran successfully` or `{"success": true}` printed for them, and errors thrown from the handler are printed as `{"error": "<ClassName>", "message": "<message>"}` in json and wire mode.
To have the command tree become part of clusterioctl it needs to be added to the rootCommand tree in the `addCommands` hook of the ctl entrypoint: To have the command tree become part of clusterioctl it needs to be added to the rootCommand tree in the `addCommands` hook of the ctl entrypoint:
```js ```js
+3 -3
View File
@@ -8,8 +8,8 @@
"build": "tsc --build", "build": "tsc --build",
"watch": "tsc --build --watch", "watch": "tsc --build --watch",
"build-mod": "pnpm run -r build-mod --output-dir ../../dist", "build-mod": "pnpm run -r build-mod --output-dir ../../dist",
"test": "pnpm run build && NODE_EXTRA_CA_CERTS=$(pwd)/test/file/tls/cert.pem mocha --enable-source-maps --check-leaks -R spec --recursive test \"plugins/*/test/**\" --exclude \"test/manual/**\"", "test": "pnpm run build && NODE_EXTRA_CA_CERTS=$(pwd)/test/file/tls/cert.pem mocha --enable-source-maps --check-leaks -R ./test/reporter.cjs --recursive test \"plugins/*/test/**\" --exclude \"test/manual/**\"",
"silent-test": "SILENT_TEST=y pnpm test", "silent-test": "TEST_OUTPUT=silent pnpm test",
"fast-test": "FAST_TEST=y pnpm test", "fast-test": "FAST_TEST=y pnpm test",
"no-external-test": "NO_EXTERNAL_TEST=y pnpm test", "no-external-test": "NO_EXTERNAL_TEST=y pnpm test",
"cover": "c8 pnpm test", "cover": "c8 pnpm test",
@@ -47,7 +47,7 @@
"c8": "^12.0.0", "c8": "^12.0.0",
"express": "catalog:", "express": "catalog:",
"jsonwebtoken": "catalog:", "jsonwebtoken": "catalog:",
"mocha": "^11.8.0", "mocha": "^12.0.3",
"oxlint": "^1.88.0", "oxlint": "^1.88.0",
"typescript": "catalog:", "typescript": "catalog:",
"yargs": "catalog:", "yargs": "catalog:",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/controller", "name": "@clusterio/controller",
"version": "2.1.0", "version": "2.1.1",
"description": "Implementation of Clusterio controller", "description": "Implementation of Clusterio controller",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+6 -208
View File
@@ -631,7 +631,8 @@ export default class ControlConnection extends BaseConnection {
* @param request - The request object containing factorioVersion. * @param request - The request object containing factorioVersion.
*/ */
async handleModPortalGetAllRequest(request: lib.ModPortalGetAllRequest) { async handleModPortalGetAllRequest(request: lib.ModPortalGetAllRequest) {
const cacheKey = `${request.factorioVersion}-${request.hide_deprecated}`; const hideDeprecated = request.hide_deprecated ?? false;
const cacheKey = `${request.factorioVersion}-${hideDeprecated}`;
const cachedData = this._controller.modPortalCache.get(cacheKey); const cachedData = this._controller.modPortalCache.get(cacheKey);
const cacheDuration = this._controller.config.get("controller.mod_portal_cache_duration_minutes") * 60 * 1000; const cacheDuration = this._controller.config.get("controller.mod_portal_cache_duration_minutes") * 60 * 1000;
@@ -644,7 +645,7 @@ export default class ControlConnection extends BaseConnection {
const mods = await lib.ModStore.fetchAllModsFromPortal( const mods = await lib.ModStore.fetchAllModsFromPortal(
request.factorioVersion, request.factorioVersion,
this._controller.config.get("controller.mod_portal_page_size"), this._controller.config.get("controller.mod_portal_page_size"),
request.hide_deprecated hideDeprecated
); );
this._controller.modPortalCache.set(cacheKey, { timestamp: Date.now(), data: mods }); this._controller.modPortalCache.set(cacheKey, { timestamp: Date.now(), data: mods });
// The Response class is defined inline within the Request class // The Response class is defined inline within the Request class
@@ -661,36 +662,7 @@ export default class ControlConnection extends BaseConnection {
* @param request - Request object with mod name, version, and factorioVersion. * @param request - Request object with mod name, version, and factorioVersion.
*/ */
async handleModPortalDownloadRequest(request: lib.ModPortalDownloadRequest) { async handleModPortalDownloadRequest(request: lib.ModPortalDownloadRequest) {
const factorioVersion = request.factorioVersion; await this._controller.downloadModsFromPortal(request.mods, request.factorioVersion);
const toDownload = this._controller.modStore.filterInstalled(request.mods, false);
if (toDownload.length > 0) {
// Get Factorio credentials from config
const username = this._controller.config.get("controller.factorio_username");
const token = this._controller.config.get("controller.factorio_token");
if (!username || username === "" || !token || token === "") {
throw new lib.ConflictError("Factorio credentials (username, token) not configured on the controller.");
}
try {
logger.info(`Downloading ${toDownload.length} mods for Factorio ${factorioVersion} from portal.`);
await this._controller.modStore.downloadMods(toDownload, username, token, factorioVersion);
} catch (error: any) {
logger.error(`Error downloading mods from portal: ${error.message}`);
const errorMessage = "Mod portal download failed";
if (error.message.includes("401") || error.message.includes("Unauthorized")) {
throw new lib.ConflictError(
`${errorMessage}: Authentication failed. Check Factorio username/token in controller config.`
);
}
if (error.message.includes("404") || error.message.includes("Not Found")) {
throw new lib.NotFoundError(`${errorMessage}: Mod version not found on the portal.`);
}
throw new lib.InternalServerError(`${errorMessage}: ${error.message}`);
}
}
// Verify all mods are present // Verify all mods are present
const modInfos = []; const modInfos = [];
@@ -706,183 +678,9 @@ export default class ControlConnection extends BaseConnection {
return modInfos; return modInfos;
} }
// eslint-disable-next-line complexity
async handleModDependencyResolveRequest(request: lib.ModDependencyResolveRequest) { async handleModDependencyResolveRequest(request: lib.ModDependencyResolveRequest) {
const {factorioVersion, checkForUpdates} = request; return await this._controller.resolveModDependencies(
const searchQueue = [...request.mods]; request.mods, request.factorioVersion, request.checkForUpdates,
const localInfos = [...this._controller.modStore.mods()];
const builtInModNames = lib.ModPack.getBuiltinModNames(factorioVersion);
const skipSearch = new Set(builtInModNames);
type ModPortalModType = InstanceType<typeof lib.ModPortalGetAllRequest.Response>["mods"][number];
type ModPortalReleaseType = NonNullable<ModPortalModType["releases"]>[number];
const dependencyRequirements = new Map<string, lib.ModVersionRange>();
const recommendedRequirements = new Map<string, lib.ModVersionRange>();
const optionalRequirements = new Map<string, lib.ModVersionRange>();
const candidateReleases = new Map<string, lib.ModInfo>();
const errors = new Map<string, lib.ModDependencyResolveErrors>();
const _modPortalReleases = new Map<string, ModPortalModType | null>();
async function fetchModReleases(modName: string) {
let releases = _modPortalReleases.get(modName);
if (releases === undefined) {
try {
releases = await lib.ModStore.fetchModReleases(modName);
} catch (error: any) {
if (!error.message.includes("404") && !error.message.includes("Not Found")) {
logger.error(`Error fetching mod from portal: ${error.message}`);
throw new lib.InternalServerError(`Dependency resolution failed: ${error.message}`);
} else {
releases = null;
}
}
}
_modPortalReleases.set(modName, releases);
return releases;
}
function selectModRelease(releases: ModPortalModType | null, versionRange: lib.ModVersionRange) {
if (!releases || !releases.releases) {
return null;
}
// Select the latest matching version for the mod pack's Factorio version
const release = releases.releases
.filter(info => (
versionRange.testVersion(info.version)
&& lib.normaliseMajorMinorVersion(
lib.normaliseSourceVersion(info.info_json.factorio_version)
) === factorioVersion
))
.reduce<ModPortalReleaseType | undefined>((max, cur) => (
max && lib.integerSourceVersion(max.version) > lib.integerSourceVersion(cur.version) ? max : cur
), undefined);
if (!release) {
return null;
}
// Create the mod info class from the selected
try {
return lib.ModInfo.fromJSON({
...release.info_json, // includes dependencies
sha1: release.sha1,
version: release.version,
name: releases.name,
author: releases.owner,
title: releases.title,
});
} catch {
// Can error if there are invalid dependencies
errors.set(releases.name, "invalidDependency");
return null;
}
}
while (searchQueue.length > 0) {
// Dequeue the next mod, skip if incompatible
const mod = searchQueue.shift()!;
if (mod.incompatible) {
errors.set(mod.name, "incompatible");
continue;
}
// Get the current version range (dependencies can be promoted to a stricter requirement)
const versionRange =
dependencyRequirements.get(mod.name)
?? recommendedRequirements.get(mod.name)
?? optionalRequirements.get(mod.name)
?? new lib.ModVersionRange();
if (mod.required) {
// Add to required, remove from recommended and optional
dependencyRequirements.set(mod.name, versionRange);
recommendedRequirements.delete(mod.name);
optionalRequirements.delete(mod.name);
} else if (mod.recommended) {
// If not required, add to recommended and remove from optional
if (!dependencyRequirements.has(mod.name)) {
recommendedRequirements.set(mod.name, versionRange);
optionalRequirements.delete(mod.name);
}
} else if (
!dependencyRequirements.has(mod.name)
&& !recommendedRequirements.has(mod.name)
) {
// If not required or recommended, add to optional
optionalRequirements.set(mod.name, versionRange);
}
// Update the version range if needed
if (mod.version) {
versionRange.combineVersion(mod.version);
if (!versionRange.valid) {
candidateReleases.delete(mod.name);
errors.set(mod.name, "unsatisfiable");
continue;
}
}
// Do not add dependencies for errors, skips, optionals, or valid candidates
const existingCandidate = candidateReleases.get(mod.name);
if (errors.has(mod.name) || skipSearch.has(mod.name) || optionalRequirements.has(mod.name) || (
existingCandidate && versionRange.testVersion(existingCandidate.version)
)) {
continue;
}
// Check if a local version fits the requirements
let candidate = localInfos
.filter(info => (
info.name === mod.name
&& info.factorioVersion === factorioVersion
&& versionRange.testVersion(info.version)
))
.reduce<lib.ModInfo | undefined>((max, cur) => (
max && max.integerVersion > cur.integerVersion ? max : cur
), undefined);
// Check if a mod poral version fits the requirements
if (!candidate || checkForUpdates) {
const releases = await fetchModReleases(mod.name);
const release = selectModRelease(releases, versionRange);
// We only select the mod portal release if it is more recent than the local candidate
if (release && (!candidate || release.integerVersion > candidate.integerVersion)) {
candidate = release;
}
}
// Queue the dependencies of the new candidate
if (candidate) {
candidateReleases.set(mod.name, candidate);
for (const dep of candidate.dependencies) {
searchQueue.push(dep);
}
} else {
candidateReleases.delete(mod.name);
skipSearch.add(mod.name);
}
}
// Add the not found dependencies as errors
for (const modName of dependencyRequirements.keys()) {
if (!candidateReleases.has(modName) && !errors.has(modName)) {
errors.set(modName, "notFound");
}
}
// Remove incompatible and unsatisfiable dependencies if they aren't required by others
for (const [modName, error] of errors) {
if (!dependencyRequirements.has(modName) && (error === "incompatible" || error === "unsatisfiable")) {
errors.delete(modName);
}
}
return new lib.ModDependencyResolveRequest.Response(
[...candidateReleases.values()], errors,
); );
} }
+286 -2
View File
@@ -2,6 +2,7 @@ import type winston from "winston";
import { BlockList, type AddressInfo, isIPv4, isIPv6 } from "net"; import { BlockList, type AddressInfo, isIPv4, isIPv6 } from "net";
import type { ControllerArgs } from "../controller.js"; import type { ControllerArgs } from "../controller.js";
import type ControlConnection from "./ControlConnection.js"; import type ControlConnection from "./ControlConnection.js";
import type HostConnection from "./HostConnection.js";
import express, { type Request, type Response, type NextFunction, type Application } from "express"; import express, { type Request, type Response, type NextFunction, type Application } from "express";
import type { Static } from "@sinclair/typebox"; import type { Static } from "@sinclair/typebox";
import finalhandler from "finalhandler"; import finalhandler from "finalhandler";
@@ -233,6 +234,12 @@ export default class Controller {
// Add default mod packs // Add default mod packs
if (modPacks.size === 0) { if (modPacks.size === 0) {
modPacks.setMany(lib.ModPack.defaultModPacks); modPacks.setMany(lib.ModPack.defaultModPacks);
if (config.get("controller.default_mod_pack_id") === null) {
const baseGame = lib.ModPack.defaultModPacks
.filter(pack => pack.name.startsWith("Base Game "))
.reduce((a, b) => (b.integerFactorioVersion > a.integerFactorioVersion ? b : a));
config.set("controller.default_mod_pack_id", baseGame.id);
}
} }
// Add default roles // Add default roles
@@ -502,11 +509,14 @@ export default class Controller {
} }
this.hostPairing.start(); this.hostPairing.start();
this.onAutosaveIntervalChanged();
this.onSystemMetricsIntervalChanged();
logger.info("Started controller"); logger.info("Started controller");
this.startedAtMs = Date.now(); this.startedAtMs = Date.now();
this.downloadRequiredMods().catch(
err => logger.warn(`Unable to download mods required by plugins: ${err.message}`)
);
this.onAutosaveIntervalChanged();
this.onSystemMetricsIntervalChanged();
this._state = "running"; this._state = "running";
} }
@@ -663,6 +673,7 @@ export default class Controller {
this.updateSystems.bind(this), this.updateSystems.bind(this),
systemMetricsIntervalSeconds * 1000 systemMetricsIntervalSeconds * 1000
); );
this.updateSystems();
} }
} }
@@ -688,6 +699,20 @@ export default class Controller {
} }
} }
/** Fetch system info from a host that just connected instead of waiting for the next interval */
async updateHostSystem(hostConnection: HostConnection) {
if (!(this.config.get("controller.system_metrics_interval") > 0)) {
return;
}
try {
this.systems.set(await hostConnection.send(new lib.SystemInfoRequest()));
} catch (err: any) {
logger.error(
`Unexpected error updating system info of host ${hostConnection.id}:\n${err.stack ?? err.message}`
);
}
}
/** Plugins on the controller, including those installed or updated since it started */ /** Plugins on the controller, including those installed or updated since it started */
async listPlugins() { async listPlugins() {
return await lib.listPluginDetails( return await lib.listPluginDetails(
@@ -1147,6 +1172,265 @@ export default class Controller {
} }
/**
* Download mods from the Factorio mod portal to the mod store
*
* Mods already in the mod store are skipped.
*
* @throws {lib.ConflictError} if the Factorio credentials are not configured.
*/
async downloadModsFromPortal(mods: lib.ModNameVersionPair[], factorioVersion: lib.MajorMinorVersion) {
const toDownload = this.modStore.filterInstalled(mods, false);
if (toDownload.length > 0) {
// Get Factorio credentials from config
const username = this.config.get("controller.factorio_username");
const token = this.config.get("controller.factorio_token");
if (!username || username === "" || !token || token === "") {
throw new lib.ConflictError("Factorio credentials (username, token) not configured on the controller.");
}
try {
logger.info(`Downloading ${toDownload.length} mods for Factorio ${factorioVersion} from portal.`);
await this.modStore.downloadMods(toDownload, username, token, factorioVersion);
} catch (error: any) {
logger.error(`Error downloading mods from portal: ${error.message}`);
const errorMessage = "Mod portal download failed";
if (error.message.includes("401") || error.message.includes("Unauthorized")) {
throw new lib.ConflictError(
`${errorMessage}: Authentication failed. Check Factorio username/token in controller config.`
);
}
if (error.message.includes("404") || error.message.includes("Not Found")) {
throw new lib.NotFoundError(`${errorMessage}: Mod version not found on the portal.`);
}
throw new lib.InternalServerError(`${errorMessage}: ${error.message}`);
}
}
}
/**
* Resolve mods and their dependencies against the mod store and the Factorio mod portal
*/
// eslint-disable-next-line complexity
async resolveModDependencies(
mods: lib.ModDependency[],
factorioVersion: lib.MajorMinorVersion,
checkForUpdates = false,
) {
const searchQueue = [...mods];
const localInfos = [...this.modStore.mods()];
const builtInModNames = lib.ModPack.getBuiltinModNames(factorioVersion);
const skipSearch = new Set(builtInModNames);
type ModPortalModType = InstanceType<typeof lib.ModPortalGetAllRequest.Response>["mods"][number];
type ModPortalReleaseType = NonNullable<ModPortalModType["releases"]>[number];
const dependencyRequirements = new Map<string, lib.ModVersionRange>();
const recommendedRequirements = new Map<string, lib.ModVersionRange>();
const optionalRequirements = new Map<string, lib.ModVersionRange>();
const candidateReleases = new Map<string, lib.ModInfo>();
const errors = new Map<string, lib.ModDependencyResolveErrors>();
const _modPortalReleases = new Map<string, ModPortalModType | null>();
async function fetchModReleases(modName: string) {
let releases = _modPortalReleases.get(modName);
if (releases === undefined) {
try {
releases = await lib.ModStore.fetchModReleases(modName);
} catch (error: any) {
if (!error.message.includes("404") && !error.message.includes("Not Found")) {
logger.error(`Error fetching mod from portal: ${error.message}`);
throw new lib.InternalServerError(`Dependency resolution failed: ${error.message}`);
} else {
releases = null;
}
}
}
_modPortalReleases.set(modName, releases);
return releases;
}
function selectModRelease(releases: ModPortalModType | null, versionRange: lib.ModVersionRange) {
if (!releases || !releases.releases) {
return null;
}
// Select the latest matching version for the mod pack's Factorio version
const release = releases.releases
.filter(info => (
versionRange.testVersion(info.version)
&& lib.normaliseMajorMinorVersion(
lib.normaliseSourceVersion(info.info_json.factorio_version)
) === factorioVersion
))
.reduce<ModPortalReleaseType | undefined>((max, cur) => (
max && lib.integerSourceVersion(max.version) > lib.integerSourceVersion(cur.version) ? max : cur
), undefined);
if (!release) {
return null;
}
// Create the mod info class from the selected
try {
return lib.ModInfo.fromJSON({
...release.info_json, // includes dependencies
sha1: release.sha1,
version: release.version,
name: releases.name,
author: releases.owner,
title: releases.title,
});
} catch {
// Can error if there are invalid dependencies
errors.set(releases.name, "invalidDependency");
return null;
}
}
while (searchQueue.length > 0) {
// Dequeue the next mod, skip if incompatible
const mod = searchQueue.shift()!;
if (mod.incompatible) {
errors.set(mod.name, "incompatible");
continue;
}
// Get the current version range (dependencies can be promoted to a stricter requirement)
const versionRange =
dependencyRequirements.get(mod.name)
?? recommendedRequirements.get(mod.name)
?? optionalRequirements.get(mod.name)
?? new lib.ModVersionRange();
if (mod.required) {
// Add to required, remove from recommended and optional
dependencyRequirements.set(mod.name, versionRange);
recommendedRequirements.delete(mod.name);
optionalRequirements.delete(mod.name);
} else if (mod.recommended) {
// If not required, add to recommended and remove from optional
if (!dependencyRequirements.has(mod.name)) {
recommendedRequirements.set(mod.name, versionRange);
optionalRequirements.delete(mod.name);
}
} else if (
!dependencyRequirements.has(mod.name)
&& !recommendedRequirements.has(mod.name)
) {
// If not required or recommended, add to optional
optionalRequirements.set(mod.name, versionRange);
}
// Update the version range if needed
if (mod.version) {
versionRange.combineVersion(mod.version);
if (!versionRange.valid) {
candidateReleases.delete(mod.name);
errors.set(mod.name, "unsatisfiable");
continue;
}
}
// Do not add dependencies for errors, skips, optionals, or valid candidates
const existingCandidate = candidateReleases.get(mod.name);
if (errors.has(mod.name) || skipSearch.has(mod.name) || optionalRequirements.has(mod.name) || (
existingCandidate && versionRange.testVersion(existingCandidate.version)
)) {
continue;
}
// Check if a local version fits the requirements
let candidate = localInfos
.filter(info => (
info.name === mod.name
&& info.factorioVersion === factorioVersion
&& versionRange.testVersion(info.version)
))
.reduce<lib.ModInfo | undefined>((max, cur) => (
max && max.integerVersion > cur.integerVersion ? max : cur
), undefined);
// Check if a mod poral version fits the requirements
if (!candidate || checkForUpdates) {
const releases = await fetchModReleases(mod.name);
const release = selectModRelease(releases, versionRange);
// We only select the mod portal release if it is more recent than the local candidate
if (release && (!candidate || release.integerVersion > candidate.integerVersion)) {
candidate = release;
}
}
// Queue the dependencies of the new candidate
if (candidate) {
candidateReleases.set(mod.name, candidate);
for (const dep of candidate.dependencies) {
searchQueue.push(dep);
}
} else {
candidateReleases.delete(mod.name);
skipSearch.add(mod.name);
}
}
// Add the not found dependencies as errors
for (const modName of dependencyRequirements.keys()) {
if (!candidateReleases.has(modName) && !errors.has(modName)) {
errors.set(modName, "notFound");
}
}
// Remove incompatible and unsatisfiable dependencies if they aren't required by others
for (const [modName, error] of errors) {
if (!dependencyRequirements.has(modName) && (error === "incompatible" || error === "unsatisfiable")) {
errors.delete(modName);
}
}
return new lib.ModDependencyResolveRequest.Response(
[...candidateReleases.values()], errors,
);
}
/**
* Download the mods required by plugins that are missing from the mod store
*
* Resolves for the Factorio version of the default mod pack. Dependencies
* of the required mods are downloaded too.
*/
async downloadRequiredMods() {
const required = this.pluginInfos.flatMap(pluginInfo => lib.pluginRequiredMods(pluginInfo));
const modPackId = this.config.get("controller.default_mod_pack_id");
const modPack = modPackId !== null ? this.modPacks.get(modPackId) : undefined;
if (!required.length || !modPack) {
return;
}
const factorioVersion = lib.normaliseMajorMinorVersion(modPack.factorioVersion);
const { dependencies, errors } = await this.resolveModDependencies(required, factorioVersion);
const builtInMods = new Set(lib.ModPack.getBuiltinModNames(factorioVersion));
for (const [name, error] of errors) {
if (!builtInMods.has(name)) {
logger.warn(`Unable to resolve mod ${name} required by plugins: ${error}`);
}
}
const toDownload = this.modStore.filterInstalled(dependencies.map(
info => ({ name: info.name, version: new lib.ModVersionEquality("=", info.normalisedVersion) })
), false);
if (!toDownload.length) {
return;
}
const names = toDownload.map(mod => `${mod.name} ${mod.version.version}`).join(", ");
logger.info(`Downloading mods required by plugins: ${names}`);
await this.downloadModsFromPortal(toDownload, factorioVersion);
}
modPacksUpdated(modPacks: lib.ModPack[]) { modPacksUpdated(modPacks: lib.ModPack[]) {
this.subscriptions.broadcast(new lib.ModPackUpdatesEvent(modPacks)); this.subscriptions.broadcast(new lib.ModPackUpdatesEvent(modPacks));
this.hooks.modPacksUpdated.invoke(modPacks); this.hooks.modPacksUpdated.invoke(modPacks);
@@ -51,6 +51,7 @@ export default class HostConnection extends BaseConnection {
for (let event of ["connect", "drop", "resume", "close"] as const) { for (let event of ["connect", "drop", "resume", "close"] as const) {
this.connector.on(event, () => this._controller.hooks.hostConnectionEvent.invoke(this, event)); this.connector.on(event, () => this._controller.hooks.hostConnectionEvent.invoke(this, event));
} }
this.connector.on("connect", () => { this._controller.updateHostSystem(this); });
this.connector.on("close", () => { this.connector.on("close", () => {
// Update status to unknown for instances on this host. // Update status to unknown for instances on this host.
+6 -4
View File
@@ -446,7 +446,7 @@ set "NODE_OPTIONS=--enable-source-maps %NODE_OPTIONS%"
:restart :restart
call .\\node_modules\\.bin\\clusteriocontroller.cmd run --can-restart call .\\node_modules\\.bin\\clusteriocontroller.cmd run --can-restart
if %errorlevel% equ 0 exit /b if %errorlevel% equ 0 exit /b
if %errorlevel% equ 8 exit /b if %errorlevel% equ 8 exit /b 8
goto restart goto restart
` `
); );
@@ -458,7 +458,8 @@ goto restart
export "NODE_OPTIONS=--enable-source-maps $NODE_OPTIONS" export "NODE_OPTIONS=--enable-source-maps $NODE_OPTIONS"
while true; do while true; do
./node_modules/.bin/clusteriocontroller run --can-restart ./node_modules/.bin/clusteriocontroller run --can-restart
if [[ $? -eq 0 || $? -eq 8 ]]; then exit $?; fi code=$?
if [[ $code -eq 0 || $code -eq 8 ]]; then exit $code; fi
done done
`, `,
{ mode: 0o755 }, { mode: 0o755 },
@@ -498,7 +499,7 @@ set "NODE_OPTIONS=--enable-source-maps %NODE_OPTIONS%"
${extraCaCertsLine}:restart ${extraCaCertsLine}:restart
call .\\node_modules\\.bin\\clusteriohost.cmd run --can-restart call .\\node_modules\\.bin\\clusteriohost.cmd run --can-restart
if %errorlevel% equ 0 exit /b if %errorlevel% equ 0 exit /b
if %errorlevel% equ 8 exit /b if %errorlevel% equ 8 exit /b 8
goto restart goto restart
` `
); );
@@ -511,7 +512,8 @@ goto restart
export "NODE_OPTIONS=--enable-source-maps $NODE_OPTIONS" export "NODE_OPTIONS=--enable-source-maps $NODE_OPTIONS"
${extraCaCertsLine}while true; do ${extraCaCertsLine}while true; do
./node_modules/.bin/clusteriohost run --can-restart ./node_modules/.bin/clusteriohost run --can-restart
if [[ $? -eq 0 || $? -eq 8 ]]; then exit $?; fi code=$?
if [[ $code -eq 0 || $code -eq 8 ]]; then exit $code; fi
done done
`, `,
{ mode: 0o755 }, { mode: 0o755 },
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/create", "name": "@clusterio/create",
"version": "2.1.0", "version": "2.1.1",
"description": "Installer for clusterio", "description": "Installer for clusterio",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+5
View File
@@ -23,6 +23,11 @@ Common options:
* `--create-config` If the config file is missing, create a new one and carry on instead of exiting with an error. * `--create-config` If the config file is missing, create a new one and carry on instead of exiting with an error.
Alternative to the `config create` command. Alternative to the `config create` command.
* `--output <human|json|wire>` Format to print the results of cluster commands in.
Defaults to `human`.
See [Output format](/docs/managing-a-cluster.md#output-format) for what `json` and `wire` print.
The `plugin` and `config` commands described below always print human readable text.
### `plugin <command>` ### `plugin <command>`
+57 -17
View File
@@ -17,6 +17,7 @@ import { ConsoleTransport, levels, logger } from "@clusterio/lib";
import * as commands from "./src/commands.js"; import * as commands from "./src/commands.js";
import { BaseCtlPlugin, CtlHooks } from "./src/BaseCtlPlugin.js"; import { BaseCtlPlugin, CtlHooks } from "./src/BaseCtlPlugin.js";
import { CommandOutput, errorToJson, outputFormats, print, type OutputFormat } from "./src/command_ops.js";
/** /**
@@ -55,6 +56,8 @@ export class Ctl extends lib.Link {
config: lib.CtlConfig; config: lib.CtlConfig;
/** Keep the control connection alive after the command completes. */ /** Keep the control connection alive after the command completes. */
keepOpen = false; keepOpen = false;
/** Output of the command being run. */
output = new CommandOutput();
constructor( constructor(
connector: CtlConnector, connector: CtlConnector,
@@ -82,12 +85,20 @@ export class Ctl extends lib.Link {
} }
async handleLogMessageEvent(event: lib.LogMessageEvent) { async handleLogMessageEvent(event: lib.LogMessageEvent) {
logger.log(event.info as any); // Only log following commands print log events as results, the rest log them to stderr
if (this.output.streamingLog) {
this.output.result(event.info, () => logger.log(event.info as any));
} else {
logger.log(event.info as any);
}
} }
async handleDebugWsMessageEvent(event: lib.DebugWsMessageEvent) { async handleDebugWsMessageEvent(event: lib.DebugWsMessageEvent) {
// eslint-disable-next-line no-console this.output.result(
console.log("WS", event.direction, event.content); { direction: event.direction, content: event.content },
// eslint-disable-next-line no-console
() => console.log("WS", event.direction, event.content),
);
} }
async shutdown() { async shutdown() {
@@ -126,6 +137,7 @@ interface CtlArguments {
$0: string, $0: string,
_: (string | number)[], _: (string | number)[],
logLevel: keyof typeof levels, logLevel: keyof typeof levels,
output: OutputFormat,
config: string, config: string,
pluginList: string, pluginList: string,
bypassLockFile: boolean, bypassLockFile: boolean,
@@ -165,6 +177,13 @@ export async function initialize(
choices: Object.keys(levels), choices: Object.keys(levels),
type: "string", type: "string",
}) })
.option("output", {
nargs: 1,
describe: "Format to print command results in",
default: "human",
choices: outputFormats,
type: "string",
})
.option("config", { .option("config", {
nargs: 1, nargs: 1,
describe: "config file to get credentails from", describe: "config file to get credentails from",
@@ -260,6 +279,20 @@ export function selectTargetCommand(args: CtlArguments, rootCommands: lib.Comman
return targetCommand; return targetCommand;
} }
/**
* Run the command selected by args, printing results in the format given by args.output
*/
export async function runCommand(
args: CtlArguments,
rootCommands: lib.CommandTree,
link: lib.Link & { output?: CommandOutput },
) {
link.output = new CommandOutput(args.output);
const targetCommand = selectTargetCommand(args, rootCommands);
await targetCommand.run(args, link);
link.output.finish();
}
async function startCtl() { async function startCtl() {
const { const {
args, args,
@@ -271,10 +304,19 @@ async function startCtl() {
return; return;
} }
try {
await connectAndRun(args, rootCommands, ctlConfig);
} catch (err) {
if (args.output !== "human") {
print(JSON.stringify(errorToJson(err)));
}
throw err;
}
}
async function connectAndRun(args: CtlArguments, rootCommands: lib.CommandTree, ctlConfig: lib.CtlConfig) {
if (!ctlConfig.get("ctl.controller_url") || !ctlConfig.get("ctl.controller_token")) { if (!ctlConfig.get("ctl.controller_url") || !ctlConfig.get("ctl.controller_token")) {
logger.error("Missing URL and/or token to connect with. See README.md for setting up access."); throw new lib.StartupError("Missing URL and/or token to connect with. See README.md for setting up access.");
process.exitCode = 1;
return;
} }
let controlConnector = new CtlConnector( let controlConnector = new CtlConnector(
@@ -298,26 +340,25 @@ async function startCtl() {
process.on("SIGTERM", lib.createShutdownGuard(logger, "termination", ctl.shutdown.bind(ctl))); process.on("SIGTERM", lib.createShutdownGuard(logger, "termination", ctl.shutdown.bind(ctl)));
try { try {
const targetCommand = selectTargetCommand(args, rootCommands); await runCommand(args, rootCommands, ctl);
await targetCommand.run(args, ctl);
} catch (err) { } catch (err) {
ctl.keepOpen = false; ctl.keepOpen = false;
let message: string;
if (err instanceof lib.CommandError) { if (err instanceof lib.CommandError) {
logger.error(`Error running command: ${err.message}`); message = `Error running command: ${err.message}`;
process.exitCode = 1;
} else if (err instanceof lib.ClientError || err instanceof lib.UnavailableError) { } else if (err instanceof lib.ClientError || err instanceof lib.UnavailableError) {
logger.error(`Error sending request: ${err.message}`); message = `Error sending request: ${err.message}`;
process.exitCode = 1;
} else if (err instanceof lib.ServerError) { } else if (err instanceof lib.ServerError) {
logger.error(`Error sending request:\n${err.stack ?? err.message}`); message = `Error sending request:\n${err.stack ?? err.message}`;
process.exitCode = 1;
} else { } else {
throw err; throw err;
} }
ctl.output.error(err, () => logger.error(message));
process.exitCode = 1;
} finally { } finally {
if (!ctl.keepOpen) { if (!ctl.keepOpen) {
@@ -328,6 +369,7 @@ async function startCtl() {
export function bootstrap() { export function bootstrap() {
startCtl().catch(err => { startCtl().catch(err => {
process.exitCode = 1;
if (err.errors) { if (err.errors) {
logger.fatal(JSON.stringify(err.errors, null, "\t")); logger.fatal(JSON.stringify(err.errors, null, "\t"));
} }
@@ -340,15 +382,13 @@ export function bootstrap() {
${err.stack}` ${err.stack}`
); );
} else { } else {
logger.error(` logger.fatal(`
+---------------------------------+ +---------------------------------+
| Unable to to start clusterioctl | | Unable to to start clusterioctl |
+---------------------------------+ +---------------------------------+
${err.stack}` ${err.stack}`
); );
} }
process.exitCode = 1;
}); });
} }
+1
View File
@@ -1,5 +1,6 @@
import { bootstrap } from "./ctl.js"; import { bootstrap } from "./ctl.js";
export * from "./ctl.js"; export * from "./ctl.js";
export { CommandOutput, type OutputFormat } from "./src/command_ops.js";
export { BaseCtlPlugin, CtlHooks, type CtlPluginContext } from "./src/BaseCtlPlugin.js"; export { BaseCtlPlugin, CtlHooks, type CtlPluginContext } from "./src/BaseCtlPlugin.js";
if (import.meta.main) { if (import.meta.main) {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/ctl", "name": "@clusterio/ctl",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio command line management tool", "description": "Clusterio command line management tool",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+128
View File
@@ -1,4 +1,132 @@
import asTableModule from "as-table";
export function print(...content: any) { export function print(...content: any) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(...content); console.log(...content);
} }
function formatCell(value: unknown) {
if (value instanceof Set) {
return [...value].join(", ");
}
return String(value);
}
export const asTable = asTableModule.configure({ delimiter: " | ", print: formatCell });
/** Copy of object with only the given fields, or all of them if fields includes "all" */
export function pickFields(object: object, fields: string[]) {
if (fields.includes("all")) {
return object;
}
return Object.fromEntries(Object.entries(object).filter(([field]) => fields.includes(field)));
}
/** Convert class instances, Sets and Maps into plain JSON values, ignoring wire format toJSON methods */
export function toPlain(value: unknown): unknown {
if (value instanceof Set || value instanceof Array) {
return [...value].map(toPlain);
}
if (value instanceof Map) {
return Object.fromEntries([...value].map(([key, entry]) => [key, toPlain(entry)]));
}
if (value instanceof Date) {
return value.toISOString();
}
if (typeof value === "object" && value !== null) {
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, toPlain(entry)]));
}
return value;
}
/** Describe an error as `{ error: ClassName, message: string }` */
export function errorToJson(err: unknown) {
if (err instanceof Error) {
return { error: err.constructor.name, message: err.message };
}
return { error: "Error", message: String(err) };
}
/** JSON.stringify replacer for Sets and Maps that have no toJSON method */
function wireReplacer(key: string, value: unknown) {
if (value instanceof Set) {
return [...value];
}
if (value instanceof Map) {
return Object.fromEntries(value);
}
return value;
}
export const outputFormats = ["human", "json", "wire"] as const;
export type OutputFormat = typeof outputFormats[number];
/**
* Prints the results of a ctl command in the format chosen with --output
*
* In json and wire mode each call to result prints one line of JSON to stdout. json uses the
* property names of the objects, wire uses their toJSON methods to give the format sent over the link.
*/
export class CommandOutput {
/** True once a result has been printed */
written = false;
/** Command prints log events as results while it runs and has no final result */
streamingLog = false;
constructor(
public format: OutputFormat = "human",
) { }
get human() {
return this.format === "human";
}
/**
* Print the result of a command
*
* @param data - Printed as JSON in json and wire mode.
* @param human - Prints the result in human mode, defaults to printing data as is.
*/
result(data: unknown, human: () => void = () => print(data)) {
this.written = true;
if (this.format === "json") {
print(JSON.stringify(toPlain(data)));
} else if (this.format === "wire") {
print(JSON.stringify(data, wireReplacer));
} else {
human();
}
}
/**
* Print a list of objects as a table, or as an array in json and wire mode
*
* @param rows - Printed as is in json and wire mode.
* @param map - Converts each row into the table row printed in human mode.
*/
table<T extends object>(rows: T[], map: (row: T) => object = row => row) {
this.result(rows, () => print(asTable(rows.map(map))));
}
/**
* Print an error as `{ "error": ClassName, "message": string }` in json and wire mode
*
* @param err - Error to print.
* @param human - Reports the error in human mode.
*/
error(err: unknown, human: () => void) {
this.written = true;
if (this.human) {
human();
} else {
print(JSON.stringify(errorToJson(err)));
}
}
/** Print a success result for commands that finished without printing one */
finish() {
if (!this.written && !this.streamingLog) {
this.result({ success: true }, () => print("Command ran successfully"));
}
}
}
+25 -26
View File
@@ -1,15 +1,15 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events"; import events from "events";
import os from "os"; import os from "os";
import child_process from "child_process"; import child_process from "child_process";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { asTable, print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal } from "./config_ops.js"; import {
serializedConfigToString, getEditor, configToKeyVal, printConfig, printRestartRequired,
const asTable = asTableModule.configure({ delimiter: " | " }); } from "./config_ops.js";
export const controllerCommands = new lib.CommandTree({ name: "controller", description: "Controller management" }); export const controllerCommands = new lib.CommandTree({ name: "controller", description: "Controller management" });
controllerCommands.add(new lib.Command({ controllerCommands.add(new lib.Command({
@@ -44,9 +44,8 @@ controllerCommands.add(new lib.Command({
} }
if (args.restart) { if (args.restart) {
await ctl.send(new lib.ControllerRestartRequest()); await ctl.send(new lib.ControllerRestartRequest());
} else {
print("Controller updated; a restart is required to apply the changes.");
} }
printRestartRequired(ctl, !args.restart, "Controller updated");
}, },
})); }));
@@ -57,11 +56,7 @@ const controllerConfigCommands = new lib.CommandTree({
controllerConfigCommands.add(new lib.Command({ controllerConfigCommands.add(new lib.Command({
definition: ["list", "List controller configuration"], definition: ["list", "List controller configuration"],
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let config = await ctl.send(new lib.ControllerConfigGetRequest()); printConfig(ctl, await ctl.send(new lib.ControllerConfigGetRequest()));
for (let [name, value] of Object.entries(config)) {
print(`${name} ${JSON.stringify(value)}`);
}
}, },
})); }));
@@ -146,19 +141,22 @@ controllerConfigCommands.add(new lib.Command({
index, index,
final[index], final[index],
)); ));
} catch (err) { } catch (err: any) {
// eslint-disable-next-line
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
print(err);
// If the string is empty, it's better to just print "" instead of nothing // If the string is empty, it's better to just print "" instead of nothing
logger.error(
`Attempt to set ${index} to ${final[index] || String(null)} failed; ` +
`set back to previous value: ${err.message}`
);
} }
} }
} }
try { try {
await fs.unlink(tmpFile); await fs.unlink(tmpFile);
} catch (err) { } catch (err) {
print("err: temporary file", tmpFile, "could not be deleted."); logger.warn(
print("This is not fatal, but they may build up over time if the issue persists."); `Temporary file ${tmpFile} could not be deleted. ` +
"This is not fatal, but they may build up over time if the issue persists."
);
} }
}, },
})); }));
@@ -175,7 +173,7 @@ controllerPluginCommands.add(new lib.Command({
let url = new URL(ctl.config.get("ctl.controller_url")!); let url = new URL(ctl.config.get("ctl.controller_url")!);
url.pathname += "api/plugins"; url.pathname += "api/plugins";
let response = await fetch(url); let response = await fetch(url);
print(asTable(await response.json() as [])); ctl.output.table(await response.json() as object[]);
}, },
})); }));
controllerPluginCommands.add(new lib.Command({ controllerPluginCommands.add(new lib.Command({
@@ -187,9 +185,8 @@ controllerPluginCommands.add(new lib.Command({
await ctl.sendTo("controller", new lib.PluginUpdateRequest(args.plugin)); await ctl.sendTo("controller", new lib.PluginUpdateRequest(args.plugin));
if (args.restart) { if (args.restart) {
await ctl.send(new lib.ControllerRestartRequest()); await ctl.send(new lib.ControllerRestartRequest());
} else {
print("Plugin updated; a restart is required to apply the changes.");
} }
printRestartRequired(ctl, !args.restart, "Plugin updated");
}, },
})); }));
controllerPluginCommands.add(new lib.Command({ controllerPluginCommands.add(new lib.Command({
@@ -202,13 +199,16 @@ controllerPluginCommands.add(new lib.Command({
const response = await ctl.sendTo( const response = await ctl.sendTo(
"controller", new lib.PluginSearchRequest(args.query, args.page, args.pageSize) "controller", new lib.PluginSearchRequest(args.query, args.page, args.pageSize)
); );
print(asTable(response.results.map(plugin => ({ const results = response.results.map(plugin => ({
name: plugin.name, name: plugin.name,
version: plugin.version, version: plugin.version,
publisher: plugin.publisher ?? "", publisher: plugin.publisher ?? "",
description: plugin.description ?? "", description: plugin.description ?? "",
})))); }));
print(`Showing ${response.results.length} of ${response.total} results`); ctl.output.result({ total: response.total, results }, () => {
print(asTable(results));
print(`Showing ${results.length} of ${response.total} results`);
});
}, },
})); }));
controllerPluginCommands.add(new lib.Command({ controllerPluginCommands.add(new lib.Command({
@@ -220,9 +220,8 @@ controllerPluginCommands.add(new lib.Command({
await ctl.sendTo("controller", new lib.PluginInstallRequest(args.plugin)); await ctl.sendTo("controller", new lib.PluginInstallRequest(args.plugin));
if (args.restart) { if (args.restart) {
await ctl.send(new lib.ControllerRestartRequest()); await ctl.send(new lib.ControllerRestartRequest());
} else {
print("Plugin installed; a restart is required to apply the changes.");
} }
printRestartRequired(ctl, !args.restart, "Plugin installed");
}, },
})); }));
+3 -7
View File
@@ -1,5 +1,6 @@
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
export const debugCommands = new lib.CommandTree({ name: "debug", description: "Debugging utilities" }); export const debugCommands = new lib.CommandTree({ name: "debug", description: "Debugging utilities" });
debugCommands.add(new lib.Command({ debugCommands.add(new lib.Command({
@@ -7,6 +8,7 @@ debugCommands.add(new lib.Command({
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
await ctl.send(new lib.DebugDumpWsRequest()); await ctl.send(new lib.DebugDumpWsRequest());
ctl.keepOpen = true; ctl.keepOpen = true;
ctl.output.streamingLog = true;
}, },
})); }));
@@ -20,12 +22,6 @@ debugCommands.add(new lib.Command({
}], }],
handler: async function(args: { exchangeString: string, pretty: boolean }, ctl: Ctl) { handler: async function(args: { exchangeString: string, pretty: boolean }, ctl: Ctl) {
const result = lib.readMapExchangeString(args.exchangeString); const result = lib.readMapExchangeString(args.exchangeString);
if (args.pretty) { ctl.output.result(result, () => print(JSON.stringify(result, null, args.pretty ? 2 : undefined)));
// eslint-disable-next-line no-console
console.log(JSON.stringify(result, null, 2));
} else {
// eslint-disable-next-line no-console
console.log(JSON.stringify(result));
}
}, },
})); }));
+38 -38
View File
@@ -1,15 +1,15 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events"; import events from "events";
import os from "os"; import os from "os";
import child_process from "child_process"; import child_process from "child_process";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { asTable, print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal } from "./config_ops.js"; import {
serializedConfigToString, getEditor, configToKeyVal, printConfig, printRestartRequired,
const asTable = asTableModule.configure({ delimiter: " | " }); } from "./config_ops.js";
export const hostCommands = new lib.CommandTree({ name: "host", description: "Host management" }); export const hostCommands = new lib.CommandTree({ name: "host", description: "Host management" });
hostCommands.add(new lib.Command({ hostCommands.add(new lib.Command({
@@ -45,8 +45,7 @@ hostCommands.add(new lib.Command({
hostCommands.add(new lib.Command({ hostCommands.add(new lib.Command({
definition: [["list", "l"], "List hosts connected to the controller"], definition: [["list", "l"], "List hosts connected to the controller"],
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let hosts = await ctl.send(new lib.HostListRequest()); ctl.output.table(await ctl.send(new lib.HostListRequest()));
print(asTable(hosts));
}, },
})); }));
@@ -71,17 +70,19 @@ hostCommands.add(new lib.Command({
definition: ["pending", "List host installers waiting for approval"], definition: ["pending", "List host installers waiting for approval"],
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let requests = await ctl.send(new lib.HostPairingListRequest()); let requests = await ctl.send(new lib.HostPairingListRequest());
if (!requests.length) { ctl.output.result(requests, () => {
print("No pending host requests"); if (!requests.length) {
return; print("No pending host requests");
} return;
let now = Date.now(); }
print(asTable(requests.map(request => ({ let now = Date.now();
code: request.code, print(asTable(requests.map(request => ({
name: request.name, code: request.code,
address: request.remoteAddress, name: request.name,
age: formatAge(now - request.createdAtMs), address: request.remoteAddress,
})))); age: formatAge(now - request.createdAtMs),
}))));
});
}, },
})); }));
@@ -99,7 +100,10 @@ hostCommands.add(new lib.Command({
name = requests.find(request => plainCode(request.code) === plainCode(args.code))?.name; name = requests.find(request => plainCode(request.code) === plainCode(args.code))?.name;
} }
let hostId = await ctl.send(new lib.HostPairingApproveRequest(args.code, args.name)); let hostId = await ctl.send(new lib.HostPairingApproveRequest(args.code, args.name));
print(name ? `Approved host ${name} with id ${hostId}` : `Approved host with id ${hostId}`); ctl.output.result(
{ id: hostId, name },
() => print(name ? `Approved host ${name} with id ${hostId}` : `Approved host with id ${hostId}`),
);
}, },
})); }));
@@ -137,9 +141,8 @@ hostCommands.add(new lib.Command({
} }
if (args.restart) { if (args.restart) {
await ctl.sendTo({ hostId }, new lib.HostRestartRequest()); await ctl.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Host updated; a restart is required to apply the changes.");
} }
printRestartRequired(ctl, !args.restart, "Host updated");
}, },
})); }));
@@ -153,11 +156,7 @@ hostConfigCommands.add(new lib.Command({
}], }],
handler: async function(args: { host: string }, ctl: Ctl) { handler: async function(args: { host: string }, ctl: Ctl) {
let hostId = await lib.resolveHost(ctl, args.host); let hostId = await lib.resolveHost(ctl, args.host);
let config = await ctl.sendTo({ hostId }, new lib.HostConfigGetRequest()); printConfig(ctl, await ctl.sendTo({ hostId }, new lib.HostConfigGetRequest()));
for (let [name, value] of Object.entries(config)) {
print(`${name} ${JSON.stringify(value)}`);
}
}, },
})); }));
@@ -255,19 +254,22 @@ hostConfigCommands.add(new lib.Command({
index, index,
final[index], final[index],
)); ));
} catch (err) { } catch (err: any) {
// eslint-disable-next-line
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
// If the string is empty, it's better to just print "" instead of nothing // If the string is empty, it's better to just print "" instead of nothing
print(err); logger.error(
`Attempt to set ${index} to ${final[index] || String(null)} failed; ` +
`set back to previous value: ${err.message}`
);
} }
} }
} }
try { try {
await fs.unlink(tmpFile); await fs.unlink(tmpFile);
} catch (err) { } catch (err) {
print("err: temporary file", tmpFile, "could not be deleted."); logger.warn(
print("This is not fatal, but they may build up over time if the issue persists."); `Temporary file ${tmpFile} could not be deleted. ` +
"This is not fatal, but they may build up over time if the issue persists."
);
} }
}, },
})); }));
@@ -285,9 +287,9 @@ hostPluginCommands.add(new lib.Command({
handler: async function(args: { host: string }, ctl: Ctl) { handler: async function(args: { host: string }, ctl: Ctl) {
let hostId = await lib.resolveHost(ctl, args.host); let hostId = await lib.resolveHost(ctl, args.host);
const plugins = await ctl.sendTo({ hostId }, new lib.PluginListRequest()); const plugins = await ctl.sendTo({ hostId }, new lib.PluginListRequest());
print(asTable(plugins.map(p => ({ ctl.output.table(plugins, p => ({
title: p.title, version: p.version, state: p.state, npmPackage: p.npmPackage, title: p.title, version: p.version, state: p.state, npmPackage: p.npmPackage,
})))); }));
}, },
})); }));
hostPluginCommands.add(new lib.Command({ hostPluginCommands.add(new lib.Command({
@@ -301,9 +303,8 @@ hostPluginCommands.add(new lib.Command({
await ctl.sendTo({ hostId }, new lib.PluginUpdateRequest(args.plugin)); await ctl.sendTo({ hostId }, new lib.PluginUpdateRequest(args.plugin));
if (args.restart) { if (args.restart) {
await ctl.sendTo({ hostId }, new lib.HostRestartRequest()); await ctl.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Plugin updated; a restart is required to apply the changes.");
} }
printRestartRequired(ctl, !args.restart, "Plugin updated");
}, },
})); }));
hostPluginCommands.add(new lib.Command({ hostPluginCommands.add(new lib.Command({
@@ -317,9 +318,8 @@ hostPluginCommands.add(new lib.Command({
await ctl.sendTo({ hostId }, new lib.PluginInstallRequest(args.plugin)); await ctl.sendTo({ hostId }, new lib.PluginInstallRequest(args.plugin));
if (args.restart) { if (args.restart) {
await ctl.sendTo({ hostId }, new lib.HostRestartRequest()); await ctl.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Plugin installed; a restart is required to apply the changes.");
} }
printRestartRequired(ctl, !args.restart, "Plugin installed");
}, },
})); }));
hostCommands.add(hostPluginCommands); hostCommands.add(hostPluginCommands);
+70 -53
View File
@@ -1,5 +1,4 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events"; import events from "events";
import path from "path"; import path from "path";
import os from "os"; import os from "os";
@@ -10,10 +9,9 @@ import util from "util";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib"; import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { errorToJson, print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal } from "./config_ops.js"; import { serializedConfigToString, getEditor, configToKeyVal, printConfig } from "./config_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
const finished = util.promisify(stream.finished); const finished = util.promisify(stream.finished);
export const instanceCommands = new lib.CommandTree({ export const instanceCommands = new lib.CommandTree({
@@ -27,7 +25,7 @@ instanceCommands.add(new lib.Command({
}], }],
handler: async function(args: { templates: boolean }, ctl: Ctl) { handler: async function(args: { templates: boolean }, ctl: Ctl) {
let list = await ctl.send(new lib.InstanceDetailsListRequest()); let list = await ctl.send(new lib.InstanceDetailsListRequest());
print(asTable(list.filter(instance => args.templates || !instance.isTemplate))); ctl.output.table(list.filter(instance => args.templates || !instance.isTemplate));
}, },
})); }));
@@ -64,6 +62,7 @@ instanceCommands.add(new lib.Command({
instanceConfig.toRemote("controller", fields), instanceConfig.toRemote("controller", fields),
args.from args.from
)); ));
ctl.output.result({ id: instanceConfig.get("instance.id") }, () => {});
}, },
})); }));
@@ -81,19 +80,13 @@ instanceConfigCommands.add(new lib.Command({
? config["instance.inherited_fields"] as Record<string, true | string[]> ?? {} ? config["instance.inherited_fields"] as Record<string, true | string[]> ?? {}
: {}; : {};
for (let [name, value] of Object.entries(config)) { printConfig(ctl, config, name => {
if (name === "instance.inherited_fields") {
continue;
}
const entry = inherited[name]; const entry = inherited[name];
let note = "";
if (entry instanceof Array && entry.length) { if (entry instanceof Array && entry.length) {
note = ` (inherited except ${entry.join(", ")})`; return ` (inherited except ${entry.join(", ")})`;
} else if (entry) {
note = " (inherited)";
} }
print(`${name} ${JSON.stringify(value)}${note}`); return entry ? " (inherited)" : "";
} });
}, },
})); }));
@@ -225,19 +218,22 @@ instanceConfigCommands.add(new lib.Command({
index, index,
final[index], final[index],
)); ));
} catch (err) { } catch (err: any) {
// eslint-disable-next-line
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
// If the string is empty, it's better to just print "" instead of nothing // If the string is empty, it's better to just print "" instead of nothing
print(err); logger.error(
`Attempt to set ${index} to ${final[index] || String(null)} failed; ` +
`set back to previous value: ${err.message}`
);
} }
} }
} }
try { try {
await fs.unlink(tmpFile); await fs.unlink(tmpFile);
} catch (err) { } catch (err) {
print("err: temporary file", tmpFile, "could not be deleted."); logger.warn(
print("This is not fatal, but they may build up over time if the issue persists."); `Temporary file ${tmpFile} could not be deleted. ` +
"This is not fatal, but they may build up over time if the issue persists."
);
} }
}, },
})); }));
@@ -291,9 +287,7 @@ instanceSaveCommands.add(new lib.Command({
let instanceId = await lib.resolveInstance(ctl, args.instance); let instanceId = await lib.resolveInstance(ctl, args.instance);
let saves = await ctl.sendTo("controller", new lib.InstanceSaveDetailsListRequest()); let saves = await ctl.sendTo("controller", new lib.InstanceSaveDetailsListRequest());
saves = saves.filter(save => save.instanceId === instanceId); saves = saves.filter(save => save.instanceId === instanceId);
print(asTable(saves.map( ctl.output.table(saves, ({ mtimeMs, ...rest }) => ({ ...rest, mtime: new Date(mtimeMs).toLocaleString() }));
({ mtimeMs, ...rest }) => ({ ...rest, mtime: new Date(mtimeMs).toLocaleString() })
)));
}, },
})); }));
@@ -394,13 +388,16 @@ instanceSaveCommands.add(new lib.Command({
logger.error(requestError); logger.error(requestError);
} }
if (result.saves && result.saves.length) {
logger.info(`Successfully uploaded as ${result.saves[0]}`);
}
if ((result.errors || []).length || (result.request_errors || []).length) { if ((result.errors || []).length || (result.request_errors || []).length) {
throw new lib.CommandError("Uploading save failed"); throw new lib.CommandError("Uploading save failed");
} }
const name = result.saves?.[0];
ctl.output.result({ name }, () => {
if (name) {
print(`Successfully uploaded as ${name}`);
}
});
}, },
})); }));
@@ -439,7 +436,10 @@ instanceSaveCommands.add(new lib.Command({
args.copy args.copy
) )
); );
print(`Transferred as ${storedName} to ${args.targetInstance}.`); ctl.output.result(
{ name: storedName, instanceId: targetInstanceId },
() => print(`Transferred as ${storedName} to ${args.targetInstance}.`),
);
}, },
})); }));
@@ -456,7 +456,10 @@ instanceSaveCommands.add(new lib.Command({
url.pathname += `api/stream/${streamId}`; url.pathname += `api/stream/${streamId}`;
const filename = await lib.downloadFile(url, args.save, "rename"); const filename = await lib.downloadFile(url, args.save, "rename");
logger.info(`Downloaded ${args.save}${args.save === filename ? "" : ` as ${filename}`}`); ctl.output.result(
{ filename },
() => print(`Downloaded ${args.save}${args.save === filename ? "" : ` as ${filename}`}`),
);
}, },
})); }));
@@ -566,27 +569,34 @@ instanceCommands.add(new lib.Command({
}); });
if (instancesToStart.length === 0) { if (instancesToStart.length === 0) {
print("No instances to start."); ctl.output.result([], () => print("No instances to start."));
return; return;
} }
print(`Starting ${instancesToStart.length} instance(s):`); const human = ctl.output.human;
for (const instance of instancesToStart) { if (human) {
print(` - ${instance.name} (ID: ${instance.id})`); print(`Starting ${instancesToStart.length} instance(s):`);
for (const instance of instancesToStart) {
print(` - ${instance.name} (ID: ${instance.id})`);
}
} }
// Start all filtered instances // Start all filtered instances
const startPromises = instancesToStart.map(async (instance: lib.InstanceDetails) => { const results = await Promise.all(instancesToStart.map(async (instance: lib.InstanceDetails) => {
try { try {
await ctl.sendTo({ instanceId: instance.id }, new lib.InstanceStartRequest()); await ctl.sendTo({ instanceId: instance.id }, new lib.InstanceStartRequest());
print(`✓ Started ${instance.name}`); if (human) {
print(`✓ Started ${instance.name}`);
}
return { id: instance.id, name: instance.name };
} catch (error) { } catch (error) {
print(`✗ Failed to start ${instance.name}: ${error}`); if (human) {
print(`✗ Failed to start ${instance.name}: ${error}`);
}
return { id: instance.id, name: instance.name, error: errorToJson(error) };
} }
}); }));
ctl.output.result(results, () => print("Start all operation completed."));
await Promise.all(startPromises);
print("Start all operation completed.");
}, },
})); }));
@@ -646,27 +656,34 @@ instanceCommands.add(new lib.Command({
.filter((instance: lib.InstanceDetails) => ["starting", "running"].includes(instance.status)); .filter((instance: lib.InstanceDetails) => ["starting", "running"].includes(instance.status));
if (instancesToStop.length === 0) { if (instancesToStop.length === 0) {
print("No instances to stop."); ctl.output.result([], () => print("No instances to stop."));
return; return;
} }
print(`Stopping ${instancesToStop.length} instance(s):`); const human = ctl.output.human;
for (const instance of instancesToStop) { if (human) {
print(` - ${instance.name} (ID: ${instance.id})`); print(`Stopping ${instancesToStop.length} instance(s):`);
for (const instance of instancesToStop) {
print(` - ${instance.name} (ID: ${instance.id})`);
}
} }
// Stop all filtered instances // Stop all filtered instances
const stopPromises = instancesToStop.map(async (instance: lib.InstanceDetails) => { const results = await Promise.all(instancesToStop.map(async (instance: lib.InstanceDetails) => {
try { try {
await ctl.sendTo({ instanceId: instance.id }, new lib.InstanceStopRequest()); await ctl.sendTo({ instanceId: instance.id }, new lib.InstanceStopRequest());
print(`✓ Stopped ${instance.name}`); if (human) {
print(`✓ Stopped ${instance.name}`);
}
return { id: instance.id, name: instance.name };
} catch (error) { } catch (error) {
print(`✗ Failed to stop ${instance.name}: ${error}`); if (human) {
print(`✗ Failed to stop ${instance.name}: ${error}`);
}
return { id: instance.id, name: instance.name, error: errorToJson(error) };
} }
}); }));
ctl.output.result(results, () => print("Stop all operation completed."));
await Promise.all(stopPromises);
print("Stop all operation completed.");
}, },
})); }));
@@ -703,6 +720,6 @@ instanceCommands.add(new lib.Command({
); );
// Factorio includes a newline in its response output. // Factorio includes a newline in its response output.
process.stdout.write(result); ctl.output.result({ result }, () => process.stdout.write(result));
}, },
})); }));
+13 -10
View File
@@ -27,6 +27,7 @@ logCommands.add(new lib.Command({
let hostIds = args.host ? [await lib.resolveHost(ctl, args.host)] : []; let hostIds = args.host ? [await lib.resolveHost(ctl, args.host)] : [];
await ctl.setLogSubscriptions({ all: args.all, controller: args.controller, hostIds, instanceIds }); await ctl.setLogSubscriptions({ all: args.all, controller: args.controller, hostIds, instanceIds });
ctl.keepOpen = true; ctl.keepOpen = true;
ctl.output.streamingLog = true;
}, },
})); }));
@@ -75,16 +76,18 @@ logCommands.add(new lib.Command({
result.log.reverse(); result.log.reverse();
} }
let stdoutLogger = winston.createLogger({ ctl.output.result(result.log, () => {
level: "verbose", let stdoutLogger = winston.createLogger({
levels, level: "verbose",
format: new lib.TerminalFormat({ showTimestamp: true }), levels,
transports: [ format: new lib.TerminalFormat({ showTimestamp: true }),
new ConsoleTransport({ errorLevels: [], warnLevels: [] }), transports: [
], new ConsoleTransport({ errorLevels: [], warnLevels: [] }),
],
});
for (let info of result.log) {
stdoutLogger.log(info as any);
}
}); });
for (let info of result.log) {
stdoutLogger.log(info as any);
}
}, },
})); }));
+32 -43
View File
@@ -1,5 +1,4 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events"; import events from "events";
import path from "path"; import path from "path";
import stream from "stream"; import stream from "stream";
@@ -8,9 +7,7 @@ import util from "util";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib"; import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { asTable, pickFields, print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
const finished = util.promisify(stream.finished); const finished = util.promisify(stream.finished);
export const modCommands = new lib.CommandTree({ name: "mod", description: "Manage uploaded mods" }); export const modCommands = new lib.CommandTree({ name: "mod", description: "Manage uploaded mods" });
@@ -24,16 +21,18 @@ modCommands.add(new lib.Command({
throw new lib.CommandError("mod-version must match format digit.digit.digit"); throw new lib.CommandError("mod-version must match format digit.digit.digit");
} }
let modInfo = await ctl.send(new lib.ModGetRequest(args.name, args.modVersion)); let modInfo = await ctl.send(new lib.ModGetRequest(args.name, args.modVersion));
for (let [field, value] of Object.entries(modInfo)) { ctl.output.result(modInfo, () => {
if (value instanceof Array) { for (let [field, value] of Object.entries(modInfo)) {
print(`${field}:`); if (value instanceof Array) {
for (let entry of value) { print(`${field}:`);
print(` ${entry}`); for (let entry of value) {
print(` ${entry}`);
}
} else {
print(`${field}: ${value}`);
} }
} else {
print(`${field}: ${value}`);
} }
} });
}, },
})); }));
@@ -50,17 +49,7 @@ modCommands.add(new lib.Command({
}], }],
handler: async function(args: { fields: string[] }, ctl: Ctl) { handler: async function(args: { fields: string[] }, ctl: Ctl) {
let mods = await ctl.send(new lib.ModListRequest()); let mods = await ctl.send(new lib.ModListRequest());
if (!args.fields.includes("all")) { ctl.output.table(mods, mod => pickFields(mod, args.fields));
for (let entry of mods) {
for (let field of Object.keys(entry)) {
if (!args.fields.includes(field)) {
// @ts-expect-error terrible hack
delete entry[field];
}
}
}
}
print(asTable(mods));
}, },
})); }));
@@ -121,21 +110,19 @@ modCommands.add(new lib.Command({
args.sortOrder args.sortOrder
)); ));
let results = response.results.flatMap(result => result.versions); let results = response.results.flatMap(result => result.versions);
if (!args.fields.includes("all")) { ctl.output.result({
for (let entry of results) { page: args.page,
for (let field of Object.keys(entry)) { pageCount: response.pageCount,
if (!args.fields.includes(field)) { resultCount: response.resultCount,
// @ts-expect-error terrible hack queryIssues: response.queryIssues,
delete entry[field]; results,
} }, () => {
} for (let issue of response.queryIssues) {
print(issue);
} }
} print(`page ${args.page} of ${response.pageCount} (${response.resultCount} results)`);
for (let issue of response.queryIssues) { print(asTable(results.map(mod => pickFields(mod, args.fields))));
print(issue); });
}
print(`page ${args.page} of ${response.pageCount} (${response.resultCount} results)`);
print(asTable(results));
}, },
})); }));
@@ -174,14 +161,16 @@ modCommands.add(new lib.Command({
logger.error(requestError); logger.error(requestError);
} }
if (result.mods && result.mods.length) {
const mod = lib.ModInfo.fromJSON(result.mods[0]);
logger.info(`Successfully uploaded ${mod.filename}`);
}
if ((result.errors || []).length || (result.request_errors || []).length) { if ((result.errors || []).length || (result.request_errors || []).length) {
throw new lib.CommandError("Uploading mod failed"); throw new lib.CommandError("Uploading mod failed");
} }
const uploaded = result.mods?.length ? lib.ModInfo.fromJSON(result.mods[0]).filename : undefined;
ctl.output.result({ filename: uploaded }, () => {
if (uploaded) {
print(`Successfully uploaded ${uploaded}`);
}
});
}, },
})); }));
@@ -201,7 +190,7 @@ modCommands.add(new lib.Command({
let filename = `${args.name}_${args.modVersion}.zip`; let filename = `${args.name}_${args.modVersion}.zip`;
await lib.downloadFile(url, filename, "overwrite"); await lib.downloadFile(url, filename, "overwrite");
logger.info(`Downloaded ${filename}`); ctl.output.result({ filename }, () => print(`Downloaded ${filename}`));
}, },
})); }));
+49 -51
View File
@@ -1,11 +1,7 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib"; import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { pickFields, print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const modPackCommands = new lib.CommandTree({ name: "mod-pack", description: "Mod Pack" }); export const modPackCommands = new lib.CommandTree({ name: "mod-pack", description: "Mod Pack" });
modPackCommands.add(new lib.Command({ modPackCommands.add(new lib.Command({
@@ -19,56 +15,50 @@ modPackCommands.add(new lib.Command({
let modPack = await ctl.send( let modPack = await ctl.send(
new lib.ModPackGetRequest(await lib.resolveModPack(ctl, args.modPack)) new lib.ModPackGetRequest(await lib.resolveModPack(ctl, args.modPack))
); );
ctl.output.result(modPack, () => printModPack(modPack, mods));
for (let [field, value] of Object.entries(modPack)) {
if (field === "mods") {
print(`${field}:`);
for (let [name, entry] of value) {
const mod = mods.get(`${name}_${entry.version}`);
const missing = !mod && "? ";
const badChecksum = mod && entry.sha1 && entry.sha1 !== mod.sha1 && "! ";
const warn = missing || badChecksum || "";
const enabled = entry.enabled ? "" : "(disabled) ";
print(` ${warn}${enabled}${name} ${entry.version}${entry.sha1 ? ` (${entry.sha1})` : ""}`);
}
} else if (field === "settings") {
print(`${field}:`);
for (let [scope, settings] of Object.entries(value)) {
print(` ${scope}:`);
for (let [setting, settingValue] of settings as any) {
print(` ${setting}: ${JSON.stringify(settingValue.value)}`);
}
}
} else if (field === "exportManifest") {
print(`${field}:`);
if (value && value.assets) {
print(" assets:");
for (let [name, fileName] of Object.entries(value.assets)) {
print(` ${name}: ${fileName}`);
}
}
} else {
print(`${field}: ${value}`);
}
}
}, },
})); }));
function printModPack(modPack: lib.ModPack, mods: Map<string, lib.ModInfo>) {
for (let [field, value] of Object.entries(modPack)) {
if (field === "mods") {
print(`${field}:`);
for (let [name, entry] of value) {
const mod = mods.get(`${name}_${entry.version}`);
const missing = !mod && "? ";
const badChecksum = mod && entry.sha1 && entry.sha1 !== mod.sha1 && "! ";
const warn = missing || badChecksum || "";
const enabled = entry.enabled ? "" : "(disabled) ";
print(` ${warn}${enabled}${name} ${entry.version}${entry.sha1 ? ` (${entry.sha1})` : ""}`);
}
} else if (field === "settings") {
print(`${field}:`);
for (let [scope, settings] of Object.entries(value)) {
print(` ${scope}:`);
for (let [setting, settingValue] of settings as any) {
print(` ${setting}: ${JSON.stringify(settingValue.value)}`);
}
}
} else if (field === "exportManifest") {
print(`${field}:`);
if (value && value.assets) {
print(" assets:");
for (let [name, fileName] of Object.entries(value.assets)) {
print(` ${name}: ${fileName}`);
}
}
} else {
print(`${field}: ${value}`);
}
}
}
modPackCommands.add(new lib.Command({ modPackCommands.add(new lib.Command({
definition: [["list", "l"], "List mod packs in the cluster"], definition: [["list", "l"], "List mod packs in the cluster"],
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let modPacks = await ctl.send(new lib.ModPackListRequest()); let modPacks = await ctl.send(new lib.ModPackListRequest());
let fields = ["id", "name", "factorioVersion"]; ctl.output.table(modPacks, modPack => pickFields(modPack, ["id", "name", "factorioVersion"]));
for (let entry of modPacks) {
for (let field of Object.keys(entry)) {
if (!fields.includes(field)) {
// @ts-expect-error terrible hack
delete entry[field];
}
}
}
print(asTable(modPacks));
}, },
})); }));
@@ -194,10 +184,17 @@ modPackCommands.add(new lib.Command({
setModPackModsEnabled(modPack, args.disabledMods, false); setModPackModsEnabled(modPack, args.disabledMods, false);
setModPackSettings(modPack, args); setModPackSettings(modPack, args);
await ctl.send(new lib.ModPackCreateRequest(modPack)); await ctl.send(new lib.ModPackCreateRequest(modPack));
print(`Created mod pack ${modPack.name} (${modPack.id})`); printCreated(ctl, modPack);
}, },
})); }));
function printCreated(ctl: Ctl, modPack: lib.ModPack) {
ctl.output.result(
{ id: modPack.id, name: modPack.name },
() => print(`Created mod pack ${modPack.name} (${modPack.id})`),
);
}
modPackCommands.add(new lib.Command({ modPackCommands.add(new lib.Command({
definition: ["import <string>", "Import mod pack string", (yargs) => { definition: ["import <string>", "Import mod pack string", (yargs) => {
yargs.positional("string", { describe: "Mod pack string to import", type: "string" }); yargs.positional("string", { describe: "Mod pack string to import", type: "string" });
@@ -205,7 +202,7 @@ modPackCommands.add(new lib.Command({
handler: async function(args: { string: string }, ctl: Ctl) { handler: async function(args: { string: string }, ctl: Ctl) {
const modPack = lib.ModPack.fromModPackString(args.string); const modPack = lib.ModPack.fromModPackString(args.string);
await ctl.send(new lib.ModPackCreateRequest(modPack)); await ctl.send(new lib.ModPackCreateRequest(modPack));
print(`Created mod pack ${modPack.name} (${modPack.id})`); printCreated(ctl, modPack);
}, },
})); }));
@@ -217,7 +214,8 @@ modPackCommands.add(new lib.Command({
const modPack = await ctl.send( const modPack = await ctl.send(
new lib.ModPackGetRequest(await lib.resolveModPack(ctl, args.modPack)) new lib.ModPackGetRequest(await lib.resolveModPack(ctl, args.modPack))
); );
print(modPack.toModPackString()); const modPackString = modPack.toModPackString();
ctl.output.result({ modPackString }, () => print(modPackString));
}, },
})); }));
+3 -7
View File
@@ -1,11 +1,7 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const noteCommands = new lib.CommandTree({ export const noteCommands = new lib.CommandTree({
name: "note", description: "Notes attached to hosts, instances, users and other resources", name: "note", description: "Notes attached to hosts, instances, users and other resources",
}); });
@@ -20,12 +16,12 @@ noteCommands.add(new lib.Command({
if (args.type !== undefined) { if (args.type !== undefined) {
notes = notes.filter(note => note.resourceType === args.type); notes = notes.filter(note => note.resourceType === args.type);
} }
print(asTable(notes.map(note => ({ ctl.output.table(notes, note => ({
type: note.resourceType, type: note.resourceType,
id: note.resourceId, id: note.resourceId,
updatedBy: note.updatedBy, updatedBy: note.updatedBy,
content: note.content, content: note.content,
})))); }));
}, },
})); }));
@@ -42,7 +38,7 @@ noteCommands.add(new lib.Command({
if (!note) { if (!note) {
throw new lib.CommandError(`No note for ${args.type} ${args.id}`); throw new lib.CommandError(`No note for ${args.type} ${args.id}`);
} }
print(note.content); ctl.output.result(note, () => print(note.content));
}, },
})); }));
+1 -7
View File
@@ -1,16 +1,10 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const permissionCommands = new lib.CommandTree({ name: "permission", description: "Permission inspection" }); export const permissionCommands = new lib.CommandTree({ name: "permission", description: "Permission inspection" });
permissionCommands.add(new lib.Command({ permissionCommands.add(new lib.Command({
definition: [["list", "l"], "List permissions in the cluster"], definition: [["list", "l"], "List permissions in the cluster"],
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let permissions = await ctl.send(new lib.PermissionListRequest()); ctl.output.table(await ctl.send(new lib.PermissionListRequest()));
print(asTable(permissions));
}, },
})); }));
+2 -7
View File
@@ -1,18 +1,13 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const roleCommands = new lib.CommandTree({ name: "role", description: "Role management" }); export const roleCommands = new lib.CommandTree({ name: "role", description: "Role management" });
roleCommands.add(new lib.Command({ roleCommands.add(new lib.Command({
definition: [["list", "l"], "List roles in the cluster"], definition: [["list", "l"], "List roles in the cluster"],
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let roles = await ctl.send(new lib.RoleListRequest()); ctl.output.table(await ctl.send(new lib.RoleListRequest()));
print(asTable(roles));
}, },
})); }));
@@ -30,7 +25,7 @@ roleCommands.add(new lib.Command({
args.description, args.description,
args.permissions, args.permissions,
)); ));
logger.info(`Created role ID ${id}`); ctl.output.result({ id }, () => print(`Created role ID ${id}`));
}, },
})); }));
+42 -51
View File
@@ -1,12 +1,13 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import asTableModule from "as-table";
import path from "path"; import path from "path";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js"; import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js"; import { asTable, print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " }); function printProperties(object: object) {
print(asTable(Object.entries(object).map(([property, value]) => ({ property, value }))));
}
export const userCommands = new lib.CommandTree({ name: "user", alias: ["u"], description: "User management" }); export const userCommands = new lib.CommandTree({ name: "user", alias: ["u"], description: "User management" });
userCommands.add(new lib.Command({ userCommands.add(new lib.Command({
@@ -17,30 +18,23 @@ userCommands.add(new lib.Command({
}); });
}], }],
handler: async function(args: { name: string, instanceStats: boolean }, ctl: Ctl) { handler: async function(args: { name: string, instanceStats: boolean }, ctl: Ctl) {
let user = await ctl.send(new lib.UserGetRequest(args.name)); const user = await ctl.send(new lib.UserGetRequest(args.name));
Object.assign(user, user.playerStats); const { playerStats, instanceStats, ...properties } = user;
// @ts-expect-error Terrible hack const instances = args.instanceStats && ctl.output.human
delete user.playerStats; ? await ctl.send(new lib.InstanceDetailsListRequest())
let instanceStats = user.instanceStats; : [];
// @ts-expect-error Terrible hack ctl.output.result(user, () => {
delete user.instanceStats; printProperties({ ...properties, ...playerStats });
print(asTable(Object.entries(user).map(([property, value]) => ({ property, value })))); if (!args.instanceStats) {
return;
if (args.instanceStats) {
let instances = await ctl.send(new lib.InstanceDetailsListRequest());
function instanceName(id: number) {
let instance = instances.find(i => i.id === id);
if (instance) {
return instance.name;
}
return "<deleted>";
} }
for (let [id, playerInstanceStats] of instanceStats || []) { for (let [id, playerInstanceStats] of instanceStats) {
const name = instances.find(i => i.id === id)?.name ?? "<deleted>";
print(); print();
print(`Instance ${instanceName(id)} (${id}):`); print(`Instance ${name} (${id}):`);
print(asTable(Object.entries(playerInstanceStats).map(([property, value]) => ({ property, value })))); printProperties(playerInstanceStats);
} }
} });
}, },
})); }));
@@ -52,29 +46,17 @@ userCommands.add(new lib.Command({
}); });
}], }],
handler: async function(args: { stats: boolean, attributes: boolean }, ctl: Ctl) { handler: async function(args: { stats: boolean, attributes: boolean }, ctl: Ctl) {
let users = await ctl.send(new lib.UserListRequest()); const users = await ctl.send(new lib.UserListRequest());
for (let user of users) { ctl.output.table(users, user => {
if (args.stats) { const {
Object.assign(user, user.playerStats); playerStats, isDeleted, banReason, instanceStats, isAdmin, isWhitelisted, isBanned, ...row
} } = user;
// @ts-expect-error Terrible hack return {
delete user.playerStats; ...row,
// @ts-expect-error Terrible hack ...args.attributes && { isAdmin, isWhitelisted, isBanned },
delete user.isDeleted; ...args.stats && playerStats,
// @ts-expect-error Terrible hack };
delete user.banReason; });
// @ts-expect-error Terrible hack
delete user.instanceStats;
if (!args.attributes) {
// @ts-expect-error Terrible hack
delete user.isAdmin;
// @ts-expect-error Terrible hack
delete user.isWhitelisted;
// @ts-expect-error Terrible hack
delete user.isBanned;
}
}
print(asTable(users));
}, },
})); }));
@@ -222,11 +204,17 @@ async function handleImportOrRestore(args: {
new lib.UserBulkImportRequest(importType, importType === "users" ? data.users : data, restore) new lib.UserBulkImportRequest(importType, importType === "users" ? data.users : data, restore)
); );
print(`${restore ? "Restored" : "Imported"} ${importType} from ${args.filepath}`); let backupFile: string | undefined;
if (restore && backup && !args.noBackup) { if (restore && backup && !args.noBackup) {
await fs.writeFile(`${importType}-backup.json`, JSON.stringify(backup, undefined, "\t")); backupFile = `${importType}-backup.json`;
print(`Wrote backup to ${importType}-backup.json`); await fs.writeFile(backupFile, JSON.stringify(backup, undefined, "\t"));
} }
ctl.output.result({ type: importType, backupFile }, () => {
print(`${restore ? "Restored" : "Imported"} ${importType} from ${args.filepath}`);
if (backupFile) {
print(`Wrote backup to ${backupFile}`);
}
});
} }
userCommands.add(new lib.Command({ userCommands.add(new lib.Command({
@@ -299,6 +287,9 @@ userCommands.add(new lib.Command({
await fs.writeFile(args.filepath, JSON.stringify(await ctl.send( await fs.writeFile(args.filepath, JSON.stringify(await ctl.send(
new lib.UserBulkExportRequest(exportType) new lib.UserBulkExportRequest(exportType)
), undefined, "\t")); ), undefined, "\t"));
print(`Exported ${exportType} to ${args.filepath}`); ctl.output.result(
{ type: exportType, filepath: args.filepath },
() => print(`Exported ${exportType} to ${args.filepath}`),
);
}, },
})); }));
+23
View File
@@ -1,4 +1,6 @@
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
export async function getEditor(argsEditor: string) { export async function getEditor(argsEditor: string) {
// eslint-disable-next-line // eslint-disable-next-line
@@ -53,3 +55,24 @@ export function serializedConfigToString(
} }
return allConfigElements; return allConfigElements;
} }
/** Print config fields as `name value` lines, or as an object in json mode */
export function printConfig(ctl: Ctl, config: lib.ConfigSchema, note = (name: string) => "") {
ctl.output.result(config, () => {
for (let [name, value] of Object.entries(config)) {
// Shown as an (inherited) note on each field by instance config list instead
if (name !== "instance.inherited_fields") {
print(`${name} ${JSON.stringify(value)}${note(name)}`);
}
}
});
}
/** Print whether a restart is still needed after an update or install */
export function printRestartRequired(ctl: Ctl, restartRequired: boolean, action: string) {
ctl.output.result({ restartRequired }, () => {
if (restartRequired) {
print(`${action}; a restart is required to apply the changes.`);
}
});
}
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/host", "name": "@clusterio/host",
"version": "2.1.0", "version": "2.1.1",
"description": "Implementation of Clusterio host server", "description": "Implementation of Clusterio host server",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+33
View File
@@ -1032,6 +1032,39 @@ end`.replace(/\r?\n/g, " ");
} }
await this.syncMods(); await this.syncMods();
this.checkRequiredMods(this.activeModPack);
}
/**
* Check that the mod pack has the mods required by the loaded plugins
*
* @throws {lib.ConflictError} if a required mod is missing, disabled or the wrong version.
*/
checkRequiredMods(modPack: lib.ModPack) {
const problems = [];
for (const pluginInfo of this.loadedPlugins) {
const unmet = [];
for (const dependency of lib.pluginRequiredMods(pluginInfo)) {
const mod = modPack.mods.get(dependency.name);
if (!mod) {
unmet.push(`${dependency.spec} (missing)`);
} else if (!mod.enabled) {
unmet.push(`${dependency.spec} (disabled)`);
} else if (dependency.version && !dependency.version.testVersion(mod.version)) {
unmet.push(`${dependency.spec} (found ${mod.version})`);
}
}
if (unmet.length) {
problems.push(`Plugin ${pluginInfo.name} requires ${unmet.join(", ")}.`);
}
}
if (problems.length) {
throw new lib.ConflictError(
`Mod pack ${modPack.name} does not have the mods required by the loaded plugins. ` +
`${problems.join(" ")} ` +
"Add the mods to the mod pack or set <plugin>.load_plugin to false for this instance."
);
}
} }
/** /**
+3
View File
@@ -297,9 +297,12 @@ export async function ensurePaired(
}, },
}); });
// The host has not started yet, so the new token needs no restart.
const restartRequired = config.restartRequired;
config.set("host.id", result.hostId); config.set("host.id", result.hostId);
config.set("host.name", result.name); config.set("host.name", result.name);
config.set("host.controller_token", result.token); config.set("host.controller_token", result.token);
config.restartRequired = restartRequired;
await config.save(); await config.save();
logger.info(`Host approved as "${result.name}" with id ${result.hostId}`); logger.info(`Host approved as "${result.name}" with id ${result.hostId}`);
} }
+42 -12
View File
@@ -1,6 +1,6 @@
// Factorio Server interface // Factorio Server interface
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import { type FSWatcher, createWriteStream, watch as fsWatch, writeFileSync } from "node:fs"; import { type FSWatcher, createWriteStream, existsSync, watch as fsWatch, writeFileSync } from "node:fs";
import child_process from "child_process"; import child_process from "child_process";
import dgram from "dgram"; import dgram from "dgram";
import path from "path"; import path from "path";
@@ -182,6 +182,15 @@ async function downloadAndExtractTar(url: string, targetDir: string) {
} }
} }
/**
* Factorio Linux build architecture matching this host
*
* @returns "arm64" on ARM64 Linux, otherwise "x64".
*/
function factorioArch(): lib.FactorioHeadlessArch {
return process.platform === "linux" && process.arch === "arm64" ? "arm64" : "x64";
}
// Factorio downloads in progress by target directory, shared by every server in the process // Factorio downloads in progress by target directory, shared by every server in the process
const downloadsInProgress = new Map<string, Promise<void>>(); const downloadsInProgress = new Map<string, Promise<void>>();
@@ -202,14 +211,27 @@ export async function checkForUpdates(
logger: lib.Logger = lib.logger, logger: lib.Logger = lib.logger,
) { ) {
const latestVersion = availableVersions const matchesTarget = (version: string) => (
.filter(v => ( targetVersion === "latest" || version === targetVersion || version.startsWith(targetVersion)
targetVersion === "latest" || v.version === targetVersion || v.version.startsWith(targetVersion) );
)) const arch = factorioArch();
const matching = availableVersions.filter(v => matchesTarget(v.version));
const latestVersion = matching
.filter(v => process.platform !== "linux" || lib.hasFactorioHeadless(v.version, "linux", arch))
.sort((v1, v2) => versionOrder(v1.version, v2.version)) .sort((v1, v2) => versionOrder(v1.version, v2.version))
[0]; [0];
if (!latestVersion) { if (!latestVersion) {
if (matching.length && arch === "arm64" && process.platform === "linux") {
const installed = await listFactorioVersions(factorioDir);
if (!installed.direct && ![...installed.versions].some(matchesTarget)) {
throw new Error(
`Factorio ${targetVersion} has no ARM64 Linux headless build to download, ` +
`ARM64 builds start at ${lib.minimumArm64HeadlessVersion}. Install it manually ` +
`or use version ${lib.minimumArm64HeadlessVersion} or later.`
);
}
}
return; return;
} }
@@ -242,7 +264,7 @@ export async function checkForUpdates(
logger.info(`A newer version of factorio is available (${v}), waiting for download...`); logger.info(`A newer version of factorio is available (${v}), waiting for download...`);
} else { } else {
logger.info(`A newer version of factorio is available (${v}), starting download...`); logger.info(`A newer version of factorio is available (${v}), starting download...`);
download = downloadAndExtractTar(latestVersion.headlessUrl, dst).finally(() => { download = downloadAndExtractTar(lib.factorioHeadlessUrl(v, "linux", arch), dst).finally(() => {
downloadsInProgress.delete(dst); downloadsInProgress.delete(dst);
}); });
downloadsInProgress.set(dst, download); downloadsInProgress.set(dst, download);
@@ -1155,9 +1177,8 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
} }
const newWhitelist = new Set(newWhitelistJson); const newWhitelist = new Set(newWhitelistJson);
// Set.difference added in node v22 and so we can not use this due to v20 support const added = [...newWhitelist.difference(this._whitelist)];
const added = newWhitelistJson.filter(e => !this._whitelist.has(e)); const removed = [...this._whitelist.difference(newWhitelist)];
const removed = [...this._whitelist.values()].filter(e => !newWhitelist.has(e));
this._whitelist = newWhitelist; this._whitelist = newWhitelist;
this.emit("whitelist-change", added, removed); this.emit("whitelist-change", added, removed);
@@ -1735,8 +1756,8 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
/** /**
* Get Factorio binary path * Get Factorio binary path
* *
* Get the path to the factorio binary depending on the configuration * Get the path to the factorio binary depending on the configuration,
* and or the platform (MacOS support) * the platform (MacOS support) and the bin/x64 or bin/arm64 dir present
* *
* @returns Path to factorio binary * @returns Path to factorio binary
*/ */
@@ -1747,7 +1768,16 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
if (process.platform === "darwin") { if (process.platform === "darwin") {
return this.dataPath("..", "MacOS", "factorio"); return this.dataPath("..", "MacOS", "factorio");
} }
return this.dataPath("..", "bin", "x64", "factorio"); // Prefer the native build, fall back to whichever one is installed
const arches = factorioArch() === "arm64" ? ["arm64", "x64"] : ["x64", "arm64"];
for (const arch of arches) {
const binary = this.dataPath("..", "bin", arch, "factorio");
// eslint-disable-next-line node/no-sync
if (existsSync(binary)) {
return binary;
}
}
return this.dataPath("..", "bin", arches[0], "factorio");
} }
/** /**
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/lib", "name": "@clusterio/lib",
"version": "2.1.0", "version": "2.1.1",
"description": "Shared library for Clusterio", "description": "Shared library for Clusterio",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+52 -2
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox"; import { Static, Type } from "@sinclair/typebox";
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js"; import { FullVersion, FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js";
const ARCHIVE_URL = "https://factorio.com/download/archive/"; const ARCHIVE_URL = "https://factorio.com/download/archive/";
@@ -9,11 +9,61 @@ const ARCHIVE_URL = "https://factorio.com/download/archive/";
export const ExternalFactorioVersionSchema = Type.Object({ export const ExternalFactorioVersionSchema = Type.Object({
stable: Type.Boolean(), stable: Type.Boolean(),
version: FullVersionSchema, version: FullVersionSchema,
/** @deprecated Linux x64 only, use {@link factorioHeadlessUrl} instead. */
headlessUrl: Type.String(), headlessUrl: Type.String(),
}); });
export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchema>; export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchema>;
/** Platforms Factorio may publish headless builds for, named as in Node's process.platform */
export type FactorioHeadlessPlatform = "linux" | "win32";
/** CPU architectures Factorio may publish headless builds for, named as in Node's process.arch */
export type FactorioHeadlessArch = "x64" | "arm64";
/** First Factorio version with an ARM64 Linux headless build */
export const minimumArm64HeadlessVersion = "2.1.18";
/**
* Check if Factorio publishes a headless build of a version
*
* @param version - Factorio version to check.
* @param platform - Platform of the build.
* @param arch - CPU architecture of the build.
* @returns true if the build can be downloaded.
*/
export function hasFactorioHeadless(
version: FullVersion,
platform: FactorioHeadlessPlatform,
arch: FactorioHeadlessArch,
) {
if (platform !== "linux") {
return false;
}
return arch === "x64" || integerFullVersion(version) >= integerFullVersion(minimumArm64HeadlessVersion);
}
/**
* Download URL of the headless build of a Factorio version
*
* @param version - Factorio version to download.
* @param platform - Platform of the build.
* @param arch - CPU architecture of the build.
* @returns URL redirecting to the archive.
* @throws Error if Factorio has no headless build for the platform and arch.
*/
export function factorioHeadlessUrl(
version: FullVersion,
platform: FactorioHeadlessPlatform,
arch: FactorioHeadlessArch,
) {
if (!hasFactorioHeadless(version, platform, arch)) {
throw new Error(`Factorio ${version} has no headless build for ${platform} ${arch}`);
}
const build = arch === "arm64" ? "linux-arm64" : "linux64";
return `https://www.factorio.com/get-download/${version}/headless/${build}`;
}
/** /**
* Fetch all factorio versions that support headless * Fetch all factorio versions that support headless
* *
@@ -60,7 +110,7 @@ function parseFactorioVersions(html: string): ExternalFactorioVersion[] {
versions.set(version, { versions.set(version, {
version: version, version: version,
stable: kind === "stable", stable: kind === "stable",
headlessUrl: `https://www.factorio.com/get-download/${version}/headless/linux64`, headlessUrl: factorioHeadlessUrl(version, "linux", "x64"),
}); });
} }
} }
+23
View File
@@ -9,6 +9,7 @@ import type { PermissionDefinition } from "./permissions.js";
import type { PlayerStats, Role } from "./data/index.js"; import type { PlayerStats, Role } from "./data/index.js";
import type { SyncHook } from "./SyncHook.js"; import type { SyncHook } from "./SyncHook.js";
import type { ErrorClass } from "./link/index.js"; import type { ErrorClass } from "./link/index.js";
import { ModDependency } from "./data/ModInfo.js";
export const PluginFeatureFlags = [ export const PluginFeatureFlags = [
/** The plugin requires module code to be patched into the save */ /** The plugin requires module code to be patched into the save */
@@ -41,6 +42,11 @@ export type PluginDeclaration = {
migrationEntrypoint?: string; migrationEntrypoint?: string;
features?: (typeof PluginFeatureFlags)[number][]; features?: (typeof PluginFeatureFlags)[number][];
/**
* Factorio mods the plugin needs in the instance's mod pack, as Factorio
* dependency strings like `"subspace_storage >= 2.3.0"`.
*/
requiredMods?: string[];
messages?: any[]; messages?: any[];
errors?: ErrorClass[]; errors?: ErrorClass[];
@@ -65,6 +71,23 @@ export function pluginNeedsWebBuild(info: PluginDeclaration) {
); );
} }
/**
* Parse the requiredMods of a plugin
*
* A mod listed more than once must satisfy every entry, which gives a range.
*
* @throws Error if an entry is invalid or has a dependency prefix.
*/
export function pluginRequiredMods(pluginInfo: PluginDeclaration) {
return (pluginInfo.requiredMods ?? []).map(spec => {
const dependency = new ModDependency(spec);
if (dependency.type !== "required") {
throw new Error(`requiredMods entry "${spec}" can not have a dependency prefix`);
}
return dependency;
});
}
export type PluginNodeEnvInfo = PluginDeclaration & { export type PluginNodeEnvInfo = PluginDeclaration & {
/** /**
* Path to the folder with the static files that should be hosted on the web * Path to the folder with the static files that should be hosted on the web
+6
View File
@@ -84,6 +84,12 @@ export async function loadPluginInfos(pluginList: Map<string, string>, skippedNp
); );
} }
try {
libPlugin.pluginRequiredMods(pluginInfo);
} catch (err: any) {
throw new libErrors.PluginError(pluginName, err);
}
// migrate: ignore incompatible old plugins // migrate: ignore incompatible old plugins
if (pluginInfo.messages && !(pluginInfo.messages instanceof Array)) { if (pluginInfo.messages && !(pluginInfo.messages instanceof Array)) {
logger.warn(`Ignoring incompatible pre alpha.14 plugin ${pluginName}`); logger.warn(`Ignoring incompatible pre alpha.14 plugin ${pluginName}`);
+13 -2
View File
@@ -13,6 +13,7 @@ import { InvalidFieldError, InvalidValueError } from "./errors.js";
import { logger } from "./logging.js"; import { logger } from "./logging.js";
import * as libHelpers from "./helpers.js"; import * as libHelpers from "./helpers.js";
import { LockFile } from "./LockFile.js"; import { LockFile } from "./LockFile.js";
import { LineSplitter } from "./stream.js";
import { pathToFileURL } from "url"; import { pathToFileURL } from "url";
@@ -88,11 +89,21 @@ function npmInstall(packageSpec: string) {
const npm = process.platform === "win32" ? "npm.cmd" : "npm"; const npm = process.platform === "win32" ? "npm.cmd" : "npm";
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const child = spawn(npm, ["install", "--save", "--no-audit", "--no-fund", packageSpec], { const child = spawn(npm, ["install", "--save", "--no-audit", "--no-fund", packageSpec], {
stdio: "inherit", stdio: ["ignore", "pipe", "pipe"],
shell: process.platform === "win32", shell: process.platform === "win32",
}); });
for (const [output, level] of [[child.stdout, "info"], [child.stderr, "warn"]] as const) {
const lines = new LineSplitter({ readableObjectMode: true });
lines.on("data", (line: Buffer) => {
const text = line.toString("utf8").trimEnd();
if (text) {
logger.log(level, text);
}
});
output.pipe(lines);
}
child.on("error", reject); child.on("error", reject);
child.on("exit", code => { child.on("close", code => {
if (code === 0) { if (code === 0) {
resolve(); resolve();
} else { } else {
+2
View File
@@ -4,5 +4,7 @@
"exclude": ["src/**/*.browser.ts"], "exclude": ["src/**/*.browser.ts"],
"compilerOptions": { "compilerOptions": {
"allowJs": true, "allowJs": true,
// SubscriptionFilters.union clashes with the native Set.union
"lib": ["es2024"],
}, },
} }
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/web_ui", "name": "@clusterio/web_ui",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio web interface implementation", "description": "Clusterio web interface implementation",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser", "author": "Hornwitser",
@@ -1,5 +1,5 @@
import React, {useContext, useState} from "react"; import React, {useContext, useState} from "react";
import { Descriptions, Flex, Popconfirm, Space, Tooltip, Typography } from "antd"; import { Descriptions, Flex, Modal, Popconfirm, Space, Tooltip, Typography } from "antd";
import { ExclamationCircleOutlined } from "@ant-design/icons"; import { ExclamationCircleOutlined } from "@ant-design/icons";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
@@ -28,6 +28,7 @@ type ControllerControlButtonProps = { canRestart?: boolean, restartRequired?: bo
function ControllerControlButton({ canRestart, restartRequired }: ControllerControlButtonProps) { function ControllerControlButton({ canRestart, restartRequired }: ControllerControlButtonProps) {
const account = useAccount(); const account = useAccount();
const control = useContext(ControlContext); const control = useContext(ControlContext);
const [modal, modalContext] = Modal.useModal();
const actions: VariableDropdownButtonProps["actions"] = []; const actions: VariableDropdownButtonProps["actions"] = [];
if (account.hasPermission("core.controller.restart") && canRestart) { if (account.hasPermission("core.controller.restart") && canRestart) {
@@ -40,9 +41,18 @@ function ControllerControlButton({ canRestart, restartRequired }: ControllerCont
</Space></Tooltip> </Space></Tooltip>
), ),
onClick: () => { onClick: () => {
control.send( modal.confirm({
new lib.ControllerRestartRequest() title: "Restart the controller?",
).catch(notifyErrorHandler("Error restarting controller")); content: "Hosts and this page lose their connection until the controller is back.",
okText: "Restart",
okButtonProps: { danger: true },
autoFocusButton: "cancel",
onOk: () => {
control.send(
new lib.ControllerRestartRequest()
).catch(notifyErrorHandler("Error restarting controller"));
},
});
}, },
}); });
} }
@@ -87,7 +97,10 @@ function ControllerControlButton({ canRestart, restartRequired }: ControllerCont
}); });
} }
return <VariableDropdownButton actions={actions} />; return <>
<VariableDropdownButton actions={actions} />
{modalContext}
</>;
} }
@@ -1,5 +1,5 @@
import React, { useContext, useState } from "react"; import React, { useContext, useState } from "react";
import { Descriptions, Typography, Button, Space, Popconfirm, Flex, Tooltip } from "antd"; import { Descriptions, Typography, Button, Space, Popconfirm, Flex, Tooltip, Modal } from "antd";
import { ExclamationCircleOutlined } from "@ant-design/icons"; import { ExclamationCircleOutlined } from "@ant-design/icons";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
@@ -29,11 +29,17 @@ import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableD
const { Title } = Typography; const { Title } = Typography;
type HostControlButtonProps = { hostId: number, canRestart?: boolean, restartRequired?: boolean }; type HostControlButtonProps = {
hostId: number,
instances: ReadonlyMap<number, lib.InstanceDetails>,
canRestart?: boolean,
restartRequired?: boolean,
};
function HostControlButton({ hostId, canRestart, restartRequired }: HostControlButtonProps) { function HostControlButton({ hostId, instances, canRestart, restartRequired }: HostControlButtonProps) {
const account = useAccount(); const account = useAccount();
const control = useContext(ControlContext); const control = useContext(ControlContext);
const [modal, modalContext] = Modal.useModal();
const actions: VariableDropdownButtonProps["actions"] = []; const actions: VariableDropdownButtonProps["actions"] = [];
if (account.hasPermission("core.host.restart") && canRestart) { if (account.hasPermission("core.host.restart") && canRestart) {
@@ -46,10 +52,28 @@ function HostControlButton({ hostId, canRestart, restartRequired }: HostControlB
</Space></Tooltip> </Space></Tooltip>
), ),
onClick: () => { onClick: () => {
control.sendTo( const running = [...instances.values()]
{ hostId }, .filter(instance => !["unknown", "unassigned", "stopped", "deleted"].includes(instance.status))
new lib.HostRestartRequest() .map(instance => instance.name);
).catch(notifyErrorHandler("Error restarting host")); modal.confirm({
title: "Restart the host?",
content: running.length
? <>
This stops {running.length} running {running.length === 1 ? "instance" : "instances"}:
{" "}{running.join(", ")}.<br />
They stay stopped after the restart unless instance.auto_start is enabled.
</>
: "No instances are running on this host.",
okText: "Restart",
okButtonProps: { danger: true },
autoFocusButton: "cancel",
onOk: () => {
control.sendTo(
{ hostId },
new lib.HostRestartRequest()
).catch(notifyErrorHandler("Error restarting host"));
},
});
}, },
}); });
} }
@@ -96,7 +120,10 @@ function HostControlButton({ hostId, canRestart, restartRequired }: HostControlB
}); });
} }
return <VariableDropdownButton actions={actions} />; return <>
<VariableDropdownButton actions={actions} />
{modalContext}
</>;
} }
type RevokeHostButtonProps = { host: lib.HostDetails, instances: ReadonlyMap<number, lib.InstanceDetails> }; type RevokeHostButtonProps = { host: lib.HostDetails, instances: ReadonlyMap<number, lib.InstanceDetails> };
@@ -146,7 +173,12 @@ export default function HostViewPage() {
} }
let hostButtons = <Space wrap> let hostButtons = <Space wrap>
<HostControlButton hostId={hostId} canRestart={system?.canRestart} restartRequired={system?.restartRequired}/> <HostControlButton
hostId={hostId}
instances={hostInstances}
canRestart={system?.canRestart}
restartRequired={system?.restartRequired}
/>
{account.hasPermission("core.host.revoke") && <RevokeHostButton host={host} instances={hostInstances} />} {account.hasPermission("core.host.revoke") && <RevokeHostButton host={host} instances={hostInstances} />}
{canShowNpm && ( {canShowNpm && (
<NpmButton target={{ hostId }} canRestart={system?.canRestart} disabled={!host["connected"]}/> <NpmButton target={{ hostId }} canRestart={system?.canRestart} disabled={!host["connected"]}/>
@@ -181,7 +181,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
function confirmDelete() { function confirmDelete() {
Modal.confirm({ Modal.confirm({
autoFocusButton: "cancel", autoFocusButton: "cancel",
content: "Permamently delete instance and server saves?", content: "Permanently delete instance and server saves?",
okText: "Delete", okText: "Delete",
okButtonProps: { danger: true }, okButtonProps: { danger: true },
onOk: () => { onOk: () => {
+24 -17
View File
@@ -11,7 +11,7 @@ import * as lib from "@clusterio/lib";
import { useAccount } from "../model/account"; import { useAccount } from "../model/account";
import { useMods } from "../model/mods"; import { useMods } from "../model/mods";
import { useModPacks } from "../model/mod_pack"; import { useDefaultModPack, useModPacks } from "../model/mod_pack";
import { notifyErrorHandler } from "../util/notify"; import { notifyErrorHandler } from "../util/notify";
import ControlContext from "./ControlContext"; import ControlContext from "./ControlContext";
import PageHeader from "./PageHeader"; import PageHeader from "./PageHeader";
@@ -143,6 +143,16 @@ function CreateModPackButton() {
</>; </>;
} }
function defaultSearchVersion(modPack?: lib.ModPack): lib.MajorMinorVersion {
if (modPack) {
const version = lib.normaliseMajorMinorVersion(modPack.factorioVersion);
if ((lib.ApiVersions as readonly string[]).includes(version)) {
return version;
}
}
return lib.ApiVersions[lib.ApiVersions.length - 1];
}
function SearchModsButton() { function SearchModsButton() {
const control = useContext(ControlContext); const control = useContext(ControlContext);
const account = useAccount(); const account = useAccount();
@@ -150,7 +160,9 @@ function SearchModsButton() {
const [form] = Form.useForm(); const [form] = Form.useForm();
const nameInputRef = useRef<InputRef>(null); const nameInputRef = useRef<InputRef>(null);
const [searchText, setSearchText] = useState(""); const [searchText, setSearchText] = useState("");
const [factorioVersion, setFactorioVersion] = useState<lib.MajorMinorVersion>("2.0"); const defaultModPack = useDefaultModPack();
const [pickedVersion, setPickedVersion] = useState<lib.MajorMinorVersion>();
const factorioVersion = pickedVersion ?? defaultSearchVersion(defaultModPack);
// State for all mods fetched from backend // State for all mods fetched from backend
const [allMods, setAllMods] = useState<ModPortalModType[]>([]); const [allMods, setAllMods] = useState<ModPortalModType[]>([]);
@@ -274,22 +286,20 @@ function SearchModsButton() {
return filtered.length; return filtered.length;
}, [allMods, searchText]); }, [allMods, searchText]);
// Update search text and factorio version from form // Update search text from form
const handleSearch = (changedValues: any, allValues: any) => { const handleSearch = (changedValues: any, allValues: any) => {
const nameValue = allValues.name; const nameValue = allValues.name;
const versionValue = allValues.factorioVersion as lib.MajorMinorVersion | undefined;
// Only trigger state updates if values actually changed
if (nameValue !== searchText) { if (nameValue !== searchText) {
setSearchText(nameValue || ""); setSearchText(nameValue || "");
setPage(1);
} }
if (versionValue && versionValue !== factorioVersion) { };
setFactorioVersion(versionValue);
const handleVersionChange = (version: lib.MajorMinorVersion) => {
if (version !== factorioVersion) {
setPickedVersion(version);
setAllMods([]); // Clear mods when version changes, useEffect will fetch new ones setAllMods([]); // Clear mods when version changes, useEffect will fetch new ones
setLoading(true); // Show loading indicator immediately setLoading(true); // Show loading indicator immediately
}
// Reset page only if search text or version changed
if (nameValue !== searchText || (versionValue && versionValue !== factorioVersion)) {
setPage(1); setPage(1);
} }
}; };
@@ -349,16 +359,13 @@ function SearchModsButton() {
labelCol={{ span: 6 }} labelCol={{ span: 6 }}
wrapperCol={{ span: 18 }} wrapperCol={{ span: 18 }}
onValuesChange={handleSearch} onValuesChange={handleSearch}
initialValues={{ factorioVersion: factorioVersion, name: searchText }} initialValues={{ name: searchText }}
> >
<Form.Item name="name" label="Filter by Name or Title"> <Form.Item name="name" label="Filter by Name or Title">
<Input ref={nameInputRef} placeholder="Start typing to filter..." /> <Input ref={nameInputRef} placeholder="Start typing to filter..." />
</Form.Item> </Form.Item>
<Form.Item <Form.Item label="Factorio Version">
name="factorioVersion" <Select value={factorioVersion} onChange={handleVersionChange}>
label="Factorio Version"
>
<Select>
{lib.ApiVersions.map(version => ( {lib.ApiVersions.map(version => (
<Select.Option key={version} value={version}> <Select.Option key={version} value={version}>
{version} {version}
@@ -172,10 +172,12 @@ export function PluginVersions(props: { status: PluginStatus, latest?: string })
{versions.map(version => { {versions.map(version => {
const now = count(machine => machine.running === version); const now = count(machine => machine.running === version);
const after = count(machine => machine.afterRestart === version); const after = count(machine => machine.afterRestart === version);
return <div key={version}> return <div key={version} className="plugin-version">
<Text>{version}</Text> <span>
{props.latest === version && <Tag color="success" style={{ marginLeft: 6 }}>latest</Tag>} <Text>{version}</Text>
<Text type="secondary" style={{ marginLeft: 12 }}> {props.latest === version && <Tag color="success" style={{ marginLeft: 6 }}>latest</Tag>}
</span>
<Text type="secondary">
{plural(now, "machine")}{now !== after && ` → ${after} after restart`} {plural(now, "machine")}{now !== after && ` → ${after} after restart`}
</Text> </Text>
</div>; </div>;
+36 -22
View File
@@ -1,5 +1,5 @@
import React, { useContext, useEffect, useState } from "react"; import React, { useContext, useEffect, useState } from "react";
import { Space, Table, Tooltip, Typography } from "antd"; import { Grid, Space, Table, Tooltip, Typography } from "antd";
import CloseCircleFilled from "@ant-design/icons/CloseCircleFilled"; import CloseCircleFilled from "@ant-design/icons/CloseCircleFilled";
import type { PluginWebApi } from "@clusterio/lib"; import type { PluginWebApi } from "@clusterio/lib";
@@ -48,6 +48,8 @@ function WebError(props: { error: string }) {
export default function PluginsPage() { export default function PluginsPage() {
const control = useContext(ControlContext); const control = useContext(ControlContext);
const account = useAccount(); const account = useAccount();
const screens = Grid.useBreakpoint();
const isMobile = screens.md === false;
let [pluginList, setPluginList] = useState<PluginWebApi[]>([]); let [pluginList, setPluginList] = useState<PluginWebApi[]>([]);
const tableState = useTableQueryState<PluginRow>({ const tableState = useTableQueryState<PluginRow>({
namespace: "plugin", defaultSortKey: "name", namespace: "plugin", defaultSortKey: "name",
@@ -96,6 +98,24 @@ export default function PluginsPage() {
const latestOf = (plugin: PluginRow) => ( const latestOf = (plugin: PluginRow) => (
plugin.status?.npmPackage ? latestVersions[plugin.status.npmPackage] : undefined plugin.status?.npmPackage ? latestVersions[plugin.status.npmPackage] : undefined
); );
const versions = (plugin: PluginRow) => <>
{plugin.status
? <PluginVersions status={plugin.status} latest={latestOf(plugin)} />
: plugin.meta?.version}
{plugin.webError && <WebError error={plugin.webError} />}
</>;
const updateAction = (plugin: PluginRow) => (plugin.status
// Keep clicks on the update action from opening the plugin.
? <span onClick={event => { event.stopPropagation(); }}>
<PluginUpdateButton
status={plugin.status}
title={plugin.title}
latest={latestOf(plugin)}
installer={installer}
/>
</span>
: null
);
return <PageLayout nav={[{ name: "Plugins" }]}> return <PageLayout nav={[{ name: "Plugins" }]}>
<PageHeader <PageHeader
@@ -115,14 +135,22 @@ export default function PluginsPage() {
</Space>} </Space>}
/> />
<Table <Table
className="plugins-table"
tableLayout={isMobile ? "fixed" : "auto"}
columns={[ columns={[
{ {
title: "Name", title: "Name",
key: "name", key: "name",
className: "table-link-cell", className: "table-link-cell",
render: (_, plugin) => <Link to={pluginPath(plugin)} style={{ color: "inherit" }}> render: (_, plugin) => <>
{plugin.title} <Link to={pluginPath(plugin)} style={{ color: "inherit" }}>
</Link>, {plugin.title}
</Link>
{isMobile && <div className="plugin-mobile-details">
<div>{versions(plugin)}</div>
{updateAction(plugin)}
</div>}
</>,
sorter: (a, b) => strcmp(a.title, b.title), sorter: (a, b) => strcmp(a.title, b.title),
sortOrder: tableState.sortOrder("name"), sortOrder: tableState.sortOrder("name"),
filteredValue: tableState.filteredValue("name"), filteredValue: tableState.filteredValue("name"),
@@ -131,28 +159,14 @@ export default function PluginsPage() {
{ {
title: "Versions", title: "Versions",
key: "versions", key: "versions",
render: (_, plugin) => <> responsive: ["md"],
{plugin.status render: (_, plugin) => versions(plugin),
? <PluginVersions status={plugin.status} latest={latestOf(plugin)} />
: plugin.meta?.version}
{plugin.webError && <WebError error={plugin.webError} />}
</>,
}, },
{ {
key: "actions", key: "actions",
align: "right", align: "right",
render: (_, plugin) => (plugin.status responsive: ["md"],
// Keep clicks on the update action from opening the plugin render: (_, plugin) => updateAction(plugin),
? <span onClick={event => { event.stopPropagation(); }}>
<PluginUpdateButton
status={plugin.status}
title={plugin.title}
latest={latestOf(plugin)}
installer={installer}
/>
</span>
: null
),
}, },
]} ]}
dataSource={tableContents} dataSource={tableContents}
@@ -169,8 +169,10 @@ export default function SiteLayout() {
/>; />;
let accountMenu = <Dropdown menu={accountMenuProps} trigger={["click"]} placement="bottomRight"> let accountMenu = <Dropdown menu={accountMenuProps} trigger={["click"]} placement="bottomRight">
<Button type="text" icon={<UserOutlined />} style={{ marginInlineStart: "auto" }}> <Button type="text" icon={<UserOutlined />} style={{ marginInlineStart: "auto" }}
{account.name} aria-label={`Account menu for ${account.name}`}
>
<span className="site-layout-account-name">{account.name}</span>
</Button> </Button>
</Dropdown>; </Dropdown>;
@@ -228,7 +230,7 @@ export default function SiteLayout() {
<Link to="/" style={{ display: "flex", alignItems: "center" }}> <Link to="/" style={{ display: "flex", alignItems: "center" }}>
<img src={logo} width={56} height={56} alt="Clusterio logo" /> <img src={logo} width={56} height={56} alt="Clusterio logo" />
</Link> </Link>
<Flex vertical justify="center"> <Flex vertical justify="center" className="site-layout-brand">
<Typography.Title level={4} style={{ margin: 0, lineHeight: 1.2 }}>Clusterio</Typography.Title> <Typography.Title level={4} style={{ margin: 0, lineHeight: 1.2 }}>Clusterio</Typography.Title>
<Tooltip title="View changelog" placement="right"> <Tooltip title="View changelog" placement="right">
<Typography.Text <Typography.Text
+32
View File
@@ -72,6 +72,38 @@ body {
background: #001529; background: #001529;
} }
.site-layout-brand {
flex-shrink: 0;
white-space: nowrap;
}
@media (max-width: 767px) {
.site-layout-account-name {
display: none;
}
.plugins-table .table-link-cell > a {
font-weight: 600;
overflow-wrap: anywhere;
}
}
.plugin-mobile-details {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
margin-top: 8px;
overflow-wrap: anywhere;
}
.plugin-version {
display: flex;
flex-wrap: wrap;
align-items: baseline;
column-gap: 12px;
}
/* Keep the sidebar fixed in place (below the header) while content scrolls. */ /* Keep the sidebar fixed in place (below the header) while content scrolls. */
.site-layout-sider { .site-layout-sider {
position: sticky; position: sticky;
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/plugin-global_chat", "name": "@clusterio/plugin-global_chat",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio plugin forwarding between Factorio servers", "description": "Clusterio plugin forwarding between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/plugin-player_auth", "name": "@clusterio/plugin-player_auth",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio plugin authenticating logged in players to the web interface", "description": "Clusterio plugin authenticating logged in players to the web interface",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+1 -1
View File
@@ -6,7 +6,7 @@ As a player, it mostly just works. Most important to know is that factorio data
A note on crashing servers: A note on crashing servers:
When a server crashes while you are online there won't be time to immediately upload the inventory. Instead, the inventory is uploaded as soon as the server restarts. This means if you have been playing on a different server from the one that crashed and gathered/consumed items but are offline at the time of the first server coming online again your inventory will reset to the autosave. When a server crashes while you are online there won't be time to immediately upload the inventory. Instead, the inventory is uploaded as soon as the server restarts. If you have been playing on a different server in the meantime the controller keeps what that server uploaded, and the crashed server downloads it the next time you join it.
## Installation ## Installation
+1
View File
@@ -258,6 +258,7 @@ export default async function(context: ControllerPluginContext) {
prefetch(instanceId, request.destinationInstanceId, merged); prefetch(instanceId, request.destinationInstanceId, merged);
} }
} }
return new msg.UploadRequest.Response(store);
}); });
controller.handle(msg.DownloadRequest, async (request: msg.DownloadRequest) => { controller.handle(msg.DownloadRequest, async (request: msg.DownloadRequest) => {
+51 -26
View File
@@ -30,14 +30,20 @@ playerSyncCommands.add(new lib.Command({
handler: async function(args: object, ctl: Ctl) { handler: async function(args: object, ctl: Ctl) {
let players = await ctl.send(new msg.ListPlayersRequest()); let players = await ctl.send(new msg.ListPlayersRequest());
players.sort((a, b) => a.name.localeCompare(b.name)); players.sort((a, b) => a.name.localeCompare(b.name));
print("name | generation | size | acquired by"); if (!ctl.output.human) {
for (let player of players) { ctl.output.result(players);
print( return;
`${player.name} | ${player.generation ?? "-"} | ` +
`${player.size === undefined ? "-" : lib.formatBytes(player.size)} | ` +
`${await instanceName(ctl, player.instanceId)}`
);
} }
let rows = [];
for (let player of players) {
rows.push({
name: player.name,
generation: player.generation ?? "-",
size: player.size === undefined ? "-" : lib.formatBytes(player.size),
"acquired by": await instanceName(ctl, player.instanceId),
});
}
ctl.output.table(rows);
}, },
})); }));
@@ -47,24 +53,37 @@ playerSyncCommands.add(new lib.Command({
}], }],
handler: async function(args: { player: string }, ctl: Ctl) { handler: async function(args: { player: string }, ctl: Ctl) {
let response = await ctl.send(new msg.GetPlayerDataRequest(args.player)); let response = await ctl.send(new msg.GetPlayerDataRequest(args.player));
print(`acquired by: ${await instanceName(ctl, response.instanceId)}`);
let playerData = response.playerData; let playerData = response.playerData;
if (!playerData) { const summary = {
print(`No player data stored for ${args.player}`); instanceId: response.instanceId,
return; ...playerData && {
} generation: playerData.generation,
print(`generation: ${playerData.generation}`); size: JSON.stringify(playerData).length,
print(`size: ${lib.formatBytes(JSON.stringify(playerData).length)}`); controller: playerData.controller,
print(`controller: ${playerData.controller}`); force: playerData.force,
print(`force: ${playerData.force}`); inventories: summarizePlayerInventories(playerData),
for (let inventory of summarizePlayerInventories(playerData)) { },
print(`${inventory.name}:`); };
for (let item of inventory.items) { const acquiredBy = ctl.output.human ? await instanceName(ctl, response.instanceId) : "";
let quality = item.quality ? ` (${item.quality})` : ""; ctl.output.result(summary, () => {
let size = item.exportSize ? ` ${lib.formatBytes(item.exportSize)}` : ""; print(`acquired by: ${acquiredBy}`);
print(` ${item.count} ${item.name}${quality}${size}`); if (!playerData) {
print(`No player data stored for ${args.player}`);
return;
} }
} print(`generation: ${summary.generation}`);
print(`size: ${lib.formatBytes(summary.size!)}`);
print(`controller: ${summary.controller}`);
print(`force: ${summary.force}`);
for (let inventory of summary.inventories!) {
print(`${inventory.name}:`);
for (let item of inventory.items) {
let quality = item.quality ? ` (${item.quality})` : "";
let size = item.exportSize ? ` ${lib.formatBytes(item.exportSize)}` : "";
print(` ${item.count} ${item.name}${quality}${size}`);
}
}
});
}, },
})); }));
@@ -81,9 +100,12 @@ playerSyncCommands.add(new lib.Command({
let content = JSON.stringify(response.playerData, null, "\t"); let content = JSON.stringify(response.playerData, null, "\t");
if (args.file) { if (args.file) {
await fs.writeFile(args.file, content); await fs.writeFile(args.file, content);
print(`Wrote player data for ${args.player} to ${args.file}`); ctl.output.result(
{ file: args.file },
() => print(`Wrote player data for ${args.player} to ${args.file}`),
);
} else { } else {
print(content); ctl.output.result(response.playerData, () => print(content));
} }
}, },
})); }));
@@ -101,7 +123,10 @@ playerSyncCommands.add(new lib.Command({
playerData.name = args.player; playerData.name = args.player;
playerData.generation ??= 0; playerData.generation ??= 0;
let response = await ctl.send(new msg.SetPlayerDataRequest(args.player, playerData)); let response = await ctl.send(new msg.SetPlayerDataRequest(args.player, playerData));
print(`Stored player data for ${args.player} as generation ${response.generation}`); ctl.output.result(
{ generation: response.generation },
() => print(`Stored player data for ${args.player} as generation ${response.generation}`),
);
}, },
})); }));
+6 -3
View File
@@ -200,11 +200,12 @@ export default async function(context: InstancePluginContext) {
delete player_data.destination_instance_id; delete player_data.destination_instance_id;
logger.verbose(`Uploading ${player_data.name} (${JSON.stringify(player_data).length / 1000}kB)`); logger.verbose(`Uploading ${player_data.name} (${JSON.stringify(player_data).length / 1000}kB)`);
let stored: boolean;
try { try {
await instance.sendTo( ({ stored } = await instance.sendTo(
"controller", "controller",
new UploadRequest(instance.id, player_data.name, player_data, destinationInstanceId), new UploadRequest(instance.id, player_data.name, player_data, destinationInstanceId),
); ));
} catch (err: any) { } catch (err: any) {
if (!(err instanceof lib.SessionLost)) { if (!(err instanceof lib.SessionLost)) {
@@ -213,8 +214,10 @@ export default async function(context: InstancePluginContext) {
return; return;
} }
// A copy the controller did not store must not pass for the stored one: the save downloads at the next join
const reply = stored ? "confirm_upload" : "reject_upload";
await instance.sendRcon( await instance.sendRcon(
`/sc player_sync.confirm_upload("${player_data.name}", ${player_data.generation})`, true, plugin.name `/sc player_sync.${reply}("${player_data.name}", ${player_data.generation})`, true, plugin.name
); );
} }
+18
View File
@@ -262,12 +262,30 @@ export class ReleaseRequest {
} }
} }
export class UploadResponse {
constructor(
/** False when the controller kept what it has: another copy is newer, locked or from another epoch */
public stored: boolean,
) {
}
// No type: a controller from before this response replies with nothing, which counts as stored
static jsonSchema = Type.Unsafe<{ stored: boolean } | undefined>({
properties: { "stored": { type: "boolean" } },
});
static fromJSON(json: Static<typeof UploadResponse.jsonSchema>) {
return new this(json?.stored ?? true);
}
}
export class UploadRequest { export class UploadRequest {
declare ["constructor"]: typeof UploadRequest; declare ["constructor"]: typeof UploadRequest;
static type = "request" as const; static type = "request" as const;
static src = "instance" as const; static src = "instance" as const;
static dst = "controller" as const; static dst = "controller" as const;
static plugin = "player_sync" as const; static plugin = "player_sync" as const;
static Response = UploadResponse;
constructor( constructor(
public instanceId: number, public instanceId: number,
+52 -1
View File
@@ -384,12 +384,14 @@ function player_sync.check_players_in_cutscene()
end end
-- Whether a join should take the stored data: there is some, and this save hasn't synced the player in that epoch at -- Whether a join should take the stored data: there is some, and this save hasn't synced the player in that epoch at
-- that generation or newer with every component synced here now. A player without a record here has nothing to keep. -- that generation or newer with every component synced here now, or the controller did not store its last upload
-- (reject_upload). A player without a record here has nothing to keep.
local function download_needed(player_record, acquire_response, script_data) local function download_needed(player_record, acquire_response, script_data)
return acquire_response.has_data return acquire_response.has_data
and ( and (
not player_record not player_record
or not player_record.sync or not player_record.sync
or player_record.outdated
or (player_record.epoch or 0) ~= (acquire_response.epoch or 0) or (player_record.epoch or 0) ~= (acquire_response.epoch or 0)
or acquire_response.generation > player_record.generation or acquire_response.generation > player_record.generation
or serialize.newly_synced(player_record.components, script_data.components) or serialize.newly_synced(player_record.components, script_data.components)
@@ -543,6 +545,8 @@ function player_sync.sync_player(acquire_response)
start_sync(player_record, script_data.components) start_sync(player_record, script_data.components)
player_record.dirty = true player_record.dirty = true
player_record.sync_in_progress = false player_record.sync_in_progress = false
-- Nothing stored to download: the copy here is all there is
player_record.outdated = nil
-- Deleted data keeps its generation and epoch so the next upload is accepted as the new baseline -- Deleted data keeps its generation and epoch so the next upload is accepted as the new baseline
player_record.epoch = epoch player_record.epoch = epoch
-- A copy from another epoch has nothing to do with the stored generation -- A copy from another epoch has nothing to do with the stored generation
@@ -573,6 +577,7 @@ function player_sync.finish_download(player, finished_record)
player_record.dirty = true player_record.dirty = true
player_record.sync_in_progress = false player_record.sync_in_progress = false
player_record.outdated = nil
start_sync(player_record, finished_record.components) start_sync(player_record, finished_record.components)
player_record.generation = finished_record.generation player_record.generation = finished_record.generation
player_record.epoch = finished_record.epoch player_record.epoch = finished_record.epoch
@@ -634,6 +639,7 @@ player_sync.on_nth_tick[33] = function(event)
player_sync.check_players_in_cutscene() player_sync.check_players_in_cutscene()
player_sync.check_active_downloads() player_sync.check_active_downloads()
player_sync.check_active_uploads() player_sync.check_active_uploads()
player_sync.check_missing_quick_bar_records()
end end
player_sync.events[defines.events.on_cutscene_waypoint_reached] = function(event) player_sync.events[defines.events.on_cutscene_waypoint_reached] = function(event)
@@ -724,6 +730,32 @@ function player_sync.confirm_upload(player_name, generation)
player_record.dirty = false player_record.dirty = false
end end
-- Invoked by the instance code when the controller did not store the uploaded data: it holds another copy that is
-- newer, locked by another instance or from another epoch. This save's copy lost, even at the same generation, so it
-- is not uploaded again and the next join downloads. A retry of an upload the controller already stored ends up here
-- too, and then downloads what this save sent.
function player_sync.reject_upload(player_name, generation)
local player = game.get_player(player_name)
if not player then
return
end
local script_data = get_script_data()
-- A player who is back already still waits for the acquire reply, which the controller sent after this one, and
-- that join then downloads. Past that reply the join has decided and this is not about the copy here any more.
if player.connected and not script_data.players_waiting_for_acquire[player_name] then
return
end
local player_record = script_data.players[player.name]
if not player_record or player_record.generation ~= generation then
return
end
log("Upload of " .. player_name .. " was not stored, the next join downloads")
script_data.active_uploads[player_name] = nil
player_record.dirty = false
player_record.outdated = true
end
function player_sync.initiate_inventory_download(player, player_record, generation, epoch) function player_sync.initiate_inventory_download(player, player_record, generation, epoch)
player.print("Initiating inventory download...") player.print("Initiating inventory download...")
local record = { local record = {
@@ -781,6 +813,25 @@ function player_sync.check_active_downloads()
end end
end end
-- Quick bar slots pointing into the blueprint library are set once the save has the player's library
function player_sync.check_missing_quick_bar_records()
local script_data = get_script_data()
-- Not once the quick bar stopped being synced here. The entries stay for the upload, which goes by the
-- components of the player's session.
if not serialize.syncs(script_data.components, "quick_bar") then
return
end
for player_name, failed in pairs(script_data.failed_deserialization) do
local player = failed.quick_bar and game.get_player(player_name)
if player and player.connected then
failed.quick_bar = serialize.restore_missing_quick_bar_records(player, failed.quick_bar)
if not next(failed) then
script_data.failed_deserialization[player_name] = nil
end
end
end
end
function player_sync.check_active_uploads() function player_sync.check_active_uploads()
local script_data = get_script_data() local script_data = get_script_data()
for player_name, record in pairs(script_data.active_uploads) do for player_name, record in pairs(script_data.active_uploads) do
+75 -15
View File
@@ -494,6 +494,27 @@ local function find_item_path(inventory, item_number)
return nil return nil
end end
--- The record a quick bar slot entry points at, if the player's library has it at that path
--- @param player LuaPlayer
--- @param entry QuickBarSlotEncoded.record
--- @return LuaRecord?
local function find_record(player, entry)
-- The record must still be what was stored, a shifted library must not set the wrong one
local records = player.blueprints
local record --[[@as LuaRecord?]]
for _, index in ipairs(entry.p) do
record = records[index]
if not (record and record.valid) then
return nil
end
records = record.type == "blueprint-book" and record.contents or {}
end
if record and record.type == entry.y and (record.label or "") == (entry.l or "") then
return record
end
return nil
end
--- @param filter ItemFilter --- @param filter ItemFilter
--- @return QuickBarSlotEncoded --- @return QuickBarSlotEncoded
local function serialize_filter(filter) local function serialize_filter(filter)
@@ -611,18 +632,8 @@ function serialize.deserialize_quick_bar_slot(entry, player)
end end
if entry.t == "r" then if entry.t == "r" then
-- The record must still be what was stored, a shifted library must not set the wrong one local record = find_record(player, entry)
local records = player.blueprints if record then
local record --[[@as LuaRecord?]]
for _, index in ipairs(entry.p) do
record = records[index]
if not (record and record.valid) then
record = nil
break
end
records = record.type == "blueprint-book" and record.contents or {}
end
if record and record.valid and record.type == entry.y and (record.label or "") == (entry.l or "") then
return { type = "record", record = record } --[[@as QuickBarSlot]] return { type = "record", record = record } --[[@as QuickBarSlot]]
end end
@@ -670,8 +681,9 @@ local function quick_bar_slot_count(player)
end end
--- @param player LuaPlayer --- @param player LuaPlayer
--- @param missing table<string, QuickBarSlotEncoded.record>? Entries deserialize_quick_bar could not find the record of
--- @return table<string, QuickBarSlotEncoded>? --- @return table<string, QuickBarSlotEncoded>?
function serialize.serialize_quick_bar(player) function serialize.serialize_quick_bar(player, missing)
local serialized local serialized
local width = v2_1_quick_bar_api and player.quick_bar_width local width = v2_1_quick_bar_api and player.quick_bar_width
@@ -696,19 +708,63 @@ function serialize.serialize_quick_bar(player)
end end
end end
-- A record still missing here is stored again as it came, unless the player put something else in its slot
for index, entry in pairs(missing or {}) do
serialized = serialized or {}
serialized[index] = serialized[index] or entry
end
return serialized return serialized
end end
--- Slot index to page and slot on it
--- @param player LuaPlayer
--- @param i integer
local function quick_bar_location(player, i)
local width = player.quick_bar_width
return math.floor((i - 1) / width) + 1, ((i - 1) % width) + 1
end
--- Set the slots whose records were not in the player's library when the data was applied. A save gets the library
--- of a player from the client some seconds after the join, later than the data of a player new to it is applied.
--- @param player LuaPlayer
--- @param missing table<string, QuickBarSlotEncoded.record>
--- @return table<string, QuickBarSlotEncoded.record>? missing Entries whose record is still not there
function serialize.restore_missing_quick_bar_records(player, missing)
for index, entry in pairs(missing) do
local page, page_slot = quick_bar_location(player, tonumber(index) --[[@as integer]])
if player.get_quick_bar_slot(page, page_slot) ~= nil then
-- The player put something else there
missing[index] = nil
else
local record = find_record(player, entry)
if record then
player.set_quick_bar_slot(page, page_slot, { type = "record", record = record })
missing[index] = nil
end
end
end
return next(missing) and missing or nil
end
--- @param player LuaPlayer --- @param player LuaPlayer
--- @param serialized table<string, string | QuickBarSlotEncoded>? --- @param serialized table<string, string | QuickBarSlotEncoded>?
--- @return table<string, QuickBarSlotEncoded.record>? missing Entries whose record is not in the player's library
function serialize.deserialize_quick_bar(player, serialized) function serialize.deserialize_quick_bar(player, serialized)
-- Every slot is set, and the ones not in the data are cleared, so a slot moved or cleared on the -- Every slot is set, and the ones not in the data are cleared, so a slot moved or cleared on the
-- previous server does not come back from this server's old copy -- previous server does not come back from this server's old copy
serialized = serialized or {} serialized = serialized or {}
local width = v2_1_quick_bar_api and player.quick_bar_width local width = v2_1_quick_bar_api and player.quick_bar_width
local missing
for i = 1, quick_bar_slot_count(player) do for i = 1, quick_bar_slot_count(player) do
local entry = serialized[tostring(i)] local entry = serialized[tostring(i)]
local slot = entry ~= nil and serialize.deserialize_quick_bar_slot(entry, player) or nil local slot = entry ~= nil and serialize.deserialize_quick_bar_slot(entry, player) or nil
if slot == nil and v2_1_quick_bar_api and type(entry) == "table" and entry.t == "r" then
-- ponytail: kept until the record shows up or the slot is used for something else, so the entry of a
-- record deleted from the library is carried along. Drop them by age if that ever matters.
missing = missing or {}
missing[tostring(i)] = entry
end
if v2_1_quick_bar_api then if v2_1_quick_bar_api then
local page = math.floor((i - 1) / width) + 1 local page = math.floor((i - 1) / width) + 1
local page_slot = ((i - 1) % width) + 1 local page_slot = ((i - 1) % width) + 1
@@ -718,6 +774,7 @@ function serialize.deserialize_quick_bar(player, serialized)
player.set_quick_bar_slot(i, slot) player.set_quick_bar_slot(i, slot)
end end
end end
return missing
end end
--- Quick bar page shown on each quick bar row of the screen, kept by every save on its own --- Quick bar page shown on each quick bar row of the screen, kept by every save on its own
@@ -1212,7 +1269,7 @@ function serialize.serialize_player(player, failed_deserialization, components)
-- Serialize quick bar -- Serialize quick bar
if syncs(components, "quick_bar") then if syncs(components, "quick_bar") then
serialized.quick_bar = serialize.serialize_quick_bar(player) serialized.quick_bar = serialize.serialize_quick_bar(player, failed_deserialization.quick_bar)
serialized.quick_bar_pages = serialize.serialize_quick_bar_pages(player) serialized.quick_bar_pages = serialize.serialize_quick_bar_pages(player)
end end
@@ -1333,6 +1390,7 @@ end
--- @class FailedDeserializationPlayerData --- @class FailedDeserializationPlayerData
--- @field recipe_notifications string[]? --- @field recipe_notifications string[]?
--- @field quick_bar table<string, QuickBarSlotEncoded.record>? Slots whose record is not in the library (yet)
--- @param player LuaPlayer --- @param player LuaPlayer
--- @param serialized SerializedPlayerData --- @param serialized SerializedPlayerData
@@ -1495,7 +1553,9 @@ function serialize.deserialize_player(player, serialized, components, local_cont
-- Deserialize quick bar (named hotbar in old data) -- Deserialize quick bar (named hotbar in old data)
if syncs(components, "quick_bar") then if syncs(components, "quick_bar") then
serialize.deserialize_quick_bar(player, serialized.quick_bar or serialized.hotbar) failed_deserialization.quick_bar = serialize.deserialize_quick_bar(
player, serialized.quick_bar or serialized.hotbar
)
serialize.deserialize_quick_bar_pages(player, serialized.quick_bar_pages) serialize.deserialize_quick_bar_pages(player, serialized.quick_bar_pages)
end end
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/plugin-player_sync", "name": "@clusterio/plugin-player_sync",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio plugin forwarding between Factorio servers", "description": "Clusterio plugin forwarding between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Danielv <danielv@danielv.no>", "author": "Danielv <danielv@danielv.no>",
+12
View File
@@ -77,6 +77,18 @@ describe("player_sync controller", function() {
.playerData; .playerData;
} }
it("should reply whether the upload was stored", async function() {
const handler = mock.getHandler(controller, msg.UploadRequest);
const send = (instanceId, generation) => handler(
{ instanceId, playerName: "test", playerData: { generation, name: "test" } }
);
assert.deepEqual(await send(1, 2), new msg.UploadRequest.Response(true));
// A crashed instance restarting with the generation another instance already uploaded
assert.deepEqual(await send(1, 2), new msg.UploadRequest.Response(false));
await mock.getHandler(controller, msg.AcquireRequest)(new msg.AcquireRequest(1, "test"));
controller.instances.set(2, controller.instances.get(1));
assert.deepEqual(await send(2, 3), new msg.UploadRequest.Response(false));
});
it("should store the upload as is when it has no components", async function() { it("should store the upload as is when it has no components", async function() {
const playerData = { generation: 2, name: "test", force: "enemy", tag: "uploaded" }; const playerData = { generation: 2, name: "test", force: "enemy", tag: "uploaded" };
assert.deepEqual(await upload(playerData), playerData); assert.deepEqual(await upload(playerData), playerData);
+20 -1
View File
@@ -5,7 +5,7 @@ import * as lib from "@clusterio/lib";
import * as mock from "../../../test/mock.js"; import * as mock from "../../../test/mock.js";
import loadInstancePlugin, { recipeNotificationDelta, applyRecipeNotificationDelta } from "../dist/node/instance.js"; import loadInstancePlugin, { recipeNotificationDelta, applyRecipeNotificationDelta } from "../dist/node/instance.js";
import { AcquireResponse, DownloadResponse, PrefetchEvent } from "../dist/node/messages.js"; import { AcquireResponse, DownloadResponse, PrefetchEvent, UploadResponse } from "../dist/node/messages.js";
import { components } from "../dist/node/components.js"; import { components } from "../dist/node/components.js";
import { plugin as info } from "../dist/node/index.js"; import { plugin as info } from "../dist/node/index.js";
@@ -275,6 +275,25 @@ describe("player_sync", function() {
const request = await upload({ name: "test", generation: 2 }); const request = await upload({ name: "test", generation: 2 });
assert.equal(request.destinationInstanceId, undefined); assert.equal(request.destinationInstanceId, undefined);
}); });
async function rconAfterReply(stored) {
instance.server.reset();
instance.connector.once("send", message => {
instance.connector.emit("message", new lib.MessageResponse(
1, message.dst, message.src, new UploadResponse(stored)
));
});
instance.server.emit("ipc-player_sync_upload", { name: "test", generation: 2 });
for (let i = 0; i < 100 && !instance.server.rconCommands.length; i++) {
await lib.wait(1);
}
return instance.server.rconCommands;
}
it("should confirm an upload the controller stored", async function() {
assert.deepEqual(await rconAfterReply(true), ['/sc player_sync.confirm_upload("test", 2)']);
});
it("should reject an upload the controller did not store", async function() {
assert.deepEqual(await rconAfterReply(false), ['/sc player_sync.reject_upload("test", 2)']);
});
}); });
describe("PrefetchEvent", function() { describe("PrefetchEvent", function() {
+35 -6
View File
@@ -4,7 +4,7 @@ import os from "node:os";
import path from "node:path"; import path from "node:path";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { CtlHooks } from "@clusterio/ctl"; import { CommandOutput, CtlHooks } from "@clusterio/ctl";
import * as mock from "../../../test/mock.js"; import * as mock from "../../../test/mock.js";
import { testRoundTripJsonSerialisable, testMatrix } from "../../../test/common.js"; import { testRoundTripJsonSerialisable, testMatrix } from "../../../test/common.js";
@@ -60,6 +60,14 @@ describe("player_sync plugin", function() {
assert(!validate({ instanceId: 1, playerName: "player", playerData: data }), `accepted ${deleted}`); assert(!validate({ instanceId: 1, playerName: "player", playerData: data }), `accepted ${deleted}`);
} }
}); });
it("should take the empty reply of an older controller as a stored upload", function() {
const validate = lib.compile(msg.UploadResponse.jsonSchema);
assert(validate(undefined));
assert.equal(msg.UploadResponse.fromJSON(undefined).stored, true);
assert(validate({ stored: false }));
assert.equal(msg.UploadResponse.fromJSON({ stored: false }).stored, false);
assert(!validate({ stored: "no" }));
});
describe("SetPlayerDataRequest schema", function() { describe("SetPlayerDataRequest schema", function() {
const validate = lib.compile(msg.SetPlayerDataRequest.jsonSchema); const validate = lib.compile(msg.SetPlayerDataRequest.jsonSchema);
// Shaped like module/serialize.lua output // Shaped like module/serialize.lua output
@@ -295,6 +303,7 @@ describe("player_sync plugin", function() {
let printed; let printed;
let responses; let responses;
const ctl = { const ctl = {
output: new CommandOutput(),
async send(message) { async send(message) {
sent.push(message); sent.push(message);
const handler = responses.get(message.constructor); const handler = responses.get(message.constructor);
@@ -323,6 +332,7 @@ describe("player_sync plugin", function() {
beforeEach(function() { beforeEach(function() {
sent = []; sent = [];
printed = []; printed = [];
ctl.output = new CommandOutput();
responses = new Map([ responses = new Map([
[lib.InstanceDetailsGetRequest, request => ({ id: request.instanceId, name: "Test" })], [lib.InstanceDetailsGetRequest, request => ({ id: request.instanceId, name: "Test" })],
[msg.ListPlayersRequest, () => [ [msg.ListPlayersRequest, () => [
@@ -352,10 +362,12 @@ describe("player_sync plugin", function() {
it("list should print sorted entries", async function() { it("list should print sorted entries", async function() {
await run("list"); await run("list");
assert.deepEqual(printed, [ const lines = printed.join("\n").split("\n").map(line => line.trimEnd());
"name | generation | size | acquired by", assert.deepEqual(lines, [
`alice | 2 | ${lib.formatBytes(100)} | -`, "name | generation | size | acquired by",
"bob | - | - | Test (1)", "--------------------------------------------",
`alice | 2 | ${lib.formatBytes(100)} | -`,
"bob | - | - | Test (1)",
]); ]);
}); });
it("show should print a summary", async function() { it("show should print a summary", async function() {
@@ -376,7 +388,7 @@ describe("player_sync plugin", function() {
throw new lib.PermissionError("Permission denied"); throw new lib.PermissionError("Permission denied");
}); });
await run("list"); await run("list");
assert.equal(printed[2], "bob | - | - | 1"); assert.match(printed[0], /bob +\| - +\| - +\| 1/);
printed = []; printed = [];
await run("show", { player: "alice" }); await run("show", { player: "alice" });
assert.equal(printed[0], "acquired by: 1"); assert.equal(printed[0], "acquired by: 1");
@@ -413,6 +425,23 @@ describe("player_sync plugin", function() {
new lib.CommandError("Player data must be a JSON object"), new lib.CommandError("Player data must be a JSON object"),
); );
}); });
it("should print JSON in json mode", async function() {
ctl.output = new CommandOutput("json");
await run("list");
assert.deepEqual(JSON.parse(printed[0]), [
{ name: "alice", generation: 2, size: 100 },
{ name: "bob", instanceId: 1 },
]);
printed = [];
await run("show", { player: "alice" });
const shown = JSON.parse(printed[0]);
assert.equal(shown.instanceId, 1);
assert.equal(shown.generation, 2);
assert.deepEqual(shown.inventories[0].items[0], { name: "wood", count: 5, quality: "rare" });
printed = [];
await run("export", { player: "alice" });
assert.deepEqual(JSON.parse(printed[0]), storedData);
});
it("delete and release should send requests", async function() { it("delete and release should send requests", async function() {
await run("delete", { player: "alice" }); await run("delete", { player: "alice" });
await run("release", { player: "alice" }); await run("release", { player: "alice" });
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/plugin-research_sync", "name": "@clusterio/plugin-research_sync",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio plugin for synchronizing research between Factorio servers", "description": "Clusterio plugin for synchronizing research between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/plugin-statistics_exporter", "name": "@clusterio/plugin-statistics_exporter",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio plugin for gathering statistics from Factorio servers", "description": "Clusterio plugin for gathering statistics from Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+4
View File
@@ -12,6 +12,10 @@ Run the following commands in the folder Clusterio is installed to:
Substitute clusteriocontroller with clusteriohost or clusterioctl if this a dedicate host or ctl installation respectively. Substitute clusteriocontroller with clusteriohost or clusterioctl if this a dedicate host or ctl installation respectively.
The plugin needs the [Subspace Storage mod](https://mods.factorio.com/mod/subspace_storage) in the mod pack of every instance it is loaded on.
The controller downloads it from the mod portal on start if the Factorio credentials are configured, but you have to add it to the mod packs yourself.
Instances with the plugin loaded refuse to start without it. Set `subspace_storage.load_plugin` to false on instances that should not use it.
## Controller Configuration ## Controller Configuration
+1
View File
@@ -19,6 +19,7 @@ export const plugin: lib.PluginDeclaration = {
title: "Subspace Storage", title: "Subspace Storage",
description: "Provides shared storage across instances for the Subspace Storage mod", description: "Provides shared storage across instances for the Subspace Storage mod",
instanceEntrypoint: "dist/node/instance.js", instanceEntrypoint: "dist/node/instance.js",
requiredMods: ["subspace_storage >= 1.99.0"],
instanceConfigFields: { instanceConfigFields: {
"subspace_storage.log_item_transfers": { "subspace_storage.log_item_transfers": {
title: "Log Item Transfers", title: "Log Item Transfers",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@clusterio/plugin-subspace_storage", "name": "@clusterio/plugin-subspace_storage",
"version": "2.1.0", "version": "2.1.1",
"description": "Clusterio plugin for sharing storage between Factorio servers", "description": "Clusterio plugin for sharing storage between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme", "homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>", "author": "Hornwitser <github@hornwitser.no>",
+71
View File
@@ -0,0 +1,71 @@
// Checks the changelog section of a pull request body, read from the PR_BODY environment variable.
import fs from "node:fs";
import { checkPullRequestBody, changelogSections } from "./pr_to_changelog.mts";
const expectedFormat = [
"### Changelog",
"```",
"### Features",
"- Added doodad to web interface. #123",
"",
"### Fixes",
"- Fixed doodad not showing up. #456",
"```",
"",
"Or, when nothing user facing changed, replace the code block with:",
"",
"### Changelog",
"None. Reason no change log is required.",
].join("\n");
/** Returns the problems found in the body, an empty list means it is valid. */
export function findChangelogProblems(body: string | null | undefined) {
const { error, warnings } = checkPullRequestBody(body);
return error ? [error] : warnings;
}
function escapeAnnotation(text: string) {
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
}
function main() {
const problems = findChangelogProblems(process.env.PR_BODY);
const summaryPath = process.env.GITHUB_STEP_SUMMARY;
if (!problems.length) {
console.log("Changelog section is valid.");
if (summaryPath) {
fs.appendFileSync(summaryPath, "Changelog section is valid.\n");
}
return;
}
for (const problem of problems) {
console.log(`::error title=Invalid changelog::${escapeAnnotation(problem)}`);
}
const rules = `Valid sections are ${changelogSections.join(", ")}. ` +
"Each entry must reference an issue, or this pull request if there is no issue.";
console.log(`\n${rules}`);
console.log(`Expected format in the pull request description:\n\n${expectedFormat}`);
if (summaryPath) {
fs.appendFileSync(summaryPath, [
"## Invalid changelog",
"",
"The changelog section of the pull request description has problems:",
"",
...problems.map(problem => `- ${problem.replaceAll("<", "&lt;")}`),
"",
rules,
"Edit the description and this check runs again. Expected format:",
"",
"````markdown",
expectedFormat,
"````",
"",
].join("\n"));
}
process.exitCode = 1;
}
if (import.meta.main) {
main();
}
+43 -16
View File
@@ -20,7 +20,7 @@ type Statistics = {
const repository = "clusterio/clusterio"; const repository = "clusterio/clusterio";
const branch = "master"; const branch = "master";
const changelogSections = [ export const changelogSections = [
"Major Features", "Major Features",
"Features", "Features",
"Fixes", "Fixes",
@@ -79,7 +79,7 @@ function extractPullRequestChangelog(
{ {
const headerFail = skipToLine(parser, /^###? Changelog *$/); const headerFail = skipToLine(parser, /^###? Changelog *$/);
if (headerFail) { if (headerFail) {
return [failure, , "Header not found"]; return [failure, , "\"### Changelog\" header not found"];
} }
parser.pos += 1; parser.pos += 1;
skipEmptyLines(parser); skipEmptyLines(parser);
@@ -97,10 +97,19 @@ function extractPullRequestChangelog(
if (blockEndFail) { if (blockEndFail) {
return [failure, , "End of code block not found after header"]; return [failure, , "End of code block not found after header"];
} }
return [success, parser.lines.slice(blockStartPos, parser.pos)]; const lines = parser.lines.slice(blockStartPos, parser.pos);
if (!lines.some(line => line.trim() !== "")) {
return [failure, , "Code block is empty, replace it with \"None.\" if nothing applies"];
}
return [success, lines];
} }
function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Changelog, string[]] { /** Pass no issues to skip the checks that need the GitHub API. */
function parseChangelog(
parser: ParserState,
pr?: Issue,
issues?: Issue[],
): [Changelog, string[]] {
let section: string | undefined; let section: string | undefined;
let changelog: Changelog = Object.create(null); let changelog: Changelog = Object.create(null);
let warnings: string[] = []; let warnings: string[] = [];
@@ -120,6 +129,10 @@ function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Chang
changelog[section] = changelog[section] ?? []; changelog[section] = changelog[section] ?? [];
continue; continue;
} }
if (!section && /^None/i.test(line)) {
warnings.push(`"${line}" on line ${parser.pos} must replace the code block, not sit inside it`);
continue;
}
if (!section) { if (!section) {
warnings.push(`Unexpected content "${line}" outside of a changelog section on line ${parser.pos}`); warnings.push(`Unexpected content "${line}" outside of a changelog section on line ${parser.pos}`);
continue; continue;
@@ -132,6 +145,9 @@ function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Chang
let hasRef = false let hasRef = false
for (const ref of entry.matchAll(/#(\d+)/g)) { for (const ref of entry.matchAll(/#(\d+)/g)) {
hasRef = true; hasRef = true;
if (!issues || !pr) {
continue;
}
const id = Number.parseInt(ref[1], 10); const id = Number.parseInt(ref[1], 10);
const refIssue = issues.find(issue => issue.number === id); const refIssue = issues.find(issue => issue.number === id);
if (!refIssue) { if (!refIssue) {
@@ -157,23 +173,34 @@ function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Chang
return [changelog, warnings]; return [changelog, warnings];
} }
export function checkPullRequestBody(
body: string | null | undefined,
pr?: Issue,
issues?: Issue[],
): { changelog: Changelog, error?: string, warnings: string[] } {
if (!body) {
return { changelog: Object.create(null), error: "Pull request has no body", warnings: [] };
}
const [
changelogFail,
changelogLines,
changelogFailReason
] = extractPullRequestChangelog(createParser(body.split(/\r?\n/)));
if (changelogFail) {
return { changelog: Object.create(null), error: changelogFailReason, warnings: [] };
}
const [changelog, warnings] = parseChangelog(createParser(changelogLines), pr, issues);
return { changelog, warnings };
}
function changelogFromPullRequests(pullRequests: Issue[], issues: Issue[]) { function changelogFromPullRequests(pullRequests: Issue[], issues: Issue[]) {
const mergedChangelog: Changelog = Object.create(null); const mergedChangelog: Changelog = Object.create(null);
for (const pr of pullRequests) { for (const pr of pullRequests) {
if (!pr.body) { const { changelog, error, warnings } = checkPullRequestBody(pr.body, pr, issues);
console.error(`Failed to extract changelog in ${pr.html_url}: Pull request has no body`); if (error) {
console.error(`Failed to extract changelog in ${pr.html_url}: ${error}`)
continue; continue;
} }
const [
changelogFail,
changelogLines,
changelogFailReason
] = extractPullRequestChangelog(createParser(pr.body.split(/\r?\n/)));
if (changelogFail) {
console.error(`Failed to extract changelog in ${pr.html_url}: ${changelogFailReason}`)
continue;
}
const [changelog, warnings] = parseChangelog(createParser(changelogLines), pr, issues);
for (const warning of warnings) { for (const warning of warnings) {
console.warn(`Warning in changelog for ${pr.html_url}: ${warning}`) console.warn(`Warning in changelog for ${pr.html_url}: ${warning}`)
} }
+37
View File
@@ -123,6 +123,43 @@ describe("controller/src/ControlConnection", function() {
}); });
}); });
describe(".handleModPortalGetAllRequest()", function() {
let mockController;
let _fetch;
let urls;
beforeEach(function() {
mockController = {
config: new lib.ControllerConfig("controller"),
modPortalCache: new Map(),
};
urls = [];
_fetch = global.fetch;
global.fetch = async url => {
urls.push(new URL(url));
return { ok: true, json: async () => ({ results: [], pagination: { page_count: 1 } }) };
};
});
afterEach(function() {
global.fetch = _fetch;
});
async function getAll(request) {
return await ControlConnection.prototype.handleModPortalGetAllRequest.call(
{ _controller: mockController }, request,
);
}
it("treats a missing hide_deprecated as false", async function() {
await getAll(new lib.ModPortalGetAllRequest("2.1"));
assert.deepEqual([...mockController.modPortalCache.keys()], ["2.1-false"]);
assert.equal(urls[0].searchParams.get("hide_deprecated"), "false");
await getAll(new lib.ModPortalGetAllRequest("2.1", false));
assert.equal(urls.length, 1, "explicit false should hit the cache");
});
});
describe(".handleInstanceCreateRequest()", function() { describe(".handleInstanceCreateRequest()", function() {
let mockController; let mockController;
+107
View File
@@ -93,6 +93,43 @@ describe("controller/src/Controller", function() {
assert(loaded.modPacks.size > 0, "default mod packs were not added"); assert(loaded.modPacks.size > 0, "default mod packs were not added");
await fs.access(modsDirectory); await fs.access(modsDirectory);
}); });
it("should default to the newest Base Game pack on a new cluster", async function() {
const config = new ControllerConfig("controller", {
"controller.version": controllerVersion,
"controller.database_directory": path.join(dir, "database"),
"controller.mods_directory": path.join(dir, "mods"),
});
const loaded = await Controller.fromConfig(config, [], {});
const id = config.get("controller.default_mod_pack_id");
assert.equal(loaded.modPacks.get(id).name, "Base Game 2.1");
});
it("should keep a set default mod pack when seeding mod packs", async function() {
const config = new ControllerConfig("controller", {
"controller.version": controllerVersion,
"controller.database_directory": path.join(dir, "database"),
"controller.mods_directory": path.join(dir, "mods"),
"controller.default_mod_pack_id": 42,
});
await Controller.fromConfig(config, [], {});
assert.equal(config.get("controller.default_mod_pack_id"), 42);
});
it("should not set a default mod pack when mod packs exist", async function() {
const databaseDirectory = path.join(dir, "database");
await fs.mkdir(databaseDirectory);
await fs.writeFile(path.join(databaseDirectory, "mod-packs.json"), JSON.stringify([
new ModPack().toJSON(),
]));
const config = new ControllerConfig("controller", {
"controller.version": controllerVersion,
"controller.database_directory": databaseDirectory,
"controller.mods_directory": path.join(dir, "mods"),
});
await Controller.fromConfig(config, [], {});
assert.equal(config.get("controller.default_mod_pack_id"), null);
});
it("should start and stop with an existing database", async function() { it("should start and stop with an existing database", async function() {
this.timeout(10000); this.timeout(10000);
const databaseDirectory = path.join(dir, "database"); const databaseDirectory = path.join(dir, "database");
@@ -268,6 +305,50 @@ describe("controller/src/Controller", function() {
await assert.rejects(Controller.loadJsonObject(path.join(dir, "invalid.json")), SyntaxError); await assert.rejects(Controller.loadJsonObject(path.join(dir, "invalid.json")), SyntaxError);
}); });
}); });
describe(".downloadRequiredMods()", function() {
let ctrl, resolveCalls, downloadCalls, resolved;
function modInfo(name, version) {
return lib.ModInfo.fromJSON({ name, version, factorio_version: "2.0" });
}
beforeEach(function() {
const config = new ControllerConfig("controller", { "controller.version": controllerVersion });
ctrl = new Controller({}, [
{ name: "foo", title: "Foo", requiredMods: ["foo_mod >= 1.0.0"] },
{ name: "bar", title: "Bar" },
], config);
const modPack = ModPack.fromJSON({ id: 7, name: "default", factorio_version: "2.0.10" });
ctrl.modPacks.set(modPack);
config.set("controller.default_mod_pack_id", 7);
resolveCalls = [];
downloadCalls = [];
resolved = [modInfo("foo_mod", "1.2.0"), modInfo("dep_mod", "0.1.0")];
ctrl.resolveModDependencies = async (mods, factorioVersion) => {
resolveCalls.push([mods.map(mod => mod.spec), factorioVersion]);
return new lib.ModDependencyResolveRequest.Response(resolved, new Map());
};
ctrl.downloadModsFromPortal = async (mods, factorioVersion) => {
downloadCalls.push([mods.map(mod => `${mod.name} ${mod.version}`), factorioVersion]);
};
});
it("should download required mods and their dependencies for the default mod pack", async function() {
await ctrl.downloadRequiredMods();
assert.deepEqual(resolveCalls, [[["foo_mod >= 1.0.0"], "2.0"]]);
assert.deepEqual(downloadCalls, [[["foo_mod = 1.2.0", "dep_mod = 0.1.0"], "2.0"]]);
});
it("should skip mods already in the mod store", async function() {
ctrl.modStore = new lib.ModStore("mods", new Map(resolved.map(info => [info.filename, info])));
await ctrl.downloadRequiredMods();
assert.deepEqual(downloadCalls, []);
});
it("should do nothing when no plugin requires mods", async function() {
ctrl.pluginInfos = [{ name: "bar", title: "Bar" }];
await ctrl.downloadRequiredMods();
assert.deepEqual(resolveCalls, []);
});
});
describe(".loggableHeaders()", function() { describe(".loggableHeaders()", function() {
it("should redact credentials and keep other headers", function() { it("should redact credentials and keep other headers", function() {
const headers = { const headers = {
@@ -597,6 +678,32 @@ describe("controller/src/Controller", function() {
assert.equal(controller.config.restartRequired, true); assert.equal(controller.config.restartRequired, true);
}); });
}); });
describe(".onSystemMetricsIntervalChanged()", function() {
let calls;
beforeEach(function() {
calls = 0;
controller.updateSystems = async () => { calls += 1; };
});
afterEach(function() {
clearInterval(controller.systemMetricsInterval);
controller.systemMetricsInterval = undefined;
delete controller.updateSystems;
controller.config.set("controller.system_metrics_interval", 10);
});
it("updates systems right away and then on every interval", async function() {
controller.config.set("controller.system_metrics_interval", 0.01);
controller.onSystemMetricsIntervalChanged();
assert.equal(calls, 1, "first update waited for the interval");
await new Promise(resolve => setTimeout(resolve, 35));
assert(calls >= 3, `expected interval updates, got ${calls} calls`);
});
it("does nothing when the interval is 0", function() {
controller.config.set("controller.system_metrics_interval", 0);
controller.onSystemMetricsIntervalChanged();
assert.equal(calls, 0);
assert.equal(controller.systemMetricsInterval, undefined);
});
});
describe(".checkRestartDowngrade()", function() { describe(".checkRestartDowngrade()", function() {
it("returns null when the installed version matches", async function() { it("returns null when the installed version matches", async function() {
controller.config.set("controller.version", controllerVersion); controller.config.set("controller.version", controllerVersion);
+40
View File
@@ -136,6 +136,46 @@ describe("controller/src/HostConnection", function() {
}); });
}); });
describe("system info on connect", function() {
let controller;
let connector;
let connection;
beforeEach(function() {
controller = new Controller(lib.logger, [], new lib.ControllerConfig("controller"));
controller.hosts.records.set(new HostRecord(10, "host-10", "1.0.0", new Map()));
connector = new MockConnector(addr("controller"), addr({ hostId: 10 }));
connection = new HostConnection(
new lib.RegisterHostData("", "2.0.0", 10, {}), connector, controller, "10.0.0.1"
);
});
function systemInfoRequests() {
return connector.sentMessages.filter(message => message.name === "SystemInfoRequest");
}
it("should request system info as soon as the host connects", async function() {
connector.emit("connect");
const [request] = systemInfoRequests();
assert(request, "SystemInfoRequest was not sent");
assert.equal(connection.id, 10);
const info = await lib.gatherSystemInfo(10, false, true, 0);
connector.emit("message", new lib.MessageResponse(request.seq, request.dst, request.src, info));
await new Promise(resolve => setImmediate(resolve));
assert.equal(controller.systems.get(10)?.restartRequired, true);
});
it("should not request system info when a session resumes", function() {
connector.emit("resume");
assert.deepEqual(systemInfoRequests(), []);
});
it("should not request system info when system metrics are disabled", function() {
controller.config.set("controller.system_metrics_interval", 0);
connector.emit("connect");
assert.deepEqual(systemInfoRequests(), []);
});
});
describe(".handleInstanceSaveDetailsUpdatesEvent()", function() { describe(".handleInstanceSaveDetailsUpdatesEvent()", function() {
// Instance 1 is on host 10, instance 2 is on host 20. // Instance 1 is on host 10, instance 2 is on host 20.
const assignments = new Map([[1, 10], [2, 20]]); const assignments = new Map([[1, 10], [2, 20]]);
+14
View File
@@ -195,6 +195,20 @@ describe("create installer CLI", function() {
await fs.access(path.join(directory, `run-host${process.platform === "win32" ? ".cmd" : ".sh"}`)); await fs.access(path.join(directory, `run-host${process.platform === "win32" ? ".cmd" : ".sh"}`));
}); });
it("writes a run script that exits with the code that stopped the restart loop", async function() {
if (process.platform === "win32") {
this.skip();
}
await run(hostArgs());
const binDir = path.join(directory, "node_modules", ".bin");
await fs.mkdir(binDir, { recursive: true });
await fs.writeFile(path.join(binDir, "clusteriohost"), "#!/bin/sh\nexit 8\n", { mode: 0o755 });
await assert.rejects(
execFileAsync(path.join(directory, "run-host.sh"), [], { cwd: directory }),
{ code: 8 },
);
});
it("adds the trailing slash the host requires to the controller URL", async function() { it("adds the trailing slash the host requires to the controller URL", async function() {
await run(hostArgs(controllerUrl.slice(0, -1))); await run(hostArgs(controllerUrl.slice(0, -1)));
assert((await commands()).includes(hostSet("host.controller_url", controllerUrl))); assert((await commands()).includes(hostSet("host.controller_url", controllerUrl)));
+95
View File
@@ -0,0 +1,95 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { CommandOutput, errorToJson, pickFields, toPlain } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/command_ops", function() {
describe("toPlain()", function() {
it("converts Sets, Maps and Dates", function() {
assert.deepEqual(
toPlain({ set: new Set([1, 2]), map: new Map([[3, { a: new Set(["b"]) }]]), date: new Date(0) }),
{ set: [1, 2], map: { 3: { a: ["b"] } }, date: "1970-01-01T00:00:00.000Z" },
);
});
it("uses own properties instead of toJSON", function() {
const role = new lib.Role(1, "name", "desc", new Set(["core.admin"]));
assert.deepEqual(toPlain(role).permissions, ["core.admin"]);
});
});
describe("pickFields()", function() {
it("keeps only the given fields unless all is given", function() {
assert.deepEqual(pickFields({ a: 1, b: 2 }, ["a"]), { a: 1 });
assert.deepEqual(pickFields({ a: 1, b: 2 }, ["all"]), { a: 1, b: 2 });
});
});
describe("errorToJson()", function() {
it("uses the class name of the error", function() {
assert.deepEqual(errorToJson(new lib.NotFoundError("gone")), { error: "NotFoundError", message: "gone" });
assert.deepEqual(errorToJson("text"), { error: "Error", message: "text" });
});
});
describe("class CommandOutput", function() {
it("calls the human formatter in human mode", function() {
const output = new CommandOutput("human");
const human = () => console.log("human"); // eslint-disable-line no-console
assert.deepEqual(captureLog(() => output.result({ a: 1 }, human)), ["human"]);
assert.deepEqual(captureLog(() => output.finish()), []);
});
it("prints a success line in human mode for commands without a result", function() {
assert.deepEqual(captureLog(() => new CommandOutput("human").finish()), ["Command ran successfully"]);
});
it("prints one line of JSON per result in json mode", function() {
const output = new CommandOutput("json");
const lines = captureLog(() => {
output.result({ a: new Set([1]) }, () => assert.fail("called human formatter"));
output.table([{ b: 2 }]);
output.finish();
});
assert.deepEqual(lines, ['{"a":[1]}', '[{"b":2}]']);
});
it("prints the toJSON format in wire mode", function() {
const output = new CommandOutput("wire");
const role = new lib.Role(1, "name", "desc", new Set(["core.admin"]));
const lines = captureLog(() => output.result({ role, set: new Set([1]), map: new Map([["a", 2]]) }));
assert.deepEqual(JSON.parse(lines[0]), { role: JSON.parse(JSON.stringify(role)), set: [1], map: { a: 2 } });
});
it("prints success for commands without a result", function() {
assert.deepEqual(captureLog(() => new CommandOutput("json").finish()), ['{"success":true}']);
const streaming = new CommandOutput("json");
streaming.streamingLog = true;
assert.deepEqual(captureLog(() => streaming.finish()), []);
});
it("prints errors as JSON", function() {
const output = new CommandOutput("json");
const lines = captureLog(() => {
output.error(new lib.CommandError("failed"), () => assert.fail("called human formatter"));
output.finish();
});
assert.deepEqual(lines, ['{"error":"CommandError","message":"failed"}']);
});
it("calls the human error reporter in human mode", function() {
const output = new CommandOutput("human");
const human = () => console.log("failed"); // eslint-disable-line no-console
const lines = captureLog(() => {
output.error(new lib.CommandError("failed"), human);
output.finish();
});
assert.deepEqual(lines, ["failed"]);
});
});
});
+88
View File
@@ -0,0 +1,88 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { modCommands } from "@clusterio/ctl/dist/node/src/commands_mod.js";
import { modPackCommands } from "@clusterio/ctl/dist/node/src/commands_mod_pack.js";
import { userCommands } from "@clusterio/ctl/dist/node/src/commands_user.js";
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
async function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
/** Mock ctl answering requests by their class */
function mockCtl(responses) {
return {
output: new CommandOutput(),
async send(message) {
return responses.get(message.constructor)(message);
},
async sendTo(destination, message) {
return this.send(message);
},
};
}
describe("ctl/commands", function() {
describe("mod show", function() {
const show = modCommands.get("show");
it("prints list fields one entry per line", async function() {
const ctl = mockCtl(new Map([
[lib.ModGetRequest, () => ({ name: "a_mod", dependencies: ["base", "? other"] })],
]));
const lines = await captureLog(() => show.run({ name: "a_mod", modVersion: "1.0.0" }, ctl));
assert.deepEqual(lines, ["name: a_mod", "dependencies:", " base", " ? other"]);
});
it("rejects an invalid version", async function() {
await assert.rejects(
show.run({ name: "a_mod", modVersion: "latest" }, mockCtl(new Map())),
new lib.CommandError("mod-version must match format digit.digit.digit"),
);
});
});
describe("mod-pack show", function() {
it("marks missing mods and checksum mismatches and lists export assets", async function() {
const modPack = lib.ModPack.fromJSON({ id: 7, name: "pack" });
modPack.mods.set("gone", { name: "gone", enabled: true, version: "1.0.0" });
modPack.mods.set("changed", { name: "changed", enabled: false, version: "1.0.0", sha1: "aaa" });
modPack.exportManifest = { assets: { settings: "settings.json" } };
const ctl = mockCtl(new Map([
[lib.ModListRequest, () => [{ id: "changed_1.0.0", sha1: "bbb" }]],
[lib.ModPackListRequest, () => [modPack]],
[lib.ModPackGetRequest, () => modPack],
]));
const lines = await captureLog(() => modPackCommands.get("show").run({ modPack: "pack" }, ctl));
assert(lines.includes(" ? gone 1.0.0"), lines.join("\n"));
assert(lines.includes(" ! (disabled) changed 1.0.0 (aaa)"), lines.join("\n"));
assert(lines.includes(" settings: settings.json"), lines.join("\n"));
});
});
describe("user show", function() {
const show = userCommands.get("show");
it("prints stats per instance", async function() {
const stats = new lib.PlayerStats();
stats.joinCount = 2;
const user = new lib.UserDetails("alice");
user.instanceStats = new Map([[1, stats], [2, stats]]);
user.recalculatePlayerStats();
const ctl = mockCtl(new Map([
[lib.UserGetRequest, () => user],
[lib.InstanceDetailsListRequest, () => [{ id: 1, name: "one" }]],
]));
const lines = await captureLog(() => show.run({ name: "alice", instanceStats: true }, ctl));
assert(lines.includes("Instance one (1):"), lines.join("\n"));
assert(lines.includes("Instance <deleted> (2):"), lines.join("\n"));
assert(lines.some(line => /joinCount +\| 4/.test(line)), lines.join("\n"));
});
});
});
+91
View File
@@ -0,0 +1,91 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { instanceCommands } from "@clusterio/ctl/dist/node/src/commands_instance.js";
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
async function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/commands_instance", function() {
let instances;
let sentTo;
const ctl = {
output: new CommandOutput(),
async send(message) {
assert(message instanceof lib.InstanceDetailsListRequest);
return instances;
},
async sendTo(destination, message) {
sentTo.push([destination.instanceId, message.constructor.name]);
if (destination.instanceId === 2) {
throw new lib.CommandError("broken");
}
},
};
beforeEach(function() {
sentTo = [];
instances = [
{ id: 1, name: "one", status: "stopped", excludeFromStartAll: false },
{ id: 2, name: "two", status: "stopped", excludeFromStartAll: false },
{ id: 3, name: "three", status: "stopped", excludeFromStartAll: true },
{ id: 4, name: "four", status: "running", excludeFromStartAll: false },
];
});
describe("start-all", function() {
const startAll = instanceCommands.get("start-all");
it("starts stopped instances and reports each one", async function() {
ctl.output = new CommandOutput("human");
const lines = await captureLog(() => startAll.run({ force: false }, ctl));
assert.deepEqual(sentTo, [[1, "InstanceStartRequest"], [2, "InstanceStartRequest"]]);
assert(lines.includes("✓ Started one"), lines.join("\n"));
assert(lines.some(line => line.startsWith("✗ Failed to start two")), lines.join("\n"));
});
it("prints an entry per instance in json mode", async function() {
ctl.output = new CommandOutput("json");
const lines = await captureLog(() => startAll.run({ force: true }, ctl));
assert.deepEqual(JSON.parse(lines[0]), [
{ id: 1, name: "one" },
{ id: 2, name: "two", error: { error: "CommandError", message: "broken" } },
{ id: 3, name: "three" },
]);
});
it("reports when there is nothing to start", async function() {
instances = [];
ctl.output = new CommandOutput("human");
assert.deepEqual(await captureLog(() => startAll.run({ force: false }, ctl)), ["No instances to start."]);
});
});
describe("stop-all", function() {
const stopAll = instanceCommands.get("stop-all");
it("stops running instances and reports each one", async function() {
instances[1].status = "running";
ctl.output = new CommandOutput("human");
const lines = await captureLog(() => stopAll.run({}, ctl));
assert.deepEqual(sentTo, [[2, "InstanceStopRequest"], [4, "InstanceStopRequest"]]);
assert(lines.includes("✓ Stopped four"), lines.join("\n"));
assert(lines.some(line => line.startsWith("✗ Failed to stop two")), lines.join("\n"));
});
it("prints an entry per instance in json mode", async function() {
ctl.output = new CommandOutput("json");
const lines = await captureLog(() => stopAll.run({}, ctl));
assert.deepEqual(JSON.parse(lines[0]), [{ id: 4, name: "four" }]);
});
it("reports when there is nothing to stop", async function() {
instances = [];
ctl.output = new CommandOutput("human");
assert.deepEqual(await captureLog(() => stopAll.run({}, ctl)), ["No instances to stop."]);
});
});
});
+56
View File
@@ -0,0 +1,56 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { Ctl } from "@clusterio/ctl/dist/node/ctl.js";
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
async function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/ctl", function() {
describe("class Ctl", function() {
let ctl;
beforeEach(function() {
const connector = new lib.VirtualConnector(
lib.Address.fromShorthand({ controlId: 1 }),
lib.Address.fromShorthand("controller"),
);
ctl = new Ctl(connector, new lib.CtlConfig("control"));
});
it("prints log events as JSON lines only when streaming the log", async function() {
const info = { level: "info", message: "hello" };
ctl.output = new CommandOutput("json");
const logged = await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info })));
assert(logged.every(line => !line.startsWith("{")), "log event printed as a result");
ctl.output.streamingLog = true;
assert.deepEqual(
await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info, timestamp: 1 }))),
['{"level":"info","message":"hello","timestamp":1}'],
);
});
it("logs log events in human mode", async function() {
ctl.output.streamingLog = true;
const info = { level: "verbose", message: "hello" };
assert.deepEqual(await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent(info))), []);
});
it("prints debug WebSocket messages", async function() {
const event = new lib.DebugWsMessageEvent("in", "content");
assert.deepEqual(await captureLog(() => ctl.handleDebugWsMessageEvent(event)), ["WS in content"]);
ctl.output = new CommandOutput("json");
assert.deepEqual(
await captureLog(() => ctl.handleDebugWsMessageEvent(event)),
['{"direction":"in","content":"content"}'],
);
});
});
});
+43
View File
@@ -336,6 +336,49 @@ describe("class Instance", function() {
}); });
}); });
describe(".checkRequiredMods()", function() {
beforeEach(function() {
instance.loadedPlugins = new Set([
{ name: "foo", requiredMods: ["foo_mod", "bar_mod >= 1.1.0", "bar_mod < 2.0.0"] },
{ name: "bar" },
]);
});
function modPack(mods) {
return lib.ModPack.fromJSON({ name: "pack", mods });
}
it("should accept a mod pack with the required mods", function() {
instance.checkRequiredMods(modPack([
{ name: "foo_mod", enabled: true, version: "1.0.0" },
{ name: "bar_mod", enabled: true, version: "1.1.0" },
]));
});
it("should reject missing, disabled and out of range mods", function() {
const expected = (unmet) => new lib.ConflictError(
"Mod pack pack does not have the mods required by the loaded plugins. " +
`Plugin foo requires ${unmet}. ` +
"Add the mods to the mod pack or set <plugin>.load_plugin to false for this instance."
);
assert.throws(
() => instance.checkRequiredMods(modPack([{ name: "bar_mod", enabled: true, version: "1.0.0" }])),
expected("foo_mod (missing), bar_mod >= 1.1.0 (found 1.0.0)"),
);
assert.throws(
() => instance.checkRequiredMods(modPack([
{ name: "foo_mod", enabled: false, version: "1.0.0" },
{ name: "bar_mod", enabled: true, version: "2.0.0" },
])),
expected("foo_mod (disabled), bar_mod < 2.0.0 (found 2.0.0)"),
);
});
it("should ignore plugins that are not loaded", function() {
instance.loadedPlugins = new Set([{ name: "bar" }]);
instance.checkRequiredMods(modPack([]));
});
});
describe("save name checks", function() { describe("save name checks", function() {
it("should reject a start save outside the saves directory", async function() { it("should reject a start save outside the saves directory", async function() {
await assert.rejects( await assert.rejects(
+6
View File
@@ -252,6 +252,7 @@ describe("host/src/pairing", function() {
config = new lib.HostConfig("host", undefined, path.join(directory, "config-host.json")); config = new lib.HostConfig("host", undefined, path.join(directory, "config-host.json"));
config.set("host.name", "My Host"); config.set("host.name", "My Host");
config.set("host.controller_url", stub.url); config.set("host.controller_url", stub.url);
config.restartRequired = false;
}); });
afterEach(async function() { afterEach(async function() {
await fs.rm(directory, { recursive: true, force: true, maxRetries: 10 }); await fs.rm(directory, { recursive: true, force: true, maxRetries: 10 });
@@ -279,6 +280,9 @@ describe("host/src/pairing", function() {
assert.equal(saved["host.id"], 1234); assert.equal(saved["host.id"], 1234);
assert.equal(saved["host.name"], "Approved"); assert.equal(saved["host.name"], "Approved");
assert.equal(saved["host.controller_token"], "host-token"); assert.equal(saved["host.controller_token"], "host-token");
assert.equal(config.restartRequired, false, "pairing flagged a restart");
config.set("host.controller_token", "other-token");
assert.equal(config.restartRequired, true, "later change did not flag a restart");
}); });
it("does nothing when the controller accepts the saved token", async function() { it("does nothing when the controller accepts the saved token", async function() {
@@ -294,6 +298,7 @@ describe("host/src/pairing", function() {
it("asks to join again with the user token when the saved token is not accepted", async function() { it("asks to join again with the user token when the saved token is not accepted", async function() {
config.set("host.controller_token", "revoked-token"); config.set("host.controller_token", "revoked-token");
config.restartRequired = false;
stub.handler = ({ url, headers }) => { stub.handler = ({ url, headers }) => {
if (url === "/api/host-pairing/check") { if (url === "/api/host-pairing/check") {
return [401, undefined]; return [401, undefined];
@@ -307,6 +312,7 @@ describe("host/src/pairing", function() {
assert.equal(saved["host.id"], 5); assert.equal(saved["host.id"], 5);
assert.equal(saved["host.controller_token"], "new-token"); assert.equal(saved["host.controller_token"], "new-token");
assert(!JSON.stringify(saved).includes("user-token"), "user token must not be saved"); assert(!JSON.stringify(saved).includes("user-token"), "user token must not be saved");
assert.equal(config.restartRequired, false, "pairing flagged a restart");
}); });
it("stops when aborted while checking the saved token", async function() { it("stops when aborted while checking the saved token", async function() {
+103 -2
View File
@@ -273,6 +273,28 @@ describe("host/server", function() {
}); });
}); });
describe("._watchWhitelist()", function() {
it("should emit the added and removed players on change", async function() {
const whitelistPath = path.join(writePath, "server-whitelist.json");
await fs.mkdir(writePath, { recursive: true });
await fs.writeFile(whitelistPath, JSON.stringify(["a", "b"]));
server.enableWhitelist = true;
server._whitelist = new Set(["a", "b"]);
server._watchWhitelist();
try {
const waiter = events.once(server, "whitelist-change");
await fs.writeFile(whitelistPath, JSON.stringify(["b", "c", "c"]));
const [added, removed] = await waiter;
assert.deepEqual(added, ["c"]);
assert.deepEqual(removed, ["a"]);
assert.deepEqual([...server._whitelist], ["b", "c"]);
} finally {
server._whitelistWatcher?.close();
server.enableWhitelist = false;
}
});
});
describe(".fromOptions()", function() { describe(".fromOptions()", function() {
it("should resolve the requested version", async function() { it("should resolve the requested version", async function() {
const found = await hostServer.FactorioServer.fromOptions( const found = await hostServer.FactorioServer.fromOptions(
@@ -306,6 +328,54 @@ describe("host/server", function() {
}); });
}); });
describe(".binaryPath()", function() {
const _platform = process.platform;
const _arch = process.arch;
const installDir = path.join("temp", "test", "binary-path");
function serverFor(dir) {
return new hostServer.FactorioServer(
installDir, writePath, path.join(installDir, dir, "data"), "2.1.24", { rconPassword: "" }
);
}
function mockHost(platform, arch) {
Object.defineProperty(process, "platform", { value: platform });
Object.defineProperty(process, "arch", { value: arch });
}
before(async function() {
await fs.rm(installDir, { force: true, recursive: true });
const layouts = [["none", []], ["x64", ["x64"]], ["arm64", ["arm64"]], ["both", ["x64", "arm64"]]];
for (const [dir, arches] of layouts) {
await fs.mkdir(path.join(installDir, dir, "data"), { recursive: true });
for (const arch of arches) {
await fs.mkdir(path.join(installDir, dir, "bin", arch), { recursive: true });
await fs.writeFile(path.join(installDir, dir, "bin", arch, "factorio"), "");
}
}
});
afterEach(function() {
mockHost(_platform, _arch);
});
const binary = (dir, arch) => path.join(installDir, dir, "bin", arch, "factorio");
for (const [platform, arch, dir, expected] of [
["linux", "x64", "both", "x64"],
["linux", "arm64", "both", "arm64"],
["linux", "x64", "arm64", "arm64"],
["linux", "arm64", "x64", "x64"],
["linux", "x64", "none", "x64"],
["linux", "arm64", "none", "arm64"],
["win32", "arm64", "none", "x64"],
]) {
it(`should pick bin/${expected} on ${platform} ${arch} with ${dir} installed`, function() {
mockHost(platform, arch);
assert.equal(serverFor(dir).binaryPath(), binary(dir, expected));
});
}
it("should use MacOS/factorio on darwin", function() {
mockHost("darwin", "arm64");
assert.equal(serverFor("both").binaryPath(), path.join(installDir, "both", "MacOS", "factorio"));
});
});
describe("._handleIpc()", function() { describe("._handleIpc()", function() {
it("should emit the correct ipc event", async function() { it("should emit the correct ipc event", async function() {
let waiter = events.once(server, "ipc-channel"); let waiter = events.once(server, "ipc-channel");
@@ -738,15 +808,46 @@ describe("host/server", function() {
stable: false, stable: false,
version: "0.1.0", version: "0.1.0",
headlessUrl: "test3", headlessUrl: "test3",
}], logger), new Error("Failed to fetch test1: -1 Fetch called")); }], logger), /Failed to fetch \S+\/get-download\/0\.1\.5\/headless\/linux64/);
assert.equal(fetchCalledWith, "test1"); assert.equal(fetchCalledWith, "https://www.factorio.com/get-download/0.1.5/headless/linux64");
assert.ok(logLine !== null); assert.ok(logLine !== null);
assert.ok(logLine.endsWith("starting download...")); assert.ok(logLine.endsWith("starting download..."));
}); });
}); });
/* eslint-enable no-loop-func */ /* eslint-enable no-loop-func */
} }
describe("on ARM64 Linux", function() {
const _arch = process.arch;
const factorioDir = path.join("test", "file", "factorio");
beforeEach(function() {
Object.defineProperty(process, "platform", { value: "linux" });
Object.defineProperty(process, "arch", { value: "arm64" });
});
afterEach(function() {
Object.defineProperty(process, "arch", { value: _arch });
});
const versions = [
{ stable: false, version: "2.1.25", headlessUrl: "test1" },
{ stable: true, version: "2.1.17", headlessUrl: "test2" },
{ stable: true, version: "0.1.5", headlessUrl: "test3" },
];
it("should download the linux-arm64 build", async function() {
await assert.rejects(hostServer.checkForUpdates(factorioDir, "latest", versions), /Fetch called/);
assert.equal(fetchCalledWith, "https://www.factorio.com/get-download/2.1.25/headless/linux-arm64");
});
it("should reject versions older than 2.1.18", async function() {
await assert.rejects(
hostServer.checkForUpdates(factorioDir, "2.1.17", versions),
/Factorio 2.1.17 has no ARM64 Linux headless build to download, ARM64 builds start at 2.1.18/
);
assert.equal(fetchCalledWith, null);
});
it("should accept an older version that is already installed", async function() {
await hostServer.checkForUpdates(factorioDir, "0.1", versions);
assert.equal(fetchCalledWith, null);
});
});
describe("concurrent downloads", function() { describe("concurrent downloads", function() {
const versions = [{ stable: true, version: "0.1.5", headlessUrl: "test1" }]; const versions = [{ stable: true, version: "0.1.5", headlessUrl: "test1" }];
+132 -4
View File
@@ -2064,6 +2064,9 @@ describe("Integration of Clusterio", function() {
const roundtrip = lib.ModPack.fromModPackString(result.stdout.trim()); const roundtrip = lib.ModPack.fromModPackString(result.stdout.trim());
roundtrip.id = reference.id; roundtrip.id = reference.id;
assert.deepEqual(roundtrip, reference); assert.deepEqual(roundtrip, reference);
const shown = await execCtlOutput("mod-pack show imported-pack");
assert.match(shown, /empty_mod 1\.0\.0/);
assert.match(shown, /MyDouble: 12\.25/);
}); });
}); });
@@ -2119,6 +2122,7 @@ describe("Integration of Clusterio", function() {
describe("mod show", function() { describe("mod show", function() {
it("gives details of a mod", async function() { it("gives details of a mod", async function() {
assert.match(await execCtlOutput("mod show empty_mod 1.0.0"), /name: empty_mod/);
let result = await execCtlProcess("mod show empty_mod 1.0.0"); let result = await execCtlProcess("mod show empty_mod 1.0.0");
let hash = await lib.hashFile(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip")); let hash = await lib.hashFile(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip"));
let stat = await fs.stat(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip")); let stat = await fs.stat(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip"));
@@ -2204,8 +2208,10 @@ describe("Integration of Clusterio", function() {
}); });
describe("role list", function() { describe("role list", function() {
it("runs", async function() { it("prints permissions as a list", async function() {
await execCtl("role list"); const output = await execCtlOutput("role list");
assert(!output.includes("[object"), output);
assert.match(output, /Cluster Admin .*core\.admin/);
}); });
}); });
@@ -2315,9 +2321,131 @@ describe("Integration of Clusterio", function() {
}); });
}); });
describe("config edit", function() {
/** Write an editor script that replaces the edited file with content */
async function writeEditor(content) {
const file = path.resolve("temp", "test", "editor.sh");
await fs.writeFile(file, `#!/bin/sh\ncat > "$1" <<'EOF'\n${content}\nEOF\n`, { mode: 0o755 });
return file;
}
before(function() {
if (process.platform === "win32") {
this.skip();
}
});
it("sets controller config fields and reports failed ones", async function() {
const config = await getControl().send(new lib.ControllerConfigGetRequest());
const name = config["controller.name"];
const editor = await writeEditor("controller.name = Edited-Cluster\ncontroller.no_such_field = 1");
await execCtl(`controller config edit ${editor}`);
const edited = await getControl().send(new lib.ControllerConfigGetRequest());
assert.equal(edited["controller.name"], "Edited-Cluster");
await execCtl(`controller config set controller.name ${name}`);
});
it("sets host config fields and reports failed ones", async function() {
const config = await getControl().sendTo({ hostId }, new lib.HostConfigGetRequest());
const editor = await writeEditor(`host.name = ${config["host.name"]}\nhost.no_such_field = 1`);
await execCtl(`host config edit ${hostId} ${editor}`);
});
it("sets instance config fields and reports failed ones", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json instance create config-edit"));
try {
const editor = await writeEditor("instance.auto_start = true\ninstance.no_such_field = 1");
await execCtl(`instance config edit ${id} ${editor}`);
const config = await getControl().send(new lib.InstanceConfigGetRequest(id));
assert.equal(config["instance.auto_start"], true);
} finally {
await execCtl(`instance delete ${id}`);
}
});
});
describe("--output json", function() {
it("prints lists as an array of objects", async function() {
const roles = JSON.parse(await execCtlOutput("--output json role list"));
assert.deepEqual(roles.find(role => role.id === 0).permissions, ["core.admin"]);
});
it("prints single results as an object", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json role create json-role"));
const roles = await getControl().send(new lib.RoleListRequest());
assert.equal(roles.find(role => role.id === id)?.name, "json-role");
assert.equal(await execCtlOutput(`--output json role delete ${id}`), '{"success":true}');
});
it("prints the toJSON format in wire mode", async function() {
const [jsonUser] = JSON.parse(await execCtlOutput("--output json user list"));
const [wireUser] = JSON.parse(await execCtlOutput("--output wire user list"));
assert("roleIds" in jsonUser);
assert(!("roleIds" in wireUser));
assert.equal(wireUser.name, jsonUser.name);
});
it("prints a success line for commands without a result", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json role create human-role"));
assert.equal(await execCtlOutput(`role delete ${id}`), "Command ran successfully");
});
it("prints whole objects for lists that hide fields in human mode", async function() {
const [mod] = JSON.parse(await execCtlOutput("--output json mod list"));
assert("sha1" in mod);
const [wireMod] = JSON.parse(await execCtlOutput("--output wire mod list"));
assert("factorio_version" in wireMod);
const [modPack] = JSON.parse(await execCtlOutput("--output wire mod-pack list"));
assert("mods" in modPack);
const user = JSON.parse(await execCtlOutput("--output wire user show test --instance-stats"));
assert.equal(user.name, "test");
});
it("prints the user and instance stats in human mode", async function() {
const output = await execCtlOutput("user show test --instance-stats");
assert.match(output, /name +\| test/);
});
it("prints the result of human mode commands with more options", async function() {
assert.match(await execCtlOutput("user list --stats --attributes"), /isAdmin/);
const exchange = JSON.parse(await execCtlOutput(
`--output json debug parse-exchange-string ${testStrings.default.replace(/\s/g, "")}`
));
assert.equal(typeof exchange, "object");
});
it("follows the log without a final result", async function() {
try {
assert.equal(await execCtlOutput("--output json log follow --controller"), "");
} finally {
getControl().keepOpen = false;
}
});
it("prints log queries in human mode", async function() {
assert.notEqual(await execCtlOutput("log query --controller --limit 1"), "");
});
it("prints pending host requests in human mode", async function() {
assert.notEqual(await execCtlOutput("host pending"), "");
});
it("prints log queries as an array", async function() {
const log = JSON.parse(await execCtlOutput("--output json log query --controller --limit 1"));
assert.equal(log.length, 1);
assert.equal(typeof log[0].message, "string");
});
it("prints errors as JSON with a non-zero exit code", async function() {
await assert.rejects(
execCtlProcess("--output json instance stop no-such-instance"),
err => {
assert.equal(err.code, 1);
assert.deepEqual(
JSON.parse(err.stdout),
{ error: "CommandError", message: "No instance named no-such-instance" },
);
return true;
},
);
await assert.rejects(
execCtlProcess("--output json user set-admin no-such-user"),
err => JSON.parse(err.stdout).error === "NotFoundError",
);
});
});
describe("user list", function() { describe("user list", function() {
it("runs", async function() { it("prints roles and instances as lists", async function() {
await execCtl("user list"); const output = await execCtlOutput("user list");
assert(!output.includes("[object"), output);
assert.match(output, /roleIds/);
}); });
}); });
+23 -23
View File
@@ -10,7 +10,7 @@ import events from "node:events";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import { LineSplitter, logger } from "@clusterio/lib"; import { LineSplitter, logger } from "@clusterio/lib";
import { selectTargetCommand, initialize as initializeCtl } from "@clusterio/ctl"; import { runCommand, initialize as initializeCtl } from "@clusterio/ctl";
import { _listFactorioVersions } from "@clusterio/host/dist/node/src/server.js"; import { _listFactorioVersions } from "@clusterio/host/dist/node/src/server.js";
import { pairHost } from "@clusterio/host"; import { pairHost } from "@clusterio/host";
@@ -242,8 +242,7 @@ export async function execCtl(command) {
} }
control.config = initArgs.ctlConfig; control.config = initArgs.ctlConfig;
const targetCommand = selectTargetCommand(initArgs.args, initArgs.rootCommands); await runCommand(initArgs.args, initArgs.rootCommands, control);
await targetCommand.run(initArgs.args, control);
} finally { } finally {
process.chdir("../.."); process.chdir("../..");
inExecCtl = false; inExecCtl = false;
@@ -297,16 +296,8 @@ export async function writeHostConfig(configPath, pairing, fields = {}) {
/** @returns {Promise<child_process.ChildProcess>} */ /** @returns {Promise<child_process.ChildProcess>} */
export function spawn(name, cmd, waitFor, options = {}) { export function spawn(name, cmd, waitFor, options = {}) {
const silent = process.env.SILENT_TEST;
const bootstrap = !controllerProcess || !hostProcess;
function log(...args) {
if (!silent || bootstrap) {
console.log(...args);
}
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
log(cmd); console.log(cmd);
let parts = cmd.split(" "); let parts = cmd.split(" ");
let process = child_process.spawn(parts[0], parts.slice(1), childOptions(options)); let process = child_process.spawn(parts[0], parts.slice(1), childOptions(options));
let stdout = new LineSplitter({ readableObjectMode: true }); let stdout = new LineSplitter({ readableObjectMode: true });
@@ -314,13 +305,9 @@ export function spawn(name, cmd, waitFor, options = {}) {
let onData = line => { let onData = line => {
line = line.toString("utf8"); line = line.toString("utf8");
if (waitFor.test(line)) { if (waitFor.test(line)) {
if (silent) {
stdout.off("data", onData);
stderr.off("data", onData);
}
resolve(process); resolve(process);
} }
log(name, line); console.log(name, line);
}; };
stdout.on("data", onData); stdout.on("data", onData);
stderr.on("data", onData); stderr.on("data", onData);
@@ -336,12 +323,6 @@ export function spawnNode(name, cmd, waitFor, options) {
before(async function() { before(async function() {
this.timeout(40000); this.timeout(40000);
const silent = process.env.SILENT_TEST;
if (silent) {
console.log("SILENT_TEST is present in env, loggers after bootstrap will be muted.");
}
// If fast test is enabled then output that it is // If fast test is enabled then output that it is
if (process.env.FAST_TEST) { if (process.env.FAST_TEST) {
@@ -371,6 +352,22 @@ before(async function() {
sourceDir: "packages/host/lua/clusterio_lib", sourceDir: "packages/host/lua/clusterio_lib",
outputDir: modsDir, outputDir: modsDir,
}); });
// Stand-in for the mod required by the subspace_storage plugin
const subspaceStorageDir = path.join("temp", "test", "subspace_storage");
await fs.rm(subspaceStorageDir, { force: true, recursive: true });
await fs.mkdir(subspaceStorageDir, { recursive: true });
await fs.writeFile(path.join(subspaceStorageDir, "info.json"), JSON.stringify({
name: "subspace_storage",
variants: [
{ version: "2.3.11", factorio_version: "1.1" },
{ version: "2.3.20", factorio_version: "2.0" },
{ version: "2.3.21", factorio_version: "2.1" },
],
title: "Subspace Storage test stub",
author: "Clusterio Team",
dependencies: ["base", "clusterio_lib"],
}));
await lib.build({ build: true, pack: true, sourceDir: subspaceStorageDir, outputDir: modsDir });
console.log("Setting Controller Config"); console.log("Setting Controller Config");
await execController("config create"); await execController("config create");
@@ -428,6 +425,9 @@ before(async function() {
const [major, minor] = lib.normaliseMajorMinorVersion(latestFactorioVersion).split("."); const [major, minor] = lib.normaliseMajorMinorVersion(latestFactorioVersion).split(".");
testPack.mods.set("clusterio_lib", { name: "clusterio_lib", enabled: true, version: `2.1.${major}${minor}` }); testPack.mods.set("clusterio_lib", { name: "clusterio_lib", enabled: true, version: `2.1.${major}${minor}` });
testPack.mods.set(
"subspace_storage", { name: "subspace_storage", enabled: true, version: `2.3.${major}${minor}` }
);
await control.sendTo("controller", new lib.ModPackCreateRequest(testPack)); await control.sendTo("controller", new lib.ModPackCreateRequest(testPack));
await control.sendTo( await control.sendTo(
+3
View File
@@ -33,6 +33,9 @@ describe("Integration of lib/link", function() {
hostConnector = new TestHostConnector(url, 0.2); hostConnector = new TestHostConnector(url, 0.2);
hostConnector.token = linkHost.token; hostConnector.token = linkHost.token;
hostConnector.hostId = linkHost.hostId; hostConnector.hostId = linkHost.hostId;
// The controller requests system info on connect and waits for it before disconnecting
const hostLink = new lib.Link(hostConnector);
hostLink.handle(lib.SystemInfoRequest, () => lib.gatherSystemInfo(linkHost.hostId, false, false, 0));
}); });
afterEach(async function() { afterEach(async function() {
await controlConnector.disconnect(); await controlConnector.disconnect();
+40 -1
View File
@@ -1,10 +1,49 @@
import assert from "node:assert/strict"; import assert from "node:assert/strict";
import { readFile } from "node:fs/promises"; import { readFile } from "node:fs/promises";
import { fetchFactorioVersions } from "@clusterio/lib"; import { factorioHeadlessUrl, fetchFactorioVersions, hasFactorioHeadless } from "@clusterio/lib";
import { slowTest, externalTest } from "../../common.js"; import { slowTest, externalTest } from "../../common.js";
describe("FactorioVersions", function() { describe("FactorioVersions", function() {
describe("hasFactorioHeadless", function() {
for (const [version, platform, arch, expected] of [
["0.17.79", "linux", "x64", true],
["2.1.17", "linux", "arm64", false],
["2.1.18", "linux", "arm64", true],
["2.1.24", "win32", "x64", false],
["2.1.24", "win32", "arm64", false],
]) {
it(`should return ${expected} for ${version} ${platform} ${arch}`, function() {
assert.equal(hasFactorioHeadless(version, platform, arch), expected);
});
}
});
describe("factorioHeadlessUrl", function() {
it("should return the linux64 build for x64", function() {
assert.equal(
factorioHeadlessUrl("2.1.24", "linux", "x64"),
"https://www.factorio.com/get-download/2.1.24/headless/linux64",
);
});
it("should return the linux-arm64 build for arm64", function() {
assert.equal(
factorioHeadlessUrl("2.1.24", "linux", "arm64"),
"https://www.factorio.com/get-download/2.1.24/headless/linux-arm64",
);
});
it("should throw for Windows", function() {
assert.throws(
() => factorioHeadlessUrl("2.1.24", "win32", "x64"),
new Error("Factorio 2.1.24 has no headless build for win32 x64"),
);
});
it("should throw for linux arm64 before 2.1.18", function() {
assert.throws(
() => factorioHeadlessUrl("2.1.17", "linux", "arm64"),
new Error("Factorio 2.1.17 has no headless build for linux arm64"),
);
});
});
describe("fetchFactorioVersions", function() { describe("fetchFactorioVersions", function() {
const originalFetch = global.fetch; const originalFetch = global.fetch;
+18
View File
@@ -24,4 +24,22 @@ describe("lib/plugin", function() {
} }
}); });
}); });
describe("pluginRequiredMods()", function() {
it("should parse dependency strings", function() {
const mods = lib.pluginRequiredMods({
name: "foo", title: "Foo", requiredMods: ["bar", "baz >= 1.2.0", "baz < 2.0.0"],
});
assert.deepEqual(mods.map(mod => [mod.name, mod.version?.toString()]), [
["bar", undefined], ["baz", ">= 1.2.0"], ["baz", "< 2.0.0"],
]);
});
it("should return an empty list without requiredMods", function() {
assert.deepEqual(lib.pluginRequiredMods({ name: "foo", title: "Foo" }), []);
});
it("should reject dependency prefixes and invalid versions", function() {
for (const spec of ["? bar", "! bar", "~ bar", "bar >= 1..2"]) {
assert.throws(() => lib.pluginRequiredMods({ name: "foo", title: "Foo", requiredMods: [spec] }));
}
});
});
}); });
+15
View File
@@ -2,6 +2,21 @@
This folder contains files used for performing manual tests that are non-trivial to automate. This folder contains files used for performing manual tests that are non-trivial to automate.
## Plugins page on small screens
Use a cluster with several plugins, including one with different running and installed versions and one with a web load error.
Compare the Plugins page at 390px and 1440px viewport widths, and check the 768px breakpoint.
1. On phones, check that each row shows its name, versions, machine counts, status and update action vertically, without clipping or breaking ordinary words.
2. Check that a long plugin name wraps and the header branding stays on one line.
3. Search and sort by name, reload, and check that the selected filter and sort order remain applied.
4. Open an update or restart checklist, then cancel it; update actions must not navigate to the plugin page.
5. Open a plugin by its name and by clicking elsewhere in its row.
6. Open the account menu using the user icon on mobile; the full account name should appear in the menu.
7. On desktop, check that the name, versions and update action remain separate columns.
## Setup test mod ## Setup test mod
Run `node packages/lib/dist/node/build_mod.js --source-dir test/manual/test_mod/ --output-dir mods/` to create the test mod. Run `node packages/lib/dist/node/build_mod.js --source-dir test/manual/test_mod/ --output-dir mods/` to create the test mod.
+61
View File
@@ -0,0 +1,61 @@
import assert from "node:assert/strict";
import { checkPullRequestBody } from "../scripts/pr_to_changelog.mts";
import { findChangelogProblems } from "../scripts/check_pr_changelog.mts";
describe("scripts/check_pr_changelog", function() {
function body(...lines) {
return ["Description.", "", "### Changelog", ...lines].join("\r\n");
}
it("accepts entries in a code block", function() {
const text = body(
"```",
"### Features",
"- Added doodad. #12",
"",
"### Fixes",
"- Fixed [#13](https://github.com/clusterio/clusterio/issues/13)",
"```",
);
assert.deepEqual(findChangelogProblems(text), []);
assert.deepEqual({ ...checkPullRequestBody(text).changelog }, {
"Features": ["Added doodad. #12"],
"Fixes": ["Fixed #13"],
});
});
it("accepts None. in place of the code block", function() {
assert.deepEqual(findChangelogProblems(body("", "None. Only changes CI.")), []);
});
it("rejects a missing body or section", function() {
assert.deepEqual(findChangelogProblems(null), ["Pull request has no body"]);
assert.deepEqual(findChangelogProblems("Description only."), ["\"### Changelog\" header not found"]);
assert.deepEqual(findChangelogProblems(body("Text")), ["Code block not found after header"]);
assert.deepEqual(findChangelogProblems(body("```", "### Fixes")), ["End of code block not found after header"]);
assert.deepEqual(findChangelogProblems(body("```", "", "```")), [
"Code block is empty, replace it with \"None.\" if nothing applies",
]);
});
it("rejects None. inside the code block", function() {
assert.deepEqual(findChangelogProblems(body("```", "None.", "```")), [
"\"None.\" on line 1 must replace the code block, not sit inside it",
]);
});
it("rejects malformed entries", function() {
const text = body("```", "- Orphan #1", "### Bugs", "- Fixed it", "Loose text", "```");
assert.deepEqual(findChangelogProblems(text), [
"Unexpected content \"- Orphan #1\" outside of a changelog section on line 1",
"Changelog section Bugs on line 2 is not one of " +
"Major Features, Features, Fixes, Changes, Breaking Changes, Meta",
"Changelog entry on line 3 does not reference an issue",
"Unexpected content \"Loose text\" on line 4",
]);
});
it("rejects the unedited template", function() {
const text = body(
"```", "### Features", "- Added doodad to web interface. #999", "", "### Changes", "- ...", "```",
);
assert.deepEqual(findChangelogProblems(text), ["Changelog entry on line 5 does not reference an issue"]);
});
});
+46
View File
@@ -0,0 +1,46 @@
// Spec reporter controlling console output with TEST_OUTPUT:
// verbose prints everything, normal (default) prints only output from failing
// tests and hooks, silent prints only the results.
"use strict";
const util = require("node:util");
const Mocha = require("mocha");
const { Spec } = Mocha.reporters;
const { EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_TEST_PENDING } = Mocha.Runner.constants;
const methods = ["log", "info", "debug", "warn", "error"];
const modes = ["verbose", "normal", "silent"];
class CaptureReporter extends Spec {
constructor(runner, options) {
super(runner, options);
const mode = process.env.TEST_OUTPUT || "normal";
if (!modes.includes(mode)) {
throw new Error(`Invalid TEST_OUTPUT "${mode}", expected one of ${modes.join(", ")}`);
}
if (mode === "verbose") {
return;
}
const original = {};
const captured = [];
for (const method of methods) {
original[method] = console[method];
console[method] = mode === "silent"
? () => {}
: (...args) => { captured.push([method, util.format(...args)]); };
}
const discard = () => { captured.length = 0; };
runner.on(EVENT_TEST_PASS, discard);
runner.on(EVENT_TEST_PENDING, discard);
// Prepend so the output lands above the failure line.
runner.prependListener(EVENT_TEST_FAIL, () => {
for (const [method, text] of captured) {
original[method](text);
}
discard();
});
}
}
module.exports = CaptureReporter;
-3
View File
@@ -5,11 +5,8 @@ import { ConsoleTransport, TerminalFormat, logger } from "@clusterio/lib";
// running tests individually. // running tests individually.
before(function() { before(function() {
const silent = process.env.SILENT_TEST;
logger.add(new ConsoleTransport({ logger.add(new ConsoleTransport({
level: "info", level: "info",
format: new TerminalFormat(), format: new TerminalFormat(),
filter: () => !silent,
})); }));
}); });
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"extends": "./tsconfig.base.json", "extends": "./tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"lib": ["es2023"], "lib": ["es2024", "es2025.collection"],
"types": ["node"], "types": ["node"],
"module": "nodenext", "module": "nodenext",
"moduleResolution": "node16", "moduleResolution": "node16",