0ebf647adffb448aa7943fdf9d9585eb74554e61
[arvados.git] / lib / boot / service.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         "path/filepath"
10
11         "git.arvados.org/arvados.git/sdk/go/arvados"
12 )
13
14 type runServiceCommand struct {
15         name    string
16         svc     arvados.Service
17         depends []bootTask
18 }
19
20 func (runner runServiceCommand) String() string {
21         return runner.name
22 }
23
24 func (runner runServiceCommand) Run(ctx context.Context, fail func(error), boot *Booter) error {
25         boot.wait(ctx, runner.depends...)
26         binfile := filepath.Join(boot.tempdir, "bin", "arvados-server")
27         err := boot.RunProgram(ctx, boot.tempdir, nil, nil, binfile, "-version")
28         if err != nil {
29                 return err
30         }
31         go func() {
32                 var u arvados.URL
33                 for u = range runner.svc.InternalURLs {
34                 }
35                 fail(boot.RunProgram(ctx, boot.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, binfile, runner.name, "-config", boot.configfile))
36         }()
37         return nil
38 }
39
40 type runGoProgram struct {
41         src     string
42         svc     arvados.Service
43         depends []bootTask
44 }
45
46 func (runner runGoProgram) String() string {
47         _, basename := filepath.Split(runner.src)
48         return basename
49 }
50
51 func (runner runGoProgram) Run(ctx context.Context, fail func(error), boot *Booter) error {
52         boot.wait(ctx, runner.depends...)
53         binfile, err := boot.installGoProgram(ctx, runner.src)
54         if err != nil {
55                 return err
56         }
57         if ctx.Err() != nil {
58                 return ctx.Err()
59         }
60
61         err = boot.RunProgram(ctx, boot.tempdir, nil, nil, binfile, "-version")
62         if err != nil {
63                 return err
64         }
65         if len(runner.svc.InternalURLs) > 0 {
66                 // Run one for each URL
67                 for u := range runner.svc.InternalURLs {
68                         u := u
69                         boot.waitShutdown.Add(1)
70                         go func() {
71                                 defer boot.waitShutdown.Done()
72                                 fail(boot.RunProgram(ctx, boot.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, binfile))
73                         }()
74                 }
75         } else {
76                 // Just run one
77                 boot.waitShutdown.Add(1)
78                 go func() {
79                         defer boot.waitShutdown.Done()
80                         fail(boot.RunProgram(ctx, boot.tempdir, nil, nil, binfile))
81                 }()
82         }
83         return nil
84 }