14717: Remove LegacyComponentConfig behavior
[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         "io/ioutil"
10         "os"
11         "strings"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvados"
14         "github.com/ghodss/yaml"
15 )
16
17 type deprRequestLimits struct {
18         MaxItemsPerResponse            *int
19         MultiClusterRequestConcurrency *int
20 }
21
22 type deprCluster struct {
23         RequestLimits deprRequestLimits
24         NodeProfiles  map[string]nodeProfile
25 }
26
27 type deprecatedConfig struct {
28         Clusters map[string]deprCluster
29 }
30
31 type nodeProfile struct {
32         Controller    systemServiceInstance `json:"arvados-controller"`
33         Health        systemServiceInstance `json:"arvados-health"`
34         Keepbalance   systemServiceInstance `json:"keep-balance"`
35         Keepproxy     systemServiceInstance `json:"keepproxy"`
36         Keepstore     systemServiceInstance `json:"keepstore"`
37         Keepweb       systemServiceInstance `json:"keep-web"`
38         Nodemanager   systemServiceInstance `json:"arvados-node-manager"`
39         DispatchCloud systemServiceInstance `json:"arvados-dispatch-cloud"`
40         RailsAPI      systemServiceInstance `json:"arvados-api-server"`
41         Websocket     systemServiceInstance `json:"arvados-ws"`
42         Workbench1    systemServiceInstance `json:"arvados-workbench"`
43 }
44
45 type systemServiceInstance struct {
46         Listen   string
47         TLS      bool
48         Insecure bool
49 }
50
51 func (ldr *Loader) applyDeprecatedConfig(cfg *arvados.Config) error {
52         var dc deprecatedConfig
53         err := yaml.Unmarshal(ldr.configdata, &dc)
54         if err != nil {
55                 return err
56         }
57         hostname, err := os.Hostname()
58         if err != nil {
59                 return err
60         }
61         for id, dcluster := range dc.Clusters {
62                 cluster, ok := cfg.Clusters[id]
63                 if !ok {
64                         return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
65                 }
66                 for name, np := range dcluster.NodeProfiles {
67                         if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
68                                 name = "localhost"
69                         } else if ldr.Logger != nil {
70                                 ldr.Logger.Warnf("overriding Clusters.%s.Services using Clusters.%s.NodeProfiles.%s (guessing %q is a hostname)", id, id, name, name)
71                         }
72                         applyDeprecatedNodeProfile(name, np.RailsAPI, &cluster.Services.RailsAPI)
73                         applyDeprecatedNodeProfile(name, np.Controller, &cluster.Services.Controller)
74                         applyDeprecatedNodeProfile(name, np.DispatchCloud, &cluster.Services.DispatchCloud)
75                 }
76                 if dst, n := &cluster.API.MaxItemsPerResponse, dcluster.RequestLimits.MaxItemsPerResponse; n != nil && *n != *dst {
77                         *dst = *n
78                 }
79                 if dst, n := &cluster.API.MaxRequestAmplification, dcluster.RequestLimits.MultiClusterRequestConcurrency; n != nil && *n != *dst {
80                         *dst = *n
81                 }
82                 cfg.Clusters[id] = cluster
83         }
84         return nil
85 }
86
87 func applyDeprecatedNodeProfile(hostname string, ssi systemServiceInstance, svc *arvados.Service) {
88         scheme := "https"
89         if !ssi.TLS {
90                 scheme = "http"
91         }
92         if svc.InternalURLs == nil {
93                 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
94         }
95         host := ssi.Listen
96         if host == "" {
97                 return
98         }
99         if strings.HasPrefix(host, ":") {
100                 host = hostname + host
101         }
102         svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}
103 }
104
105 const defaultKeepstoreConfigPath = "/etc/arvados/keepstore/keepstore.yml"
106
107 type oldKeepstoreConfig struct {
108         Debug *bool
109 }
110
111 func (ldr *Loader) loadOldConfigHelper(component, path string, target interface{}) error {
112         if path == "" {
113                 return nil
114         }
115         buf, err := ioutil.ReadFile(path)
116         if err != nil {
117                 return err
118         }
119
120         ldr.Logger.Warnf("you should remove the legacy %v config file (%s) after migrating all config keys to the cluster configuration file (%s)", component, path, ldr.Path)
121
122         err = yaml.Unmarshal(buf, target)
123         if err != nil {
124                 return fmt.Errorf("%s: %s", path, err)
125         }
126         return nil
127 }
128
129 // update config using values from an old-style keepstore config file.
130 func (ldr *Loader) loadOldKeepstoreConfig(cfg *arvados.Config) error {
131         var oc oldKeepstoreConfig
132         err := ldr.loadOldConfigHelper("keepstore", ldr.KeepstorePath, &oc)
133         if os.IsNotExist(err) && (ldr.KeepstorePath == defaultKeepstoreConfigPath) {
134                 return nil
135         } else if err != nil {
136                 return err
137         }
138
139         cluster, err := cfg.GetCluster("")
140         if err != nil {
141                 return err
142         }
143
144         if v := oc.Debug; v == nil {
145         } else if *v && cluster.SystemLogs.LogLevel != "debug" {
146                 cluster.SystemLogs.LogLevel = "debug"
147         } else if !*v && cluster.SystemLogs.LogLevel != "info" {
148                 cluster.SystemLogs.LogLevel = "info"
149         }
150
151         cfg.Clusters[cluster.ClusterID] = *cluster
152         return nil
153 }
154
155 type oldCrunchDispatchSlurmConfig struct {
156         Client *arvados.Client
157
158         SbatchArguments *[]string
159         PollPeriod      *arvados.Duration
160         PrioritySpread  *int64
161
162         // crunch-run command to invoke. The container UUID will be
163         // appended. If nil, []string{"crunch-run"} will be used.
164         //
165         // Example: []string{"crunch-run", "--cgroup-parent-subsystem=memory"}
166         CrunchRunCommand *[]string
167
168         // Extra RAM to reserve (in Bytes) for SLURM job, in addition
169         // to the amount specified in the container's RuntimeConstraints
170         ReserveExtraRAM *int64
171
172         // Minimum time between two attempts to run the same container
173         MinRetryPeriod *arvados.Duration
174
175         // Batch size for container queries
176         BatchSize *int64
177 }
178
179 const defaultCrunchDispatchSlurmConfigPath = "/etc/arvados/crunch-dispatch-slurm/crunch-dispatch-slurm.yml"
180
181 func loadOldClientConfig(cluster *arvados.Cluster, client *arvados.Client) {
182         if client == nil {
183                 return
184         }
185         if client.APIHost != "" {
186                 cluster.Services.Controller.ExternalURL.Host = client.APIHost
187         }
188         if client.Scheme != "" {
189                 cluster.Services.Controller.ExternalURL.Scheme = client.Scheme
190         } else {
191                 cluster.Services.Controller.ExternalURL.Scheme = "https"
192         }
193         if client.AuthToken != "" {
194                 cluster.SystemRootToken = client.AuthToken
195         }
196         cluster.TLS.Insecure = client.Insecure
197 }
198
199 // update config using values from an crunch-dispatch-slurm config file.
200 func (ldr *Loader) loadOldCrunchDispatchSlurmConfig(cfg *arvados.Config) error {
201         var oc oldCrunchDispatchSlurmConfig
202         err := ldr.loadOldConfigHelper("crunch-dispatch-slurm", ldr.CrunchDispatchSlurmPath, &oc)
203         if os.IsNotExist(err) && (ldr.CrunchDispatchSlurmPath == defaultCrunchDispatchSlurmConfigPath) {
204                 return nil
205         } else if err != nil {
206                 return err
207         }
208
209         cluster, err := cfg.GetCluster("")
210         if err != nil {
211                 return err
212         }
213
214         loadOldClientConfig(cluster, oc.Client)
215
216         if oc.SbatchArguments != nil {
217                 cluster.Containers.SLURM.SbatchArgumentsList = *oc.SbatchArguments
218         }
219         if oc.PollPeriod != nil {
220                 cluster.Containers.CloudVMs.PollInterval = *oc.PollPeriod
221         }
222         if oc.PrioritySpread != nil {
223                 cluster.Containers.SLURM.PrioritySpread = *oc.PrioritySpread
224         }
225         if oc.CrunchRunCommand != nil {
226                 if len(*oc.CrunchRunCommand) >= 1 {
227                         cluster.Containers.CrunchRunCommand = (*oc.CrunchRunCommand)[0]
228                 }
229                 if len(*oc.CrunchRunCommand) >= 2 {
230                         cluster.Containers.CrunchRunArgumentsList = (*oc.CrunchRunCommand)[1:]
231                 }
232         }
233         if oc.ReserveExtraRAM != nil {
234                 cluster.Containers.ReserveExtraRAM = arvados.ByteSize(*oc.ReserveExtraRAM)
235         }
236         if oc.MinRetryPeriod != nil {
237                 cluster.Containers.MinRetryPeriod = *oc.MinRetryPeriod
238         }
239         if oc.BatchSize != nil {
240                 cluster.API.MaxItemsPerResponse = int(*oc.BatchSize)
241         }
242
243         cfg.Clusters[cluster.ClusterID] = *cluster
244         return nil
245 }
246
247 type oldWsConfig struct {
248         Client       *arvados.Client
249         Postgres     *arvados.PostgreSQLConnection
250         PostgresPool *int
251         Listen       *string
252         LogLevel     *string
253         LogFormat    *string
254
255         PingTimeout      *arvados.Duration
256         ClientEventQueue *int
257         ServerEventQueue *int
258
259         ManagementToken *string
260 }
261
262 const defaultWebsocketConfigPath = "/etc/arvados/ws/ws.yml"
263
264 // update config using values from an crunch-dispatch-slurm config file.
265 func (ldr *Loader) loadOldWebsocketConfig(cfg *arvados.Config) error {
266         var oc oldWsConfig
267         err := ldr.loadOldConfigHelper("arvados-ws", ldr.WebsocketPath, &oc)
268         if os.IsNotExist(err) && ldr.WebsocketPath == defaultWebsocketConfigPath {
269                 return nil
270         } else if err != nil {
271                 return err
272         }
273
274         cluster, err := cfg.GetCluster("")
275         if err != nil {
276                 return err
277         }
278
279         loadOldClientConfig(cluster, oc.Client)
280
281         if oc.Postgres != nil {
282                 cluster.PostgreSQL.Connection = *oc.Postgres
283         }
284         if oc.PostgresPool != nil {
285                 cluster.PostgreSQL.ConnectionPool = *oc.PostgresPool
286         }
287         if oc.Listen != nil {
288                 cluster.Services.Websocket.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
289         }
290         if oc.LogLevel != nil {
291                 cluster.SystemLogs.LogLevel = *oc.LogLevel
292         }
293         if oc.LogFormat != nil {
294                 cluster.SystemLogs.Format = *oc.LogFormat
295         }
296         if oc.PingTimeout != nil {
297                 cluster.API.SendTimeout = *oc.PingTimeout
298         }
299         if oc.ClientEventQueue != nil {
300                 cluster.API.WebsocketClientEventQueue = *oc.ClientEventQueue
301         }
302         if oc.ServerEventQueue != nil {
303                 cluster.API.WebsocketServerEventQueue = *oc.ServerEventQueue
304         }
305         if oc.ManagementToken != nil {
306                 cluster.ManagementToken = *oc.ManagementToken
307         }
308
309         cfg.Clusters[cluster.ClusterID] = *cluster
310         return nil
311 }