-
Notifications
You must be signed in to change notification settings - Fork 100
/
start.go
743 lines (639 loc) · 16.6 KB
/
start.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package devtool
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/fatih/color"
"github.com/hashicorp/go-version"
"github.com/jackc/pgx/v5"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/gitutil"
"github.com/rilldata/rill/runtime/pkg/graceful"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
const (
minGoVersion = "1.22"
minNodeVersion = "18"
stateDirCloud = "dev-cloud-state"
stateDirLocal = "dev-project"
rillGithubURL = "https://github.com/rilldata/rill"
)
var (
logErr = color.New(color.FgHiRed)
logWarn = color.New(color.FgHiYellow)
logInfo = color.New(color.FgHiGreen)
)
func StartCmd(ch *cmdutil.Helper) *cobra.Command {
var verbose, reset, refreshDotenv bool
services := &servicesCfg{}
cmd := &cobra.Command{
Use: "start [cloud|local]",
Short: "Start a local development environment",
RunE: func(cmd *cobra.Command, args []string) error {
var preset string
if len(args) > 0 {
preset = args[0]
} else {
res, err := cmdutil.SelectPrompt("Select preset", []string{"cloud", "local"}, "cloud")
if err != nil {
return err
}
preset = res
}
err := services.parse()
if err != nil {
return fmt.Errorf("failed to parse services: %w", err)
}
return start(ch, preset, verbose, reset, refreshDotenv, services)
},
}
cmd.Flags().BoolVar(&verbose, "verbose", false, "Set log level to debug")
cmd.Flags().BoolVar(&reset, "reset", false, "Reset local development state")
cmd.Flags().BoolVar(&refreshDotenv, "refresh-dotenv", true, "Refresh .env file from shared storage")
services.addFlags(cmd)
return cmd
}
func start(ch *cmdutil.Helper, preset string, verbose, reset, refreshDotenv bool, services *servicesCfg) error {
ctx := graceful.WithCancelOnTerminate(context.Background())
err := errors.Join(
checkGoVersion(),
checkNodeVersion(ctx),
checkDocker(ctx),
checkRillRepo(),
)
if err != nil {
return err
}
switch preset {
case "cloud":
err = cloud{}.start(ctx, ch, verbose, reset, refreshDotenv, services)
case "local":
err = local{}.start(ctx, verbose, reset, services)
default:
err = fmt.Errorf("Unknown preset %q", preset)
}
// If ctx.Err() != nil, we don't return the err because any graceful shutdown will cause sub-commands to return non-zero exit code errors.
// In these cases, ignoring the error doesn't really matter since "real" errors are probably also logged to stdout anyway.
if err != nil && (ctx.Err() == nil || verbose) {
return err
}
return nil
}
func checkGoVersion() error {
v := version.Must(version.NewVersion(strings.TrimPrefix(runtime.Version(), "go")))
min := version.Must(version.NewVersion(minGoVersion))
if v.LessThan(min) {
return fmt.Errorf("Go version %s or higher is required", minGoVersion)
}
return nil
}
func checkNodeVersion(ctx context.Context) error {
out, err := newCmd(ctx, "node", "--version").Output()
if err != nil {
return fmt.Errorf("error executing the 'node --version' command: %w", err)
}
v := version.Must(version.NewVersion(strings.TrimSpace(string(out))))
min := version.Must(version.NewVersion(minNodeVersion))
if v.LessThan(min) {
return fmt.Errorf("node.js version %s or higher is required", minNodeVersion)
}
return nil
}
func checkDocker(ctx context.Context) error {
out, err := newCmd(ctx, "docker", "info", "--format", "json").Output()
if err != nil {
return fmt.Errorf("error executing the 'docker info' command: %w", err)
}
info := make(map[string]any)
err = json.Unmarshal(out, &info)
if err != nil {
return fmt.Errorf("error parsing the output of 'docker info': %w", err)
}
if sv, ok := info["ServerVersion"].(string); !ok || sv == "" {
return errors.New("error extracting the Docker server version (is Docker running?)")
}
if se, ok := info["ServerErrors"].([]string); ok && len(se) > 0 {
return fmt.Errorf("docker not available: found errors: %v", se)
}
return nil
}
func checkRillRepo() error {
_, err := os.Stat(".git")
if err != nil {
return fmt.Errorf("you must run `rill devtool` from the root of the rill repository")
}
_, githubURL, err := gitutil.ExtractGitRemote("", "", false)
if err != nil {
return fmt.Errorf("error extracting git remote: %w", err)
}
if githubURL != rillGithubURL {
return fmt.Errorf("you must run `rill devtool` from the rill repository (expected remote %q, got %q)", rillGithubURL, githubURL)
}
return nil
}
type servicesCfg struct {
admin bool
deps bool
runtime bool
ui bool
only []string
except []string
}
func (s *servicesCfg) addFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVar(&s.only, "only", []string{}, "Only start the listed services (options: admin, deps, runtime, ui)")
cmd.Flags().StringSliceVar(&s.except, "except", []string{}, "Start all except the listed services (options: admin, deps, runtime, ui)")
}
func (s *servicesCfg) parse() error {
if len(s.only) > 0 && len(s.except) > 0 {
return errors.New("cannot use both --only and --except")
}
vals := s.except
def := true
if len(s.only) > 0 {
vals = s.only
def = false
}
s.admin = def
s.deps = def
s.runtime = def
s.ui = def
for _, v := range vals {
switch v {
case "admin":
s.admin = !def
case "deps":
s.deps = !def
case "runtime":
s.runtime = !def
case "ui":
s.ui = !def
default:
return fmt.Errorf("invalid service %q", v)
}
}
return nil
}
type cloud struct{}
func (s cloud) start(ctx context.Context, ch *cmdutil.Helper, verbose, reset, refreshDotenv bool, services *servicesCfg) error {
if reset {
err := s.resetState(ctx)
if err != nil {
return fmt.Errorf("reset cloud deps: %w", err)
}
logInfo.Printf("Reset cloud dependencies\n")
}
err := os.MkdirAll(stateDirCloud, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create state dir %q: %w", stateDirCloud, err)
}
if refreshDotenv {
err := downloadDotenv(ctx, "cloud")
if err != nil {
return fmt.Errorf("failed to refresh .env: %w", err)
}
logInfo.Printf("Refreshed .env\n")
}
// Validate the .env file is well-formed.
err = checkDotenv()
if err != nil {
return err
}
_, err = godotenv.Read()
if err != nil {
return fmt.Errorf("error parsing .env: %w", err)
}
g, ctx := errgroup.WithContext(ctx)
if services.deps {
g.Go(func() error { return s.runDeps(ctx, verbose) })
}
depsReadyCh := make(chan struct{})
g.Go(func() error {
if services.deps {
err := s.awaitPostgres(ctx)
if err != nil {
return err
}
err = s.awaitRedis(ctx)
if err != nil {
return err
}
}
close(depsReadyCh)
return nil
})
if services.admin {
g.Go(func() error {
if err := awaitClose(ctx, depsReadyCh); err != nil {
return err
}
return s.runAdmin(ctx, verbose)
})
}
if services.runtime {
g.Go(func() error {
if err := awaitClose(ctx, depsReadyCh); err != nil {
return err
}
return s.runRuntime(ctx, verbose)
})
}
backendReadyCh := make(chan struct{})
g.Go(func() error {
if err := awaitClose(ctx, depsReadyCh); err != nil {
return err
}
if services.admin {
err := s.awaitAdmin(ctx)
if err != nil {
return err
}
}
if services.runtime {
err := s.awaitRuntime(ctx)
if err != nil {
return err
}
}
close(backendReadyCh)
return nil
})
g.Go(func() error {
if !services.admin {
return nil
}
if err := awaitClose(ctx, backendReadyCh); err != nil {
return err
}
// NOTE: Will revert back to previous env on ctx.Done()
switchEnvToDevTemporarily(ctx, ch)
return nil
})
if services.ui {
npmReadyCh := make(chan struct{})
g.Go(func() error {
err := s.runUIInstall(ctx)
if err != nil {
return err
}
close(npmReadyCh)
return nil
})
g.Go(func() error {
if err := awaitClose(ctx, backendReadyCh, npmReadyCh); err != nil {
return err
}
return s.runUI(ctx)
})
}
uiReadyCh := make(chan struct{})
g.Go(func() error {
if services.ui {
err := s.awaitUI(ctx)
if err != nil {
return err
}
}
close(uiReadyCh)
return nil
})
g.Go(func() error {
if err := awaitClose(ctx, backendReadyCh, uiReadyCh); err != nil {
return err
}
logInfo.Printf("All services ready\n")
return nil
})
return g.Wait()
}
func (s cloud) resetState(ctx context.Context) (err error) {
logInfo.Printf("Resetting state\n")
defer func() {
if err == nil {
logInfo.Printf("Reset state\n")
} else {
logErr.Printf("Failed to reset state: %v", err)
}
}()
_ = os.RemoveAll(stateDirCloud)
return newCmd(ctx, "docker", "compose", "-f", "cli/cmd/devtool/data/cloud-deps.docker-compose.yml", "down", "--volumes").Run()
}
func (s cloud) runDeps(ctx context.Context, verbose bool) error {
logInfo.Printf("Starting dependencies\n")
defer logInfo.Printf("Stopped dependencies\n")
cmd := newCmd(ctx, "docker", "compose", "-f", "cli/cmd/devtool/data/cloud-deps.docker-compose.yml", "up", "--no-recreate")
if verbose {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
}
return cmd.Run()
}
func (s cloud) awaitPostgres(ctx context.Context) error {
logInfo.Printf("Waiting for Postgres\n")
dbURL := lookupDotenv("RILL_ADMIN_DATABASE_URL")
for {
conn, err := pgx.Connect(ctx, dbURL)
if err == nil {
conn.Close(ctx)
logInfo.Printf("Postgres ready\n")
return nil
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s cloud) awaitRedis(ctx context.Context) error {
dbURL := lookupDotenv("RILL_ADMIN_REDIS_URL")
if dbURL == "" {
return nil
}
logInfo.Printf("Waiting for Redis\n")
opts, err := redis.ParseURL(dbURL)
if err != nil {
return fmt.Errorf("failed to parse redis url: %w", err)
}
for {
c := redis.NewClient(opts)
res, err := c.Echo(ctx, "hello").Result()
c.Close()
if err == nil && res == "hello" {
logInfo.Printf("Redis ready\n")
return nil
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s cloud) runAdmin(ctx context.Context, verbose bool) (err error) {
logInfo.Printf("Starting admin\n")
defer logInfo.Printf("Stopped admin\n")
cmd := newCmd(ctx, "go", "run", "cli/main.go", "admin", "start")
if verbose {
cmd.Env = append(os.Environ(), "RILL_ADMIN_LOG_LEVEL=debug")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
return cmd.Run()
}
func (s cloud) awaitAdmin(ctx context.Context) error {
pingURL := lookupDotenv("RILL_ADMIN_EXTERNAL_URL")
pingURL, err := url.JoinPath(pingURL, "/v1/ping")
if err != nil {
return fmt.Errorf("failed to parse admin url: %w", err)
}
for {
resp, err := http.Get(pingURL)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
logInfo.Printf("Admin ready\n")
return nil
}
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s cloud) runRuntime(ctx context.Context, verbose bool) (err error) {
logInfo.Printf("Starting runtime\n")
defer logInfo.Printf("Stopped runtime\n")
cmd := newCmd(ctx, "go", "run", "cli/main.go", "runtime", "start")
if verbose {
cmd.Env = append(os.Environ(), "RILL_RUNTIME_LOG_LEVEL=debug")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
return cmd.Run()
}
func (s cloud) awaitRuntime(ctx context.Context) error {
pingURL := lookupDotenv("RILL_RUNTIME_AUTH_AUDIENCE_URL") // TODO: This is a proxy for the runtime's external URL. Should be less implicit.
pingURL, err := url.JoinPath(pingURL, "/v1/ping")
if err != nil {
return fmt.Errorf("failed to parse admin url: %w", err)
}
for {
resp, err := http.Get(pingURL)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
logInfo.Printf("Runtime ready\n")
return nil
}
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s cloud) runUIInstall(ctx context.Context) (err error) {
logInfo.Printf("Running `npm install -w web-admin`\n")
defer func() {
if err == nil {
logInfo.Printf("Finished `npm install -w web-admin`\n")
} else {
logErr.Printf("Failed running `npm install -w web-admin`: %v", err)
}
}()
return newCmd(ctx, "npm", "install", "-w", "web-admin").Run()
}
func (s cloud) runUI(ctx context.Context) (err error) {
logInfo.Printf("Starting UI\n")
defer logInfo.Printf("Stopped UI\n")
cmd := newCmd(ctx, "npm", "run", "dev", "-w", "web-admin")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
return cmd.Run()
}
func (s cloud) awaitUI(ctx context.Context) error {
uiURL := lookupDotenv("RILL_ADMIN_FRONTEND_URL") // TODO: This is a proxy for the frontend's external URL. Should be less implicit.
for {
resp, err := http.Get(uiURL)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
logInfo.Printf("UI ready\n")
return nil
}
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
type local struct{}
func (s local) start(ctx context.Context, verbose, reset bool, services *servicesCfg) error {
g, ctx := errgroup.WithContext(ctx)
if services.runtime {
g.Go(func() error { return s.runRuntime(ctx, verbose, reset) })
}
runtimeReadyCh := make(chan struct{})
g.Go(func() error {
if services.runtime {
err := s.awaitRuntime(ctx)
if err != nil {
return err
}
}
close(runtimeReadyCh)
return nil
})
if services.ui {
npmReadyCh := make(chan struct{})
g.Go(func() error {
err := s.runUIInstall(ctx)
if err != nil {
return err
}
close(npmReadyCh)
return nil
})
g.Go(func() error {
if err := awaitClose(ctx, runtimeReadyCh, npmReadyCh); err != nil {
return err
}
return s.runUI(ctx)
})
}
uiReadyCh := make(chan struct{})
g.Go(func() error {
if services.ui {
err := s.awaitUI(ctx)
if err != nil {
return err
}
}
close(uiReadyCh)
return nil
})
g.Go(func() error {
// Wait for runtime, then UI
if err := awaitClose(ctx, runtimeReadyCh, uiReadyCh); err != nil {
return err
}
logInfo.Printf("All services ready\n")
return nil
})
return g.Wait()
}
func (s local) runRuntime(ctx context.Context, verbose, reset bool) error {
logInfo.Printf("Starting runtime\n")
defer func() { logInfo.Printf("Stopped runtime\n") }()
args := []string{"run", "cli/main.go", "start", stateDirLocal, "--no-ui", "--debug"}
if verbose {
args = append(args, "--verbose")
}
if reset {
args = append(args, "--reset")
}
cmd := newCmd(ctx, "go", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
return cmd.Run()
}
func (s local) awaitRuntime(ctx context.Context) error {
pingURL := "http://localhost:9009/v1/ping"
for {
resp, err := http.Get(pingURL)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
logInfo.Printf("Backend ready\n")
return nil
}
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s local) runUIInstall(ctx context.Context) (err error) {
logInfo.Printf("Running `npm install -w web-local`\n")
defer func() {
if err == nil {
logInfo.Printf("Finished `npm install -w web-local`\n")
} else {
logErr.Printf("Failed running `npm install -w web-local`: %v", err)
}
}()
return newCmd(ctx, "npm", "install", "-w", "web-local").Run()
}
func (s local) runUI(ctx context.Context) (err error) {
logInfo.Printf("Starting UI\n")
defer logInfo.Printf("Stopped UI\n")
cmd := newCmd(ctx, "npm", "run", "dev", "-w", "web-local")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
return cmd.Run()
}
func (s local) awaitUI(ctx context.Context) error {
uiURL := "http://localhost:3000"
for {
resp, err := http.Get(uiURL)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
logInfo.Printf("UI ready\n")
return nil
}
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return ctx.Err()
}
}
}
// awaitClose waits for all of the given channels to close.
// It returns an error if the context is canceled.
func awaitClose(ctx context.Context, chs ...<-chan struct{}) error {
for _, ch := range chs {
select {
case <-ch:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}
// newCmd initializes an exec.Cmd that sends SIGINT instead of SIGKILL when the ctx is canceled.
func newCmd(ctx context.Context, name string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Cancel = func() error {
return cmd.Process.Signal(os.Interrupt)
}
return cmd
}
// lookupDotenv returns a value from the .env file.
// NOTE: Not using godotenv.Load() since the OpenTelemetry-related env vars clash with `docker compose`.
func lookupDotenv(key string) string {
env, err := godotenv.Read()
if err != nil {
return ""
}
return env[key]
}