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 28, 2024
1 parent 4903b4f commit 21c6872
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
46 changes: 16 additions & 30 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import {
type TelemetryLogger,
type PodInfo,
type Webview,
HostConfig,
} from '@podman-desktop/api';
import type { AIConfig, AIConfigFile, ContainerConfig } from '../models/AIConfig';
import { parseYamlFile } from '../models/AIConfig';
import type { Task } from '@shared/src/models/ITask';
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 @@ -256,62 +257,47 @@ 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',
Mode: 'Z',
},
],
};
envs = [`MODEL_PATH=/${modelName}`];
} 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, {

const podifiedName = this.getRandomName(`${image.appName}-podified`);
await containerEngine.createContainer(podInfo.engineId, {
Image: image.id,
name: podifiedName,
Detach: true,
HostConfig: hostConfig,
Env: envs,
start: false,
pod: podInfo.Id,
});
containers.push({
name: podifiedName,
modelService: image.modelService,
ports: image.ports,
});

// 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 },
);
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}`);
}
}),
);
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 21c6872

Please sign in to comment.