20601: Rename Scheduler.maxSupervisors -> Scheduler.supervisorFraction
[arvados.git] / cmd / arvados-server / cmd.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "context"
9         "encoding/json"
10         "fmt"
11         "io"
12         "net/http"
13         "os"
14         "path"
15         "path/filepath"
16         "strings"
17
18         "git.arvados.org/arvados.git/lib/boot"
19         "git.arvados.org/arvados.git/lib/cloud/cloudtest"
20         "git.arvados.org/arvados.git/lib/cmd"
21         "git.arvados.org/arvados.git/lib/config"
22         "git.arvados.org/arvados.git/lib/controller"
23         "git.arvados.org/arvados.git/lib/crunchrun"
24         "git.arvados.org/arvados.git/lib/dispatchcloud"
25         "git.arvados.org/arvados.git/lib/install"
26         "git.arvados.org/arvados.git/lib/lsf"
27         "git.arvados.org/arvados.git/lib/recovercollection"
28         "git.arvados.org/arvados.git/lib/service"
29         "git.arvados.org/arvados.git/sdk/go/arvados"
30         "git.arvados.org/arvados.git/sdk/go/health"
31         dispatchslurm "git.arvados.org/arvados.git/services/crunch-dispatch-slurm"
32         "git.arvados.org/arvados.git/services/githttpd"
33         keepbalance "git.arvados.org/arvados.git/services/keep-balance"
34         keepweb "git.arvados.org/arvados.git/services/keep-web"
35         "git.arvados.org/arvados.git/services/keepproxy"
36         "git.arvados.org/arvados.git/services/keepstore"
37         "git.arvados.org/arvados.git/services/ws"
38         "github.com/prometheus/client_golang/prometheus"
39 )
40
41 var (
42         handler = cmd.Multi(map[string]cmd.Handler{
43                 "version":   cmd.Version,
44                 "-version":  cmd.Version,
45                 "--version": cmd.Version,
46
47                 "boot":               boot.Command,
48                 "check":              health.CheckCommand,
49                 "cloudtest":          cloudtest.Command,
50                 "config-check":       config.CheckCommand,
51                 "config-defaults":    config.DumpDefaultsCommand,
52                 "config-dump":        config.DumpCommand,
53                 "controller":         controller.Command,
54                 "crunch-run":         crunchrun.Command,
55                 "dispatch-cloud":     dispatchcloud.Command,
56                 "dispatch-lsf":       lsf.DispatchCommand,
57                 "dispatch-slurm":     dispatchslurm.Command,
58                 "git-httpd":          githttpd.Command,
59                 "health":             healthCommand,
60                 "install":            install.Command,
61                 "init":               install.InitCommand,
62                 "keep-balance":       keepbalance.Command,
63                 "keep-web":           keepweb.Command,
64                 "keepproxy":          keepproxy.Command,
65                 "keepstore":          keepstore.Command,
66                 "recover-collection": recovercollection.Command,
67                 "workbench2":         wb2command{},
68                 "ws":                 ws.Command,
69         })
70 )
71
72 func main() {
73         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
74 }
75
76 type wb2command struct{}
77
78 func (wb2command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
79         if len(args) != 3 {
80                 fmt.Fprintf(stderr, "usage: %s api-host listen-addr app-dir\n", prog)
81                 return 1
82         }
83         configJSON, err := json.Marshal(map[string]string{"API_HOST": args[0]})
84         if err != nil {
85                 fmt.Fprintf(stderr, "json.Marshal: %s\n", err)
86                 return 1
87         }
88         servefs := http.FileServer(http.Dir(args[2]))
89         mux := http.NewServeMux()
90         mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
91                 for _, ent := range strings.Split(req.URL.Path, "/") {
92                         if ent == ".." {
93                                 http.Error(w, "invalid URL path", http.StatusBadRequest)
94                                 return
95                         }
96                 }
97                 fnm := filepath.Join(args[2], filepath.FromSlash(path.Clean("/"+req.URL.Path)))
98                 if _, err := os.Stat(fnm); os.IsNotExist(err) {
99                         req.URL.Path = "/"
100                 }
101                 servefs.ServeHTTP(w, req)
102         }))
103         mux.HandleFunc("/config.json", func(w http.ResponseWriter, _ *http.Request) {
104                 w.Write(configJSON)
105         })
106         mux.HandleFunc("/_health/ping", func(w http.ResponseWriter, _ *http.Request) {
107                 io.WriteString(w, `{"health":"OK"}`)
108         })
109         err = http.ListenAndServe(args[1], mux)
110         if err != nil {
111                 fmt.Fprintln(stderr, err.Error())
112                 return 1
113         }
114         return 0
115 }
116
117 var healthCommand cmd.Handler = service.Command(arvados.ServiceNameHealth, func(ctx context.Context, cluster *arvados.Cluster, _ string, reg *prometheus.Registry) service.Handler {
118         mClockSkew := prometheus.NewGauge(prometheus.GaugeOpts{
119                 Namespace: "arvados",
120                 Subsystem: "health",
121                 Name:      "clock_skew_seconds",
122                 Help:      "Clock skew observed in most recent health check",
123         })
124         reg.MustRegister(mClockSkew)
125         return &health.Aggregator{
126                 Cluster:         cluster,
127                 MetricClockSkew: mClockSkew,
128         }
129 })