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