Merge branch '17343-workbench1-optional'
[arvados.git] / lib / boot / cmd.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package boot
6
7 import (
8         "context"
9         "errors"
10         "flag"
11         "fmt"
12         "io"
13         "time"
14
15         "git.arvados.org/arvados.git/lib/cmd"
16         "git.arvados.org/arvados.git/lib/config"
17         "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 )
19
20 var Command cmd.Handler = bootCommand{}
21
22 type supervisedTask interface {
23         // Execute the task. Run should return nil when the task is
24         // done enough to satisfy a dependency relationship (e.g., the
25         // service is running and ready). If the task starts a
26         // goroutine that fails after Run returns (e.g., the service
27         // shuts down), it should call fail().
28         Run(ctx context.Context, fail func(error), super *Supervisor) error
29         String() string
30 }
31
32 var errNeedConfigReload = errors.New("config changed, restart needed")
33
34 type bootCommand struct{}
35
36 func (bcmd bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
37         logger := ctxlog.New(stderr, "json", "info")
38         ctx := ctxlog.Context(context.Background(), logger)
39         for {
40                 err := bcmd.run(ctx, prog, args, stdin, stdout, stderr)
41                 if err == errNeedConfigReload {
42                         continue
43                 } else if err != nil {
44                         logger.WithError(err).Info("exiting")
45                         return 1
46                 } else {
47                         return 0
48                 }
49         }
50 }
51
52 func (bcmd bootCommand) run(ctx context.Context, prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
53         ctx, cancel := context.WithCancel(ctx)
54         defer cancel()
55         super := &Supervisor{
56                 Stderr: stderr,
57                 logger: ctxlog.FromContext(ctx),
58         }
59
60         flags := flag.NewFlagSet(prog, flag.ContinueOnError)
61         flags.SetOutput(stderr)
62         loader := config.NewLoader(stdin, super.logger)
63         loader.SetupFlags(flags)
64         versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
65         flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
66         flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
67         flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
68         flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
69         flags.BoolVar(&super.NoWorkbench1, "no-workbench1", false, "do not run workbench1")
70         flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
71         timeout := flags.Duration("timeout", 0, "maximum time to wait for cluster to be ready")
72         shutdown := flags.Bool("shutdown", false, "shut down when the cluster becomes ready")
73         err := flags.Parse(args)
74         if err == flag.ErrHelp {
75                 return nil
76         } else if err != nil {
77                 return err
78         } else if *versionFlag {
79                 cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
80                 return nil
81         } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
82                 return fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
83         }
84
85         loader.SkipAPICalls = true
86         cfg, err := loader.Load()
87         if err != nil {
88                 return err
89         }
90
91         super.Start(ctx, cfg, loader.Path)
92         defer super.Stop()
93
94         var timer *time.Timer
95         if *timeout > 0 {
96                 timer = time.AfterFunc(*timeout, super.Stop)
97         }
98
99         url, ok := super.WaitReady()
100         if timer != nil && !timer.Stop() {
101                 return errors.New("boot timed out")
102         } else if !ok {
103                 super.logger.Error("boot failed")
104         } else {
105                 // Write controller URL to stdout. Nothing else goes
106                 // to stdout, so this provides an easy way for a
107                 // calling script to discover the controller URL when
108                 // everything is ready.
109                 fmt.Fprintln(stdout, url)
110                 if *shutdown {
111                         super.Stop()
112                         // Wait for children to exit. Don't report the
113                         // ensuing "context cancelled" error, though:
114                         // return nil to indicate successful startup.
115                         _ = super.Wait()
116                         fmt.Fprintln(stderr, "PASS - all services booted successfully")
117                         return nil
118                 }
119         }
120         // Wait for signal/crash + orderly shutdown
121         return super.Wait()
122 }