From 4a41457aeae3c38b16d1ddc1d6ce62b2376779fb Mon Sep 17 00:00:00 2001 From: lstocchi Date: Wed, 13 Mar 2024 10:45:29 +0100 Subject: [PATCH] fix: calculate machine name from running connection Signed-off-by: lstocchi --- .../backend/src/utils/WSLUploader.spec.ts | 13 +--- packages/backend/src/utils/WSLUploader.ts | 12 ++-- packages/backend/src/utils/podman.spec.ts | 71 +++++++++++++++++++ packages/backend/src/utils/podman.ts | 28 +++++--- 4 files changed, 99 insertions(+), 25 deletions(-) diff --git a/packages/backend/src/utils/WSLUploader.spec.ts b/packages/backend/src/utils/WSLUploader.spec.ts index d0b2cfac9..4ce1e01eb 100644 --- a/packages/backend/src/utils/WSLUploader.spec.ts +++ b/packages/backend/src/utils/WSLUploader.spec.ts @@ -57,15 +57,6 @@ describe('canUpload', () => { }); describe('upload', () => { - const machine2: utils.MachineJSON = { - Name: 'machine2', - CPUs: 2, - Memory: '2000', - DiskSize: '100', - Running: true, - Starting: false, - Default: true, - }; vi.spyOn(utils, 'getPodmanCli').mockReturnValue('podman'); vi.spyOn(utils, 'getFirstRunningPodmanConnection').mockResolvedValue({ connection: { @@ -83,13 +74,13 @@ describe('upload', () => { }); test('copy model if not exists on podman machine', async () => { mocks.execMock.mockRejectedValueOnce('error'); - vi.spyOn(utils, 'getFirstRunningMachine').mockResolvedValue(machine2); + vi.spyOn(utils, 'getFirstRunningMachineName').mockReturnValue('machine2'); await wslUploader.upload('C:\\Users\\podman\\folder\\file'); expect(mocks.execMock).toBeCalledWith('podman', ['machine', 'ssh', 'machine2', 'stat', '/home/user/file']); }); test('do not copy model if it exists on podman machine', async () => { mocks.execMock.mockResolvedValue(''); - vi.spyOn(utils, 'getFirstRunningMachine').mockResolvedValue(machine2); + vi.spyOn(utils, 'getFirstRunningMachineName').mockReturnValue('machine2'); await wslUploader.upload('C:\\Users\\podman\\folder\\file'); expect(mocks.execMock).toBeCalledWith('podman', ['machine', 'ssh', 'machine2', 'stat', '/home/user/file']); expect(mocks.execMock).toBeCalledWith('podman', [ diff --git a/packages/backend/src/utils/WSLUploader.ts b/packages/backend/src/utils/WSLUploader.ts index 5799d7188..49325000e 100644 --- a/packages/backend/src/utils/WSLUploader.ts +++ b/packages/backend/src/utils/WSLUploader.ts @@ -18,7 +18,7 @@ import path from 'node:path'; import * as podmanDesktopApi from '@podman-desktop/api'; -import { getFirstRunningMachine, getPodmanCli } from './podman'; +import { getFirstRunningMachineName, getPodmanCli } from './podman'; import type { UploadWorker } from './uploader'; export class WSLUploader implements UploadWorker { @@ -36,11 +36,15 @@ export class WSLUploader implements UploadWorker { .replace(`${driveLetter}:\\`, `/mnt/${driveLetter.toLowerCase()}/`) .replace(/\\/g, '/'); const remotePath = `/home/user/${path.basename(convertToMntPath)}`; - const machine = await getFirstRunningMachine(); + const machineName = getFirstRunningMachineName(); + + if (!machineName) { + throw new Error('No podman machine is running'); + } // check if model already loaded on the podman machine let existsRemote = true; try { - await podmanDesktopApi.process.exec(getPodmanCli(), ['machine', 'ssh', machine.Name, 'stat', remotePath]); + await podmanDesktopApi.process.exec(getPodmanCli(), ['machine', 'ssh', machineName, 'stat', remotePath]); } catch (e) { existsRemote = false; } @@ -50,7 +54,7 @@ export class WSLUploader implements UploadWorker { await podmanDesktopApi.process.exec(getPodmanCli(), [ 'machine', 'ssh', - machine.Name, + machineName, 'cp', convertToMntPath, remotePath, diff --git a/packages/backend/src/utils/podman.spec.ts b/packages/backend/src/utils/podman.spec.ts index 7a5a49251..57fe47328 100644 --- a/packages/backend/src/utils/podman.spec.ts +++ b/packages/backend/src/utils/podman.spec.ts @@ -70,6 +70,77 @@ describe('getPodmanCli', () => { }); }); +describe('getFirstRunningMachineName', () => { + test('return machine name if connection name does contain default Podman Machine name', () => { + mocks.getContainerConnectionsMock.mockReturnValue([ + { + connection: { + name: 'Podman Machine', + status: () => 'started', + endpoint: { + socketPath: '/endpoint.sock', + }, + type: 'podman', + }, + providerId: 'podman', + }, + ]); + const machineName = utils.getFirstRunningMachineName(); + expect(machineName).equals('podman-machine-default'); + }); + test('return machine name if connection name does contain custom Podman Machine name', () => { + mocks.getContainerConnectionsMock.mockReturnValue([ + { + connection: { + name: 'Podman Machine test', + status: () => 'started', + endpoint: { + socketPath: '/endpoint.sock', + }, + type: 'podman', + }, + providerId: 'podman', + }, + ]); + const machineName = utils.getFirstRunningMachineName(); + expect(machineName).equals('podman-machine-test'); + }); + test('return machine name if connection name does not contain Podman Machine', () => { + mocks.getContainerConnectionsMock.mockReturnValue([ + { + connection: { + name: 'test', + status: () => 'started', + endpoint: { + socketPath: '/endpoint.sock', + }, + type: 'podman', + }, + providerId: 'podman', + }, + ]); + const machineName = utils.getFirstRunningMachineName(); + expect(machineName).equals('test'); + }); + test('return undefined if there is no running connection', () => { + mocks.getContainerConnectionsMock.mockReturnValue([ + { + connection: { + name: 'machine', + status: () => 'stopped', + endpoint: { + socketPath: '/endpoint.sock', + }, + type: 'podman', + }, + providerId: 'podman', + }, + ]); + const machineName = utils.getFirstRunningMachineName(); + expect(machineName).toBeUndefined(); + }); +}); + describe('getFirstRunningPodmanConnection', () => { test('should return undefined if failing at retrieving connection', async () => { mocks.getConfigurationMock.mockRejectedValue('error'); diff --git a/packages/backend/src/utils/podman.ts b/packages/backend/src/utils/podman.ts index 10b989393..cfa6dd70a 100644 --- a/packages/backend/src/utils/podman.ts +++ b/packages/backend/src/utils/podman.ts @@ -16,7 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ import type { ProviderContainerConnection } from '@podman-desktop/api'; -import { configuration, env, process, provider } from '@podman-desktop/api'; +import { configuration, env, provider } from '@podman-desktop/api'; export type MachineJSON = { Name: string; @@ -48,16 +48,25 @@ export function getCustomBinaryPath(): string | undefined { return configuration.getConfiguration('podman').get('binary.path'); } -async function getJSONMachineList(): Promise { - const { stdout } = await process.exec(getPodmanCli(), ['machine', 'list', '--format', 'json']); - return stdout; -} +export function getFirstRunningMachineName(): string | undefined { + // the name of the podman connection is the name of the podman machine updated to make it more user friendly, + // so to retrieve the real machine name we need to revert the process -export async function getFirstRunningMachine(): Promise { + // podman-machine-default -> Podman Machine + // podman-machine-{name} -> Podman Machine {name} + // {name} -> {name} try { - const machineListOutput = await getJSONMachineList(); - const machines = JSON.parse(machineListOutput) as MachineJSON[]; - return machines.find(machine => machine.Default && machine.Running); + const runningConnection = getFirstRunningPodmanConnection(); + const runningConnectionName = runningConnection.connection.name; + if (runningConnectionName.startsWith('Podman Machine')) { + const machineName = runningConnectionName.replace(/Podman Machine\s*/, 'podman-machine-'); + if (machineName.endsWith('-')) { + return `${machineName}default`; + } + return machineName; + } else { + return runningConnectionName; + } } catch (e) { console.log(e); } @@ -75,6 +84,5 @@ export function getFirstRunningPodmanConnection(): ProviderContainerConnection | } catch (e) { console.log(e); } - return engine; }