-
Notifications
You must be signed in to change notification settings - Fork 8
/
step.go
253 lines (206 loc) · 7.16 KB
/
step.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package devsession
import (
"context"
"fmt"
"sync"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/integrations"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/planning/config"
"namespacelabs.dev/foundation/internal/planning/deploy"
"namespacelabs.dev/foundation/internal/planning/snapshot"
"namespacelabs.dev/foundation/internal/portforward"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
func setWorkspace(ctx context.Context, env cfg.Context, rt runtime.ClusterNamespace, packageNames []string, session *Session, portForward *portforward.PortForward) error {
planner, err := runtime.PlannerFor(ctx, env)
if err != nil {
return err
}
return compute.Do(ctx, func(ctx context.Context) error {
serverPackages := schema.PackageNames(packageNames...)
focusServers := snapshot.RequireServers(env, planner, serverPackages...)
fmt.Fprintf(console.Debug(ctx), "devworkflow: setWorkspace.Do\n")
// Changing the graph is fairly heavy-weight at this point, as it will lead to
// a rebuild of all packages (although they'll likely hit the cache), as well
// as a full re-deployment, re-port forward, etc. Ideally this would be more
// incremental by having narrower dependencies. E.g. single server would have
// a single build, single deployment, etc. And changes to siblings servers
// would only impact themselves, not all servers. #362
if err := compute.Continuously(ctx, &buildAndDeploy{
session: session,
portForward: portForward,
env: env,
serverPackages: serverPackages,
focusServers: focusServers,
cluster: rt,
planner: planner,
}, nil); err != nil {
return err
}
return nil
})
}
type buildAndDeploy struct {
session *Session
portForward *portforward.PortForward
env cfg.Context
serverPackages []schema.PackageName
focusServers compute.Computable[*snapshot.ServerSnapshot]
cluster runtime.ClusterNamespace
planner runtime.Planner
mu sync.Mutex
cancelRunning func()
}
func (do *buildAndDeploy) Inputs() *compute.In {
return compute.Inputs().Computable("focusServers", do.focusServers)
}
func (do *buildAndDeploy) Updated(ctx context.Context, r compute.Resolved) error {
fmt.Fprintf(console.Debug(ctx), "devworkflow: buildAndDeploy.Updated\n")
do.mu.Lock()
defer do.mu.Unlock()
// If a previous run is on-going, cancel it.
if do.cancelRunning != nil {
do.cancelRunning()
do.cancelRunning = nil
}
snapshot := compute.MustGetDepValue(r, do.focusServers, "focusServers")
focus, err := snapshot.Get(do.serverPackages...)
if err != nil {
return err
}
do.session.updateStackInPlace(func(stack *Stack) {
resetStack(stack, do.env, do.session.availableEnvs, focus)
})
switch do.env.Environment().Purpose {
case schema.Environment_DEVELOPMENT, schema.Environment_TESTING:
var observers []integrations.DevObserver
defer func() {
for _, obs := range observers {
obs.Close()
}
}()
if do.env.Environment().Purpose == schema.Environment_DEVELOPMENT {
for _, srv := range focus {
var observer integrations.DevObserver
// Must be invoked before building to make sure stack computation and building
// uses the updated context.
ctx, observer, err = integrations.IntegrationFor(srv.Framework()).PrepareDev(ctx, do.cluster, srv)
if err != nil {
return err
}
if observer != nil {
observers = append(observers, observer)
}
}
}
stack := snapshot.Stack()
do.session.updateStackInPlace(func(s *Stack) {
s.Stack = stack.Proto()
})
observers = append(observers, updateDeploymentStatus{do.session})
p, err := planning.NewPlannerFromExisting(do.env, do.planner)
if err != nil {
return err
}
plan, err := deploy.PrepareDeployStack(ctx, p, stack)
if err != nil {
return err
}
// The actual build + deploy is deferred into a separate Continuously() call, which
// reacts to changes to the dependencies of build/deploy (e.g. sources). We can't
// block here either or else we won't have updates to the package graph to be
// delivered (Continuously doesn't call Updated, until the previous call returns).
// A channel is used to signal that the child Continuously() has returned, and
// thus we can be sure that its Cleanup has been called.
done := make(chan struct{})
transformError := func(err error) error {
if err != nil {
if msg, ok := fnerrors.IsExpected(err); ok {
fmt.Fprintf(console.Stderr(ctx), "\n %s\n\n", msg)
return nil // Swallow the error in case it is expected.
}
}
return err
}
cancel := compute.SpawnCancelableOnContinuously(ctx, func(ctx context.Context) error {
defer close(done)
return compute.Continuously(ctx,
newUpdateCluster(snapshot.Env(), do.cluster, stack.Proto(), do.serverPackages, observers, plan, do.portForward),
transformError)
})
do.cancelRunning = func() {
cancel()
<-done
}
case schema.Environment_PRODUCTION:
if len(focus) > 1 {
fmt.Fprintf(console.Stderr(ctx), "Ignoring the following servers when fetching production configuration: %s\n", do.serverPackages[1:])
}
server := focus[0]
if err := tasks.Action("stack.rehydrate").Scope(server.PackageName()).Run(ctx,
func(ctx context.Context) error {
buildID, err := do.cluster.DeployedConfigImageID(ctx, server.Proto())
if err != nil {
return err
}
s, err := config.Rehydrate(ctx, server.Server, buildID)
if err != nil {
return err
}
do.session.updateStackInPlace(func(stack *Stack) {
stack.Stack = s.Stack
})
return nil
}); err != nil {
return err
}
}
return nil
}
type updateDeploymentStatus struct {
session *Session
}
func (u updateDeploymentStatus) OnDeployment(ctx context.Context) {
u.session.updateStackInPlace(func(s *Stack) {
s.Deployed = true
s.DeploymentRevision++
})
}
func (updateDeploymentStatus) Close() error {
return nil
}
func (do *buildAndDeploy) Cleanup(ctx context.Context) error {
do.mu.Lock()
defer do.mu.Unlock()
if do.cancelRunning != nil {
do.cancelRunning()
}
return nil
}
func resetStack(out *Stack, env cfg.Context, availableEnvs []*schema.Environment, focus []planning.PlannedServer) {
workspace := protos.Clone(env.Workspace().Proto())
out.AbsRoot = env.Workspace().LoadedFrom().AbsPath
out.Env = env.Environment()
out.Workspace = workspace
out.AvailableEnv = availableEnvs
out.Deployed = false
// The DeploymentRevision is purposely not reset. The contract is that it increases monotonically.
out.Focus = nil
out.Current = nil
if len(focus) > 0 {
out.Current = focus[0].StackEntry() // XXX legacy
for _, fs := range focus {
out.Focus = append(out.Focus, fs.PackageName().String())
}
}
}