18870: Deploy individual nodes
[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
15         "git.arvados.org/arvados.git/lib/boot"
16         "git.arvados.org/arvados.git/lib/cloud/cloudtest"
17         "git.arvados.org/arvados.git/lib/cmd"
18         "git.arvados.org/arvados.git/lib/config"
19         "git.arvados.org/arvados.git/lib/controller"
20         "git.arvados.org/arvados.git/lib/crunchrun"
21         "git.arvados.org/arvados.git/lib/dispatchcloud"
22         "git.arvados.org/arvados.git/lib/install"
23         "git.arvados.org/arvados.git/lib/lsf"
24         "git.arvados.org/arvados.git/lib/recovercollection"
25         "git.arvados.org/arvados.git/lib/service"
26         "git.arvados.org/arvados.git/sdk/go/arvados"
27         "git.arvados.org/arvados.git/sdk/go/health"
28         "git.arvados.org/arvados.git/services/githttpd"
29         keepbalance "git.arvados.org/arvados.git/services/keep-balance"
30         keepweb "git.arvados.org/arvados.git/services/keep-web"
31         "git.arvados.org/arvados.git/services/keepproxy"
32         "git.arvados.org/arvados.git/services/keepstore"
33         "git.arvados.org/arvados.git/services/ws"
34         "github.com/prometheus/client_golang/prometheus"
35 )
36
37 var (
38         handler = cmd.Multi(map[string]cmd.Handler{
39                 "version":   cmd.Version,
40                 "-version":  cmd.Version,
41                 "--version": cmd.Version,
42
43                 "boot":               boot.Command,
44                 "check":              health.CheckCommand,
45                 "cloudtest":          cloudtest.Command,
46                 "config-check":       config.CheckCommand,
47                 "config-defaults":    config.DumpDefaultsCommand,
48                 "config-dump":        config.DumpCommand,
49                 "controller":         controller.Command,
50                 "crunch-run":         crunchrun.Command,
51                 "dispatch-cloud":     dispatchcloud.Command,
52                 "dispatch-lsf":       lsf.DispatchCommand,
53                 "git-httpd":          githttpd.Command,
54                 "health":             healthCommand,
55                 "install":            install.Command,
56                 "init":               install.InitCommand,
57                 "keep-balance":       keepbalance.Command,
58                 "keep-web":           keepweb.Command,
59                 "keepproxy":          keepproxy.Command,
60                 "keepstore":          keepstore.Command,
61                 "recover-collection": recovercollection.Command,
62                 "workbench2":         wb2command{},
63                 "ws":                 ws.Command,
64         })
65 )
66
67 func main() {
68         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
69 }
70
71 type wb2command struct{}
72
73 func (wb2command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
74         if len(args) != 3 {
75                 fmt.Fprintf(stderr, "usage: %s api-host listen-addr app-dir\n", prog)
76                 return 1
77         }
78         configJSON, err := json.Marshal(map[string]string{"API_HOST": args[0]})
79         if err != nil {
80                 fmt.Fprintf(stderr, "json.Marshal: %s\n", err)
81                 return 1
82         }
83         mux := http.NewServeMux()
84         mux.Handle("/", http.FileServer(http.Dir(args[2])))
85         mux.HandleFunc("/config.json", func(w http.ResponseWriter, _ *http.Request) {
86                 w.Write(configJSON)
87         })
88         mux.HandleFunc("/_health/ping", func(w http.ResponseWriter, _ *http.Request) {
89                 io.WriteString(w, `{"health":"OK"}`)
90         })
91         err = http.ListenAndServe(args[1], mux)
92         if err != nil {
93                 fmt.Fprintln(stderr, err.Error())
94                 return 1
95         }
96         return 0
97 }
98
99 var healthCommand cmd.Handler = service.Command(arvados.ServiceNameHealth, func(ctx context.Context, cluster *arvados.Cluster, _ string, reg *prometheus.Registry) service.Handler {
100         mClockSkew := prometheus.NewGauge(prometheus.GaugeOpts{
101                 Namespace: "arvados",
102                 Subsystem: "health",
103                 Name:      "clock_skew_seconds",
104                 Help:      "Clock skew observed in most recent health check",
105         })
106         reg.MustRegister(mClockSkew)
107         return &health.Aggregator{
108                 Cluster:         cluster,
109                 MetricClockSkew: mClockSkew,
110         }
111 })