From ca6c59bfb87e76102d82a37625eb42c9a81f97a6 Mon Sep 17 00:00:00 2001 From: Gianluca Zuccarelli Date: Tue, 29 Apr 2025 16:38:38 +0000 Subject: [PATCH] store/cockpitApi: add worker config mutation Add an endpoint to update the worker config. --- src/store/cockpit/cockpitApi.ts | 51 ++++++++++++++++++++++++- src/store/cockpit/enhancedCockpitApi.ts | 8 +++- src/store/cockpit/types.ts | 16 ++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/store/cockpit/cockpitApi.ts b/src/store/cockpit/cockpitApi.ts index 0fc9f02b..972181c6 100644 --- a/src/store/cockpit/cockpitApi.ts +++ b/src/store/cockpit/cockpitApi.ts @@ -6,7 +6,7 @@ import path from 'path'; // the `tsconfig` to stubs of the `cockpit` and `cockpit/fsinfo` // modules. These stubs are in the `src/test/mocks/cockpit` directory. // We also needed to create an alias in vitest to make this work. -import TOML from '@ltd/j-toml'; +import TOML, { Section } from '@ltd/j-toml'; import cockpit from 'cockpit'; import { fsinfo } from 'cockpit/fsinfo'; import { v4 as uuidv4 } from 'uuid'; @@ -18,7 +18,11 @@ import { v4 as uuidv4 } from 'uuid'; // the same unix socket. This allows us to split out the code a little // bit so that the `cockpitApi` doesn't become a monolith. import { contentSourcesApi } from './contentSourcesApi'; -import type { WorkerConfigResponse } from './types'; +import type { + UpdateWorkerConfigApiArg, + WorkerConfigFile, + WorkerConfigResponse, +} from './types'; import { mapHostedToOnPrem, @@ -608,6 +612,48 @@ export const cockpitApi = contentSourcesApi.injectEndpoints({ } }, }), + updateWorkerConfig: builder.mutation< + WorkerConfigResponse, + UpdateWorkerConfigApiArg + >({ + queryFn: async ({ updateWorkerConfigRequest }) => { + try { + const workerConfig = cockpit.file( + '/etc/osbuild-worker/osbuild-worker.toml', + { + superuser: 'required', + } + ); + + const contents = await workerConfig.modify((prev: string) => { + if (!updateWorkerConfigRequest) { + return prev; + } + + const merged = { + ...TOML.parse(prev), + ...updateWorkerConfigRequest, + } as WorkerConfigFile; + + const contents: WorkerConfigFile = {}; + Object.keys(merged).forEach((key: string) => { + contents[key] = Section({ + ...merged[key], + }); + }); + + return TOML.stringify(contents, { + newline: '\n', + newlineAround: 'document', + }); + }); + + return { data: TOML.parse(contents) }; + } catch (error) { + return { error }; + } + }, + }), }; }, // since we are inheriting some endpoints, @@ -632,4 +678,5 @@ export const { useGetBlueprintComposesQuery, useGetComposeStatusQuery, useGetWorkerConfigQuery, + useUpdateWorkerConfigMutation, } = cockpitApi; diff --git a/src/store/cockpit/enhancedCockpitApi.ts b/src/store/cockpit/enhancedCockpitApi.ts index 71958828..a346068d 100644 --- a/src/store/cockpit/enhancedCockpitApi.ts +++ b/src/store/cockpit/enhancedCockpitApi.ts @@ -1,7 +1,7 @@ import { cockpitApi } from './cockpitApi'; const enhancedApi = cockpitApi.enhanceEndpoints({ - addTagTypes: ['Blueprint', 'Blueprints', 'Composes'], + addTagTypes: ['Blueprint', 'Blueprints', 'Composes', 'WorkerConfig'], endpoints: { getBlueprint: { providesTags: () => { @@ -31,6 +31,12 @@ const enhancedApi = cockpitApi.enhanceEndpoints({ getBlueprintComposes: { providesTags: [{ type: 'Composes' }], }, + getWorkerConfig: { + providesTags: [{ type: 'WorkerConfig' }], + }, + updateWorkerConfig: { + invalidatesTags: [{ type: 'WorkerConfig' }], + }, }, }); diff --git a/src/store/cockpit/types.ts b/src/store/cockpit/types.ts index 5d7350fc..3614390b 100644 --- a/src/store/cockpit/types.ts +++ b/src/store/cockpit/types.ts @@ -28,6 +28,22 @@ export type WorkerConfigResponse = { aws?: AWSWorkerConfig; }; +export type WorkerConfigFile = { + // the worker file has a key value/pair for + // each section, which could be of any type. + // Disable the linter warning for this. + // eslint-disable-next-line + [key: string]: any; +}; + export type CloudProviderConfigState = { aws: AWSWorkerConfig; }; + +export type WorkerConfigRequest = { + aws?: AWSWorkerConfig | undefined; +}; + +export type UpdateWorkerConfigApiArg = { + updateWorkerConfigRequest: WorkerConfigRequest | undefined; +};