-
Notifications
You must be signed in to change notification settings - Fork 170
/
framework_service_dotnet.go
275 lines (240 loc) · 8.77 KB
/
framework_service_dotnet.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package project
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/async"
"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/tools"
"github.com/azure/azure-dev/cli/azd/pkg/tools/dotnet"
)
const (
defaultDotNetBuildConfiguration string = "Release"
)
type dotnetProject struct {
env *environment.Environment
dotnetCli dotnet.DotNetCli
}
// NewDotNetProject creates a new instance of a dotnet project
func NewDotNetProject(
dotNetCli dotnet.DotNetCli,
env *environment.Environment,
) FrameworkService {
return &dotnetProject{
env: env,
dotnetCli: dotNetCli,
}
}
func (dp *dotnetProject) Requirements() FrameworkRequirements {
return FrameworkRequirements{
// dotnet will automatically restore & build the project if needed
Package: FrameworkPackageRequirements{
RequireRestore: false,
RequireBuild: false,
},
}
}
// Gets the required external tools for the project
func (dp *dotnetProject) RequiredExternalTools(context.Context) []tools.ExternalTool {
return []tools.ExternalTool{dp.dotnetCli}
}
// Initializes the dotnet project
func (dp *dotnetProject) Initialize(ctx context.Context, serviceConfig *ServiceConfig) error {
// NOTE(ellismg): For dotnet based apps, we installed a lifecycle hook that would write all the outputs from a deployment
// into dotnet user-secrets. The goal of this was to make it easy to consume the values from your infrastructure in your
// dotnet app, but the strategy doesn't work well in practice and it ends up being an abuse of the user secrets setup.
//
// We'd like to stop doing this at some point for all .NET projects, but we can make sure that we don't inherit the
// bad behavior for containerized projects, without being concerned about it being considered a breaking change.
if serviceConfig.Host != DotNetContainerAppTarget {
projFile, err := findProjectFile(serviceConfig.Name, serviceConfig.Path())
if err != nil {
return err
}
if err := dp.dotnetCli.InitializeSecret(ctx, projFile); err != nil {
return err
}
handler := func(ctx context.Context, args ServiceLifecycleEventArgs) error {
return dp.setUserSecretsFromOutputs(ctx, serviceConfig, args)
}
if err := serviceConfig.AddHandler(ServiceEventEnvUpdated, handler); err != nil {
return err
}
}
return nil
}
// Restores the dependencies for the project
func (dp *dotnetProject) Restore(
ctx context.Context,
serviceConfig *ServiceConfig,
) *async.TaskWithProgress[*ServiceRestoreResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServiceRestoreResult, ServiceProgress]) {
task.SetProgress(NewServiceProgress("Restoring .NET project dependencies"))
projFile, err := findProjectFile(serviceConfig.Name, serviceConfig.Path())
if err != nil {
task.SetError(err)
return
}
if err := dp.dotnetCli.Restore(ctx, projFile); err != nil {
task.SetError(err)
return
}
task.SetResult(&ServiceRestoreResult{})
},
)
}
// Builds the dotnet project using the dotnet CLI
func (dp *dotnetProject) Build(
ctx context.Context,
serviceConfig *ServiceConfig,
restoreOutput *ServiceRestoreResult,
) *async.TaskWithProgress[*ServiceBuildResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServiceBuildResult, ServiceProgress]) {
task.SetProgress(NewServiceProgress("Building .NET project"))
projFile, err := findProjectFile(serviceConfig.Name, serviceConfig.Path())
if err != nil {
task.SetError(err)
return
}
if err := dp.dotnetCli.Build(ctx, projFile, defaultDotNetBuildConfiguration, ""); err != nil {
task.SetError(err)
return
}
defaultOutputDir := filepath.Join("./bin", defaultDotNetBuildConfiguration)
// Attempt to find the default build output location
buildOutputDir := serviceConfig.Path()
_, err = os.Stat(filepath.Join(buildOutputDir, defaultOutputDir))
if err == nil {
buildOutputDir = filepath.Join(buildOutputDir, defaultOutputDir)
}
// By default dotnet build will create a sub folder for the project framework version, etc. net8.0
// If we have a single folder under build configuration assume this location as build output result
subDirs, err := os.ReadDir(buildOutputDir)
if err == nil {
if len(subDirs) == 1 {
buildOutputDir = filepath.Join(buildOutputDir, subDirs[0].Name())
}
}
task.SetResult(&ServiceBuildResult{
Restore: restoreOutput,
BuildOutputPath: buildOutputDir,
})
},
)
}
func (dp *dotnetProject) Package(
ctx context.Context,
serviceConfig *ServiceConfig,
buildOutput *ServiceBuildResult,
) *async.TaskWithProgress[*ServicePackageResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServicePackageResult, ServiceProgress]) {
if serviceConfig.Host == DotNetContainerAppTarget {
// TODO(weilim): For containerized projects, we publish the produced container image in a single call
// via `dotnet publish /p:PublishProfile=DefaultContainer`, thus the default `dotnet publish` command
// executed here is not useful.
//
// It's probably right for us to think about "package" for a containerized application as meaning
// "produce the tgz" of the image, as would be done by `docker save`, but this is currently not supported.
//
// See related comment in cmd/package.go.
task.SetResult(&ServicePackageResult{})
return
}
packageDest, err := os.MkdirTemp("", "azd")
if err != nil {
task.SetError(fmt.Errorf("creating package directory for %s: %w", serviceConfig.Name, err))
return
}
task.SetProgress(NewServiceProgress("Publishing .NET project"))
projFile, err := findProjectFile(serviceConfig.Name, serviceConfig.Path())
if err != nil {
task.SetError(err)
return
}
if err := dp.dotnetCli.Publish(ctx, projFile, defaultDotNetBuildConfiguration, packageDest); err != nil {
task.SetError(err)
return
}
if serviceConfig.OutputPath != "" {
packageDest = filepath.Join(packageDest, serviceConfig.OutputPath)
}
if err := validatePackageOutput(packageDest); err != nil {
task.SetError(err)
return
}
task.SetResult(&ServicePackageResult{
Build: buildOutput,
PackagePath: packageDest,
})
},
)
}
func (dp *dotnetProject) setUserSecretsFromOutputs(
ctx context.Context,
serviceConfig *ServiceConfig,
args ServiceLifecycleEventArgs,
) error {
bicepOutputArgs := args.Args["bicepOutput"]
if bicepOutputArgs == nil {
log.Println("no bicep outputs set as secrets to dotnet project, map args.Args doesn't contain key \"bicepOutput\"")
return nil
}
bicepOutput, ok := bicepOutputArgs.(map[string]provisioning.OutputParameter)
if !ok {
return fmt.Errorf("fail on interface conversion: no type in map")
}
secrets := map[string]string{}
for key, val := range bicepOutput {
secrets[normalizeDotNetSecret(key)] = fmt.Sprint(val.Value)
}
if err := dp.dotnetCli.SetSecrets(ctx, secrets, serviceConfig.Path()); err != nil {
return fmt.Errorf("failed to set secrets: %w", err)
}
return nil
}
func normalizeDotNetSecret(key string) string {
// dotnet recognizes "__" as the hierarchy key separator for environment variables, but for user secrets, it has to be
// ":".
return strings.ReplaceAll(key, "__", ":")
}
/* findProjectFile locates the project file to pass to the `dotnet` tool for a given dotnet service.
**
** projectPath is either a path to a directory, or to a project file. When projectPath is a directory,
** the first file matching the glob expression *.*proj (what dotnet expects) is returned.
** If multiple files match, an error is returned.
*/
func findProjectFile(serviceName string, projectPath string) (string, error) {
info, err := os.Stat(projectPath)
if err != nil {
return "", err
}
if !info.IsDir() {
return projectPath, nil
}
files, err := filepath.Glob(filepath.Join(projectPath, "*.*proj"))
if err != nil {
return "", fmt.Errorf("searching for project file: %w", err)
}
if len(files) == 0 {
return "", fmt.Errorf(
"could not locate a dotnet project file for service %s in %s. Update the project setting of "+
"azure.yaml for service %s to be the path to the dotnet project for this service",
serviceName, projectPath, serviceName)
} else if len(files) > 1 {
return "", fmt.Errorf(
"could not locate a dotnet project file for service %s in %s. Multiple project files exist. Update "+
"the \"project\" setting of azure.yaml for service %s to be the path to the dotnet project to use for this "+
"service",
serviceName, projectPath, serviceName)
}
return files[0], nil
}