-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
365 lines (299 loc) · 8.01 KB
/
bootstrap.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
package bootstrap
import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/hephbuild/heph/buildfiles"
"github.com/hephbuild/heph/config"
"github.com/hephbuild/heph/exprs"
"github.com/hephbuild/heph/gitstatus"
"github.com/hephbuild/heph/graph"
"github.com/hephbuild/heph/hbuiltin"
"github.com/hephbuild/heph/hroot"
"github.com/hephbuild/heph/lcache"
"github.com/hephbuild/heph/log/log"
"github.com/hephbuild/heph/observability"
obsummary "github.com/hephbuild/heph/observability/summary"
"github.com/hephbuild/heph/packages"
"github.com/hephbuild/heph/platform"
"github.com/hephbuild/heph/rcache"
"github.com/hephbuild/heph/scheduler"
"github.com/hephbuild/heph/specs"
"github.com/hephbuild/heph/targetrun"
"github.com/hephbuild/heph/upgrade"
"github.com/hephbuild/heph/utils/finalizers"
"github.com/hephbuild/heph/worker2"
"github.com/pbnjay/memory"
"os"
"path/filepath"
"strings"
)
func Getwd() (string, error) {
if ecwd := os.Getenv("HEPH_CWD"); ecwd != "" {
err := os.Chdir(ecwd)
if err != nil {
return "", err
}
return ecwd, nil
}
return os.Getwd()
}
func findRoot(cwd string) (string, error) {
parts := strings.Split(cwd, string(filepath.Separator))
for len(parts) > 0 {
p := "/" + filepath.Join(parts...)
if _, err := os.Stat(filepath.Join(p, ".hephconfig")); err == nil {
return p, nil
}
parts = parts[:len(parts)-1]
}
return "", errors.New("root not found, are you running this command in the repo directory?")
}
type BootOpts struct {
Profiles []string
Workers int
Params map[string]string
Summary bool
JaegerEndpoint string
DisableCloudTelemetry bool
Pool *worker2.Engine
PostBootBase func(bs BaseBootstrap) error
FlowID string
}
type BaseBootstrap struct {
Cwd string
Root *hroot.State
Config *config.Config
}
func BootBase(ctx context.Context, opts BootOpts) (BaseBootstrap, error) {
lipgloss.SetDefaultRenderer(log.Renderer())
bs := BaseBootstrap{}
cwd, err := Getwd()
if err != nil {
return bs, err
}
bs.Cwd = cwd
rootPath, err := findRoot(cwd)
if err != nil {
return bs, fmt.Errorf("findRoot: %w", err)
}
root, err := hroot.NewState(rootPath)
if err != nil {
return bs, fmt.Errorf("hroot: %w", err)
}
bs.Root = root
cfg, err := BuildConfig(root, opts.Profiles)
if err != nil {
return bs, fmt.Errorf("config: %w", err)
}
for k, v := range opts.Params {
cfg.Params[k] = v
}
bs.Config = cfg
err = upgrade.CheckAndUpdate(ctx, *cfg)
if err != nil {
return bs, fmt.Errorf("upgrade: %w", err)
}
if opts.PostBootBase != nil {
err := opts.PostBootBase(bs)
if err != nil {
return bs, err
}
}
return bs, nil
}
type Bootstrap struct {
BaseBootstrap
Finalizers *finalizers.Finalizers
Observability *observability.Observability
Cloud Cloud
Summary *obsummary.Summary
Pool *worker2.Engine
Packages *packages.Registry
BuildFiles *buildfiles.State
Graph *graph.State
PlatformProviders []platform.PlatformProvider
}
func DefaultScheduler(cpu int) *worker2.ResourceScheduler {
return worker2.NewResourceScheduler(map[string]float64{
"cpu": float64(cpu),
"memory": float64(memory.TotalMemory()),
}, map[string]float64{
"cpu": float64(1),
})
}
func Boot(ctx context.Context, opts BootOpts) (Bootstrap, error) {
bs := Bootstrap{}
bbs, err := BootBase(ctx, opts)
if err != nil {
return bs, err
}
bs.BaseBootstrap = bbs
root := bbs.Root
cfg := bbs.Config
fins := &finalizers.Finalizers{}
bs.Finalizers = fins
obs := observability.NewTelemetry()
bs.Observability = obs
err = setupJaeger(fins, obs, opts.JaegerEndpoint)
if err != nil {
return bs, fmt.Errorf("jaeger: %w", err)
}
cloud, err := setupHephcloud(ctx, root, cfg, fins, obs, !opts.DisableCloudTelemetry, opts.FlowID)
if err != nil {
return bs, fmt.Errorf("cloud: %w", err)
}
bs.Cloud = cloud
if opts.Summary {
sum := &obsummary.Summary{}
obs.RegisterHook(sum)
bs.Summary = sum
}
ctx, rootSpan := obs.SpanRoot(ctx)
fins.RegisterWithErr(func(err error) {
rootSpan.EndError(err)
})
pool := opts.Pool
if pool == nil {
pool = worker2.NewEngine()
pool.SetDefaultScheduler(DefaultScheduler(opts.Workers))
go pool.Run()
}
bs.Pool = pool
pkgs := packages.NewRegistry(packages.Registry{
Root: root,
RootsCachePath: root.Home.Join("roots").Abs(),
Roots: cfg.BuildFiles.Roots,
})
bs.Packages = pkgs
buildfilesState := buildfiles.NewState(buildfiles.State{
Ignore: cfg.BuildFiles.Ignore,
Patterns: cfg.BuildFiles.Patterns,
Packages: pkgs,
})
bs.BuildFiles = buildfilesState
g := graph.NewState(root, cfg)
bs.Graph = g
{
bopts := hbuiltin.Bootstrap(hbuiltin.Opts{
Pkgs: pkgs,
Root: root,
Config: cfg,
RegisterTarget: func(spec specs.Target) error {
_, err := g.Register(spec)
return err
},
})
for name, cfg := range cfg.BuildFiles.Roots {
opts := bopts.Copy()
p, err := pkgs.FetchRoot(ctx, name, cfg)
if err != nil {
return bs, fmt.Errorf("fetch: %w", err)
}
opts.RootPkg = pkgs.GetOrCreate(packages.Package{
Path: name,
Root: p,
})
files, err := buildfilesState.CollectFilesInRoot(ctx, opts.RootPkg.Root.Abs())
if err != nil {
return bs, err
}
err = buildfilesState.RunBuildFiles(ctx, files, opts)
if err != nil {
return bs, fmt.Errorf("buildfiles: root %v: %w", name, err)
}
}
files, err := buildfilesState.CollectFiles(ctx, bopts.RootPkg.Root.Abs())
if err != nil {
return bs, err
}
err = buildfilesState.RunBuildFiles(ctx, files, bopts)
if err != nil {
return bs, fmt.Errorf("buildfiles: %w", err)
}
for _, p := range bs.Packages.All() {
p.Globals = nil
}
}
bs.PlatformProviders = platform.Bootstrap(cfg)
return bs, nil
}
func BootScheduler(ctx context.Context, bs Bootstrap) (*scheduler.Scheduler, error) {
fins := &finalizers.Finalizers{}
localCache, err := lcache.NewState(bs.Root, bs.Pool, bs.Graph.Targets(), bs.Observability, fins, bs.Config.Engine.GC, bs.Config.Engine.ParallelCaching, bs.Config.Engine.CacheRW)
if err != nil {
return nil, err
}
remoteCache, err := rcache.New(bs.Root, bs.Config, localCache, bs.Observability)
if err != nil {
return nil, err
}
var getFlowId func() string
if hook := bs.Cloud.Hook; hook != nil {
getFlowId = func() string {
return hook.FlowId
}
}
runner := &targetrun.Runner{
Root: bs.Root,
Observability: bs.Observability,
Finalizers: fins,
LocalCache: localCache,
PlatformProviders: bs.PlatformProviders,
GetFlowID: getFlowId,
QueryFunctions: func(target *graph.Target) map[string]exprs.Func {
return QueryFunctions(bs.Root, localCache, bs.Graph, target)
},
Cwd: bs.Cwd,
Config: bs.Config,
Pool: bs.Pool,
}
e := scheduler.New(scheduler.Scheduler{
Cwd: bs.Cwd,
Root: bs.Root,
Config: bs.Config,
Observability: bs.Observability,
GetFlowID: getFlowId,
LocalCache: localCache,
RemoteCache: remoteCache,
Packages: bs.Packages,
BuildFilesState: bs.BuildFiles,
Graph: bs.Graph,
Pool: bs.Pool,
BackgroundTracker: worker2.NewRunningTracker(),
Finalizers: fins,
Runner: runner,
})
if bs.Config.Engine.GitCacheHints {
e.GitStatus = gitstatus.New(bs.Root.Root.Abs())
}
bs.Finalizers.RegisterWithErr(func(err error) {
fins.Run(err)
})
if bs.Config.Engine.InstallTools {
err = InstallTools(ctx, bs.Root, bs.Graph, bs.Config.Version.String)
if err != nil {
return nil, err
}
}
return e, nil
}
type SchedulerBootstrap struct {
Bootstrap
Scheduler *scheduler.Scheduler
}
func BootWithScheduler(ctx context.Context, opts BootOpts) (SchedulerBootstrap, error) {
ebs := SchedulerBootstrap{}
bs, err := Boot(ctx, opts)
if err != nil {
return ebs, err
}
ebs.Bootstrap = bs
e, err := BootScheduler(ctx, bs)
if err != nil {
return ebs, err
}
ebs.Scheduler = e
return ebs, nil
}