-
Notifications
You must be signed in to change notification settings - Fork 522
/
start-container.ts
109 lines (96 loc) · 4 KB
/
start-container.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as cp from 'child_process';
import * as fs from 'fs';
import os = require('os');
import vscode = require('vscode');
import { ImageNode } from '../explorer/models/imageNode';
import { reporter } from '../telemetry/telemetry';
import { createTerminal } from './utils/create-terminal';
import { docker, DockerEngineType } from './utils/docker-endpoint';
import { ImageItem, quickPickImage } from './utils/quick-pick-image';
const teleCmdId: string = 'vscode-docker.container.start';
export async function startContainer(context?: ImageNode, interactive?: boolean): Promise<void> {
let imageName: string;
let imageToStart: Docker.ImageDesc;
if (context && context.imageDesc) {
imageToStart = context.imageDesc;
imageName = context.label;
} else {
const selectedItem: ImageItem = await quickPickImage(false)
if (selectedItem) {
imageToStart = selectedItem.imageDesc;
imageName = selectedItem.label;
}
}
if (imageToStart) {
docker.getExposedPorts(imageToStart.Id).then((ports: string[]) => {
let options = `--rm ${interactive ? '-it' : '-d'}`;
if (ports.length) {
const portMappings = ports.map((port) => `-p ${port}:${port}`);
options += ` ${portMappings.join(' ')}`;
}
const terminal = createTerminal(imageName);
terminal.sendText(`docker run ${options} ${imageName}`);
terminal.show();
if (reporter) {
/* __GDPR__
"command" : {
"command" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
reporter.sendTelemetryEvent('command', {
command: interactive ? teleCmdId + '.interactive' : teleCmdId
});
}
});
}
}
export async function startContainerInteractive(context: ImageNode): Promise<void> {
await startContainer(context, true);
}
export async function startAzureCLI(): Promise<cp.ChildProcess> {
// block of we are running windows containers...
const engineType: DockerEngineType = await docker.getEngineType();
if (engineType === DockerEngineType.Windows) {
const selected = await vscode.window.showErrorMessage<vscode.MessageItem>('Currently, you can only run the Azure CLI when running Linux based containers.',
{
title: 'More Information',
},
{
title: 'Close',
isCloseAffordance: true
}
);
if (!selected || selected.isCloseAffordance) {
return;
}
return cp.exec('start https://docs.docker.com/docker-for-windows/#/switch-between-windows-and-linux-containers');
} else {
const option: string = process.platform === 'linux' ? '--net=host' : '';
// volume map .azure folder so don't have to log in every time
const homeDir: string = process.platform === 'win32' ? os.homedir().replace(/\\/g, '/') : os.homedir();
let vol: string = '';
if (fs.existsSync(`${homeDir}/.azure`)) {
vol += ` -v ${homeDir}/.azure:/root/.azure`;
}
if (fs.existsSync(`${homeDir}/.ssh`)) {
vol += ` -v ${homeDir}/.ssh:/root/.ssh`;
}
if (fs.existsSync(`${homeDir}/.kube`)) {
vol += ` -v ${homeDir}/.kube:/root/.kube`;
}
const cmd: string = `docker run ${option} ${vol.trim()} -it --rm azuresdk/azure-cli-python:latest`;
const terminal: vscode.Terminal = vscode.window.createTerminal('Azure CLI');
terminal.sendText(cmd);
terminal.show();
if (reporter) {
/* __GDPR__
"command" : {
"command" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
reporter.sendTelemetryEvent('command', {
command: teleCmdId + '.azurecli'
});
}
}
}