6a32ab142de79115c136e3e674936041af390bc5
[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/sdk/go/ctxlog"
17 )
18
19 var Command cmd.Handler = bootCommand{}
20
21 type supervisedTask interface {
22         // Execute the task. Run should return nil when the task is
23         // done enough to satisfy a dependency relationship (e.g., the
24         // service is running and ready). If the task starts a
25         // goroutine that fails after Run returns (e.g., the service
26         // shuts down), it should call fail().
27         Run(ctx context.Context, fail func(error), super *Supervisor) error
28         String() string
29 }
30
31 var errNeedConfigReload = errors.New("config changed, restart needed")
32 var errParseFlags = errors.New("error parsing command line arguments")
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 == errParseFlags {
44                         return 2
45                 } else if err != nil {
46                         logger.WithError(err).Info("exiting")
47                         return 1
48                 } else {
49                         return 0
50                 }
51         }
52 }
53
54 func (bcmd bootCommand) run(ctx context.Context, prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
55         ctx, cancel := context.WithCancel(ctx)
56         defer cancel()
57         super := &Supervisor{
58                 Stdin:  stdin,
59                 Stderr: stderr,
60                 logger: ctxlog.FromContext(ctx),
61         }
62
63         flags := flag.NewFlagSet(prog, flag.ContinueOnError)
64         versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
65         flags.StringVar(&super.ConfigPath, "config", "/etc/arvados/config.yml", "arvados config file `path`")
66         flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
67         flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
68         flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for external services, and internal services whose InternalURLs are not configured")
69         flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
70         flags.StringVar(&super.Workbench2Source, "workbench2-source", "../arvados-workbench2", "path to arvados-workbench2 source tree")
71         flags.BoolVar(&super.NoWorkbench1, "no-workbench1", false, "do not run workbench1")
72         flags.BoolVar(&super.NoWorkbench2, "no-workbench2", true, "do not run workbench2")
73         flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
74         timeout := flags.Duration("timeout", 0, "maximum time to wait for cluster to be ready")
75         shutdown := flags.Bool("shutdown", false, "shut down when the cluster becomes ready")
76         if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
77                 if code == 0 {
78                         return nil
79                 } else {
80                         return errParseFlags
81                 }
82         } else if *versionFlag {
83                 cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
84                 return nil
85         } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
86                 return fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
87         }
88
89         super.Start(ctx)
90         defer super.Stop()
91
92         var timer *time.Timer
93         if *timeout > 0 {
94                 timer = time.AfterFunc(*timeout, super.Stop)
95         }
96
97         ok := super.WaitReady()
98         if timer != nil && !timer.Stop() {
99                 return errors.New("boot timed out")
100         } else if !ok {
101                 super.logger.Error("boot failed")
102         } else {
103                 // Write each cluster's controller URL to stdout.
104                 // Nothing else goes to stdout, so this allows a
105                 // calling script to determine when the cluster is
106                 // ready to use, and the controller's host:port (which
107                 // may have been dynamically assigned depending on
108                 // config/options).
109                 for _, cc := range super.Clusters() {
110                         fmt.Fprintln(stdout, cc.Services.Controller.ExternalURL)
111                 }
112                 if *shutdown {
113                         super.Stop()
114                         // Wait for children to exit. Don't report the
115                         // ensuing "context cancelled" error, though:
116                         // return nil to indicate successful startup.
117                         _ = super.Wait()
118                         fmt.Fprintln(stderr, "PASS - all services booted successfully")
119                         return nil
120                 }
121         }
122         // Wait for signal/crash + orderly shutdown
123         return super.Wait()
124 }