|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | +* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +*--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { AzExtFsExtra } from '@microsoft/vscode-azext-utils'; |
| 7 | +import { ext } from '../../extensionVariables'; |
| 8 | +import { localize } from '../../utils/localize'; |
| 9 | +import { IngressContext } from './IngressContext'; |
| 10 | +import { getDefaultPort } from './editTargetPort/getDefaultPort'; |
| 11 | + |
| 12 | +export async function tryConfigureIngressUsingDockerfile(context: IngressContext): Promise<void> { |
| 13 | + if (!context.dockerfilePath) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + context.dockerfileExposePorts = await getDockerfileExposePort(context.dockerfilePath); |
| 18 | + |
| 19 | + if (context.alwaysPromptIngress) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + if (!context.dockerfileExposePorts) { |
| 24 | + context.enableIngress = false; |
| 25 | + context.enableExternal = false; |
| 26 | + } else if (context.dockerfileExposePorts) { |
| 27 | + context.enableIngress = true; |
| 28 | + context.enableExternal = true; |
| 29 | + context.targetPort = getDefaultPort(context); |
| 30 | + } |
| 31 | + |
| 32 | + // If a container app already exists, activity children will be added automatically in later execute steps |
| 33 | + // if (!context.containerApp) { |
| 34 | + // context.activityChildren?.push( |
| 35 | + // new GenericTreeItem(undefined, { |
| 36 | + // contextValue: createActivityChildContext(['ingressPromptStep', activitySuccessContext]), |
| 37 | + // label: context.enableIngress ? |
| 38 | + // localize('ingressEnableLabel', 'Enable ingress on port {0} (found Dockerfile configuration)', context.targetPort) : |
| 39 | + // localize('ingressDisableLabel', 'Disable ingress (found Dockerfile configuration)'), |
| 40 | + // iconPath: activitySuccessIcon |
| 41 | + // }) |
| 42 | + // ); |
| 43 | + // } |
| 44 | + |
| 45 | + ext.outputChannel.appendLog(context.enableIngress ? |
| 46 | + localize('ingressEnabledLabel', 'Detected ingress on port {0} using Dockerfile configuration.', context.targetPort) : |
| 47 | + localize('ingressDisabledLabel', 'Detected no ingress using Dockerfile configuration.') |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +export class PortRange { |
| 52 | + private readonly _start: number; |
| 53 | + private readonly _end: number; |
| 54 | + |
| 55 | + constructor(start: number, end?: number) { |
| 56 | + this._start = start; |
| 57 | + this._end = end ? end : start; |
| 58 | + } |
| 59 | + |
| 60 | + get start(): number { |
| 61 | + return this._start; |
| 62 | + } |
| 63 | + |
| 64 | + get end(): number { |
| 65 | + return this._end; |
| 66 | + } |
| 67 | + |
| 68 | + includes(port: number): boolean { |
| 69 | + return port >= this.start && port <= this.end; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export async function getDockerfileExposePort(dockerfilePath: string): Promise<PortRange[] | undefined> { |
| 74 | + if (!await AzExtFsExtra.pathExists(dockerfilePath)) { |
| 75 | + return undefined; |
| 76 | + } |
| 77 | + |
| 78 | + const content: string = await AzExtFsExtra.readFile(dockerfilePath); |
| 79 | + const lines: string[] = content.split('\n'); |
| 80 | + |
| 81 | + const portRanges: PortRange[] = []; |
| 82 | + for (const line of lines) { |
| 83 | + if (/^EXPOSE/i.test(line.trim())) { |
| 84 | + // Identify all single port numbers that aren't for udp |
| 85 | + const singlePorts: string[] = line.match(/(?<=\s)\d{2,5}(?!(\-)|(\/udp))\b/g) ?? []; |
| 86 | + for (const sp of singlePorts) { |
| 87 | + portRanges.push(new PortRange(parseInt(sp))); |
| 88 | + } |
| 89 | + |
| 90 | + // Identify all port ranges |
| 91 | + const portRange: string[] = line.match(/\d{2,5}\-\d{2,5}/g) ?? []; |
| 92 | + for (const pr of portRange) { |
| 93 | + const [start, end] = pr.split('-'); |
| 94 | + portRanges.push(new PortRange(parseInt(start), parseInt(end))); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return portRanges.length ? portRanges : undefined; |
| 100 | +} |
0 commit comments