Merge branch '16053-install-deps'
[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 type bootCommand struct{}
33
34 func (bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
35         super := &Supervisor{
36                 Stderr: stderr,
37                 logger: ctxlog.New(stderr, "json", "info"),
38         }
39
40         ctx := ctxlog.Context(context.Background(), super.logger)
41         ctx, cancel := context.WithCancel(ctx)
42         defer cancel()
43
44         var err error
45         defer func() {
46                 if err != nil {
47                         super.logger.WithError(err).Info("exiting")
48                 }
49         }()
50
51         flags := flag.NewFlagSet(prog, flag.ContinueOnError)
52         flags.SetOutput(stderr)
53         loader := config.NewLoader(stdin, super.logger)
54         loader.SetupFlags(flags)
55         versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
56         flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
57         flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
58         flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
59         flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
60         flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
61         timeout := flags.Duration("timeout", 0, "maximum time to wait for cluster to be ready")
62         shutdown := flags.Bool("shutdown", false, "shut down when the cluster becomes ready")
63         err = flags.Parse(args)
64         if err == flag.ErrHelp {
65                 err = nil
66                 return 0
67         } else if err != nil {
68                 return 2
69         } else if *versionFlag {
70                 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
71         } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
72                 err = fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
73                 return 2
74         }
75
76         loader.SkipAPICalls = true
77         cfg, err := loader.Load()
78         if err != nil {
79                 return 1
80         }
81
82         super.Start(ctx, cfg)
83         defer super.Stop()
84
85         var timer *time.Timer
86         if *timeout > 0 {
87                 timer = time.AfterFunc(*timeout, super.Stop)
88         }
89
90         url, ok := super.WaitReady()
91         if timer != nil && !timer.Stop() {
92                 err = errors.New("boot timed out")
93                 return 1
94         } else if !ok {
95                 err = errors.New("boot failed")
96                 return 1
97         }
98         // Write controller URL to stdout. Nothing else goes to
99         // stdout, so this provides an easy way for a calling script
100         // to discover the controller URL when everything is ready.
101         fmt.Fprintln(stdout, url)
102         if *shutdown {
103                 super.Stop()
104         }
105         // Wait for signal/crash + orderly shutdown
106         <-super.done
107         return 0
108 }