Skip to content

Commit

Permalink
feat: use the new function to create a container within a pod
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Feb 6, 2024
1 parent 10d4b0a commit ef06a79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
54 changes: 22 additions & 32 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import type { Recipe } from '@shared/src/models/IRecipe';
import type { GitCloneInfo, GitManager } from './gitManager';
import fs from 'fs';
import * as path from 'node:path';
import { type PodCreatePortOptions, containerEngine } from '@podman-desktop/api';
import { type PodCreatePortOptions, containerEngine, HostConfig } from '@podman-desktop/api';

Check failure on line 23 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

Import "HostConfig" is only used as types

Check failure on line 23 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Import "HostConfig" is only used as types

Check failure on line 23 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Import "HostConfig" is only used as types

Check failure on line 23 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

Import "HostConfig" is only used as types
import type { RecipeStatusRegistry } from '../registries/RecipeStatusRegistry';
import type { AIConfig, AIConfigFile, ContainerConfig } from '../models/AIConfig';
import { parseYaml } from '../models/AIConfig';
import type { Task } from '@shared/src/models/ITask';
import { RecipeStatusUtils } from '../utils/recipeStatusUtils';
import { getParentDirectory } from '../utils/pathUtils';
import { getMappedPathInPodmanMachine, getParentDirectory } from '../utils/pathUtils';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import type { ModelsManager } from './modelsManager';
import { getPortsInfo } from '../utils/ports';
Expand Down Expand Up @@ -214,62 +214,52 @@ export class ApplicationManager {
const containers: ContainerAttachedInfo[] = [];
await Promise.all(
images.map(async image => {
let hostConfig: unknown;
let hostConfig: HostConfig;
let envs: string[] = [];
// if it's a model service we mount the model as a volume
if (image.modelService) {
const modelName = path.basename(modelPath);
const mappedMountPath = getMappedPathInPodmanMachine(modelPath);
hostConfig = {
AutoRemove: true,
Mounts: [
{
Target: `/${modelName}`,
Source: modelPath,
Source: mappedMountPath,
Type: 'bind',
BindOptions: {
Propagation: 'rprivate',
},
ReadOnly: false,
},
],
};
}
envs = [`MODEL_PATH=/${modelName}`];

Check failure on line 236 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

Missing semicolon

Check failure on line 236 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Missing semicolon

Check failure on line 236 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Missing semicolon

Check failure on line 236 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

Missing semicolon
} else {
hostConfig = {
AutoRemove: true,
};
// TODO: remove static port
const modelService = images.find(image => image.modelService);
if (modelService && modelService.ports.length > 0) {
const endPoint = `http://localhost:${modelService.ports[0]}`;
envs = [`MODEL_ENDPOINT=${endPoint}`];
}
}
const createdContainer = await containerEngine.createContainer(podInfo.engineId, {
Image: image.id,
Detach: true,
HostConfig: hostConfig,
Env: envs,
start: false,
});

// now, for each container, put it in the pod
if (createdContainer) {
const podifiedName = this.getRandomName(`${image.appName}-podified`);
await containerEngine.replicatePodmanContainer(
{
id: createdContainer.id,
engineId: podInfo.engineId,
},
{ engineId: podInfo.engineId },
{ pod: podInfo.Id, name: podifiedName },
);
const podifiedName = this.getRandomName(`${image.appName}-podified`);
try {
await containerEngine.createContainer(podInfo.engineId, {
Image: image.id,
Env: envs,
name: podifiedName,
pod: podInfo.Id,
HostConfig: hostConfig,
});
containers.push({
name: podifiedName,
modelService: image.modelService,
ports: image.ports,
});
// remove the external container
await containerEngine.deleteContainer(podInfo.engineId, createdContainer.id);
} else {
throw new Error(`failed at creating container for image ${image.id}`);
} catch(e) {
console.error(e)
}

Check failure on line 261 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

Missing semicolon

Check failure on line 261 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Missing semicolon

Check failure on line 261 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Missing semicolon

Check failure on line 261 in packages/backend/src/managers/applicationManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

Missing semicolon

}),
);
return containers;
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { env } from '@podman-desktop/api';
import path from 'path';

export function getParentDirectory(filePath: string): string {
Expand All @@ -25,3 +26,11 @@ export function getParentDirectory(filePath: string): string {
// Get the directory name using path.dirname
return path.dirname(normalizedPath);
}

export function getMappedPathInPodmanMachine(path: string): string {
if (env.isWindows) {
path = path.replace(':', '').replace(/\\/g, '/').toLowerCase();
return `/mnt/${path}`;
}
return path;
}

0 comments on commit ef06a79

Please sign in to comment.