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 var errNeedConfigReload = errors.New("config changed, restart needed")
34 type bootCommand struct{}
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)
40 err := bcmd.run(ctx, prog, args, stdin, stdout, stderr)
41 if err == errNeedConfigReload {
43 } else if err != nil {
44 logger.WithError(err).Info("exiting")
52 func (bcmd bootCommand) run(ctx context.Context, prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
53 ctx, cancel := context.WithCancel(ctx)
57 logger: ctxlog.FromContext(ctx),
60 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
61 flags.SetOutput(stderr)
62 loader := config.NewLoader(stdin, super.logger)
63 loader.SetupFlags(flags)
64 versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
65 flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
66 flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
67 flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
68 flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
69 flags.BoolVar(&super.NoWorkbench1, "no-workbench1", false, "do not run workbench1")
70 flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
71 timeout := flags.Duration("timeout", 0, "maximum time to wait for cluster to be ready")
72 shutdown := flags.Bool("shutdown", false, "shut down when the cluster becomes ready")
73 err := flags.Parse(args)
74 if err == flag.ErrHelp {
76 } else if err != nil {
78 } else if *versionFlag {
79 cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
81 } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
82 return fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
85 loader.SkipAPICalls = true
86 cfg, err := loader.Load()
91 super.Start(ctx, cfg, loader.Path)
96 timer = time.AfterFunc(*timeout, super.Stop)
99 url, ok := super.WaitReady()
100 if timer != nil && !timer.Stop() {
101 return errors.New("boot timed out")
103 super.logger.Error("boot failed")
105 // Write controller URL to stdout. Nothing else goes
106 // to stdout, so this provides an easy way for a
107 // calling script to discover the controller URL when
108 // everything is ready.
109 fmt.Fprintln(stdout, url)
112 // Wait for children to exit. Don't report the
113 // ensuing "context cancelled" error, though:
114 // return nil to indicate successful startup.
116 fmt.Fprintln(stderr, "PASS - all services booted successfully")
120 // Wait for signal/crash + orderly shutdown