1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "github.com/ghodss/yaml"
20 type logger interface {
21 Warnf(string, ...interface{})
24 type deprecatedConfig struct {
25 Clusters map[string]struct {
26 NodeProfiles map[string]arvados.NodeProfile
30 func LoadFile(path string, log logger) (*arvados.Config, error) {
31 f, err := os.Open(path)
39 func Load(rdr io.Reader, log logger) (*arvados.Config, error) {
40 var cfg arvados.Config
41 buf, err := ioutil.ReadAll(rdr)
46 // Load the config into a dummy map to get the cluster ID
47 // keys, discarding the values; then set up defaults for each
48 // cluster ID; then load the real config on top of the
51 Clusters map[string]struct{}
53 err = yaml.Unmarshal(buf, &dummy)
57 if len(dummy.Clusters) == 0 {
58 return nil, errors.New("config does not define any clusters")
60 for id := range dummy.Clusters {
61 err = yaml.Unmarshal(bytes.Replace(DefaultYAML, []byte("xxxxx"), []byte(id), -1), &cfg)
63 return nil, fmt.Errorf("loading defaults for %s: %s", id, err)
66 err = yaml.Unmarshal(buf, &cfg)
71 // Check for deprecated config values, and apply them to cfg.
72 var dc deprecatedConfig
73 err = yaml.Unmarshal(buf, &dc)
77 err = applyDeprecatedConfig(&cfg, &dc, log)
84 func applyDeprecatedConfig(cfg *arvados.Config, dc *deprecatedConfig, log logger) error {
85 hostname, err := os.Hostname()
89 for id, dcluster := range dc.Clusters {
90 cluster, ok := cfg.Clusters[id]
92 return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
94 for name, np := range dcluster.NodeProfiles {
95 if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
96 applyDeprecatedNodeProfile(hostname, np.RailsAPI, &cluster.Services.RailsAPI)
97 applyDeprecatedNodeProfile(hostname, np.Controller, &cluster.Services.Controller)
98 applyDeprecatedNodeProfile(hostname, np.DispatchCloud, &cluster.Services.DispatchCloud)
101 cfg.Clusters[id] = cluster
106 func applyDeprecatedNodeProfile(hostname string, ssi arvados.SystemServiceInstance, svc *arvados.Service) {
111 if svc.InternalURLs == nil {
112 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
118 if strings.HasPrefix(host, ":") {
119 host = hostname + host
121 svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}