13493: Merge branch 'master' into 13493-federation-proxy
[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         "encoding/json"
9         "errors"
10         "fmt"
11         "os"
12
13         "git.curoverse.com/arvados.git/sdk/go/config"
14 )
15
16 const DefaultConfigFile = "/etc/arvados/config.yml"
17
18 type Config struct {
19         Clusters map[string]Cluster
20 }
21
22 // GetConfig returns the current system config, loading it from
23 // configFile if needed.
24 func GetConfig(configFile string) (*Config, error) {
25         var cfg Config
26         err := config.LoadFile(&cfg, configFile)
27         return &cfg, err
28 }
29
30 // GetCluster returns the cluster ID and config for the given
31 // cluster, or the default/only configured cluster if clusterID is "".
32 func (sc *Config) GetCluster(clusterID string) (*Cluster, error) {
33         if clusterID == "" {
34                 if len(sc.Clusters) == 0 {
35                         return nil, fmt.Errorf("no clusters configured")
36                 } else if len(sc.Clusters) > 1 {
37                         return nil, fmt.Errorf("multiple clusters configured, cannot choose")
38                 } else {
39                         for id, cc := range sc.Clusters {
40                                 cc.ClusterID = id
41                                 return &cc, nil
42                         }
43                 }
44         }
45         if cc, ok := sc.Clusters[clusterID]; !ok {
46                 return nil, fmt.Errorf("cluster %q is not configured", clusterID)
47         } else {
48                 cc.ClusterID = clusterID
49                 return &cc, nil
50         }
51 }
52
53 type Cluster struct {
54         ClusterID          string `json:"-"`
55         ManagementToken    string
56         NodeProfiles       map[string]NodeProfile
57         InstanceTypes      InstanceTypeMap
58         HTTPRequestTimeout Duration
59         RemoteClusters     map[string]RemoteCluster
60         PostgreSQL         PostgreSQL
61 }
62
63 type PostgreSQL struct {
64         Connection     PostgreSQLConnection
65         ConnectionPool int
66 }
67
68 type PostgreSQLConnection map[string]string
69
70 type RemoteCluster struct {
71         // API endpoint host or host:port; default is {id}.arvadosapi.com
72         Host string
73         // Perform a proxy request when a local client requests an
74         // object belonging to this remote.
75         Proxy bool
76         // Scheme, default "https". Can be set to "http" for testing.
77         Scheme string
78         // Disable TLS verify. Can be set to true for testing.
79         Insecure bool
80 }
81
82 type InstanceType struct {
83         Name         string
84         ProviderType string
85         VCPUs        int
86         RAM          ByteSize
87         Scratch      ByteSize
88         Price        float64
89         Preemptible  bool
90 }
91
92 type InstanceTypeMap map[string]InstanceType
93
94 var errDuplicateInstanceTypeName = errors.New("duplicate instance type name")
95
96 // UnmarshalJSON handles old config files that provide an array of
97 // instance types instead of a hash.
98 func (it *InstanceTypeMap) UnmarshalJSON(data []byte) error {
99         if len(data) > 0 && data[0] == '[' {
100                 var arr []InstanceType
101                 err := json.Unmarshal(data, &arr)
102                 if err != nil {
103                         return err
104                 }
105                 if len(arr) == 0 {
106                         *it = nil
107                         return nil
108                 }
109                 *it = make(map[string]InstanceType, len(arr))
110                 for _, t := range arr {
111                         if _, ok := (*it)[t.Name]; ok {
112                                 return errDuplicateInstanceTypeName
113                         }
114                         (*it)[t.Name] = t
115                 }
116                 return nil
117         }
118         var hash map[string]InstanceType
119         err := json.Unmarshal(data, &hash)
120         if err != nil {
121                 return err
122         }
123         // Fill in Name field using hash key.
124         *it = InstanceTypeMap(hash)
125         for name, t := range *it {
126                 t.Name = name
127                 (*it)[name] = t
128         }
129         return nil
130 }
131
132 // GetNodeProfile returns a NodeProfile for the given hostname. An
133 // error is returned if the appropriate configuration can't be
134 // determined (e.g., this does not appear to be a system node). If
135 // node is empty, use the OS-reported hostname.
136 func (cc *Cluster) GetNodeProfile(node string) (*NodeProfile, error) {
137         if node == "" {
138                 hostname, err := os.Hostname()
139                 if err != nil {
140                         return nil, err
141                 }
142                 node = hostname
143         }
144         if cfg, ok := cc.NodeProfiles[node]; ok {
145                 return &cfg, nil
146         }
147         // If node is not listed, but "*" gives a default system node
148         // config, use the default config.
149         if cfg, ok := cc.NodeProfiles["*"]; ok {
150                 return &cfg, nil
151         }
152         return nil, fmt.Errorf("config does not provision host %q as a system node", node)
153 }
154
155 type NodeProfile struct {
156         Controller  SystemServiceInstance `json:"arvados-controller"`
157         Health      SystemServiceInstance `json:"arvados-health"`
158         Keepproxy   SystemServiceInstance `json:"keepproxy"`
159         Keepstore   SystemServiceInstance `json:"keepstore"`
160         Keepweb     SystemServiceInstance `json:"keep-web"`
161         Nodemanager SystemServiceInstance `json:"arvados-node-manager"`
162         RailsAPI    SystemServiceInstance `json:"arvados-api-server"`
163         Websocket   SystemServiceInstance `json:"arvados-ws"`
164         Workbench   SystemServiceInstance `json:"arvados-workbench"`
165 }
166
167 type ServiceName string
168
169 const (
170         ServiceNameRailsAPI    ServiceName = "arvados-api-server"
171         ServiceNameController  ServiceName = "arvados-controller"
172         ServiceNameNodemanager ServiceName = "arvados-node-manager"
173         ServiceNameWorkbench   ServiceName = "arvados-workbench"
174         ServiceNameWebsocket   ServiceName = "arvados-ws"
175         ServiceNameKeepweb     ServiceName = "keep-web"
176         ServiceNameKeepproxy   ServiceName = "keepproxy"
177         ServiceNameKeepstore   ServiceName = "keepstore"
178 )
179
180 // ServicePorts returns the configured listening address (or "" if
181 // disabled) for each service on the node.
182 func (np *NodeProfile) ServicePorts() map[ServiceName]string {
183         return map[ServiceName]string{
184                 ServiceNameRailsAPI:    np.RailsAPI.Listen,
185                 ServiceNameController:  np.Controller.Listen,
186                 ServiceNameNodemanager: np.Nodemanager.Listen,
187                 ServiceNameWorkbench:   np.Workbench.Listen,
188                 ServiceNameWebsocket:   np.Websocket.Listen,
189                 ServiceNameKeepweb:     np.Keepweb.Listen,
190                 ServiceNameKeepproxy:   np.Keepproxy.Listen,
191                 ServiceNameKeepstore:   np.Keepstore.Listen,
192         }
193 }
194
195 type SystemServiceInstance struct {
196         Listen   string
197         TLS      bool
198         Insecure bool
199 }