Add config migrations (#693)

Co-authored-by: Danielv123 <danielv@danielv.no>
This commit is contained in:
Cooldude2606andDanielv123 authored and GitHub committed 2024-11-26 23:14:56 +00:00
1 parent 05ca4eb25e
commit 4248e57dcb
7 files changed
+43 -9

No files matched your search

+1 -1
View File
@@ -198,7 +198,7 @@ For the standalone installation mode you can use
If you plan to make your cluster available externally set the address
that it will be accessible under with, for example
npx clusteriocontroller config set controller.external_address http://203.0.113.4:1234/
npx clusteriocontroller config set controller.public_url http://203.0.113.4:1234/
Change the url to reflect the IP, protocol, and port the controller is accessible under, dns names are also supported.
If you're planning on making the controller accessible on the internet it's recommended to set up TLS, see the [Setting Up TLS](/docs/setting-up-tls.md) document for more details.
+2 -2
View File
@@ -51,9 +51,9 @@ interfaces.
Defaults to null.
### controller.external_address
### controller.public_url
External address the controller is accessible on.
Public address the controller is accessible on.
Currently only used for `clusteriocontroller bootstrap create-ctl-config` in order to give the right url to connect to.
This should be a full URL ending with a /.
+1 -1
View File
@@ -1115,7 +1115,7 @@ export default class Controller {
}
static calculateControllerUrl(config: lib.ControllerConfig) {
let url = config.get("controller.external_address");
let url = config.get("controller.public_url");
if (!url) {
if (config.get("controller.https_port")) {
url = `https://localhost:${config.get("controller.https_port")}/`;
+11 -1
View File
@@ -187,6 +187,16 @@ export class Config<
declare static fieldDefinitions: ConfigDefs<any>;
declare ["constructor"]: typeof Config;
/**
* Handle migration between clusterio versions
*
* @param config - Input config read and validated from file
* @returns Output config to be loaded into the class
* @protected
*/
static migrations(config: Static<typeof this.jsonSchema>) {
return config;
}
fields: Fields;
_unknownFields: Record<string, FieldValue> = {};
@@ -223,7 +233,7 @@ export class Config<
) as Fields;
if (fields) {
this.update(fields, false, location);
this.update(this.constructor.migrations(fields), false, location);
}
}
+12 -3
View File
@@ -1,7 +1,7 @@
// Core definitions for the configuration system
import * as classes from "./classes";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import { Static } from "@sinclair/typebox";
export interface ControllerConfigFields {
"controller.name": string;
@@ -11,7 +11,7 @@ export interface ControllerConfigFields {
"controller.https_port": number | null;
"controller.bind_address": string | null;
"controller.trusted_proxies": string | null;
"controller.external_address": string | null;
"controller.public_url": string | null;
"controller.tls_certificate": string | null;
"controller.tls_private_key": string | null;
"controller.auth_secret": string;
@@ -34,6 +34,15 @@ export interface ControllerConfigFields {
*/
export class ControllerConfig extends classes.Config<ControllerConfigFields> {
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => ControllerConfig;
static migrations(config: Static<typeof this.jsonSchema>) {
if (config.hasOwnProperty("controller.external_address")) {
config["controller.public_url"] = config["controller.external_address"];
delete config["controller.external_address"];
}
return config;
}
static fieldDefinitions: classes.ConfigDefs<ControllerConfigFields> = {
"controller.name": {
title: "Name",
@@ -89,7 +98,7 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
type: "string",
optional: true,
},
"controller.external_address": {
"controller.public_url": {
title: "Public URL",
description: "Public facing URL the controller is hosted on, including the protocol.",
type: "string",
+15
View File
@@ -7,6 +7,14 @@ const CA = lib.ConfigAccess;
describe("lib/config/classes", function() {
describe("Config", function() {
class TestConfig extends lib.Config {
static migrations(config) {
if (config.hasOwnProperty("test.migration")) {
config["alpha.foo"] = config["test.migration"];
delete config["test.migration"];
}
return config;
}
static fieldDefinitions = {
"alpha.foo": { type: "string", optional: true },
"beta.bar": { type: "object", initialValue: {} },
@@ -60,6 +68,13 @@ describe("lib/config/classes", function() {
assert.equal(testInstance.fields["test.enum"], "b");
assert.equal(testInstance.fields["test.priv"], undefined);
});
it("should apply migrations", function() {
const config = new TestConfig("local", {
"test.migration": "foo",
});
assert.equal(config.get("alpha.foo"), "foo");
assert.equal(config.fields["test.migration"], undefined);
});
});
describe(".toJSON()", function() {
+1 -1
View File
@@ -160,7 +160,7 @@ class MockController {
this.app.locals.controller = this;
this.app.locals.streams = new Map();
this.mockConfigEntries = new Map([
["controller.external_address", "test"],
["controller.public_url", "test"],
["controller.auth_secret", "TestSecretDoNotUse"],
["controller.proxy_stream_timeout", 1],
]);