-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathFunctionAppCreateStep.ts
More file actions
187 lines (161 loc) · 9.08 KB
/
FunctionAppCreateStep.ts
File metadata and controls
187 lines (161 loc) · 9.08 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { NameValuePair, Site, SiteConfig, WebSiteManagementClient } from '@azure/arm-appservice';
import { CustomLocation, IAppServiceWizardContext, ParsedSite, WebsiteOS } from '@microsoft/vscode-azext-azureappservice';
import { LocationListStep } from '@microsoft/vscode-azext-azureutils';
import { AzureWizardExecuteStep, parseError } from '@microsoft/vscode-azext-utils';
import { AppResource } from '@microsoft/vscode-azext-utils/hostapi';
import { Progress } from 'vscode';
import { FuncVersion, getMajorVersion } from '../../FuncVersion';
import { ConnectionKey, ProjectLanguage, contentConnectionStringKey, contentShareKey, extensionVersionKey, runFromPackageKey, webProvider } from '../../constants';
import { ext } from '../../extensionVariables';
import { localize } from '../../localize';
import { createWebSiteClient } from '../../utils/azureClients';
import { getRandomHexString } from '../../utils/fs';
import { nonNullProp } from '../../utils/nonNull';
import { getStorageConnectionString } from '../appSettings/connectionSettings/getLocalConnectionSetting';
import { enableFileLogging } from '../logstream/enableFileLogging';
import { FullFunctionAppStack, IFunctionAppWizardContext } from './IFunctionAppWizardContext';
import { showSiteCreated } from './showSiteCreated';
import { FunctionAppRuntimeSettings } from './stacks/models/FunctionAppStackModel';
export class FunctionAppCreateStep extends AzureWizardExecuteStep<IFunctionAppWizardContext> {
public priority: number = 140;
public async execute(context: IFunctionAppWizardContext, progress: Progress<{ message?: string; increment?: number }>): Promise<void> {
const os: WebsiteOS = nonNullProp(context, 'newSiteOS');
const stack: FullFunctionAppStack = nonNullProp(context, 'newSiteStack');
context.telemetry.properties.newSiteOS = os;
context.telemetry.properties.newSiteStack = stack.stack.value;
context.telemetry.properties.newSiteMajorVersion = stack.majorVersion.value;
context.telemetry.properties.newSiteMinorVersion = stack.minorVersion.value;
context.telemetry.properties.planSkuTier = context.plan?.sku?.tier;
const message: string = localize('creatingNewApp', 'Creating new function app "{0}"...', context.newSiteName);
ext.outputChannel.appendLog(message);
progress.report({ message });
const siteName: string = nonNullProp(context, 'newSiteName');
const rgName: string = nonNullProp(nonNullProp(context, 'resourceGroup'), 'name');
const client: WebSiteManagementClient = await createWebSiteClient(context);
context.site = await client.webApps.beginCreateOrUpdateAndWait(rgName, siteName, await this.getNewSite(context, stack));
context.activityResult = context.site as AppResource;
const site = new ParsedSite(context.site, context);
if (!site.isLinux) { // not supported on linux
try {
await enableFileLogging(context, site);
} catch (error) {
// optional part of creating function app, so not worth blocking on error
context.telemetry.properties.fileLoggingError = parseError(error).message;
}
}
showSiteCreated(site, context);
}
public shouldExecute(context: IFunctionAppWizardContext): boolean {
return !context.site;
}
private async getNewSite(context: IFunctionAppWizardContext, stack: FullFunctionAppStack): Promise<Site> {
const location = await LocationListStep.getLocation(context, webProvider);
const site: Site = {
name: context.newSiteName,
kind: getSiteKind(context),
location: nonNullProp(location, 'name'),
serverFarmId: context.plan?.id,
clientAffinityEnabled: false,
siteConfig: await this.getNewSiteConfig(context, stack),
reserved: context.newSiteOS === WebsiteOS.linux // The secret property - must be set to true to make it a Linux plan. Confirmed by the team who owns this API.
};
if (context.customLocation) {
this.addCustomLocationProperties(site, context.customLocation);
}
// Always on setting added for App Service plans excluding the free tier https://github.com/microsoft/vscode-azurefunctions/issues/3037
if (context.plan?.sku?.family) {
const isNotFree = context.plan.sku.family.toLowerCase() !== 'f';
const isNotElasticPremium = context.plan.sku.family.toLowerCase() !== 'ep';
const isNotConsumption: boolean = context.plan.sku.family.toLowerCase() !== 'y';
if (isNotFree && isNotElasticPremium && isNotConsumption) {
nonNullProp(site, 'siteConfig').alwaysOn = true;
}
}
return site;
}
private addCustomLocationProperties(site: Site, customLocation: CustomLocation): void {
nonNullProp(site, 'siteConfig').alwaysOn = true;
site.extendedLocation = { name: customLocation.id, type: 'customLocation' };
}
private async getNewSiteConfig(context: IFunctionAppWizardContext, stack: FullFunctionAppStack): Promise<SiteConfig> {
const stackSettings: FunctionAppRuntimeSettings = nonNullProp(stack.minorVersion.stackSettings, context.newSiteOS === WebsiteOS.linux ? 'linuxRuntimeSettings' : 'windowsRuntimeSettings');
const newSiteConfig: SiteConfig = stackSettings.siteConfigPropertiesDictionary;
const storageConnectionString: string = (await getStorageConnectionString(context)).connectionString;
const appSettings: NameValuePair[] = [
{
name: ConnectionKey.Storage,
value: storageConnectionString
},
{
name: extensionVersionKey,
value: '~' + getMajorVersion(context.version)
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
...Object.entries(stackSettings.appSettingsDictionary).map(([name, value]) => { return { name, value }; })
];
// This setting only applies for v1 https://github.com/Microsoft/vscode-azurefunctions/issues/640
if (context.version === FuncVersion.v1) {
appSettings.push({
name: 'AzureWebJobsDashboard',
value: storageConnectionString
});
}
const isElasticPremium: boolean = context.plan?.sku?.family?.toLowerCase() === 'ep';
const isConsumption: boolean = context.plan?.sku?.family?.toLowerCase() === 'y';
if (isConsumption || isElasticPremium) {
// WEBSITE_CONTENT* settings are added for consumption/premium plans, but not dedicated
// https://github.com/microsoft/vscode-azurefunctions/issues/1702
appSettings.push({
name: contentConnectionStringKey,
value: storageConnectionString
});
appSettings.push({
name: contentShareKey,
value: getNewFileShareName(nonNullProp(context, 'newSiteName'))
});
}
// This setting is not required, but we will set it since it has many benefits https://docs.microsoft.com/en-us/azure/azure-functions/run-functions-from-deployment-package
// That being said, it doesn't work on v1 C# Script https://github.com/Microsoft/vscode-azurefunctions/issues/684
// It also doesn't apply for Linux
if (context.newSiteOS === WebsiteOS.windows && !(context.language === ProjectLanguage.CSharpScript && context.version === FuncVersion.v1)) {
appSettings.push({
name: runFromPackageKey,
value: '1'
});
}
if (context.appInsightsComponent) {
appSettings.push({
name: 'APPINSIGHTS_INSTRUMENTATIONKEY',
value: context.appInsightsComponent.instrumentationKey
});
if (isElasticPremium && context.newSiteStack?.stack.value === 'java') {
// turn on full monitoring for Java on Elastic Premium
appSettings.push({
name: 'APPLICATIONINSIGHTS_ENABLE_AGENT',
value: 'true'
});
}
}
newSiteConfig.appSettings = appSettings;
return newSiteConfig;
}
}
function getNewFileShareName(siteName: string): string {
const randomLetters: number = 6;
const maxFileShareNameLength: number = 63;
return siteName.toLowerCase().substr(0, maxFileShareNameLength - randomLetters) + getRandomHexString(randomLetters);
}
function getSiteKind(context: IAppServiceWizardContext): string {
let kind: string = context.newSiteKind;
if (context.newSiteOS === 'linux') {
kind += ',linux';
}
if (context.customLocation) {
kind += ',kubernetes';
}
return kind;
}