13497: Rename SystemNodes to NodeProfiles in config.
[arvados.git] / sdk / go / arvados / config.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "fmt"
9         "os"
10
11         "git.curoverse.com/arvados.git/sdk/go/config"
12 )
13
14 const DefaultConfigFile = "/etc/arvados/config.yml"
15
16 type Config struct {
17         Clusters map[string]Cluster
18 }
19
20 // GetConfig returns the current system config, loading it from
21 // configFile if needed.
22 func GetConfig(configFile string) (*Config, error) {
23         var cfg Config
24         err := config.LoadFile(&cfg, configFile)
25         return &cfg, err
26 }
27
28 // GetCluster returns the cluster ID and config for the given
29 // cluster, or the default/only configured cluster if clusterID is "".
30 func (sc *Config) GetCluster(clusterID string) (*Cluster, error) {
31         if clusterID == "" {
32                 if len(sc.Clusters) == 0 {
33                         return nil, fmt.Errorf("no clusters configured")
34                 } else if len(sc.Clusters) > 1 {
35                         return nil, fmt.Errorf("multiple clusters configured, cannot choose")
36                 } else {
37                         for id, cc := range sc.Clusters {
38                                 cc.ClusterID = id
39                                 return &cc, nil
40                         }
41                 }
42         }
43         if cc, ok := sc.Clusters[clusterID]; !ok {
44                 return nil, fmt.Errorf("cluster %q is not configured", clusterID)
45         } else {
46                 cc.ClusterID = clusterID
47                 return &cc, nil
48         }
49 }
50
51 type Cluster struct {
52         ClusterID          string `json:"-"`
53         ManagementToken    string
54         NodeProfiles       map[string]NodeProfile
55         InstanceTypes      []InstanceType
56         HTTPRequestTimeout Duration
57 }
58
59 type InstanceType struct {
60         Name         string
61         ProviderType string
62         VCPUs        int
63         RAM          int64
64         Scratch      int64
65         Price        float64
66 }
67
68 // GetNodeProfile returns a NodeProfile for the given hostname. An
69 // error is returned if the appropriate configuration can't be
70 // determined (e.g., this does not appear to be a system node). If
71 // node is empty, use the OS-reported hostname.
72 func (cc *Cluster) GetNodeProfile(node string) (*NodeProfile, error) {
73         if node == "" {
74                 hostname, err := os.Hostname()
75                 if err != nil {
76                         return nil, err
77                 }
78                 node = hostname
79         }
80         if cfg, ok := cc.NodeProfiles[node]; ok {
81                 return &cfg, nil
82         }
83         // If node is not listed, but "*" gives a default system node
84         // config, use the default config.
85         if cfg, ok := cc.NodeProfiles["*"]; ok {
86                 return &cfg, nil
87         }
88         return nil, fmt.Errorf("config does not provision host %q as a system node", node)
89 }
90
91 type NodeProfile struct {
92         Controller  SystemServiceInstance `json:"arvados-controller"`
93         Health      SystemServiceInstance `json:"arvados-health"`
94         Keepproxy   SystemServiceInstance `json:"keepproxy"`
95         Keepstore   SystemServiceInstance `json:"keepstore"`
96         Keepweb     SystemServiceInstance `json:"keep-web"`
97         Nodemanager SystemServiceInstance `json:"arvados-node-manager"`
98         RailsAPI    SystemServiceInstance `json:"arvados-api-server"`
99         Websocket   SystemServiceInstance `json:"arvados-ws"`
100         Workbench   SystemServiceInstance `json:"arvados-workbench"`
101 }
102
103 type ServiceName string
104
105 const (
106         ServiceNameRailsAPI    ServiceName = "arvados-api-server"
107         ServiceNameController  ServiceName = "arvados-controller"
108         ServiceNameNodemanager ServiceName = "arvados-node-manager"
109         ServiceNameWorkbench   ServiceName = "arvados-workbench"
110         ServiceNameWebsocket   ServiceName = "arvados-ws"
111         ServiceNameKeepweb     ServiceName = "keep-web"
112         ServiceNameKeepproxy   ServiceName = "keepproxy"
113         ServiceNameKeepstore   ServiceName = "keepstore"
114 )
115
116 // ServicePorts returns the configured listening address (or "" if
117 // disabled) for each service on the node.
118 func (np *NodeProfile) ServicePorts() map[ServiceName]string {
119         return map[ServiceName]string{
120                 ServiceNameRailsAPI:    np.RailsAPI.Listen,
121                 ServiceNameController:  np.Controller.Listen,
122                 ServiceNameNodemanager: np.Nodemanager.Listen,
123                 ServiceNameWorkbench:   np.Workbench.Listen,
124                 ServiceNameWebsocket:   np.Websocket.Listen,
125                 ServiceNameKeepweb:     np.Keepweb.Listen,
126                 ServiceNameKeepproxy:   np.Keepproxy.Listen,
127                 ServiceNameKeepstore:   np.Keepstore.Listen,
128         }
129 }
130
131 type SystemServiceInstance struct {
132         Listen string
133         TLS    bool
134 }