Skip to content

Commit

Permalink
chore: updated to reflect changes in another PRs
Browse files Browse the repository at this point in the history
Signed-off-by: Evzen Gasta <[email protected]>
  • Loading branch information
gastoner committed Oct 17, 2024
1 parent c08cff2 commit 5555b35
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
7 changes: 3 additions & 4 deletions extensions/podman/packages/extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { getPodmanCli, getPodmanInstallation } from './podman-cli';
import { PodmanConfiguration } from './podman-configuration';
import { PodmanInfoHelper } from './podman-info-helper';
import { HyperVCheck, PodmanInstall, WSL2Check, WSLVersionCheck } from './podman-install';
import { PodmanMachineStream } from './podman-machine-stream';
import { ProviderConnectionShellAccessImpl } from './podman-machine-stream';
import { PodmanRemoteConnections } from './podman-remote-connections';
import { QemuHelper } from './qemu-helper';
import { RegistrySetup } from './registry-setup';
Expand Down Expand Up @@ -813,14 +813,13 @@ export async function registerProviderFor(
};
}

const shellAccessProvider = new PodmanMachineStream(machineInfo);

const providerConnectionShellAccess = new ProviderConnectionShellAccessImpl(machineInfo);
const containerProviderConnection: extensionApi.ContainerProviderConnection = {
name: machineInfo.name,
displayName: prettyMachineName(machineInfo.name),
type: 'podman',
status: () => podmanMachinesStatuses.get(machineInfo.name) ?? 'unknown',
shellAccess: shellAccessProvider,
shellAccess: providerConnectionShellAccess,
lifecycle,
endpoint: {
socketPath,
Expand Down
46 changes: 34 additions & 12 deletions extensions/podman/packages/extension/src/podman-machine-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ import * as fs from 'node:fs';

import type {
Event,
ProviderConnectionShellAccess,
ProviderConnectionShellAccessData,
ProviderConnectionShellAccessError,
ShellDimensions,
ProviderConnectionShellAccessSession,
ProviderConnectionShellDimensions,
} from '@podman-desktop/api';
import { EventEmitter } from '@podman-desktop/api';
import type { ClientChannel } from 'ssh2';
import { Client } from 'ssh2';

import type { MachineInfo } from './extension';

export class PodmanMachineStream {
export class ProviderConnectionShellAccessImpl implements ProviderConnectionShellAccess {
#host: string;
#port: number;
#username: string;
Expand Down Expand Up @@ -61,26 +63,38 @@ export class PodmanMachineStream {
}
}

setWindow(dimensions: ShellDimensions): void {
resize(dimensions: ProviderConnectionShellDimensions): void {
if (this.#stream) {
// rows and cols override width and height when rows and cols are non-zero.
this.#stream.setWindow(dimensions.rows, dimensions.cols, 0, 0);
}
}

stopConnection(): void {
this.#connected = false;
this.#client?.end();
this.#client?.destroy();
disposeListeners(): void {
this.onDataEmit.dispose();
this.onErrorEmit.dispose();
this.onEndEmit.dispose();
}

startConnection(): void {
// To avoid strating multiple SSH connections
close(): void {
this.#connected = false;
this.#client?.end();
this.#client?.destroy();
this.disposeListeners();
}

open(): ProviderConnectionShellAccessSession {
// Avoid multiple connections
if (this.#connected) {
return;
this.disposeListeners();
return {
onData: this.onData,
onError: this.onError,
onEnd: this.onEnd,
write: this.write.bind(this),
resize: this.resize.bind(this),
close: this.close,
};
}

this.#client = new Client();
Expand All @@ -92,14 +106,13 @@ export class PodmanMachineStream {
this.onErrorEmit.fire({ error: err.message });
return;
}

this.#connected = true;
this.#stream = stream;

stream
.on('close', () => {
this.onEndEmit.fire();
this.stopConnection();
this.close();
})
.on('data', (data: string) => {
this.onDataEmit.fire({ data: data });
Expand All @@ -115,5 +128,14 @@ export class PodmanMachineStream {
username: this.#username,
privateKey: fs.readFileSync(this.#privateKey),
});

return {
onData: this.onData,
onError: this.onError,
onEnd: this.onEnd,
write: this.write.bind(this),
resize: this.resize.bind(this),
close: this.close,
};
}
}
66 changes: 42 additions & 24 deletions packages/extension-api/src/extension-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,45 +789,63 @@ declare module '@podman-desktop/api' {
connection: ContainerProviderConnection;
}

export interface ProviderConnectionShellAccess {
onData: Event<ProviderConnectionShellAccessData>;
onError: Event<ProviderConnectionShellAccessError>;
onEnd: Event<void>;
write(data: string): void;
startConnection(): void;
stopConnection(): void;
setWindow(dimensions: ShellDimensions): void;
}

export interface ShellDimensions {
rows: number;
cols: number;
}

export interface ProviderConnectionShellAccessError {
error: string;
}

export interface ProviderConnectionShellAccessData {
data: string;
}

/**
* Callback for openning shell session
*/
export class ProviderConnectionShellAccess {
/**
* Opens new session using ProviderConnectionShellAccessImpl class
* @example
* const providerConnectionShellAccess = new ProviderConnectionShellAccessImpl(machineInfo);
* const session = providerConnectionShellAccess.open()
*/
open(): ProviderConnectionShellAccessSession;
}

/**
* Callbacks for interaction with shell session
*/
export interface ProviderConnectionShellAccessSession {
/**
* Receiving data event
* @example
* session.onData(data => {...
*/
onData: Event<ProviderConnectionShellAccessData>;

/**
* Error event
* @example
* session.onError(error => {...
*/
onError: Event<ProviderConnectionShellAccessError>;

/**
* End event
* @example
* session.onEnd(onEnd);
*/
onEnd: Event<void>;
write(data: string): void;

/**
* Sends data
* @example
* session.write(data)
*/
write(data: string | Uint8Array): void;

/**
* Notifies server that terminal window has been resized
* @example
* session.resize(dimensions)
*/
resize(dimensions: ProviderConnectionShellDimensions): void;

/**
* Closes opened session and removes all listeners
* @example
* session.close()
*/
close(): void;
}

Expand Down

0 comments on commit 5555b35

Please sign in to comment.