forked from Azure/azure-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovision.go
425 lines (373 loc) · 13 KB
/
provision.go
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cmd
import (
"context"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/cloud"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/multierr"
)
type ProvisionFlags struct {
noProgress bool
preview bool
ignoreDeploymentState bool
global *internal.GlobalCommandOptions
*internal.EnvFlag
}
const (
AINotValid = "is not valid according to the validation procedure"
openAIsubscriptionNoQuotaId = "The subscription does not have QuotaId/Feature required by SKU 'S0' " +
"from kind 'OpenAI'"
responsibleAITerms = "until you agree to Responsible AI terms for this resource"
specialFeatureOrQuotaIdRequired = "SpecialFeatureOrQuotaIdRequired"
)
func (i *ProvisionFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
i.BindNonCommon(local, global)
i.bindCommon(local, global)
}
func (i *ProvisionFlags) BindNonCommon(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
local.BoolVar(&i.noProgress, "no-progress", false, "Suppresses progress information.")
//deprecate:Flag hide --no-progress
_ = local.MarkHidden("no-progress")
i.global = global
}
func (i *ProvisionFlags) bindCommon(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
local.BoolVar(&i.preview, "preview", false, "Preview changes to Azure resources.")
local.BoolVar(
&i.ignoreDeploymentState,
"no-state",
false,
"Do not use latest Deployment State (bicep only).")
i.EnvFlag = &internal.EnvFlag{}
i.EnvFlag.Bind(local, global)
}
func (i *ProvisionFlags) SetCommon(envFlag *internal.EnvFlag) {
i.EnvFlag = envFlag
}
func NewProvisionFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *ProvisionFlags {
flags := &ProvisionFlags{}
flags.Bind(cmd.Flags(), global)
return flags
}
func NewProvisionFlagsFromEnvAndOptions(envFlag *internal.EnvFlag, global *internal.GlobalCommandOptions) *ProvisionFlags {
flags := &ProvisionFlags{
EnvFlag: envFlag,
global: global,
}
return flags
}
func NewProvisionCmd() *cobra.Command {
return &cobra.Command{
Use: "provision",
Short: "Provision the Azure resources for an application.",
}
}
type ProvisionAction struct {
flags *ProvisionFlags
provisionManager *provisioning.Manager
projectManager project.ProjectManager
resourceManager project.ResourceManager
env *environment.Environment
envManager environment.Manager
formatter output.Formatter
projectConfig *project.ProjectConfig
writer io.Writer
console input.Console
subManager *account.SubscriptionsManager
importManager *project.ImportManager
alphaFeatureManager *alpha.FeatureManager
portalUrlBase string
}
func NewProvisionAction(
flags *ProvisionFlags,
provisionManager *provisioning.Manager,
projectManager project.ProjectManager,
importManager *project.ImportManager,
resourceManager project.ResourceManager,
projectConfig *project.ProjectConfig,
env *environment.Environment,
envManager environment.Manager,
console input.Console,
formatter output.Formatter,
writer io.Writer,
subManager *account.SubscriptionsManager,
alphaFeatureManager *alpha.FeatureManager,
cloud *cloud.Cloud,
) actions.Action {
return &ProvisionAction{
flags: flags,
provisionManager: provisionManager,
projectManager: projectManager,
resourceManager: resourceManager,
env: env,
envManager: envManager,
formatter: formatter,
projectConfig: projectConfig,
writer: writer,
console: console,
subManager: subManager,
importManager: importManager,
alphaFeatureManager: alphaFeatureManager,
portalUrlBase: cloud.PortalUrlBase,
}
}
// SetFlags sets the flags for the provision action. Panics if `flags` is nil
func (p *ProvisionAction) SetFlags(flags *ProvisionFlags) {
if flags == nil {
panic("flags is nil")
}
p.flags = flags
}
func (p *ProvisionAction) Run(ctx context.Context) (*actions.ActionResult, error) {
if p.flags.noProgress {
fmt.Fprintln(
p.console.Handles().Stderr,
//nolint:Lll
output.WithWarningFormat(
"WARNING: The '--no-progress' flag is deprecated and will be removed in a future release.",
),
)
}
previewMode := p.flags.preview
// Command title
defaultTitle := "Provisioning Azure resources (azd provision)"
defaultTitleNote := "Provisioning Azure resources can take some time"
if previewMode {
defaultTitle = "Previewing Azure resource changes (azd provision --preview)"
defaultTitleNote = "This is a preview. No changes will be applied to your Azure resources."
}
p.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: defaultTitle,
TitleNote: defaultTitleNote},
)
startTime := time.Now()
if err := p.projectManager.Initialize(ctx, p.projectConfig); err != nil {
return nil, err
}
if err := p.projectManager.EnsureAllTools(ctx, p.projectConfig, nil); err != nil {
return nil, err
}
infra, err := p.importManager.ProjectInfrastructure(ctx, p.projectConfig)
if err != nil {
return nil, err
}
defer func() { _ = infra.Cleanup() }()
infraOptions := infra.Options
infraOptions.IgnoreDeploymentState = p.flags.ignoreDeploymentState
if err := p.provisionManager.Initialize(ctx, p.projectConfig.Path, infraOptions); err != nil {
return nil, fmt.Errorf("initializing provisioning manager: %w", err)
}
// Get Subscription to Display in Command Title Note
// Subscription and Location are ONLY displayed when they are available (found from env), otherwise, this message
// is not displayed.
// This needs to happen after the provisionManager initializes to make sure the env is ready for the provisioning
// provider
subscription, subErr := p.subManager.GetSubscription(ctx, p.env.GetSubscriptionId())
if subErr == nil {
location, err := p.subManager.GetLocation(ctx, p.env.GetSubscriptionId(), p.env.GetLocation())
var locationDisplay string
if err != nil {
log.Printf("failed getting location: %v", err)
} else {
locationDisplay = location.DisplayName
}
var subscriptionDisplay string
if v, err := strconv.ParseBool(os.Getenv("AZD_DEMO_MODE")); err == nil && v {
subscriptionDisplay = subscription.Name
} else {
subscriptionDisplay = fmt.Sprintf("%s (%s)", subscription.Name, subscription.Id)
}
p.console.MessageUxItem(ctx, &ux.EnvironmentDetails{
Subscription: subscriptionDisplay,
Location: locationDisplay,
})
} else {
log.Printf("failed getting subscriptions. Skip displaying sub and location: %v", subErr)
}
var deployResult *provisioning.DeployResult
var deployPreviewResult *provisioning.DeployPreviewResult
projectEventArgs := project.ProjectLifecycleEventArgs{
Project: p.projectConfig,
Args: map[string]any{
"preview": previewMode,
},
}
if p.alphaFeatureManager.IsEnabled(azapi.FeatureDeploymentStacks) {
p.console.WarnForFeature(ctx, azapi.FeatureDeploymentStacks)
}
err = p.projectConfig.Invoke(ctx, project.ProjectEventProvision, projectEventArgs, func() error {
var err error
if previewMode {
deployPreviewResult, err = p.provisionManager.Preview(ctx)
} else {
deployResult, err = p.provisionManager.Deploy(ctx)
}
return err
})
if err != nil {
if p.formatter.Kind() == output.JsonFormat {
stateResult, err := p.provisionManager.State(ctx, nil)
if err != nil {
return nil, fmt.Errorf(
"deployment failed and the deployment result is unavailable: %w",
multierr.Combine(err, err),
)
}
if err := p.formatter.Format(
provisioning.NewEnvRefreshResultFromState(stateResult.State), p.writer, nil); err != nil {
return nil, fmt.Errorf(
"deployment failed and the deployment result could not be displayed: %w",
multierr.Combine(err, err),
)
}
}
//if user don't have access to openai
errorMsg := err.Error()
if strings.Contains(errorMsg, specialFeatureOrQuotaIdRequired) && strings.Contains(errorMsg, "OpenAI") {
requestAccessLink := "https://go.microsoft.com/fwlink/?linkid=2259205&clcid=0x409"
return nil, &internal.ErrorWithSuggestion{
Err: err,
Suggestion: "\nSuggested Action: The selected subscription does not have access to" +
" Azure OpenAI Services. Please visit " + output.WithLinkFormat("%s", requestAccessLink) +
" to request access.",
}
}
if strings.Contains(errorMsg, AINotValid) &&
strings.Contains(errorMsg, openAIsubscriptionNoQuotaId) {
return nil, &internal.ErrorWithSuggestion{
Suggestion: "\nSuggested Action: The selected " +
"subscription has not been enabled for use of Azure AI service and does not have quota for " +
"any pricing tiers. Please visit " + output.WithLinkFormat("%s", p.portalUrlBase) +
" and select 'Create' on specific services to request access.",
Err: err,
}
}
//if user haven't agree to Responsible AI terms
if strings.Contains(errorMsg, responsibleAITerms) {
return nil, &internal.ErrorWithSuggestion{
Suggestion: "\nSuggested Action: Please visit azure portal in " +
output.WithLinkFormat("%s", p.portalUrlBase) + ". Create the resource in azure portal " +
"to go through Responsible AI terms, and then delete it. " +
"After that, run 'azd provision' again",
Err: err,
}
}
return nil, fmt.Errorf("deployment failed: %w", err)
}
if previewMode {
p.console.MessageUxItem(ctx, deployResultToUx(deployPreviewResult))
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: fmt.Sprintf(
"Generated provisioning preview in %s.", ux.DurationAsText(since(startTime))),
FollowUp: getResourceGroupFollowUp(
ctx,
p.formatter,
p.portalUrlBase,
p.projectConfig,
p.resourceManager,
p.env,
true,
),
},
}, nil
}
if deployResult.SkippedReason == provisioning.DeploymentStateSkipped {
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: "There are no changes to provision for your application.",
},
}, nil
}
servicesStable, err := p.importManager.ServiceStable(ctx, p.projectConfig)
if err != nil {
return nil, err
}
for _, svc := range servicesStable {
eventArgs := project.ServiceLifecycleEventArgs{
Project: p.projectConfig,
Service: svc,
Args: map[string]any{
"bicepOutput": deployResult.Deployment.Outputs,
},
}
if err := svc.RaiseEvent(ctx, project.ServiceEventEnvUpdated, eventArgs); err != nil {
return nil, err
}
}
if p.formatter.Kind() == output.JsonFormat {
stateResult, err := p.provisionManager.State(ctx, nil)
if err != nil {
return nil, fmt.Errorf(
"deployment succeeded but the deployment result is unavailable: %w",
multierr.Combine(err, err),
)
}
if err := p.formatter.Format(
provisioning.NewEnvRefreshResultFromState(stateResult.State), p.writer, nil); err != nil {
return nil, fmt.Errorf(
"deployment succeeded but the deployment result could not be displayed: %w",
multierr.Combine(err, err),
)
}
}
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: fmt.Sprintf(
"Your application was provisioned in Azure in %s.", ux.DurationAsText(since(startTime))),
FollowUp: getResourceGroupFollowUp(
ctx,
p.formatter,
p.portalUrlBase,
p.projectConfig,
p.resourceManager,
p.env,
false,
),
},
}, nil
}
// deployResultToUx creates the ux element to display from a provision preview
func deployResultToUx(previewResult *provisioning.DeployPreviewResult) ux.UxItem {
var operations []*ux.Resource
for _, change := range previewResult.Preview.Properties.Changes {
operations = append(operations, &ux.Resource{
Operation: ux.OperationType(change.ChangeType),
Type: change.ResourceType,
Name: change.Name,
})
}
return &ux.PreviewProvision{
Operations: operations,
}
}
func GetCmdProvisionHelpDescription(c *cobra.Command) string {
return generateCmdHelpDescription(fmt.Sprintf(
"Provision the Azure resources for an application."+
" This step may take a while depending on the resources provisioned."+
" You should run %s any time you update your Bicep or Terraform file."+
"\n\nThis command prompts you to input the following:",
output.WithHighLightFormat(c.CommandPath())), []string{
formatHelpNote("Azure location: The Azure location where your resources will be deployed."),
formatHelpNote("Azure subscription: The Azure subscription where your resources will be deployed."),
})
}