Merge branch '16265-security-updates' into dependabot/bundler/apps/workbench/nokogiri...
[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 fail().
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.ClusterType, "type", "production", "cluster `type`: development, test, or production")
56         flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
57         flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
58         flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
59         err = flags.Parse(args)
60         if err == flag.ErrHelp {
61                 err = nil
62                 return 0
63         } else if err != nil {
64                 return 2
65         } else if *versionFlag {
66                 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
67         } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
68                 err = fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
69                 return 2
70         }
71
72         loader.SkipAPICalls = true
73         cfg, err := loader.Load()
74         if err != nil {
75                 return 1
76         }
77
78         super.Start(ctx, cfg)
79         defer super.Stop()
80         url, ok := super.WaitReady()
81         if !ok {
82                 return 1
83         }
84         // Write controller URL to stdout. Nothing else goes to
85         // stdout, so this provides an easy way for a calling script
86         // to discover the controller URL when everything is ready.
87         fmt.Fprintln(stdout, url)
88         // Wait for signal/crash + orderly shutdown
89         <-super.done
90         return 0
91 }