enable consul webgui on :18500
[arvados.git] / services / boot / arvados_go.go
1 package main
2
3 import (
4         "context"
5         "fmt"
6         "log"
7         "math/rand"
8         "os"
9         "path"
10         "path/filepath"
11 )
12
13 var (
14         dispatchLocal = &arvadosGoBooter{name: "crunch-dispatch-local"}
15         dispatchSLURM = &arvadosGoBooter{name: "crunch-dispatch-slurm"}
16         gitHTTP       = &arvadosGoBooter{name: "arvados-git-httpd", conf: "git-httpd"}
17         keepbalance   = &arvadosGoBooter{name: "keep-balance", tmpl: keepbalanceTmpl}
18         keepproxy     = &arvadosGoBooter{name: "keepproxy"}
19         keepstore     = &arvadosGoBooter{name: "keepstore"}
20         websocket     = &arvadosGoBooter{name: "arvados-ws", conf: "ws"}
21
22         keepbalanceTmpl = `{"RunPeriod":"1m"}`
23 )
24
25 type arvadosGoBooter struct {
26         name string
27         conf string
28         tmpl string
29 }
30
31 func (agb *arvadosGoBooter) Boot(ctx context.Context) error {
32         cfg := cfg(ctx)
33
34         if agb.conf == "" {
35                 agb.conf = agb.name
36         }
37         if agb.tmpl == "" {
38                 agb.tmpl = "{}"
39         }
40
41         cmd := path.Join(cfg.UsrDir, "bin", agb.name)
42         if _, err := os.Stat(cmd); err != nil {
43                 if found, err := filepath.Glob(path.Join(cfg.UsrDir, "pkg", agb.name+"_*.deb")); err == nil && len(found) > 0 {
44                         cmd := command("dpkg", "-i", found[0])
45                         osPackageMutex.Lock()
46                         err = cmd.Run()
47                         osPackageMutex.Unlock()
48                         if err != nil {
49                                 log.Print(err)
50                         }
51                 }
52         }
53         cfgPath := path.Join("/etc/arvados", agb.conf, agb.conf+".yml")
54         if err := os.MkdirAll(path.Dir(cfgPath), 0755); err != nil {
55                 return err
56         }
57         // ctmpl := []byte(fmt.Sprintf(`{{tree %q | explode | toJSONPretty}}`, agb.name))
58         ctmpl := []byte(`{}`)
59         if err := atomicWriteFile(cfgPath+".ctmpl", ctmpl, 0644); err != nil {
60                 return err
61         }
62         return Series{
63                 &osPackage{
64                         Debian: agb.name,
65                 },
66                 &supervisedService{
67                         name: agb.name,
68                         cmd:  path.Join(cfg.UsrDir, "bin", "consul-template"),
69                         args: []string{
70                                 "-consul-addr=" + fmt.Sprintf("0.0.0.0:%d", cfg.Ports.ConsulHTTP),
71                                 "-template=" + cfgPath + ".ctmpl:" + cfgPath,
72                                 "-exec",
73                                 agb.name,
74                         },
75                 },
76         }.Boot(ctx)
77 }