Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net Core Attach support for windows container #1948

Merged
merged 5 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions src/debugging/netcore/NetCoreDebugHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { localize } from '../../localize';
import { NetCoreTaskHelper, NetCoreTaskOptions } from '../../tasks/netcore/NetCoreTaskHelper';
import { ContainerTreeItem } from '../../tree/containers/ContainerTreeItem';
import { LocalOSProvider } from '../../utils/LocalOSProvider';
import { ContainerOSType, getContainerOSType } from '../../utils/osUtils';
import { pathNormalize } from '../../utils/pathNormalize';
import { PlatformOS } from '../../utils/platform';
import { unresolveWorkspaceFolder } from '../../utils/resolveVariables';
Expand Down Expand Up @@ -194,11 +195,15 @@ export class NetCoreDebugHelper implements DebugHelper {

// If debugger path is not specified, then install the debugger if it doesn't exist in the container
if (!debuggerPath) {
const debuggerDirectory = '/remote_debugger';
debuggerPath = `${debuggerDirectory}/vsdbg`;
const isDebuggerInstalled: boolean = await this.isDebuggerInstalled(containerName, debuggerPath);
const containerOS = await getContainerOSType(containerName);
const osProvider = new LocalOSProvider();
const debuggerDirectory = containerOS === 'windows' ? 'C:\\remote_debugger' : '/remote_debugger';
debuggerPath = containerOS === 'windows'
? osProvider.pathJoin(osProvider.os, debuggerDirectory, 'win7-x64', 'latest', 'vsdbg.exe')
: osProvider.pathJoin(osProvider.os, debuggerDirectory, 'vsdbg');
const isDebuggerInstalled: boolean = await this.isDebuggerInstalled(containerName, debuggerPath, containerOS);
if (!isDebuggerInstalled) {
debuggerPath = await this.copyDebuggerToContainer(containerName, debuggerDirectory);
await this.copyDebuggerToContainer(context, containerName, debuggerDirectory, containerOS);
}
}

Expand Down Expand Up @@ -287,20 +292,32 @@ export class NetCoreDebugHelper implements DebugHelper {
return pathNormalize(result, platformOS);
}

private async copyDebuggerToContainer(containerName: string, containerDebuggerDirectory: string): Promise<string> {
private async copyDebuggerToContainer(context: DockerDebugContext, containerName: string, containerDebuggerDirectory: string, containerOS: ContainerOSType): Promise<void> {
const dockerClient = new CliDockerClient(new ChildProcessProvider());
if (containerOS === 'windows') {
const isolation = await dockerClient.inspectObject(containerName, { format: '{{ .HostConfig.Isolation}}' });
if (isolation && isolation === 'hyperv') {
context.actionContext.errorHandling.suppressReportIssue = true;
throw new Error(localize('vscode-docker.debug.netcore.isolationNotSupported', 'Attaching a debugger to a Hyper-V container is not supported.'));
}
}

const yesItem: MessageItem = DialogResponses.yes;
const message = localize('vscode-docker.debug.netcore.attachingRequiresDebugger', 'Attaching to container requires .NET Core debugger in the container. Do you want to copy the debugger to the container?');
const install = (yesItem === await window.showInformationMessage(message, ...[DialogResponses.yes, DialogResponses.no]));
if (!install) {
throw new UserCancelledError();
}

// TODO: Attach doesn't support Windows yet.
await this.acquireDebuggers('Linux');
if (containerOS === 'windows') {
await this.acquireDebuggers('Windows');
} else {
await this.acquireDebuggers('Linux');
}

const hostDebuggerPath = await this.vsDbgClientFactory().getVsDbgFolder();
const containerDebuggerPath = `${containerName}:${containerDebuggerDirectory}`;
const outputManager = new DefaultOutputManager(ext.outputChannel);
const dockerClient = new CliDockerClient(new ChildProcessProvider());

await outputManager.performOperation(
localize('vscode-docker.debug.netcore.copyDebugger', 'Copying the .NET Core debugger to the container...'),
Expand All @@ -310,15 +327,20 @@ export class NetCoreDebugHelper implements DebugHelper {
localize('vscode-docker.debug.netcore.debuggerInstalled', 'Debugger copied'),
localize('vscode-docker.debug.netcore.unableToInstallDebugger', 'Unable to copy the .NET Core debugger.')
);
return `${containerDebuggerDirectory}/vsdbg`;
}

private async isDebuggerInstalled(containerName: string, debuggerPath: string): Promise<boolean> {
private async isDebuggerInstalled(containerName: string, debuggerPath: string, containerOS: ContainerOSType): Promise<boolean> {
const dockerClient = new CliDockerClient(new ChildProcessProvider());
const osProvider = new LocalOSProvider();
const command: string = osProvider.os === 'Windows' ?
`/bin/sh -c "if [ -f ${debuggerPath} ]; then echo true; fi;"`
: `/bin/sh -c 'if [ -f ${debuggerPath} ]; then echo true; fi;'`
let command: string;

if (containerOS === 'windows') {
command = `cmd /C "IF EXIST "${debuggerPath}" (echo true) else (echo false)"`;
} else {
command = osProvider.os === 'Windows' ?
`/bin/sh -c "if [ -f ${debuggerPath} ]; then echo true; fi;"`
: `/bin/sh -c 'if [ -f ${debuggerPath} ]; then echo true; fi;'`
}
const result: string = await dockerClient.exec(containerName, command, {});
return result === 'true';
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/osUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as semver from 'semver';
import { IActionContext } from 'vscode-azureextensionui';
import { ext } from '../extensionVariables';
import { callDockerodeWithErrorHandling } from './callDockerode';
import { execAsync } from './spawnAsync';

// Minimum Windows RS3 version number
const windows10RS3MinVersion = '10.0.16299';
Expand Down Expand Up @@ -76,6 +77,7 @@ export function isMac(): boolean {
}

export type DockerOSType = "windows" | "linux";
export type ContainerOSType = "windows" | "linux";

export async function getDockerOSType(context: IActionContext): Promise<DockerOSType> {
if (!isWindows()) {
Expand All @@ -88,3 +90,9 @@ export async function getDockerOSType(context: IActionContext): Promise<DockerOS
return info.OSType;
}
}

export async function getContainerOSType(containerId: string): Promise<ContainerOSType> {
ravipal marked this conversation as resolved.
Show resolved Hide resolved
const inspectCmd: string = `docker inspect --format="{{ .Platform}}" ${containerId}`;
const execResult = await execAsync(inspectCmd);
ravipal marked this conversation as resolved.
Show resolved Hide resolved
return execResult.stdout.trim().toLowerCase() === 'windows' ? 'windows' : 'linux';
}