Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/contextdir optional #236

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import type { CatalogManager } from './catalogManager';

const mocks = vi.hoisted(() => {
return {
parseYamlMock: vi.fn(),
parseYamlFileMock: vi.fn(),
builImageMock: vi.fn(),
listImagesMock: vi.fn(),
getImageInspectMock: vi.fn(),
Expand All @@ -52,7 +52,7 @@ const mocks = vi.hoisted(() => {
};
});
vi.mock('../models/AIConfig', () => ({
parseYaml: mocks.parseYamlMock,
parseYamlFile: mocks.parseYamlFileMock,
}));
vi.mock('@podman-desktop/api', () => ({
containerEngine: {
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('pullApplication', () => {
vi.spyOn(fs, 'readFileSync').mockImplementation((_path: string) => {
return '';
});
mocks.parseYamlMock.mockReturnValue({
mocks.parseYamlFileMock.mockReturnValue({
application: {
containers: [
{
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('pullApplication', () => {
url: '',
};

mocks.parseYamlMock.mockReturnValue({
mocks.parseYamlFileMock.mockReturnValue({
application: {
containers: [],
},
Expand Down Expand Up @@ -437,7 +437,7 @@ describe('getConfiguration', () => {
],
},
};
mocks.parseYamlMock.mockReturnValue(aiConfig);
mocks.parseYamlFileMock.mockReturnValue(aiConfig);

const result = manager.getConfiguration('config', 'local');
expect(result.path).toEqual(path.join('local', 'config'));
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as path from 'node:path';
import { type PodCreatePortOptions, containerEngine, type TelemetryLogger } from '@podman-desktop/api';
import type { RecipeStatusRegistry } from '../registries/RecipeStatusRegistry';
import type { AIConfig, AIConfigFile, ContainerConfig } from '../models/AIConfig';
import { parseYaml } from '../models/AIConfig';
import { parseYamlFile } from '../models/AIConfig';
import type { Task } from '@shared/src/models/ITask';
import { RecipeStatusUtils } from '../utils/recipeStatusUtils';
import { getParentDirectory } from '../utils/pathUtils';
Expand Down Expand Up @@ -489,12 +489,12 @@ export class ApplicationManager {

// Parsing the configuration
console.log(`Reading configuration from ${configFile}.`);
const rawConfiguration = fs.readFileSync(configFile, 'utf-8');
let aiConfig: AIConfig;
try {
aiConfig = parseYaml(rawConfiguration, goarch());
aiConfig = parseYamlFile(configFile, goarch());
} catch (err) {
throw new Error('Cannot load configuration file.');
console.error('Cannot load configure file.', err);
throw new Error(`Cannot load configuration file.`);
}

// Mark as success.
Expand Down
49 changes: 49 additions & 0 deletions packages/backend/src/models/AIConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, test, describe, vi } from 'vitest';
import fs from 'fs';
import { type AIConfig, parseYamlFile } from './AIConfig';

// Define mock file paths and contents
const mockYamlPath = '/path/to/mock.yml';
const mockYamlContent = `
application:
containers:
- name: container1
contextdir: /path/to/dir1
arch: ["x86"]
model-service: true
gpu-env: ["env1", "env2"]
- name: container2
arch: ["arm"]
`;

const readFileSync = vi.spyOn(fs, 'readFileSync');

describe('parseYaml', () => {
test('should parse valid YAML file', () => {
readFileSync.mockReturnValue(mockYamlContent);

const defaultArch = 'x64';
const expectedConfig: AIConfig = {
application: {
containers: [
{
name: 'container1',
contextdir: '/path/to/dir1',
arch: ['x86'],
modelService: true,
gpu_env: ['env1', 'env2'],
},
{
name: 'container2',
contextdir: '.',
arch: ['arm'],
modelService: false,
gpu_env: [],
},
],
},
};

expect(parseYamlFile(mockYamlPath, defaultArch)).toEqual(expectedConfig);
});
});
32 changes: 23 additions & 9 deletions packages/backend/src/models/AIConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
***********************************************************************/

import * as jsYaml from 'js-yaml';
import fs from 'fs';

export interface ContainerConfig {
name: string;
Expand Down Expand Up @@ -46,7 +47,9 @@ export function assertString(value: unknown): string {
throw new Error('value not a string');
}

export function parseYaml(raw: string, defaultArch: string): AIConfig {
export function parseYamlFile(filepath: string, defaultArch: string): AIConfig {
const raw: string = fs.readFileSync(filepath, 'utf-8');

const aiStudioConfig = jsYaml.load(raw);
const application = aiStudioConfig?.['application'];
if (!application) throw new Error('AIConfig has bad formatting.');
Expand All @@ -55,14 +58,25 @@ export function parseYaml(raw: string, defaultArch: string): AIConfig {

return {
application: {
containers: containers.map(container => ({
arch: Array.isArray(container['arch']) ? container['arch'] : [defaultArch],
modelService: container['model-service'] === true,
containerfile: isString(container['containerfile']) ? container['containerfile'] : undefined,
contextdir: assertString(container['contextdir']),
name: assertString(container['name']),
gpu_env: Array.isArray(container['gpu-env']) ? container['gpu-env'] : [],
})),
containers: containers.map(container => {
if (typeof container !== 'object') throw new Error('containers array malformed');

let contextdir: string;
if ('contextdir' in container) {
contextdir = assertString(container['contextdir']);
} else {
contextdir = '.';
}

return {
arch: Array.isArray(container['arch']) ? container['arch'] : [defaultArch],
modelService: container['model-service'] === true,
containerfile: isString(container['containerfile']) ? container['containerfile'] : undefined,
contextdir: contextdir,
name: assertString(container['name']),
gpu_env: Array.isArray(container['gpu-env']) ? container['gpu-env'] : [],
};
}),
},
};
}
Loading