-
Notifications
You must be signed in to change notification settings - Fork 17
Final "Build Image in Azure" #292
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/commands/deploy/buildImageInAzure/BuildFromProjectListStep.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureWizardExecuteStep, AzureWizardPromptStep, IAzureQuickPickItem, IWizardOptions } from "@microsoft/vscode-azext-utils"; | ||
| import { ImageSource } from "../../../constants"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { IDeployBaseContext } from "../IDeployBaseContext"; | ||
| import { AcrListStep } from "../deployFromRegistry/acr/AcrListStep"; | ||
| import { BuildImageStep } from "./BuildImageStep"; | ||
| import { DockerFileItemStep } from "./DockerFileItemStep"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
| import { ImageNameStep } from "./ImageNameStep"; | ||
| import { OSPickStep } from "./OSPickStep"; | ||
| import { RootFolderStep } from "./RootFolderStep"; | ||
| import { RunStep } from "./RunStep"; | ||
| import { TarFileStep } from "./TarFileStep"; | ||
| import { UploadSourceCodeStep } from "./UploadSourceCodeStep"; | ||
|
|
||
| const buildFromProjectLabels: string[] = [ | ||
| localize('azure', 'Build from project remotely using Azure Container Registry') | ||
| //localize('docker', 'Build from project locally using Docker') | ||
| ]; | ||
|
|
||
| export class BuildFromProjectListStep extends AzureWizardPromptStep<IDeployBaseContext> { | ||
| public async prompt(context: IDeployBaseContext): Promise<void> { | ||
| const placeHolder: string = localize('buildType', 'Select how you want to build your project'); | ||
| const picks: IAzureQuickPickItem<ImageSource.LocalDockerBuild | ImageSource.RemoteAcrBuild>[] = [ | ||
| { label: buildFromProjectLabels[0], data: ImageSource.RemoteAcrBuild, suppressPersistence: true }, | ||
| //{ label: buildFromProjectLabels[1], data: ImageSource.LocalDockerBuild, suppressPersistence: true } | ||
| ]; | ||
|
|
||
| context.buildType = (await context.ui.showQuickPick(picks, { placeHolder })).data; | ||
| } | ||
|
|
||
| public async configureBeforePrompt(context: IDeployBaseContext): Promise<void> { | ||
| if (buildFromProjectLabels.length === 1) { | ||
| context.buildType = ImageSource.RemoteAcrBuild; | ||
| } | ||
| } | ||
|
|
||
| public shouldPrompt(context: IDeployBaseContext): boolean { | ||
| return !context.buildType; | ||
| } | ||
|
|
||
| public async getSubWizard(context: IBuildImageInAzureContext): Promise<IWizardOptions<IDeployBaseContext> | undefined> { | ||
| const promptSteps: AzureWizardPromptStep<IDeployBaseContext>[] = []; | ||
| const executeSteps: AzureWizardExecuteStep<IDeployBaseContext>[] = []; | ||
|
|
||
| switch (context.buildType) { | ||
| case ImageSource.RemoteAcrBuild: | ||
| promptSteps.push(new AcrListStep(), new RootFolderStep(), new DockerFileItemStep(), new ImageNameStep(), new OSPickStep()); | ||
| executeSteps.push(new TarFileStep(), new UploadSourceCodeStep(), new RunStep(), new BuildImageStep()); | ||
| break; | ||
| //TODO: case for 'Build from project locally using Docker' | ||
| } | ||
| return { promptSteps, executeSteps }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureWizardExecuteStep } from "@microsoft/vscode-azext-utils"; | ||
| import { acrDomain } from "../../../constants"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
| import { buildImageInAzure } from "./buildImageInAzure"; | ||
|
|
||
| export class BuildImageStep extends AzureWizardExecuteStep<IBuildImageInAzureContext> { | ||
| public priority: number = 225; | ||
|
|
||
| public async execute(context: IBuildImageInAzureContext): Promise<void> { | ||
| context.registryDomain = acrDomain; | ||
|
|
||
| const run = await buildImageInAzure(context); | ||
| const outputImages = run?.outputImages; | ||
| context.telemetry.properties.outputImages = outputImages?.length?.toString(); | ||
|
|
||
| if (outputImages) { | ||
|
bwateratmsft marked this conversation as resolved.
|
||
| const image = outputImages[0]; | ||
| context.image = `${image.registry}/${image.repository}:${image.tag}`; | ||
| } else { | ||
| throw new Error(localize('noImagesBuilt', 'Failed to build image.')); | ||
| } | ||
| } | ||
|
|
||
| public shouldExecute(context: IBuildImageInAzureContext): boolean { | ||
| return !context.image; | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/commands/deploy/buildImageInAzure/DockerFileItemStep.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureWizardPromptStep } from '@microsoft/vscode-azext-utils'; | ||
| import { DOCKERFILE_GLOB_PATTERN } from "../../../constants"; | ||
| import { localize } from '../../../utils/localize'; | ||
| import { selectWorkspaceFile } from "../../../utils/workspaceUtils"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
|
|
||
| export class DockerFileItemStep extends AzureWizardPromptStep<IBuildImageInAzureContext> { | ||
| public async prompt(context: IBuildImageInAzureContext): Promise<void> { | ||
| context.dockerFilePath = await selectWorkspaceFile(context, localize('dockerFilePick', 'Select a Dockerfile'), { filters: { 'Dockerfile': ['Dockerfile', 'Dockerfile.*'] } }, DOCKERFILE_GLOB_PATTERN); | ||
| } | ||
|
|
||
| public shouldPrompt(context: IBuildImageInAzureContext): boolean { | ||
| return !context.dockerFilePath; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/commands/deploy/buildImageInAzure/IBuildImageInAzureContext.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { Run as AcrRun, ContainerRegistryManagementClient } from '@azure/arm-containerregistry'; | ||
| import * as vscode from 'vscode'; | ||
| import { IDeployFromRegistryContext } from '../deployFromRegistry/IDeployFromRegistryContext'; | ||
|
|
||
| export interface IBuildImageInAzureContext extends IDeployFromRegistryContext { | ||
| rootFolder: vscode.WorkspaceFolder; | ||
| dockerFilePath: string; | ||
| imageName: string; | ||
| os: 'Windows' | 'Linux'; | ||
|
|
||
| uploadedSourceLocation: string; | ||
| tarFilePath: string; | ||
|
|
||
| client: ContainerRegistryManagementClient; | ||
| resourceGroupName: string; | ||
| registryName: string; | ||
| run: AcrRun | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureWizardPromptStep } from "@microsoft/vscode-azext-utils"; | ||
| import { URI, Utils } from "vscode-uri"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
|
|
||
| export class ImageNameStep extends AzureWizardPromptStep<IBuildImageInAzureContext> { | ||
| public async prompt(context: IBuildImageInAzureContext): Promise<void> { | ||
| const suggestedImageName = await getSuggestedName(context, context.dockerFilePath); | ||
|
|
||
| context.imageName = await context.ui.showInputBox({ | ||
| prompt: localize('imageNamePrompt', 'Enter a name for the image'), | ||
| value: suggestedImageName ? localize('dockerfilePlaceholder', suggestedImageName) : '' | ||
| }); | ||
| } | ||
|
|
||
| public shouldPrompt(context: IBuildImageInAzureContext): boolean { | ||
| return !context.imageName; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| async function getSuggestedName(context: IBuildImageInAzureContext, dockerFilePath: string): Promise<string | undefined> { | ||
| let suggestedImageName: string | undefined; | ||
| suggestedImageName = Utils.dirname(URI.parse(dockerFilePath)).path.split('/').pop(); | ||
| if (suggestedImageName === '') { | ||
| if (context.rootFolder) { | ||
| suggestedImageName = Utils.basename(context.rootFolder.uri).toLowerCase().replace(/\s/g, ''); | ||
| } | ||
| } | ||
| suggestedImageName += ":{{.Run.ID}}"; | ||
| return suggestedImageName; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureWizardPromptStep, IAzureQuickPickItem } from "@microsoft/vscode-azext-utils"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
|
|
||
| export class OSPickStep extends AzureWizardPromptStep<IBuildImageInAzureContext> { | ||
| public async prompt(context: IBuildImageInAzureContext): Promise<void> { | ||
| const placeHolder: string = localize('imageOSPrompt', 'Select image base OS'); | ||
| const picks: IAzureQuickPickItem<'Windows' | 'Linux'>[] = [ | ||
| { label: 'Linux', data: 'Linux', suppressPersistence: true }, | ||
| { label: 'Windows', data: 'Windows', suppressPersistence: true }, | ||
| ]; | ||
|
|
||
| context.os = (await context.ui.showQuickPick(picks, { placeHolder })).data; | ||
| } | ||
|
|
||
| public shouldPrompt(context: IBuildImageInAzureContext): boolean { | ||
| return !context.os; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import { AzureWizardPromptStep, UserCancelledError } from '@microsoft/vscode-azext-utils'; | ||
| import * as vscode from 'vscode'; | ||
| import { localize } from '../../../utils/localize'; | ||
| import { IBuildImageInAzureContext } from './IBuildImageInAzureContext'; | ||
|
|
||
| export class RootFolderStep extends AzureWizardPromptStep<IBuildImageInAzureContext> { | ||
| public async prompt(context: IBuildImageInAzureContext): Promise<void> { | ||
| context.rootFolder = await getRootWorkSpaceFolder(); | ||
| } | ||
|
|
||
| public shouldPrompt(context: IBuildImageInAzureContext): boolean { | ||
| return !context.rootFolder; | ||
| } | ||
| } | ||
|
|
||
| async function getRootWorkSpaceFolder(): Promise<vscode.WorkspaceFolder> { | ||
| if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { | ||
| throw new Error(localize('noOpenFolder', 'No folder is open. Please open a folder and try again.')); | ||
| } else if (vscode.workspace.workspaceFolders.length === 1) { | ||
| return vscode.workspace.workspaceFolders[0]; | ||
| } else { | ||
| const placeHolder: string = localize('selectRootWorkspace', 'Select the folder containing your Dockerfile'); | ||
| const folder = await vscode.window.showWorkspaceFolderPick({ placeHolder }); | ||
| if (!folder) { | ||
| throw new UserCancelledError('selectRootWorkspace'); | ||
| } | ||
| return folder; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { DockerBuildRequest as AcrDockerBuildRequest } from "@azure/arm-containerregistry"; | ||
| import { AzExtFsExtra, AzureWizardExecuteStep } from "@microsoft/vscode-azext-utils"; | ||
| import * as path from 'path'; | ||
| import { Progress } from "vscode"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
|
|
||
| export class RunStep extends AzureWizardExecuteStep<IBuildImageInAzureContext> { | ||
| public priority: number = 200; | ||
|
|
||
| public async execute(context: IBuildImageInAzureContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> { | ||
| try { | ||
| const rootUri = context.rootFolder.uri; | ||
|
|
||
| const runRequest: AcrDockerBuildRequest = { | ||
| type: 'DockerBuildRequest', | ||
| imageNames: [context.imageName], | ||
| isPushEnabled: true, | ||
| sourceLocation: context.uploadedSourceLocation, | ||
| platform: { os: context.os }, | ||
| dockerFilePath: path.relative(rootUri.path, context.dockerFilePath) | ||
| }; | ||
|
|
||
| const building: string = localize('buildingImage', 'Building image "{0}" in registry "{1}"...', context.imageName, context.registryName); | ||
| progress.report({ message: building }); | ||
|
|
||
| context.run = await context.client.registries.beginScheduleRunAndWait(context.resourceGroupName, context.registryName, runRequest); | ||
| } finally { | ||
| if (await AzExtFsExtra.pathExists(context.tarFilePath)) { | ||
| await AzExtFsExtra.deleteResource(context.tarFilePath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public shouldExecute(context: IBuildImageInAzureContext): boolean { | ||
| return !context.run | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzureWizardExecuteStep } from "@microsoft/vscode-azext-utils"; | ||
| import * as os from 'os'; | ||
| import { URI, Utils } from "vscode-uri"; | ||
| import { IBuildImageInAzureContext } from "./IBuildImageInAzureContext"; | ||
|
|
||
| const idPrecision = 6; | ||
|
|
||
| export class TarFileStep extends AzureWizardExecuteStep<IBuildImageInAzureContext> { | ||
| public priority: number = 150; | ||
|
|
||
| public async execute(context: IBuildImageInAzureContext): Promise<void> { | ||
| const id: number = Math.floor(Math.random() * Math.pow(10, idPrecision)); | ||
| const archive = `sourceArchive${id}.tar.gz`; | ||
| context.tarFilePath = Utils.joinPath(URI.parse(os.tmpdir()), archive).path; | ||
| } | ||
|
|
||
| public shouldExecute(context: IBuildImageInAzureContext): boolean { | ||
| return !context.tarFilePath; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.