7478: Replaces term 'preemptable' with 'preemptible'
[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         SystemNodes     map[string]SystemNode
55         InstanceTypes   []InstanceType
56 }
57
58 type InstanceType struct {
59         Name         string
60         ProviderType string
61         VCPUs        int
62         RAM          int64
63         Scratch      int64
64         Price        float64
65         Preemptible  bool
66 }
67
68 // GetThisSystemNode returns a SystemNode for the node we're running
69 // on right now.
70 func (cc *Cluster) GetThisSystemNode() (*SystemNode, error) {
71         hostname, err := os.Hostname()
72         if err != nil {
73                 return nil, err
74         }
75         return cc.GetSystemNode(hostname)
76 }
77
78 // GetSystemNode returns a SystemNode for the given hostname. An error
79 // is returned if the appropriate configuration can't be determined
80 // (e.g., this does not appear to be a system node).
81 func (cc *Cluster) GetSystemNode(node string) (*SystemNode, error) {
82         if cfg, ok := cc.SystemNodes[node]; ok {
83                 return &cfg, nil
84         }
85         // If node is not listed, but "*" gives a default system node
86         // config, use the default config.
87         if cfg, ok := cc.SystemNodes["*"]; ok {
88                 return &cfg, nil
89         }
90         return nil, fmt.Errorf("config does not provision host %q as a system node", node)
91 }
92
93 type SystemNode struct {
94         Health      SystemServiceInstance `json:"arvados-health"`
95         Keepproxy   SystemServiceInstance `json:"keepproxy"`
96         Keepstore   SystemServiceInstance `json:"keepstore"`
97         Keepweb     SystemServiceInstance `json:"keep-web"`
98         Nodemanager SystemServiceInstance `json:"arvados-node-manager"`
99         RailsAPI    SystemServiceInstance `json:"arvados-api-server"`
100         Websocket   SystemServiceInstance `json:"arvados-ws"`
101         Workbench   SystemServiceInstance `json:"arvados-workbench"`
102 }
103
104 // ServicePorts returns the configured listening address (or "" if
105 // disabled) for each service on the node.
106 func (sn *SystemNode) ServicePorts() map[string]string {
107         return map[string]string{
108                 "arvados-api-server":   sn.RailsAPI.Listen,
109                 "arvados-node-manager": sn.Nodemanager.Listen,
110                 "arvados-workbench":    sn.Workbench.Listen,
111                 "arvados-ws":           sn.Websocket.Listen,
112                 "keep-web":             sn.Keepweb.Listen,
113                 "keepproxy":            sn.Keepproxy.Listen,
114                 "keepstore":            sn.Keepstore.Listen,
115         }
116 }
117
118 type SystemServiceInstance struct {
119         Listen string
120 }