18699: boot.Supervisor support for multi-cluster config file.
[arvados.git] / lib / boot / cmd.go
index f2266d6b581a621f60e414d444875fd118a96f90..6a32ab142de79115c136e3e674936041af390bc5 100644 (file)
@@ -6,12 +6,13 @@ package boot
 
 import (
        "context"
+       "errors"
        "flag"
        "fmt"
        "io"
+       "time"
 
        "git.arvados.org/arvados.git/lib/cmd"
-       "git.arvados.org/arvados.git/lib/config"
        "git.arvados.org/arvados.git/sdk/go/ctxlog"
 )
 
@@ -22,68 +23,102 @@ type supervisedTask interface {
        // done enough to satisfy a dependency relationship (e.g., the
        // service is running and ready). If the task starts a
        // goroutine that fails after Run returns (e.g., the service
-       // shuts down), it should call cancel.
+       // shuts down), it should call fail().
        Run(ctx context.Context, fail func(error), super *Supervisor) error
        String() string
 }
 
+var errNeedConfigReload = errors.New("config changed, restart needed")
+var errParseFlags = errors.New("error parsing command line arguments")
+
 type bootCommand struct{}
 
-func (bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
-       super := &Supervisor{
-               Stderr: stderr,
-               logger: ctxlog.New(stderr, "json", "info"),
+func (bcmd bootCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
+       logger := ctxlog.New(stderr, "json", "info")
+       ctx := ctxlog.Context(context.Background(), logger)
+       for {
+               err := bcmd.run(ctx, prog, args, stdin, stdout, stderr)
+               if err == errNeedConfigReload {
+                       continue
+               } else if err == errParseFlags {
+                       return 2
+               } else if err != nil {
+                       logger.WithError(err).Info("exiting")
+                       return 1
+               } else {
+                       return 0
+               }
        }
+}
 
-       ctx := ctxlog.Context(context.Background(), super.logger)
+func (bcmd bootCommand) run(ctx context.Context, prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
-
-       var err error
-       defer func() {
-               if err != nil {
-                       super.logger.WithError(err).Info("exiting")
-               }
-       }()
+       super := &Supervisor{
+               Stdin:  stdin,
+               Stderr: stderr,
+               logger: ctxlog.FromContext(ctx),
+       }
 
        flags := flag.NewFlagSet(prog, flag.ContinueOnError)
-       flags.SetOutput(stderr)
-       loader := config.NewLoader(stdin, super.logger)
-       loader.SetupFlags(flags)
        versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
+       flags.StringVar(&super.ConfigPath, "config", "/etc/arvados/config.yml", "arvados config file `path`")
        flags.StringVar(&super.SourcePath, "source", ".", "arvados source tree `directory`")
-       flags.StringVar(&super.LibPath, "lib", "/var/lib/arvados", "`directory` to install dependencies and library files")
        flags.StringVar(&super.ClusterType, "type", "production", "cluster `type`: development, test, or production")
-       flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for service listeners")
+       flags.StringVar(&super.ListenHost, "listen-host", "localhost", "host name or interface address for external services, and internal services whose InternalURLs are not configured")
        flags.StringVar(&super.ControllerAddr, "controller-address", ":0", "desired controller address, `host:port` or `:port`")
+       flags.StringVar(&super.Workbench2Source, "workbench2-source", "../arvados-workbench2", "path to arvados-workbench2 source tree")
+       flags.BoolVar(&super.NoWorkbench1, "no-workbench1", false, "do not run workbench1")
+       flags.BoolVar(&super.NoWorkbench2, "no-workbench2", true, "do not run workbench2")
        flags.BoolVar(&super.OwnTemporaryDatabase, "own-temporary-database", false, "bring up a postgres server and create a temporary database")
-       err = flags.Parse(args)
-       if err == flag.ErrHelp {
-               err = nil
-               return 0
-       } else if err != nil {
-               return 2
+       timeout := flags.Duration("timeout", 0, "maximum time to wait for cluster to be ready")
+       shutdown := flags.Bool("shutdown", false, "shut down when the cluster becomes ready")
+       if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
+               if code == 0 {
+                       return nil
+               } else {
+                       return errParseFlags
+               }
        } else if *versionFlag {
-               return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
+               cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
+               return nil
        } else if super.ClusterType != "development" && super.ClusterType != "test" && super.ClusterType != "production" {
-               err = fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
-               return 2
+               return fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
        }
 
-       loader.SkipAPICalls = true
-       cfg, err := loader.Load()
-       if err != nil {
-               return 1
+       super.Start(ctx)
+       defer super.Stop()
+
+       var timer *time.Timer
+       if *timeout > 0 {
+               timer = time.AfterFunc(*timeout, super.Stop)
        }
 
-       super.Start(ctx, cfg)
-       defer super.Stop()
-       if url, ok := super.WaitReady(); ok {
-               fmt.Fprintln(stdout, url)
-               // Wait for signal/crash + orderly shutdown
-               <-super.done
-               return 0
+       ok := super.WaitReady()
+       if timer != nil && !timer.Stop() {
+               return errors.New("boot timed out")
+       } else if !ok {
+               super.logger.Error("boot failed")
        } else {
-               return 1
+               // Write each cluster's controller URL to stdout.
+               // Nothing else goes to stdout, so this allows a
+               // calling script to determine when the cluster is
+               // ready to use, and the controller's host:port (which
+               // may have been dynamically assigned depending on
+               // config/options).
+               for _, cc := range super.Clusters() {
+                       fmt.Fprintln(stdout, cc.Services.Controller.ExternalURL)
+               }
+               if *shutdown {
+                       super.Stop()
+                       // Wait for children to exit. Don't report the
+                       // ensuing "context cancelled" error, though:
+                       // return nil to indicate successful startup.
+                       _ = super.Wait()
+                       fmt.Fprintln(stderr, "PASS - all services booted successfully")
+                       return nil
+               }
        }
+       // Wait for signal/crash + orderly shutdown
+       return super.Wait()
 }