9c3047a7b6c1f80b911e6c19042d62cc967a80f1
[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 var errParseFlags = errors.New("error parsing command line arguments")
34
35 type bootCommand struct{}
36
37 func (bcmd bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
38         logger := ctxlog.New(stderr, "json", "info")
39         ctx := ctxlog.Context(context.Background(), logger)
40         for {
41                 err := bcmd.run(ctx, prog, args, stdin, stdout, stderr)
42                 if err == errNeedConfigReload {
43                         continue
44                 } else if err == errParseFlags {
45                         return 2
46                 } else if err != nil {
47                         logger.WithError(err).Info("exiting")
48                         return 1
49                 } else {
50                         return 0
51                 }
52         }
53 }
54
55 func (bcmd bootCommand) run(ctx context.Context, prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
56         ctx, cancel := context.WithCancel(ctx)
57         defer cancel()
58         super := &Supervisor{
59                 Stderr: stderr,
60                 logger: ctxlog.FromContext(ctx),
61         }
62
63         flags := flag.NewFlagSet(prog, flag.ContinueOnError)
64         loader := config.NewLoader(stdin, super.logger)
65         loader.SetupFlags(flags)
66         versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
67         flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
68         flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
69         flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
70         flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
71         flags.StringVar(&super.Workbench2Source, "workbench2-source", "../arvados-workbench2", "path to arvados-workbench2 source tree")
72         flags.BoolVar(&super.NoWorkbench1, "no-workbench1", false, "do not run workbench1")
73         flags.BoolVar(&super.NoWorkbench2, "no-workbench2", true, "do not run workbench2")
74         flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
75         timeout := flags.Duration("timeout", 0, "maximum time to wait for cluster to be ready")
76         shutdown := flags.Bool("shutdown", false, "shut down when the cluster becomes ready")
77         if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
78                 if code == 0 {
79                         return nil
80                 } else {
81                         return errParseFlags
82                 }
83         } else if *versionFlag {
84                 cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
85                 return nil
86         } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
87                 return fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
88         }
89
90         loader.SkipAPICalls = true
91         cfg, err := loader.Load()
92         if err != nil {
93                 return err
94         }
95
96         super.Start(ctx, cfg, loader.Path)
97         defer super.Stop()
98
99         var timer *time.Timer
100         if *timeout > 0 {
101                 timer = time.AfterFunc(*timeout, super.Stop)
102         }
103
104         url, ok := super.WaitReady()
105         if timer != nil && !timer.Stop() {
106                 return errors.New("boot timed out")
107         } else if !ok {
108                 super.logger.Error("boot failed")
109         } else {
110                 // Write controller URL to stdout. Nothing else goes
111                 // to stdout, so this provides an easy way for a
112                 // calling script to discover the controller URL when
113                 // everything is ready.
114                 fmt.Fprintln(stdout, url)
115                 if *shutdown {
116                         super.Stop()
117                         // Wait for children to exit. Don't report the
118                         // ensuing "context cancelled" error, though:
119                         // return nil to indicate successful startup.
120                         _ = super.Wait()
121                         fmt.Fprintln(stderr, "PASS - all services booted successfully")
122                         return nil
123                 }
124         }
125         // Wait for signal/crash + orderly shutdown
126         return super.Wait()
127 }