13647: Use cluster config instead of custom keepstore config.
[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 func (ldr *Loader) loadOldConfigHelper(component, path string, target interface{}) error {
106         if path == "" {
107                 return nil
108         }
109         buf, err := ioutil.ReadFile(path)
110         if err != nil {
111                 return err
112         }
113
114         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)
115
116         err = yaml.Unmarshal(buf, target)
117         if err != nil {
118                 return fmt.Errorf("%s: %s", path, err)
119         }
120         return nil
121 }
122
123 type oldCrunchDispatchSlurmConfig struct {
124         Client *arvados.Client
125
126         SbatchArguments *[]string
127         PollPeriod      *arvados.Duration
128         PrioritySpread  *int64
129
130         // crunch-run command to invoke. The container UUID will be
131         // appended. If nil, []string{"crunch-run"} will be used.
132         //
133         // Example: []string{"crunch-run", "--cgroup-parent-subsystem=memory"}
134         CrunchRunCommand *[]string
135
136         // Extra RAM to reserve (in Bytes) for SLURM job, in addition
137         // to the amount specified in the container's RuntimeConstraints
138         ReserveExtraRAM *int64
139
140         // Minimum time between two attempts to run the same container
141         MinRetryPeriod *arvados.Duration
142
143         // Batch size for container queries
144         BatchSize *int64
145 }
146
147 const defaultCrunchDispatchSlurmConfigPath = "/etc/arvados/crunch-dispatch-slurm/crunch-dispatch-slurm.yml"
148
149 func loadOldClientConfig(cluster *arvados.Cluster, client *arvados.Client) {
150         if client == nil {
151                 return
152         }
153         if client.APIHost != "" {
154                 cluster.Services.Controller.ExternalURL.Host = client.APIHost
155         }
156         if client.Scheme != "" {
157                 cluster.Services.Controller.ExternalURL.Scheme = client.Scheme
158         } else {
159                 cluster.Services.Controller.ExternalURL.Scheme = "https"
160         }
161         if client.AuthToken != "" {
162                 cluster.SystemRootToken = client.AuthToken
163         }
164         cluster.TLS.Insecure = client.Insecure
165         ks := ""
166         for i, u := range client.KeepServiceURIs {
167                 if i > 0 {
168                         ks += " "
169                 }
170                 ks += u
171         }
172         cluster.Containers.SLURM.SbatchEnvironmentVariables = map[string]string{"ARVADOS_KEEP_SERVICES": ks}
173 }
174
175 // update config using values from an crunch-dispatch-slurm config file.
176 func (ldr *Loader) loadOldCrunchDispatchSlurmConfig(cfg *arvados.Config) error {
177         if ldr.CrunchDispatchSlurmPath == "" {
178                 return nil
179         }
180         var oc oldCrunchDispatchSlurmConfig
181         err := ldr.loadOldConfigHelper("crunch-dispatch-slurm", ldr.CrunchDispatchSlurmPath, &oc)
182         if os.IsNotExist(err) && (ldr.CrunchDispatchSlurmPath == defaultCrunchDispatchSlurmConfigPath) {
183                 return nil
184         } else if err != nil {
185                 return err
186         }
187
188         cluster, err := cfg.GetCluster("")
189         if err != nil {
190                 return err
191         }
192
193         loadOldClientConfig(cluster, oc.Client)
194
195         if oc.SbatchArguments != nil {
196                 cluster.Containers.SLURM.SbatchArgumentsList = *oc.SbatchArguments
197         }
198         if oc.PollPeriod != nil {
199                 cluster.Containers.CloudVMs.PollInterval = *oc.PollPeriod
200         }
201         if oc.PrioritySpread != nil {
202                 cluster.Containers.SLURM.PrioritySpread = *oc.PrioritySpread
203         }
204         if oc.CrunchRunCommand != nil {
205                 if len(*oc.CrunchRunCommand) >= 1 {
206                         cluster.Containers.CrunchRunCommand = (*oc.CrunchRunCommand)[0]
207                 }
208                 if len(*oc.CrunchRunCommand) >= 2 {
209                         cluster.Containers.CrunchRunArgumentsList = (*oc.CrunchRunCommand)[1:]
210                 }
211         }
212         if oc.ReserveExtraRAM != nil {
213                 cluster.Containers.ReserveExtraRAM = arvados.ByteSize(*oc.ReserveExtraRAM)
214         }
215         if oc.MinRetryPeriod != nil {
216                 cluster.Containers.MinRetryPeriod = *oc.MinRetryPeriod
217         }
218         if oc.BatchSize != nil {
219                 cluster.API.MaxItemsPerResponse = int(*oc.BatchSize)
220         }
221
222         cfg.Clusters[cluster.ClusterID] = *cluster
223         return nil
224 }
225
226 type oldWsConfig struct {
227         Client       *arvados.Client
228         Postgres     *arvados.PostgreSQLConnection
229         PostgresPool *int
230         Listen       *string
231         LogLevel     *string
232         LogFormat    *string
233
234         PingTimeout      *arvados.Duration
235         ClientEventQueue *int
236         ServerEventQueue *int
237
238         ManagementToken *string
239 }
240
241 const defaultWebsocketConfigPath = "/etc/arvados/ws/ws.yml"
242
243 // update config using values from an crunch-dispatch-slurm config file.
244 func (ldr *Loader) loadOldWebsocketConfig(cfg *arvados.Config) error {
245         if ldr.WebsocketPath == "" {
246                 return nil
247         }
248         var oc oldWsConfig
249         err := ldr.loadOldConfigHelper("arvados-ws", ldr.WebsocketPath, &oc)
250         if os.IsNotExist(err) && ldr.WebsocketPath == defaultWebsocketConfigPath {
251                 return nil
252         } else if err != nil {
253                 return err
254         }
255
256         cluster, err := cfg.GetCluster("")
257         if err != nil {
258                 return err
259         }
260
261         loadOldClientConfig(cluster, oc.Client)
262
263         if oc.Postgres != nil {
264                 cluster.PostgreSQL.Connection = *oc.Postgres
265         }
266         if oc.PostgresPool != nil {
267                 cluster.PostgreSQL.ConnectionPool = *oc.PostgresPool
268         }
269         if oc.Listen != nil {
270                 cluster.Services.Websocket.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
271         }
272         if oc.LogLevel != nil {
273                 cluster.SystemLogs.LogLevel = *oc.LogLevel
274         }
275         if oc.LogFormat != nil {
276                 cluster.SystemLogs.Format = *oc.LogFormat
277         }
278         if oc.PingTimeout != nil {
279                 cluster.API.SendTimeout = *oc.PingTimeout
280         }
281         if oc.ClientEventQueue != nil {
282                 cluster.API.WebsocketClientEventQueue = *oc.ClientEventQueue
283         }
284         if oc.ServerEventQueue != nil {
285                 cluster.API.WebsocketServerEventQueue = *oc.ServerEventQueue
286         }
287         if oc.ManagementToken != nil {
288                 cluster.ManagementToken = *oc.ManagementToken
289         }
290
291         cfg.Clusters[cluster.ClusterID] = *cluster
292         return nil
293 }
294
295 type oldKeepProxyConfig struct {
296         Client          *arvados.Client
297         Listen          *string
298         DisableGet      *bool
299         DisablePut      *bool
300         DefaultReplicas *int
301         Timeout         *arvados.Duration
302         PIDFile         *string
303         Debug           *bool
304         ManagementToken *string
305 }
306
307 const defaultKeepproxyConfigPath = "/etc/arvados/keepproxy/keepproxy.yml"
308
309 func (ldr *Loader) loadOldKeepproxyConfig(cfg *arvados.Config) error {
310         if ldr.KeepproxyPath == "" {
311                 return nil
312         }
313         var oc oldKeepProxyConfig
314         err := ldr.loadOldConfigHelper("keepproxy", ldr.KeepproxyPath, &oc)
315         if os.IsNotExist(err) && ldr.KeepproxyPath == defaultKeepproxyConfigPath {
316                 return nil
317         } else if err != nil {
318                 return err
319         }
320
321         cluster, err := cfg.GetCluster("")
322         if err != nil {
323                 return err
324         }
325
326         loadOldClientConfig(cluster, oc.Client)
327
328         if oc.Listen != nil {
329                 cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
330         }
331         if oc.DefaultReplicas != nil {
332                 cluster.Collections.DefaultReplication = *oc.DefaultReplicas
333         }
334         if oc.Timeout != nil {
335                 cluster.API.KeepServiceRequestTimeout = *oc.Timeout
336         }
337         if oc.Debug != nil {
338                 if *oc.Debug && cluster.SystemLogs.LogLevel != "debug" {
339                         cluster.SystemLogs.LogLevel = "debug"
340                 } else if !*oc.Debug && cluster.SystemLogs.LogLevel != "info" {
341                         cluster.SystemLogs.LogLevel = "info"
342                 }
343         }
344         if oc.ManagementToken != nil {
345                 cluster.ManagementToken = *oc.ManagementToken
346         }
347
348         // The following legacy options are no longer supported. If they are set to
349         // true or PIDFile has a value, error out and notify the user
350         unsupportedEntry := func(cfgEntry string) error {
351                 return fmt.Errorf("the keepproxy %s configuration option is no longer supported, please remove it from your configuration file", cfgEntry)
352         }
353         if oc.DisableGet != nil && *oc.DisableGet {
354                 return unsupportedEntry("DisableGet")
355         }
356         if oc.DisablePut != nil && *oc.DisablePut {
357                 return unsupportedEntry("DisablePut")
358         }
359         if oc.PIDFile != nil && *oc.PIDFile != "" {
360                 return unsupportedEntry("PIDFile")
361         }
362
363         cfg.Clusters[cluster.ClusterID] = *cluster
364         return nil
365 }
366
367 const defaultKeepWebConfigPath = "/etc/arvados/keep-web/keep-web.yml"
368
369 type oldKeepWebConfig struct {
370         Client *arvados.Client
371
372         Listen string
373
374         AnonymousTokens    []string
375         AttachmentOnlyHost string
376         TrustAllContent    bool
377
378         Cache struct {
379                 TTL                  arvados.Duration
380                 UUIDTTL              arvados.Duration
381                 MaxCollectionEntries int
382                 MaxCollectionBytes   int64
383                 MaxPermissionEntries int
384                 MaxUUIDEntries       int
385         }
386
387         // Hack to support old command line flag, which is a bool
388         // meaning "get actual token from environment".
389         deprecatedAllowAnonymous bool
390
391         // Authorization token to be included in all health check requests.
392         ManagementToken string
393 }
394
395 func (ldr *Loader) loadOldKeepWebConfig(cfg *arvados.Config) error {
396         if ldr.KeepWebPath == "" {
397                 return nil
398         }
399         var oc oldKeepWebConfig
400         err := ldr.loadOldConfigHelper("keep-web", ldr.KeepWebPath, &oc)
401         if os.IsNotExist(err) && ldr.KeepWebPath == defaultKeepWebConfigPath {
402                 return nil
403         } else if err != nil {
404                 return err
405         }
406
407         cluster, err := cfg.GetCluster("")
408         if err != nil {
409                 return err
410         }
411
412         loadOldClientConfig(cluster, oc.Client)
413
414         cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: oc.Listen}] = arvados.ServiceInstance{}
415         cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: oc.Listen}] = arvados.ServiceInstance{}
416         cluster.Services.WebDAVDownload.ExternalURL = arvados.URL{Host: oc.AttachmentOnlyHost}
417         cluster.TLS.Insecure = oc.Client.Insecure
418         cluster.ManagementToken = oc.ManagementToken
419         cluster.Collections.TrustAllContent = oc.TrustAllContent
420         cluster.Collections.WebDAVCache.TTL = oc.Cache.TTL
421         cluster.Collections.WebDAVCache.UUIDTTL = oc.Cache.UUIDTTL
422         cluster.Collections.WebDAVCache.MaxCollectionEntries = oc.Cache.MaxCollectionEntries
423         cluster.Collections.WebDAVCache.MaxCollectionBytes = oc.Cache.MaxCollectionBytes
424         cluster.Collections.WebDAVCache.MaxPermissionEntries = oc.Cache.MaxPermissionEntries
425         cluster.Collections.WebDAVCache.MaxUUIDEntries = oc.Cache.MaxUUIDEntries
426         if len(oc.AnonymousTokens) > 0 {
427                 cluster.Users.AnonymousUserToken = oc.AnonymousTokens[0]
428                 if len(oc.AnonymousTokens) > 1 {
429                         ldr.Logger.Warn("More than 1 anonymous tokens configured, using only the first and discarding the rest.")
430                 }
431         }
432
433         cfg.Clusters[cluster.ClusterID] = *cluster
434         return nil
435 }
436
437 const defaultGitHttpdConfigPath = "/etc/arvados/git-httpd/git-httpd.yml"
438
439 type oldGitHttpdConfig struct {
440         Client          *arvados.Client
441         Listen          string
442         GitCommand      string
443         GitoliteHome    string
444         RepoRoot        string
445         ManagementToken string
446 }
447
448 func (ldr *Loader) loadOldGitHttpdConfig(cfg *arvados.Config) error {
449         if ldr.GitHttpdPath == "" {
450                 return nil
451         }
452         var oc oldGitHttpdConfig
453         err := ldr.loadOldConfigHelper("arv-git-httpd", ldr.GitHttpdPath, &oc)
454         if os.IsNotExist(err) && ldr.GitHttpdPath == defaultGitHttpdConfigPath {
455                 return nil
456         } else if err != nil {
457                 return err
458         }
459
460         cluster, err := cfg.GetCluster("")
461         if err != nil {
462                 return err
463         }
464
465         loadOldClientConfig(cluster, oc.Client)
466
467         cluster.Services.GitHTTP.InternalURLs[arvados.URL{Host: oc.Listen}] = arvados.ServiceInstance{}
468         cluster.TLS.Insecure = oc.Client.Insecure
469         cluster.ManagementToken = oc.ManagementToken
470         cluster.Git.GitCommand = oc.GitCommand
471         cluster.Git.GitoliteHome = oc.GitoliteHome
472         cluster.Git.Repositories = oc.RepoRoot
473
474         cfg.Clusters[cluster.ClusterID] = *cluster
475         return nil
476 }