1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
20 var Command cmd.Handler = bootCommand{}
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
32 type bootCommand struct{}
34 func (bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
37 logger: ctxlog.New(stderr, "json", "info"),
40 ctx := ctxlog.Context(context.Background(), super.logger)
41 ctx, cancel := context.WithCancel(ctx)
47 super.logger.WithError(err).Info("exiting")
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 {
67 } else if err != nil {
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'")
76 loader.SkipAPICalls = true
77 cfg, err := loader.Load()
87 timer = time.AfterFunc(*timeout, super.Stop)
90 url, ok := super.WaitReady()
91 if timer != nil && !timer.Stop() {
92 err = errors.New("boot timed out")
95 err = errors.New("boot failed")
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)
105 // Wait for signal/crash + orderly shutdown