Skip to content

Commit

Permalink
feat: upload model on qemu
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Stocchi authored and lstocchi committed Mar 1, 2024
1 parent c957f6d commit 06565f8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
33 changes: 10 additions & 23 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class ApplicationManager {
Target: `/${modelName}`,
Source: modelPath,
Type: 'bind',
Mode: 'Z',
},
],
};
Expand All @@ -279,35 +280,21 @@ export class ApplicationManager {
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
59 changes: 59 additions & 0 deletions packages/backend/src/models/QemuUploader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import * as podmanDesktopApi from '@podman-desktop/api';
import { getFirstRunningMachine, getPodmanCli } from '../utils/podman';
import type { UploadWorker } from './uploader';
import path from 'node:path';

export class QemuUploader implements UploadWorker {
async canUpload(): Promise<boolean> {
const machine = await getFirstRunningMachine();
return machine.VMType === 'qemu';

Check failure on line 27 in packages/backend/src/models/QemuUploader.ts

View workflow job for this annotation

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

src/models/uploader.spec.ts > perform > should return localModelPath if no workers for current system

TypeError: Cannot read properties of undefined (reading 'VMType') ❯ QemuUploader.canUpload src/models/QemuUploader.ts:27:20 ❯ Uploader.perform src/models/uploader.ts:45:25 ❯ src/models/uploader.spec.ts:55:20

Check failure on line 27 in packages/backend/src/models/QemuUploader.ts

View workflow job for this annotation

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

src/models/uploader.spec.ts > perform > should return remote path if there is a worker for current system

TypeError: Cannot read properties of undefined (reading 'VMType') ❯ QemuUploader.canUpload src/models/QemuUploader.ts:27:20 ❯ Uploader.perform src/models/uploader.ts:45:25 ❯ src/models/uploader.spec.ts:61:20

Check failure on line 27 in packages/backend/src/models/QemuUploader.ts

View workflow job for this annotation

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

src/models/uploader.spec.ts > perform > should return localModelPath if no workers for current system

TypeError: Cannot read properties of undefined (reading 'VMType') ❯ QemuUploader.canUpload src/models/QemuUploader.ts:27:20 ❯ Uploader.perform src/models/uploader.ts:45:25 ❯ src/models/uploader.spec.ts:55:20

Check failure on line 27 in packages/backend/src/models/QemuUploader.ts

View workflow job for this annotation

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

src/models/uploader.spec.ts > perform > should return remote path if there is a worker for current system

TypeError: Cannot read properties of undefined (reading 'VMType') ❯ QemuUploader.canUpload src/models/QemuUploader.ts:27:20 ❯ Uploader.perform src/models/uploader.ts:45:25 ❯ src/models/uploader.spec.ts:61:20

Check failure on line 27 in packages/backend/src/models/QemuUploader.ts

View workflow job for this annotation

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

src/models/uploader.spec.ts > perform > should return localModelPath if no workers for current system

TypeError: Cannot read properties of undefined (reading 'VMType') ❯ QemuUploader.canUpload src/models/QemuUploader.ts:27:20 ❯ Uploader.perform src/models/uploader.ts:45:25 ❯ src/models/uploader.spec.ts:55:20

Check failure on line 27 in packages/backend/src/models/QemuUploader.ts

View workflow job for this annotation

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

src/models/uploader.spec.ts > perform > should return remote path if there is a worker for current system

TypeError: Cannot read properties of undefined (reading 'VMType') ❯ QemuUploader.canUpload src/models/QemuUploader.ts:27:20 ❯ Uploader.perform src/models/uploader.ts:45:25 ❯ src/models/uploader.spec.ts:61:20
}

async upload(localPath: string): Promise<string> {
if (!localPath) {
throw new Error('invalid local path');
}

const machine = await getFirstRunningMachine();
const remotePath = `/var/home/core/${path.basename(localPath)}`;
// check if model already loaded on the podman machine
let existsRemote = true;
try {
await podmanDesktopApi.process.exec(getPodmanCli(), ['machine', 'ssh', machine.Name, 'stat', remotePath]);
} catch (e) {
existsRemote = false;
}

// if not exists remotely it copies it from the local path
if (!existsRemote) {
await podmanDesktopApi.process.exec(getPodmanCli(), [
'machine',
'ssh',
machine.Name,
'cp',
localPath,
remotePath,
]);
}

return remotePath;
}
}
2 changes: 1 addition & 1 deletion packages/backend/src/models/WSLUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { getFirstRunningMachine, getPodmanCli } from '../utils/podman';
import type { UploadWorker } from './uploader';

export class WSLUploader implements UploadWorker {
canUpload(): boolean {
async canUpload(): Promise<boolean> {
return podmanDesktopApi.env.isWindows;
}

Expand Down
13 changes: 10 additions & 3 deletions packages/backend/src/models/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import { EventEmitter, type Event } from '@podman-desktop/api';
import { WSLUploader } from './WSLUploader';
import { getDurationSecondsSince } from '../utils/utils';
import type { CompletionProgressiveEvent, ProgressiveEvent } from '../utils/progressiveEvent';
import { QemuUploader } from './QemuUploader';

export interface UploadWorker {
canUpload: () => boolean;
canUpload: () => Promise<boolean>;
upload: (path: string) => Promise<string>;
}

Expand All @@ -35,11 +36,17 @@ export class Uploader {
private localModelPath: string,
private abortSignal?: AbortSignal,
) {
this.#workers = [new WSLUploader()];
this.#workers = [new WSLUploader(), new QemuUploader()];
}

async perform(): Promise<string> {
const workers = this.#workers.filter(w => w.canUpload());
const workers = [];
for (const worker of this.#workers) {
const canUpload = await worker.canUpload();
if (canUpload) {
workers.push(worker);
}
}
let modelPath = this.localModelPath;
try {
if (workers && workers.length > 1) {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/utils/podman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type MachineJSON = {
Starting: boolean;
Default: boolean;
UserModeNetworking?: boolean;
VMType?: string;
};

export function getPodmanCli(): string {
Expand Down

0 comments on commit 06565f8

Please sign in to comment.