Skip to content

Commit

Permalink
store/cockpitApi: scan for blueprint files
Browse files Browse the repository at this point in the history
Add an initial commit to scan a preset directory for user blueprint
files. This makes use of the cockpit files api.
  • Loading branch information
kingsleyzissou committed Nov 20, 2024
1 parent 92c01e8 commit 091ff7d
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions src/store/cockpitApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import TOML from '@ltd/j-toml';

import cockpit from 'cockpit';
import { fsinfo } from 'cockpit/fsinfo';

import {
GetArchitecturesApiResponse,
GetArchitecturesApiArg,
GetBlueprintsApiArg,
GetBlueprintsApiResponse,
BlueprintItem,
} from './imageBuilderApi';

const emptyCockpitApi = createApi({
Expand All @@ -28,22 +33,58 @@ export const cockpitApi = emptyCockpitApi.injectEndpoints({
GetBlueprintsApiResponse,
GetBlueprintsApiArg
>({
queryFn: () => {
// TODO: Add cockpit file api support for reading in blueprints.
// For now we're just hardcoding a dummy response
// so we can render an empty table.
return new Promise((resolve) => {
resolve({
queryFn: async () => {
try {
if (!cockpit) {
throw new Error('Cockpit API is not available');
}

const user = await cockpit.user();

// we will use the user's `.local` directory
// to save blueprints used for on-prem
// TODO: remove the hardcode
const path = `${user.home}/.local/share/cockpit/image-builder-frontend/blueprints`;

// we probably don't need any more information other
// than the entries from the directory
const info = await fsinfo(path, ['entries'], {
superuser: 'try',
});

const entries = Object.entries(info?.entries || {});
const blueprints: BlueprintItem[] = await Promise.all(
entries.map(async ([filename, _]) => {
const file = cockpit.file(`${path}/${filename}`);

const contents = await file.read();
const parsed = TOML.parse(contents);
file.close();

return {
name: parsed.name as string,
id: parsed.version as string,
version: parsed.version as number,
description: parsed.description as string,
last_modified_at: Date.now().toString(),
};
})
);

return {
data: {
meta: { count: 0 },
meta: { count: blueprints.length },
links: {
// TODO: figure out the pagination
first: '',
last: '',
},
data: [],
data: blueprints,
},
});
});
};
} catch (error) {
return { error: error.message || 'Unknown error occurred' };
}
},
}),
};
Expand Down

0 comments on commit 091ff7d

Please sign in to comment.