-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathUploadSourceCodeStep.ts
More file actions
161 lines (136 loc) · 9.58 KB
/
UploadSourceCodeStep.ts
File metadata and controls
161 lines (136 loc) · 9.58 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getResourceGroupFromId } from '@microsoft/vscode-azext-azureutils';
import { ActivityChildItem, ActivityChildType, AzExtFsExtra, AzureWizardExecuteStep, activityFailContext, activityFailIcon, activityInfoContext, activityProgressContext, activityProgressIcon, activitySuccessContext, activitySuccessIcon, createContextValue, nonNullValue, type ActivityChildItemOptions, type ExecuteActivityOutput } from '@microsoft/vscode-azext-utils';
import { randomUUID } from 'crypto';
import { tmpdir } from 'os';
import * as path from 'path';
import * as tar from 'tar';
import { ThemeColor, ThemeIcon, TreeItemCollapsibleState, type Progress } from 'vscode';
import { ext } from '../../../../extensionVariables';
import { createContainerRegistryManagementClient } from '../../../../utils/azureClients';
import { localize } from '../../../../utils/localize';
import { type BuildImageInAzureImageSourceContext } from './BuildImageInAzureImageSourceContext';
const vcsIgnoreList = ['.git', '.gitignore', '.bzr', 'bzrignore', '.hg', '.hgignore', '.svn'];
const uploadSourceCodeStepContext: string = 'uploadSourceCodeStepItem';
export class UploadSourceCodeStep<T extends BuildImageInAzureImageSourceContext> extends AzureWizardExecuteStep<T> {
public priority: number = 530;
/** Path to a directory containing a custom Dockerfile that we sometimes build and upload for the user */
private _customDockerfileDirPath?: string;
/** Relative path of src folder from rootFolder and what gets deployed */
private _sourceFilePath: string;
public async execute(context: T, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
this._sourceFilePath = context.rootFolder.uri.fsPath === context.srcPath ? '.' : path.relative(context.rootFolder.uri.fsPath, context.srcPath);
context.telemetry.properties.sourceDepth = this._sourceFilePath === '.' ? '0' : String(this._sourceFilePath.split(path.sep).length);
context.registryName = nonNullValue(context.registry?.name);
context.resourceGroupName = getResourceGroupFromId(nonNullValue(context.registry?.id));
context.client = await createContainerRegistryManagementClient(context);
progress.report({ message: localize('uploadingSourceCode', 'Uploading source code...') });
const source: string = path.join(context.rootFolder.uri.fsPath, this._sourceFilePath);
let items = await AzExtFsExtra.readDirectory(source);
items = items.filter(i => !vcsIgnoreList.includes(i.name));
await this.buildCustomDockerfileIfNecessary(context);
if (this._customDockerfileDirPath) {
// Create an uncompressed tarball with the base project
const tempTarFilePath: string = context.tarFilePath.replace(/\.tar\.gz/, '.tar');
await tar.c({ cwd: source, file: tempTarFilePath }, items.map(i => path.relative(source, i.fsPath)));
// Append/Overwrite the original Dockerfile with the custom one that was made
await tar.r({ cwd: this._customDockerfileDirPath, file: tempTarFilePath }, [path.relative(source, context.dockerfilePath)]);
// Create the final compressed version
// Note: Noticed some hanging issues when using the async version to add existing tar archives;
// however, the issues seem to disappear when utilizing the sync version
tar.c({ cwd: tmpdir(), gzip: true, sync: true, file: context.tarFilePath }, [`@${path.basename(tempTarFilePath)}`]);
try {
// Remove temporarily created resources
await AzExtFsExtra.deleteResource(tempTarFilePath);
await AzExtFsExtra.deleteResource(this._customDockerfileDirPath, { recursive: true });
} catch {
// Swallow error, don't halt the deploy process just because we couldn't delete the temp files, provide a warning instead
ext.outputChannel.appendLog(localize('errorDeletingTempFiles', 'Warning: Could not remove some of the following temporary files: "{0}", "{1}". Try removing these manually at a later time.', tempTarFilePath, this._customDockerfileDirPath));
}
} else {
await tar.c({ cwd: source, gzip: true, file: context.tarFilePath }, items.map(i => path.relative(source, i.fsPath)));
}
const sourceUploadLocation = await context.client.registries.getBuildSourceUploadUrl(context.resourceGroupName, context.registryName);
const uploadUrl: string = nonNullValue(sourceUploadLocation.uploadUrl);
const relativePath: string = nonNullValue(sourceUploadLocation.relativePath);
const storageBlob = await import('@azure/storage-blob');
const blobClient = new storageBlob.BlockBlobClient(uploadUrl);
await blobClient.uploadFile(context.tarFilePath);
context.uploadedSourceLocation = relativePath;
}
public shouldExecute(context: T): boolean {
return !context.uploadedSourceLocation;
}
/**
* Checks and creates a custom Dockerfile if necessary to be used in place of the original
* @populates this._customDockerfileDirPath
*/
private async buildCustomDockerfileIfNecessary(context: T): Promise<void> {
// Build a custom Dockerfile if it has ACR's unsupported `--platform` flag
// See: https://github.com/Azure/acr/issues/697
const platformRegex: RegExp = /^(FROM.*)\s--platform=\S+(.*)$/gm;
let dockerfileContent: string = await AzExtFsExtra.readFile(context.dockerfilePath);
if (!platformRegex.test(dockerfileContent)) {
context.telemetry.properties.buildCustomDockerfile = 'false';
return;
}
context.telemetry.properties.buildCustomDockerfile = 'true';
ext.outputChannel.appendLog(localize('removePlatformFlag', 'Detected a "--platform" flag in the Dockerfile. This flag is not supported in ACR. Attempting to provide a Dockerfile with the "--platform" flag removed.'));
dockerfileContent = dockerfileContent.replace(platformRegex, '$1$2');
const customDockerfileDirPath: string = path.join(tmpdir(), randomUUID());
const dockerfileRelativePath: string = path.relative(context.srcPath, context.dockerfilePath);
const customDockerfilePath = path.join(customDockerfileDirPath, dockerfileRelativePath);
await AzExtFsExtra.writeFile(customDockerfilePath, dockerfileContent);
this._customDockerfileDirPath = customDockerfileDirPath;
}
public createSuccessOutput(context: T): ExecuteActivityOutput {
const baseTreeItemOptions: ActivityChildItemOptions = {
label: localize('uploadSourceCodeLabel', 'Upload source code from "{1}" directory to registry "{0}"', context.registry?.name, this._sourceFilePath),
contextValue: createContextValue([uploadSourceCodeStepContext, activitySuccessContext]),
activityType: ActivityChildType.Success,
iconPath: activitySuccessIcon,
};
let parentTreeItem: ActivityChildItem | undefined;
if (this._customDockerfileDirPath) {
parentTreeItem = new ActivityChildItem({ ...baseTreeItemOptions, initialCollapsibleState: TreeItemCollapsibleState.Expanded });
parentTreeItem.getChildren = () => {
const removePlatformFlagItem = new ActivityChildItem({
label: localize('removePlatformFlag', 'Remove unsupported ACR "--platform" flag'),
contextValue: createContextValue(['removePlatformFlagItem', activityInfoContext]),
activityType: ActivityChildType.Info,
iconPath: new ThemeIcon('dash', new ThemeColor('terminal.ansiWhite')),
});
return Promise.resolve([removePlatformFlagItem]);
};
}
return {
item: parentTreeItem ?? new ActivityChildItem({ ...baseTreeItemOptions }),
message: localize('uploadedSourceCodeSuccess', 'Uploaded source code from "{1}" directory to registry "{0}" for remote build.', context.registry?.name, this._sourceFilePath)
};
}
public createProgressOutput(context: T): ExecuteActivityOutput {
return {
item: new ActivityChildItem({
label: localize('uploadSourceCodeLabelProgress', 'Upload workspace source code to registry "{0}"', context.registry?.name),
contextValue: createContextValue([uploadSourceCodeStepContext, activityProgressContext]),
activityType: ActivityChildType.Progress,
iconPath: activityProgressIcon,
}),
};
}
public createFailOutput(context: T): ExecuteActivityOutput {
return {
item: new ActivityChildItem({
label: localize('uploadSourceCodeLabel', 'Upload source code from "{1}" directory to registry "{0}"', context.registry?.name, this._sourceFilePath),
contextValue: createContextValue([uploadSourceCodeStepContext, activityFailContext]),
activityType: ActivityChildType.Fail,
iconPath: activityFailIcon,
isParent: true,
}),
message: localize('uploadedSourceCodeFail', 'Failed to upload source code from "{1}" directory to registry "{0}" for remote build.', context.registry?.name, this._sourceFilePath)
};
}
}