X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7a24a37aa9e5ed425550403b68c270316a24d772..571e3176596b852c2f1385487e459126c3d3fd04:/lib/boot/service.go diff --git a/lib/boot/service.go b/lib/boot/service.go index 9672dccc48..090e852446 100644 --- a/lib/boot/service.go +++ b/lib/boot/service.go @@ -5,41 +5,58 @@ package boot import ( - "bytes" "context" - "fmt" + "errors" "path/filepath" - "git.arvados.org/arvados.git/lib/cmd" "git.arvados.org/arvados.git/sdk/go/arvados" ) +// Run a service using the arvados-server binary. +// +// In future this will bring up the service in the current process, +// but for now (at least until the subcommand handlers get a shutdown +// mechanism) it starts a child process using the arvados-server +// binary, which the supervisor is assumed to have installed in +// {super.tempdir}/bin/. type runServiceCommand struct { - name string - command cmd.Handler - depends []bootTask + name string // arvados-server subcommand, e.g., "controller" + svc arvados.Service // cluster.Services.* entry with the desired InternalURLs + depends []supervisedTask // wait for these tasks before starting } func (runner runServiceCommand) String() string { return runner.name } -func (runner runServiceCommand) Run(ctx context.Context, fail func(error), boot *Booter) error { - boot.wait(ctx, runner.depends...) - go func() { - // runner.command.RunCommand() doesn't have access to - // ctx, so it can't shut down by itself when the - // caller cancels. We just abandon it. - exitcode := runner.command.RunCommand(runner.name, []string{"-config", boot.configfile}, bytes.NewBuffer(nil), boot.Stderr, boot.Stderr) - fail(fmt.Errorf("exit code %d", exitcode)) - }() +func (runner runServiceCommand) Run(ctx context.Context, fail func(error), super *Supervisor) error { + binfile := filepath.Join(super.bindir, "arvados-server") + err := super.RunProgram(ctx, super.bindir, runOptions{}, binfile, "-version") + if err != nil { + return err + } + super.wait(ctx, runner.depends...) + for u := range runner.svc.InternalURLs { + u := u + if islocal, err := addrIsLocal(u.Host); err != nil { + return err + } else if !islocal { + continue + } + super.waitShutdown.Add(1) + go func() { + defer super.waitShutdown.Done() + fail(super.RunProgram(ctx, super.tempdir, runOptions{env: []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}}, binfile, runner.name, "-config", super.configfile)) + }() + } return nil } +// Run a Go service that isn't bundled in arvados-server. type runGoProgram struct { - src string - svc arvados.Service - depends []bootTask + src string // source dir, e.g., "services/keepproxy" + svc arvados.Service // cluster.Services.* entry with the desired InternalURLs + depends []supervisedTask // wait for these tasks before starting } func (runner runGoProgram) String() string { @@ -47,25 +64,36 @@ func (runner runGoProgram) String() string { return basename } -func (runner runGoProgram) Run(ctx context.Context, fail func(error), boot *Booter) error { - boot.wait(ctx, runner.depends...) - boot.RunProgram(ctx, runner.src, nil, nil, "go", "install") +func (runner runGoProgram) Run(ctx context.Context, fail func(error), super *Supervisor) error { + if len(runner.svc.InternalURLs) == 0 { + return errors.New("bug: runGoProgram needs non-empty svc.InternalURLs") + } + + binfile, err := super.installGoProgram(ctx, runner.src) + if err != nil { + return err + } if ctx.Err() != nil { return ctx.Err() } - _, basename := filepath.Split(runner.src) - if len(runner.svc.InternalURLs) > 0 { - // Run one for each URL - for u := range runner.svc.InternalURLs { - u := u - go func() { - fail(boot.RunProgram(ctx, boot.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, basename)) - }() + + err = super.RunProgram(ctx, super.tempdir, runOptions{}, binfile, "-version") + if err != nil { + return err + } + + super.wait(ctx, runner.depends...) + for u := range runner.svc.InternalURLs { + u := u + if islocal, err := addrIsLocal(u.Host); err != nil { + return err + } else if !islocal { + continue } - } else { - // Just run one + super.waitShutdown.Add(1) go func() { - fail(boot.RunProgram(ctx, boot.tempdir, nil, nil, basename)) + defer super.waitShutdown.Done() + fail(super.RunProgram(ctx, super.tempdir, runOptions{env: []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}}, binfile)) }() } return nil