15954: Rename booter to supervisor, tidy up.
[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         "flag"
10         "fmt"
11         "io"
12
13         "git.arvados.org/arvados.git/lib/cmd"
14         "git.arvados.org/arvados.git/lib/config"
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 )
17
18 var Command cmd.Handler = bootCommand{}
19
20 type supervisedTask interface {
21         // Execute the task. Run should return nil when the task is
22         // done enough to satisfy a dependency relationship (e.g., the
23         // service is running and ready). If the task starts a
24         // goroutine that fails after Run returns (e.g., the service
25         // shuts down), it should call cancel.
26         Run(ctx context.Context, fail func(error), super *Supervisor) error
27         String() string
28 }
29
30 type bootCommand struct{}
31
32 func (bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
33         super := &Supervisor{
34                 Stderr: stderr,
35                 logger: ctxlog.New(stderr, "json", "info"),
36         }
37
38         ctx := ctxlog.Context(context.Background(), super.logger)
39         ctx, cancel := context.WithCancel(ctx)
40         defer cancel()
41
42         var err error
43         defer func() {
44                 if err != nil {
45                         super.logger.WithError(err).Info("exiting")
46                 }
47         }()
48
49         flags := flag.NewFlagSet(prog, flag.ContinueOnError)
50         flags.SetOutput(stderr)
51         loader := config.NewLoader(stdin, super.logger)
52         loader.SetupFlags(flags)
53         versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
54         flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
55         flags.StringVar(&super.LibPath, "lib", "/var/lib/arvados", "`directory` to install dependencies and library files")
56         flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
57         flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
58         flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
59         flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
60         err = flags.Parse(args)
61         if err == flag.ErrHelp {
62                 err = nil
63                 return 0
64         } else if err != nil {
65                 return 2
66         } else if *versionFlag {
67                 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
68         } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
69                 err = fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
70                 return 2
71         }
72
73         loader.SkipAPICalls = true
74         cfg, err := loader.Load()
75         if err != nil {
76                 return 1
77         }
78
79         super.Start(ctx, cfg)
80         defer super.Stop()
81         if url, ok := super.WaitReady(); ok {
82                 fmt.Fprintln(stdout, url)
83                 // Wait for signal/crash + orderly shutdown
84                 <-super.done
85                 return 0
86         } else {
87                 return 1
88         }
89 }