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 28, 2024
1 parent 67fdeca commit 2e17a88
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions src/store/cockpitApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import TOML from '@ltd/j-toml';
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

// we could create an alias for this, something like
// import cockpit from 'cockpit', but this feels like
// a bit of magic and might make the code harder to
// maintain.
import cockpit from '../../pkg/lib/cockpit';
import { fsinfo } from '../../pkg/lib/cockpit/fsinfo';

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

const emptyCockpitApi = createApi({
Expand All @@ -28,22 +37,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 2e17a88

Please sign in to comment.