Skip to content

Commit

Permalink
Merge pull request #6076 from mook-as/win32/wsl-helper-rename
Browse files Browse the repository at this point in the history
WSL-Helper: Use a temporary name
  • Loading branch information
jandubois authored Nov 29, 2023
2 parents 032f6e3 + 4168dfa commit 42fdf79
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 37 deletions.
18 changes: 9 additions & 9 deletions e2e/wsl-integrations.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ test.describe('WSL Integrations', () => {
utf16le: true,
},
...['alpha', 'beta', 'gamma'].flatMap(distro => [
...[['bin', 'docker-compose'], ['wsl-helper']].flatMap(tool => ([
...[['bin', 'docker-compose'], ['wsl-helper-1.11.1']].flatMap(tool => ([
{
args: ['--distribution', distro, '--exec', '/bin/wslpath', '-a', '-u', path.join(process.cwd(), 'resources', 'linux', ...tool)],
mode: 'repeated',
stdout: `/${ distro }/${ tool.join('/') }`,
}])),
...[['bin', 'docker-buildx'], ['wsl-helper']].flatMap(tool => ([
...[['bin', 'docker-buildx'], ['wsl-helper-1.11.1']].flatMap(tool => ([
{
args: ['--distribution', distro, '--exec', '/bin/wslpath', '-a', '-u', path.join(process.cwd(), 'resources', 'linux', ...tool)],
mode: 'repeated',
stdout: `/${ distro }/${ tool.join('/') }`,
}])),
...[
[`/${ distro }/wsl-helper`, 'kubeconfig', '--enable=false'],
[`/${ distro }/wsl-helper`, 'kubeconfig', '--enable=true'],
[`/${ distro }/wsl-helper-1.11.1`, 'kubeconfig', '--enable=false'],
[`/${ distro }/wsl-helper-1.11.1`, 'kubeconfig', '--enable=true'],
['/bin/sh', '-c', 'mkdir -p "$HOME/.docker/cli-plugins"'],
['/bin/sh', '-c',
`if [ ! -e "$HOME/.docker/cli-plugins/docker-compose" -a ! -L "$HOME/.docker/cli-plugins/docker-compose" ] ; then
Expand All @@ -112,28 +112,28 @@ test.describe('WSL Integrations', () => {
stdout: '/dev/null',
},
{
args: ['--distribution', distro, '--user', 'root', '--exec', `/${ distro }/wsl-helper`, 'docker-proxy', 'serve', '--verbose'],
args: ['--distribution', distro, '--user', 'root', '--exec', `/${ distro }/wsl-helper-1.11.1`, 'docker-proxy', 'serve', '--verbose'],
mode: 'repeated',
stdout: '/dev/null',
},
{
args: ['--distribution', distro, '--user', 'root', '--exec', `/${ distro }/wsl-helper`, 'docker-proxy', 'kill', '--verbose'],
args: ['--distribution', distro, '--user', 'root', '--exec', `/${ distro }/wsl-helper-1.11.1`, 'docker-proxy', 'kill', '--verbose'],
mode: 'repeated',
stdout: '/dev/null',
},
]),
{
args: ['--distribution', 'alpha', '--exec', '/alpha/wsl-helper', 'kubeconfig', '--show'],
args: ['--distribution', 'alpha', '--exec', '/alpha/wsl-helper-1.11.1', 'kubeconfig', '--show'],
mode: 'repeated',
stdout: (opts?.alpha ?? false).toString(),
},
{
args: ['--distribution', 'beta', '--exec', '/beta/wsl-helper', 'kubeconfig', '--show'],
args: ['--distribution', 'beta', '--exec', '/beta/wsl-helper-1.11.1', 'kubeconfig', '--show'],
mode: 'repeated',
stdout: (opts?.beta ?? true).toString(),
},
{
args: ['--distribution', 'gamma', '--exec', '/gamma/wsl-helper', 'kubeconfig', '--show'],
args: ['--distribution', 'gamma', '--exec', '/gamma/wsl-helper-1.11.1', 'kubeconfig', '--show'],
mode: 'repeated',
stdout: (opts?.gamma ?? 'some error').toString(),
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/rancher-desktop/backend/wsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import clone from '@pkg/utils/clone';
import Logging from '@pkg/utils/logging';
import { wslHostIPv4Address } from '@pkg/utils/networks';
import paths from '@pkg/utils/paths';
import { executable } from '@pkg/utils/resources';
import { jsonStringifyWithWhiteSpace } from '@pkg/utils/stringify';
import { defined, RecursivePartial } from '@pkg/utils/typeUtils';

Expand Down Expand Up @@ -1745,7 +1746,7 @@ export default class WSLBackend extends events.EventEmitter implements VMBackend
// We need to get the Linux path to our helper executable; it is easier to
// just get WSL to do the transformation for us.

return this.wslify(path.join(paths.resources, 'linux', 'wsl-helper'), distro);
return this.wslify(executable('wsl-helper-linux'), distro);
}

/**
Expand Down
32 changes: 16 additions & 16 deletions pkg/rancher-desktop/integrations/windowsIntegrationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class WindowsIntegrationManager implements IntegrationManager {
console.debug('Spawning Windows docker proxy');

return spawn(
path.join(paths.resources, 'win32', 'wsl-helper.exe'),
executable('wsl-helper'),
['docker-proxy', 'serve', ...this.wslHelperDebugArgs], {
stdio: ['ignore', stream, stream],
windowsHide: true,
Expand Down Expand Up @@ -238,15 +238,14 @@ export default class WindowsIntegrationManager implements IntegrationManager {
/**
* Return the Linux path to the WSL helper executable.
*/
protected async getLinuxToolPath(distro: string, ...tool: string[]): Promise<string> {
protected async getLinuxToolPath(distro: string, tool: string): Promise<string> {
// We need to get the Linux path to our helper executable; it is easier to
// just get WSL to do the transformation for us.

const logStream = Logging[`wsl-helper.${ distro }`];
const { stdout } = await spawnFile(
await this.wslExe,
['--distribution', distro, '--exec', '/bin/wslpath', '-a', '-u',
path.join(paths.resources, 'linux', ...tool)],
['--distribution', distro, '--exec', '/bin/wslpath', '-a', '-u', tool],
{ stdio: ['ignore', 'pipe', logStream] },
);

Expand Down Expand Up @@ -291,15 +290,15 @@ export default class WindowsIntegrationManager implements IntegrationManager {

console.debug(`Syncing ${ distro } socket proxy: ${ shouldRun ? 'should' : 'should not' } run.`);
if (shouldRun) {
const executable = await this.getLinuxToolPath(distro, 'wsl-helper');
const linuxExecutable = await this.getLinuxToolPath(distro, executable('wsl-helper-linux'));
const logStream = Logging[`wsl-helper.${ distro }`];

this.distroSocketProxyProcesses[distro] ??= new BackgroundProcess(
`${ distro } socket proxy`,
{
spawn: async() => {
return spawn(await this.wslExe,
['--distribution', distro, '--user', 'root', '--exec', executable,
['--distribution', distro, '--user', 'root', '--exec', linuxExecutable,
'docker-proxy', 'serve', ...this.wslHelperDebugArgs],
{
stdio: ['ignore', await logStream.fdStream, await logStream.fdStream],
Expand All @@ -312,7 +311,7 @@ export default class WindowsIntegrationManager implements IntegrationManager {
// Ensure we kill the WSL-side process; sometimes things can get out
// of sync.
await this.execCommand({ distro, root: true },
executable, 'docker-proxy', 'kill', ...this.wslHelperDebugArgs);
linuxExecutable, 'docker-proxy', 'kill', ...this.wslHelperDebugArgs);
},
});
this.distroSocketProxyProcesses[distro].start();
Expand Down Expand Up @@ -358,7 +357,7 @@ export default class WindowsIntegrationManager implements IntegrationManager {
throw new Error("Can't find home directory");
}
const cliDir = path.join(homeDir, '.docker', 'cli-plugins');
const srcPath = executable(pluginName);
const srcPath = executable(pluginName as any); // It's an executable in `bin`
const cliPath = path.join(cliDir, path.basename(srcPath));

console.debug(`Syncing host ${ pluginName }: ${ srcPath } -> ${ cliPath }`);
Expand Down Expand Up @@ -389,7 +388,8 @@ export default class WindowsIntegrationManager implements IntegrationManager {
*/
protected async syncDistroDockerPlugin(distro: string, pluginName: string, state: boolean) {
try {
const srcPath = await this.getLinuxToolPath(distro, 'bin', pluginName);
const srcPath = await this.getLinuxToolPath(distro,
path.join(paths.resources, 'linux', 'bin', pluginName));
const destDir = '$HOME/.docker/cli-plugins';
const destPath = `${ destDir }/${ pluginName }`;

Expand Down Expand Up @@ -433,14 +433,14 @@ export default class WindowsIntegrationManager implements IntegrationManager {
if (this.settings.experimental?.virtualMachine?.networkingTunnel) {
await this.execCommand(
{ distro, root: true },
await this.getLinuxToolPath(distro, 'wsl-helper'),
await this.getLinuxToolPath(distro, executable('wsl-helper-linux')),
'update-host',
`--entries=${ entry }`,
);
} else {
await this.execCommand(
{ distro, root: true },
await this.getLinuxToolPath(distro, 'wsl-helper'),
await this.getLinuxToolPath(distro, executable('wsl-helper-linux')),
'update-host',
`--remove`,
);
Expand All @@ -464,7 +464,7 @@ export default class WindowsIntegrationManager implements IntegrationManager {
WSLENV: `${ process.env.WSLENV }:KUBECONFIG/up`,
},
},
await this.getLinuxToolPath(distro, 'wsl-helper'),
await this.getLinuxToolPath(distro, executable('wsl-helper-linux')),
'kubeconfig',
`--enable=${ state && this.settings.kubernetes?.enabled }`,
`--rd-networking=${ rdNetworking }`,
Expand Down Expand Up @@ -518,10 +518,10 @@ export default class WindowsIntegrationManager implements IntegrationManager {

protected async markIntegration(distro: string, state: boolean): Promise<void> {
try {
const executable = await this.getLinuxToolPath(distro, 'wsl-helper');
const exe = await this.getLinuxToolPath(distro, executable('wsl-helper-linux'));
const mode = state ? 'set' : 'delete';

await this.execCommand({ distro, root: true }, executable, 'wsl', 'integration-state', `--mode=${ mode }`);
await this.execCommand({ distro, root: true }, exe, 'wsl', 'integration-state', `--mode=${ mode }`);
} catch (ex) {
console.error(`Failed to mark integration for ${ distro }:`, ex);
}
Expand All @@ -548,10 +548,10 @@ export default class WindowsIntegrationManager implements IntegrationManager {
return `Rancher Desktop can only integrate with v2 WSL distributions (this is v${ distro.version }).`;
}
try {
const executable = await this.getLinuxToolPath(distro.name, 'wsl-helper');
const exe = await this.getLinuxToolPath(distro.name, executable('wsl-helper-linux'));
const stdout = await this.captureCommand(
{ distro: distro.name },
executable, 'wsl', 'integration-state', '--mode=show');
exe, 'wsl', 'integration-state', '--mode=show');

console.debug(`WSL distro "${ distro.name }": wsl-helper output: "${ stdout.trim() }"`);
if (['true', 'false'].includes(stdout.trim())) {
Expand Down
30 changes: 26 additions & 4 deletions pkg/rancher-desktop/utils/resources.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import os from 'os';
import path from 'path';

import memoize from 'lodash/memoize';

import paths from '@pkg/utils/paths';

/**
* executableMap is a mapping of valid executable names and their path.
* If the value is `undefined`, then it's assumed to be an executable in the
* user-accessible `bin` directory.
* Otherwise, it's an array containing the path to the executable.
*/
const executableMap: Record<string, string[] | undefined> = {
docker: undefined,
kubectl: undefined,
nerdctl: undefined,
rdctl: undefined,
'wsl-helper': [paths.resources, process.platform, platformBinary('wsl-helper-1.11.1')],
'wsl-helper-linux': [paths.resources, 'linux', 'wsl-helper-1.11.1'],
};

function platformBinary(name: string): string {
return process.platform === 'win32' ? `${ name }.exe` : name;
}

/**
* Gets the absolute path to an executable. Adds ".exe" to the end
* if running on Windows.
* @param name The name of the binary, without file extension.
*/
function _executable(name: string) {
const osSpecificName = os.platform().startsWith('win') ? `${ name }.exe` : name;
function _executable(name: keyof typeof executableMap): string {
const parts = executableMap[name];

if (parts === undefined) {
return path.join(paths.resources, process.platform, 'bin', platformBinary(name));
}

return path.join(paths.resources, os.platform(), 'bin', osSpecificName);
return path.join(...parts);
}
export const executable = memoize(_executable);

Expand Down
8 changes: 3 additions & 5 deletions pkg/rancher-desktop/utils/wslVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
* version.
*/

import path from 'path';

import { spawnFile } from '@pkg/utils/childProcess';
import logging from '@pkg/utils/logging';
import paths from '@pkg/utils/paths';
import { executable } from '@pkg/utils/resources';

type WSLVersionInfo = {
installed: boolean;
Expand All @@ -28,8 +26,8 @@ const console = logging['wsl-version'];
* Get information about the currently installed WSL version.
*/
export default async function getWSLVersion(): Promise<WSLVersionInfo> {
const wslHelper = path.join(paths.resources, 'win32', 'wsl-helper.exe');
const { stdout } = await spawnFile(wslHelper, ['wsl', 'info'], { stdio: ['ignore', 'pipe', console] });
const { stdout } = await spawnFile(executable('wsl-helper'),
['wsl', 'info'], { stdio: ['ignore', 'pipe', console] });

return JSON.parse(stdout);
}
3 changes: 2 additions & 1 deletion scripts/lib/build-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ export default {
* @param platform The platform to build for.
*/
const buildPlatform = async(platform: 'linux' | 'win32') => {
const exeName = platform === 'win32' ? 'wsl-helper.exe' : 'wsl-helper';
const exeRoot = 'wsl-helper-1.11.1';
const exeName = `${ exeRoot }${ platform === 'win32' ? '.exe' : '' }`;
const outFile = path.join(this.rootDir, 'resources', platform, exeName);

await this.spawn('go', 'build', '-ldflags', '-s -w', '-o', outFile, '.', {
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/sign-win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function sign(workDir: string) {
const binDir = path.join(resourcesRootDir, 'bin');
const whiteList: Record<string, Array<string>> = {
'.': ['Rancher Desktop.exe'],
[resourcesRootDir]: ['wsl-helper.exe'],
[resourcesRootDir]: ['wsl-helper-1.11.1.exe'],
[internalDir]: ['host-resolver.exe', 'host-switch.exe', 'privileged-service.exe', 'steve.exe', 'vtunnel.exe'],
[binDir]: ['docker.exe', 'docker-credential-none.exe', 'nerdctl.exe', 'rdctl.exe'],
};
Expand Down

0 comments on commit 42fdf79

Please sign in to comment.