8a4a10d15b440c55de876a7702b0ceab76633ace
[arvados.git] / services / boot / arvados_go.go
1 package main
2
3 import (
4         "context"
5         "log"
6         "math/rand"
7         "os"
8         "path"
9         "path/filepath"
10 )
11
12 var (
13         dispatchLocal = &arvadosGoBooter{name: "crunch-dispatch-local"}
14         dispatchSLURM = &arvadosGoBooter{name: "crunch-dispatch-slurm"}
15         gitHTTP       = &arvadosGoBooter{name: "arvados-git-httpd"}
16         keepbalance   = &arvadosGoBooter{name: "keep-balance"}
17         keepproxy     = &arvadosGoBooter{name: "keepproxy"}
18         keepstore     = &arvadosGoBooter{name: "keepstore"}
19         websocket     = &arvadosGoBooter{name: "arvados-ws"}
20 )
21
22 type arvadosGoBooter struct {
23         name string
24 }
25
26 func availablePort() int {
27         return rand.Intn(10000) + 20000
28 }
29
30 func (agb *arvadosGoBooter) Boot(ctx context.Context) error {
31         cfg := cfg(ctx)
32         cmd := path.Join(cfg.UsrDir, "bin", agb.name)
33         if _, err := os.Stat(cmd); err != nil {
34                 if found, err := filepath.Glob(path.Join(cfg.UsrDir, "pkg", agb.name+"_*.deb")); err == nil && len(found) > 0 {
35                         cmd := command("dpkg", "-i", found[0])
36                         osPackageMutex.Lock()
37                         err = cmd.Run()
38                         osPackageMutex.Unlock()
39                         if err != nil {
40                                 log.Print(err)
41                         }
42                 }
43         }
44         cfgPath := path.Join("/etc/arvados", agb.name, agb.name+".yml")
45         atomicWriteFile(cfgPath+".ctmpl", []byte("{}"), 0644)
46         return Series{
47                 &osPackage{
48                         Debian: agb.name,
49                 },
50                 &supervisedService{
51                         name: agb.name,
52                         cmd:  path.Join(cfg.UsrDir, "bin", "consul-template"),
53                         args: []string{
54                                 "-consul-addr=127.0.0.1:8500",
55                                 "-template="+cfgPath+".ctmpl:"+cfgPath,
56                                 "-exec",
57                                 agb.name,
58                         },
59                 },
60         }.Boot(ctx)
61 }