Merge branch 'master' into 14946-ruby-2.5
[arvados.git] / lib / config / deprecated.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package config
6
7 import (
8         "fmt"
9         "os"
10         "strings"
11
12         "git.curoverse.com/arvados.git/sdk/go/arvados"
13         "github.com/ghodss/yaml"
14 )
15
16 type deprRequestLimits struct {
17         MaxItemsPerResponse            *int
18         MultiClusterRequestConcurrency *int
19 }
20
21 type deprCluster struct {
22         RequestLimits deprRequestLimits
23         NodeProfiles  map[string]nodeProfile
24 }
25
26 type deprecatedConfig struct {
27         Clusters map[string]deprCluster
28 }
29
30 type nodeProfile struct {
31         Controller    systemServiceInstance `json:"arvados-controller"`
32         Health        systemServiceInstance `json:"arvados-health"`
33         Keepbalance   systemServiceInstance `json:"keep-balance"`
34         Keepproxy     systemServiceInstance `json:"keepproxy"`
35         Keepstore     systemServiceInstance `json:"keepstore"`
36         Keepweb       systemServiceInstance `json:"keep-web"`
37         Nodemanager   systemServiceInstance `json:"arvados-node-manager"`
38         DispatchCloud systemServiceInstance `json:"arvados-dispatch-cloud"`
39         RailsAPI      systemServiceInstance `json:"arvados-api-server"`
40         Websocket     systemServiceInstance `json:"arvados-ws"`
41         Workbench1    systemServiceInstance `json:"arvados-workbench"`
42 }
43
44 type systemServiceInstance struct {
45         Listen   string
46         TLS      bool
47         Insecure bool
48 }
49
50 func applyDeprecatedConfig(cfg *arvados.Config, configdata []byte, log logger) error {
51         var dc deprecatedConfig
52         err := yaml.Unmarshal(configdata, &dc)
53         if err != nil {
54                 return err
55         }
56         hostname, err := os.Hostname()
57         if err != nil {
58                 return err
59         }
60         for id, dcluster := range dc.Clusters {
61                 cluster, ok := cfg.Clusters[id]
62                 if !ok {
63                         return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
64                 }
65                 for name, np := range dcluster.NodeProfiles {
66                         if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
67                                 name = "localhost"
68                         } else if log != nil {
69                                 log.Warnf("overriding Clusters.%s.Services using Clusters.%s.NodeProfiles.%s (guessing %q is a hostname)", id, id, name, name)
70                         }
71                         applyDeprecatedNodeProfile(name, np.RailsAPI, &cluster.Services.RailsAPI)
72                         applyDeprecatedNodeProfile(name, np.Controller, &cluster.Services.Controller)
73                         applyDeprecatedNodeProfile(name, np.DispatchCloud, &cluster.Services.DispatchCloud)
74                 }
75                 if dst, n := &cluster.API.MaxItemsPerResponse, dcluster.RequestLimits.MaxItemsPerResponse; n != nil && *n != *dst {
76                         *dst = *n
77                 }
78                 if dst, n := &cluster.API.MaxRequestAmplification, dcluster.RequestLimits.MultiClusterRequestConcurrency; n != nil && *n != *dst {
79                         *dst = *n
80                 }
81                 cfg.Clusters[id] = cluster
82         }
83         return nil
84 }
85
86 func applyDeprecatedNodeProfile(hostname string, ssi systemServiceInstance, svc *arvados.Service) {
87         scheme := "https"
88         if !ssi.TLS {
89                 scheme = "http"
90         }
91         if svc.InternalURLs == nil {
92                 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
93         }
94         host := ssi.Listen
95         if host == "" {
96                 return
97         }
98         if strings.HasPrefix(host, ":") {
99                 host = hostname + host
100         }
101         svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}
102 }