forked from AzureCR/vscode-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerExtension.ts
More file actions
558 lines (520 loc) · 18.5 KB
/
dockerExtension.ts
File metadata and controls
558 lines (520 loc) · 18.5 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as opn from "opn";
import * as path from "path";
import * as request from "request-promise-native";
import * as vscode from "vscode";
import {
AzureUserInput,
createTelemetryReporter,
IActionContext,
registerCommand as uiRegisterCommand,
registerUIExtensionVariables,
TelemetryProperties,
UserCancelledError
} from "vscode-azureextensionui";
import {
ConfigurationParams,
DidChangeConfigurationNotification,
DocumentSelector,
LanguageClient,
LanguageClientOptions,
Middleware,
ServerOptions,
TransportKind
} from "vscode-languageclient/lib/main";
import { queueBuild } from "./commands/azureCommands/acr-build";
import { viewBuildLogs } from "./commands/azureCommands/acr-build-logs";
import { LogContentProvider } from "./commands/azureCommands/acr-build-logs-utils/logFileManager";
import { createRegistry } from "./commands/azureCommands/create-registry";
import { deleteAzureImage } from "./commands/azureCommands/delete-image";
import { deleteAzureRegistry } from "./commands/azureCommands/delete-registry";
import { deleteRepository } from "./commands/azureCommands/delete-repository";
import { pullFromAzure } from "./commands/azureCommands/pull-from-azure";
import { runBuildTask } from "./commands/azureCommands/run-buildTask";
import { showBuildTaskProperties } from "./commands/azureCommands/show-buildTask";
import { TaskContentProvider } from "./commands/azureCommands/task-utils/showTaskManager";
import { buildImage } from "./commands/build-image";
import {
composeDown,
composeRestart,
composeUp
} from "./commands/docker-compose";
import inspectImage from "./commands/inspect-image";
import { openShellContainer } from "./commands/open-shell-container";
import { pushImage } from "./commands/push-image";
import {
consolidateDefaultRegistrySettings,
setRegistryAsDefault
} from "./commands/registrySettings";
import { removeContainer } from "./commands/remove-container";
import { removeImage } from "./commands/remove-image";
import { restartContainer } from "./commands/restart-container";
import { showLogsContainer } from "./commands/showlogs-container";
import {
startAzureCLI,
startContainer,
startContainerInteractive
} from "./commands/start-container";
import { stopContainer } from "./commands/stop-container";
import { systemPrune } from "./commands/system-prune";
import { IHasImageDescriptorAndLabel, tagImage } from "./commands/tag-image";
import { docker } from "./commands/utils/docker-endpoint";
import { DefaultTerminalProvider } from "./commands/utils/TerminalProvider";
import { DockerDebugConfigProvider } from "./configureWorkspace/configDebugProvider";
import {
configure,
configureApi,
ConfigureApiOptions
} from "./configureWorkspace/configure";
import { DockerComposeCompletionItemProvider } from "./dockerCompose/dockerComposeCompletionItemProvider";
import { DockerComposeHoverProvider } from "./dockerCompose/dockerComposeHoverProvider";
import composeVersionKeys from "./dockerCompose/dockerComposeKeyInfo";
import { DockerComposeParser } from "./dockerCompose/dockerComposeParser";
import { DockerfileCompletionItemProvider } from "./dockerfile/dockerfileCompletionItemProvider";
import DockerInspectDocumentContentProvider, {
SCHEME as DOCKER_INSPECT_SCHEME
} from "./documentContentProviders/dockerInspect";
import { AzureAccountWrapper } from "./explorer/deploy/azureAccountWrapper";
import * as util from "./explorer/deploy/util";
import { WebAppCreator } from "./explorer/deploy/webAppCreator";
import { DockerExplorerProvider } from "./explorer/dockerExplorer";
import {
AzureImageTagNode,
AzureRegistryNode,
AzureRepositoryNode
} from "./explorer/models/azureRegistryNodes";
import { ContainerNode } from "./explorer/models/containerNode";
import {
connectCustomRegistry,
disconnectCustomRegistry
} from "./explorer/models/customRegistries";
import {
DockerHubImageTagNode,
DockerHubOrgNode,
DockerHubRepositoryNode
} from "./explorer/models/dockerHubNodes";
import { ImageNode } from "./explorer/models/imageNode";
import { NodeBase } from "./explorer/models/nodeBase";
import { RootNode } from "./explorer/models/rootNode";
import { BuildTaskNode } from "./explorer/models/taskNode";
import { browseAzurePortal } from "./explorer/utils/browseAzurePortal";
import {
browseDockerHub,
dockerHubLogout
} from "./explorer/utils/dockerHubUtils";
import { ext } from "./extensionVariables";
import { initializeTelemetryReporter, reporter } from "./telemetry/telemetry";
import { AzureAccount } from "./typings/azure-account.api";
import { addUserAgent } from "./utils/addUserAgent";
import { registerAzureCommand } from "./utils/Azure/common";
import { AzureUtilityManager } from "./utils/azureUtilityManager";
import { Keytar } from "./utils/keytar";
export const FROM_DIRECTIVE_PATTERN = /^\s*FROM\s*([\w-\/:]*)(\s*AS\s*[a-z][a-z0-9-_\\.]*)?$/i;
export const COMPOSE_FILE_GLOB_PATTERN = "**/[dD]ocker-[cC]ompose*.{yaml,yml}";
export const DOCKERFILE_GLOB_PATTERN = "**/{*.dockerfile,[dD]ocker[fF]ile}";
export let dockerExplorerProvider: DockerExplorerProvider;
export type KeyInfo = { [keyName: string]: string };
export interface ComposeVersionKeys {
All: KeyInfo;
v1: KeyInfo;
v2: KeyInfo;
}
let client: LanguageClient;
const DOCUMENT_SELECTOR: DocumentSelector = [
{ language: "dockerfile", scheme: "file" }
];
function initializeExtensionVariables(ctx: vscode.ExtensionContext): void {
registerUIExtensionVariables(ext);
if (!ext.ui) {
// This allows for standard interactions with the end user (as opposed to test input)
ext.ui = new AzureUserInput(ctx.globalState);
}
ext.context = ctx;
ext.outputChannel = util.getOutputChannel();
if (!ext.terminalProvider) {
ext.terminalProvider = new DefaultTerminalProvider();
}
initializeTelemetryReporter(createTelemetryReporter(ctx));
ext.reporter = reporter;
if (!ext.keytar) {
ext.keytar = Keytar.tryCreate();
}
// Set up the user agent for all direct 'request' calls in the extension (must use ext.request)
let defaultRequestOptions = {};
addUserAgent(defaultRequestOptions);
ext.request = request.defaults(defaultRequestOptions);
}
export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
const installedExtensions: any[] = vscode.extensions.all;
let azureAccount: AzureAccount | undefined;
initializeExtensionVariables(ctx);
// tslint:disable-next-line:prefer-for-of // Grandfathered in
for (let i = 0; i < installedExtensions.length; i++) {
const extension = installedExtensions[i];
if (extension.id === "ms-vscode.azure-account") {
try {
azureAccount = await extension.activate();
} catch (error) {
console.log("Failed to activate the Azure Account Extension: " + error);
}
break;
}
}
ctx.subscriptions.push(
vscode.languages.registerCompletionItemProvider(
DOCUMENT_SELECTOR,
new DockerfileCompletionItemProvider(),
"."
)
);
const YAML_MODE_ID: vscode.DocumentFilter = {
language: "yaml",
scheme: "file",
pattern: COMPOSE_FILE_GLOB_PATTERN
};
let yamlHoverProvider = new DockerComposeHoverProvider(
new DockerComposeParser(),
composeVersionKeys.All
);
ctx.subscriptions.push(
vscode.languages.registerHoverProvider(YAML_MODE_ID, yamlHoverProvider)
);
ctx.subscriptions.push(
vscode.languages.registerCompletionItemProvider(
YAML_MODE_ID,
new DockerComposeCompletionItemProvider(),
"."
)
);
ctx.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
DOCKER_INSPECT_SCHEME,
new DockerInspectDocumentContentProvider()
)
);
ctx.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
LogContentProvider.scheme,
new LogContentProvider()
)
);
ctx.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
TaskContentProvider.scheme,
new TaskContentProvider()
)
);
if (azureAccount) {
AzureUtilityManager.getInstance().setAccount(azureAccount);
}
registerDockerCommands(azureAccount);
ctx.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider(
"docker",
new DockerDebugConfigProvider()
)
);
await consolidateDefaultRegistrySettings();
activateLanguageClient(ctx);
}
async function createWebApp(
context?: AzureImageTagNode | DockerHubImageTagNode,
azureAccount?: AzureAccount
): Promise<void> {
if (context) {
if (azureAccount) {
const azureAccountWrapper = new AzureAccountWrapper(
ext.context,
azureAccount
);
const wizard = new WebAppCreator(
ext.outputChannel,
azureAccountWrapper,
context
);
const result = await wizard.run();
if (result.status === "Faulted") {
throw result.error;
} else if (result.status === "Cancelled") {
throw new UserCancelledError();
}
} else {
const open: vscode.MessageItem = { title: "View in Marketplace" };
const response = await vscode.window.showErrorMessage(
"Please install the Azure Account extension to deploy to Azure.",
open
);
if (response === open) {
opn(
"https://marketplace.visualstudio.com/items?itemName=ms-vscode.azure-account"
);
}
}
}
}
// Remove this when https://github.com/Microsoft/vscode-docker/issues/445 fixed
function registerCommand(
commandId: string,
callback: (this: IActionContext, ...args: any[]) => any
): void {
return uiRegisterCommand(
commandId,
// tslint:disable-next-line:no-function-expression
async function(this: IActionContext, ...args: any[]): Promise<any> {
if (args.length) {
let properties: {
contextValue?: string;
} & TelemetryProperties = this.properties;
const contextArg = args[0];
if (contextArg instanceof NodeBase) {
properties.contextValue = contextArg.contextValue;
} else if (contextArg instanceof vscode.Uri) {
properties.contextValue = "Uri";
}
}
return callback.call(this, ...args);
}
);
}
// tslint:disable-next-line:max-func-body-length
function registerDockerCommands(azureAccount: AzureAccount): void {
dockerExplorerProvider = new DockerExplorerProvider(azureAccount);
vscode.window.registerTreeDataProvider(
"dockerExplorer",
dockerExplorerProvider
);
registerCommand("vscode-docker.explorer.refresh", () =>
dockerExplorerProvider.refresh()
);
registerCommand("vscode-docker.configure", async function(
this: IActionContext
): Promise<void> {
await configure(this, undefined);
});
registerCommand("vscode-docker.api.configure", async function(
this: IActionContext,
options: ConfigureApiOptions
): Promise<void> {
await configureApi(this, options);
});
registerCommand("vscode-docker.container.start", async function(
this: IActionContext,
node: ImageNode | undefined
): Promise<void> {
await startContainer(this, node);
});
registerCommand("vscode-docker.container.start.interactive", async function(
this: IActionContext,
node: ImageNode | undefined
): Promise<void> {
await startContainerInteractive(this, node);
});
registerCommand("vscode-docker.container.start.azurecli", startAzureCLI);
registerCommand("vscode-docker.container.stop", async function(
this: IActionContext,
node: ContainerNode | RootNode | undefined
): Promise<void> {
await stopContainer(this, node);
});
registerCommand("vscode-docker.container.restart", async function(
this: IActionContext,
node: ContainerNode | RootNode | undefined
): Promise<void> {
await restartContainer(this, node);
});
registerCommand("vscode-docker.container.show-logs", async function(
this: IActionContext,
node: ContainerNode | RootNode | undefined
): Promise<void> {
await showLogsContainer(this, node);
});
registerCommand("vscode-docker.container.open-shell", async function(
this: IActionContext,
node: ContainerNode | RootNode | undefined
): Promise<void> {
await openShellContainer(this, node);
});
registerCommand("vscode-docker.container.remove", async function(
this: IActionContext,
node: ContainerNode | RootNode | undefined
): Promise<void> {
await removeContainer(this, node);
});
registerCommand("vscode-docker.image.build", async function(
this: IActionContext,
item: vscode.Uri | undefined
): Promise<void> {
await buildImage(this, item);
});
registerCommand("vscode-docker.image.inspect", async function(
this: IActionContext,
node: ImageNode | undefined
): Promise<void> {
await inspectImage(this, node);
});
registerCommand("vscode-docker.image.remove", async function(
this: IActionContext,
node: ImageNode | RootNode | undefined
): Promise<void> {
await removeImage(this, node);
});
registerCommand("vscode-docker.image.push", async function(
this: IActionContext,
node: ImageNode | undefined
): Promise<void> {
await pushImage(this, node);
});
registerCommand("vscode-docker.image.tag", async function(
this: IActionContext,
node: ImageNode | undefined
): Promise<void> {
await tagImage(this, node);
});
registerCommand("vscode-docker.compose.up", composeUp);
registerCommand("vscode-docker.compose.down", composeDown);
registerCommand("vscode-docker.compose.restart", composeRestart);
registerCommand("vscode-docker.system.prune", systemPrune);
registerCommand(
"vscode-docker.createWebApp",
async (context?: AzureImageTagNode | DockerHubImageTagNode) =>
await createWebApp(context, azureAccount)
);
registerCommand("vscode-docker.dockerHubLogout", dockerHubLogout);
registerCommand(
"vscode-docker.browseDockerHub",
(
context?:
| DockerHubImageTagNode
| DockerHubRepositoryNode
| DockerHubOrgNode
) => {
browseDockerHub(context);
}
);
registerCommand(
"vscode-docker.browseAzurePortal",
(context?: AzureRegistryNode | AzureRepositoryNode | AzureImageTagNode) => {
browseAzurePortal(context);
}
);
registerCommand("vscode-docker.connectCustomRegistry", connectCustomRegistry);
registerCommand(
"vscode-docker.disconnectCustomRegistry",
disconnectCustomRegistry
);
registerCommand("vscode-docker.setRegistryAsDefault", setRegistryAsDefault);
registerAzureCommand(
"vscode-docker.delete-ACR-Registry",
deleteAzureRegistry
);
registerAzureCommand("vscode-docker.delete-ACR-Image", deleteAzureImage);
registerAzureCommand("vscode-docker.delete-ACR-Repository", deleteRepository);
registerAzureCommand("vscode-docker.create-ACR-Registry", createRegistry);
registerAzureCommand("vscode-docker.pullFromAzure", pullFromAzure);
registerAzureCommand("vscode-docker.acrBuildLogs", viewBuildLogs);
registerAzureCommand("vscode-docker.run-ACR-BuildTask", runBuildTask);
registerAzureCommand(
"vscode-docker.show-ACR-buildTask",
showBuildTaskProperties
);
}
export async function deactivate(): Promise<void> {
if (!client) {
return undefined;
}
// perform cleanup
Configuration.dispose();
return await client.stop();
}
namespace Configuration {
let configurationListener: vscode.Disposable;
export function computeConfiguration(
params: ConfigurationParams
): vscode.WorkspaceConfiguration[] {
let result: vscode.WorkspaceConfiguration[] = [];
for (let item of params.items) {
let config: vscode.WorkspaceConfiguration;
if (item.scopeUri) {
config = vscode.workspace.getConfiguration(
item.section,
client.protocol2CodeConverter.asUri(item.scopeUri)
);
} else {
config = vscode.workspace.getConfiguration(item.section);
}
result.push(config);
}
return result;
}
export function initialize(): void {
configurationListener = vscode.workspace.onDidChangeConfiguration(
(e: vscode.ConfigurationChangeEvent) => {
// notify the language server that settings have change
client.sendNotification(DidChangeConfigurationNotification.type, {
settings: null
});
// Update endpoint and refresh explorer if needed
if (e.affectsConfiguration("docker")) {
docker.refreshEndpoint();
vscode.commands.executeCommand("vscode-docker.explorer.refresh");
}
}
);
}
export function dispose(): void {
if (configurationListener) {
// remove this listener when disposed
configurationListener.dispose();
}
}
}
function activateLanguageClient(ctx: vscode.ExtensionContext): void {
let serverModule = ctx.asAbsolutePath(
path.join(
"node_modules",
"dockerfile-language-server-nodejs",
"lib",
"server.js"
)
);
let debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };
let serverOptions: ServerOptions = {
run: {
module: serverModule,
transport: TransportKind.ipc,
args: ["--node-ipc"]
},
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions
}
};
let middleware: Middleware = {
workspace: {
configuration: Configuration.computeConfiguration
}
};
let clientOptions: LanguageClientOptions = {
documentSelector: DOCUMENT_SELECTOR,
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher("**/.clientrc")
},
middleware: middleware
};
client = new LanguageClient(
"dockerfile-langserver",
"Dockerfile Language Server",
serverOptions,
clientOptions
);
// tslint:disable-next-line:no-floating-promises
client.onReady().then(() => {
// attach the VS Code settings listener
Configuration.initialize();
});
client.start();
}