Merge branch '16392-url-trailing-slash'
[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         "net/url"
11         "os"
12         "strings"
13
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         "github.com/ghodss/yaml"
16 )
17
18 type deprRequestLimits struct {
19         MaxItemsPerResponse            *int
20         MultiClusterRequestConcurrency *int
21 }
22
23 type deprCluster struct {
24         RequestLimits deprRequestLimits
25         NodeProfiles  map[string]nodeProfile
26 }
27
28 type deprecatedConfig struct {
29         Clusters map[string]deprCluster
30 }
31
32 type nodeProfile struct {
33         Controller    systemServiceInstance `json:"arvados-controller"`
34         Health        systemServiceInstance `json:"arvados-health"`
35         Keepbalance   systemServiceInstance `json:"keep-balance"`
36         Keepproxy     systemServiceInstance `json:"keepproxy"`
37         Keepstore     systemServiceInstance `json:"keepstore"`
38         Keepweb       systemServiceInstance `json:"keep-web"`
39         Nodemanager   systemServiceInstance `json:"arvados-node-manager"`
40         DispatchCloud systemServiceInstance `json:"arvados-dispatch-cloud"`
41         RailsAPI      systemServiceInstance `json:"arvados-api-server"`
42         Websocket     systemServiceInstance `json:"arvados-ws"`
43         Workbench1    systemServiceInstance `json:"arvados-workbench"`
44 }
45
46 type systemServiceInstance struct {
47         Listen   string
48         TLS      bool
49         Insecure bool
50 }
51
52 func (ldr *Loader) applyDeprecatedConfig(cfg *arvados.Config) error {
53         var dc deprecatedConfig
54         err := yaml.Unmarshal(ldr.configdata, &dc)
55         if err != nil {
56                 return err
57         }
58         hostname, err := os.Hostname()
59         if err != nil {
60                 return err
61         }
62         for id, dcluster := range dc.Clusters {
63                 cluster, ok := cfg.Clusters[id]
64                 if !ok {
65                         return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
66                 }
67                 for name, np := range dcluster.NodeProfiles {
68                         if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
69                                 name = "localhost"
70                         } else if ldr.Logger != nil {
71                                 ldr.Logger.Warnf("overriding Clusters.%s.Services using Clusters.%s.NodeProfiles.%s (guessing %q is a hostname)", id, id, name, name)
72                         }
73                         applyDeprecatedNodeProfile(name, np.RailsAPI, &cluster.Services.RailsAPI)
74                         applyDeprecatedNodeProfile(name, np.Controller, &cluster.Services.Controller)
75                         applyDeprecatedNodeProfile(name, np.DispatchCloud, &cluster.Services.DispatchCloud)
76                 }
77                 if dst, n := &cluster.API.MaxItemsPerResponse, dcluster.RequestLimits.MaxItemsPerResponse; n != nil && *n != *dst {
78                         *dst = *n
79                 }
80                 if dst, n := &cluster.API.MaxRequestAmplification, dcluster.RequestLimits.MultiClusterRequestConcurrency; n != nil && *n != *dst {
81                         *dst = *n
82                 }
83                 cfg.Clusters[id] = cluster
84         }
85         return nil
86 }
87
88 func applyDeprecatedNodeProfile(hostname string, ssi systemServiceInstance, svc *arvados.Service) {
89         scheme := "https"
90         if !ssi.TLS {
91                 scheme = "http"
92         }
93         if svc.InternalURLs == nil {
94                 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
95         }
96         host := ssi.Listen
97         if host == "" {
98                 return
99         }
100         if strings.HasPrefix(host, ":") {
101                 host = hostname + host
102         }
103         svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host, Path: "/"}] = arvados.ServiceInstance{}
104 }
105
106 func (ldr *Loader) loadOldConfigHelper(component, path string, target interface{}) error {
107         if path == "" {
108                 return nil
109         }
110         buf, err := ioutil.ReadFile(path)
111         if err != nil {
112                 return err
113         }
114
115         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)
116
117         err = yaml.Unmarshal(buf, target)
118         if err != nil {
119                 return fmt.Errorf("%s: %s", path, err)
120         }
121         return nil
122 }
123
124 type oldCrunchDispatchSlurmConfig struct {
125         Client *arvados.Client
126
127         SbatchArguments *[]string
128         PollPeriod      *arvados.Duration
129         PrioritySpread  *int64
130
131         // crunch-run command to invoke. The container UUID will be
132         // appended. If nil, []string{"crunch-run"} will be used.
133         //
134         // Example: []string{"crunch-run", "--cgroup-parent-subsystem=memory"}
135         CrunchRunCommand *[]string
136
137         // Extra RAM to reserve (in Bytes) for SLURM job, in addition
138         // to the amount specified in the container's RuntimeConstraints
139         ReserveExtraRAM *int64
140
141         // Minimum time between two attempts to run the same container
142         MinRetryPeriod *arvados.Duration
143
144         // Batch size for container queries
145         BatchSize *int64
146 }
147
148 const defaultCrunchDispatchSlurmConfigPath = "/etc/arvados/crunch-dispatch-slurm/crunch-dispatch-slurm.yml"
149
150 func loadOldClientConfig(cluster *arvados.Cluster, client *arvados.Client) {
151         if client == nil {
152                 return
153         }
154         if client.APIHost != "" {
155                 cluster.Services.Controller.ExternalURL.Host = client.APIHost
156                 cluster.Services.Controller.ExternalURL.Path = "/"
157         }
158         if client.Scheme != "" {
159                 cluster.Services.Controller.ExternalURL.Scheme = client.Scheme
160         } else {
161                 cluster.Services.Controller.ExternalURL.Scheme = "https"
162         }
163         if client.AuthToken != "" {
164                 cluster.SystemRootToken = client.AuthToken
165         }
166         cluster.TLS.Insecure = client.Insecure
167         ks := ""
168         for i, u := range client.KeepServiceURIs {
169                 if i > 0 {
170                         ks += " "
171                 }
172                 ks += u
173         }
174         cluster.Containers.SLURM.SbatchEnvironmentVariables = map[string]string{"ARVADOS_KEEP_SERVICES": ks}
175 }
176
177 // update config using values from an crunch-dispatch-slurm config file.
178 func (ldr *Loader) loadOldCrunchDispatchSlurmConfig(cfg *arvados.Config) error {
179         if ldr.CrunchDispatchSlurmPath == "" {
180                 return nil
181         }
182         var oc oldCrunchDispatchSlurmConfig
183         err := ldr.loadOldConfigHelper("crunch-dispatch-slurm", ldr.CrunchDispatchSlurmPath, &oc)
184         if os.IsNotExist(err) && (ldr.CrunchDispatchSlurmPath == defaultCrunchDispatchSlurmConfigPath) {
185                 return nil
186         } else if err != nil {
187                 return err
188         }
189
190         cluster, err := cfg.GetCluster("")
191         if err != nil {
192                 return err
193         }
194
195         loadOldClientConfig(cluster, oc.Client)
196
197         if oc.SbatchArguments != nil {
198                 cluster.Containers.SLURM.SbatchArgumentsList = *oc.SbatchArguments
199         }
200         if oc.PollPeriod != nil {
201                 cluster.Containers.CloudVMs.PollInterval = *oc.PollPeriod
202         }
203         if oc.PrioritySpread != nil {
204                 cluster.Containers.SLURM.PrioritySpread = *oc.PrioritySpread
205         }
206         if oc.CrunchRunCommand != nil {
207                 if len(*oc.CrunchRunCommand) >= 1 {
208                         cluster.Containers.CrunchRunCommand = (*oc.CrunchRunCommand)[0]
209                 }
210                 if len(*oc.CrunchRunCommand) >= 2 {
211                         cluster.Containers.CrunchRunArgumentsList = (*oc.CrunchRunCommand)[1:]
212                 }
213         }
214         if oc.ReserveExtraRAM != nil {
215                 cluster.Containers.ReserveExtraRAM = arvados.ByteSize(*oc.ReserveExtraRAM)
216         }
217         if oc.MinRetryPeriod != nil {
218                 cluster.Containers.MinRetryPeriod = *oc.MinRetryPeriod
219         }
220         if oc.BatchSize != nil {
221                 cluster.API.MaxItemsPerResponse = int(*oc.BatchSize)
222         }
223
224         cfg.Clusters[cluster.ClusterID] = *cluster
225         return nil
226 }
227
228 type oldWsConfig struct {
229         Client       *arvados.Client
230         Postgres     *arvados.PostgreSQLConnection
231         PostgresPool *int
232         Listen       *string
233         LogLevel     *string
234         LogFormat    *string
235
236         PingTimeout      *arvados.Duration
237         ClientEventQueue *int
238         ServerEventQueue *int
239
240         ManagementToken *string
241 }
242
243 const defaultWebsocketConfigPath = "/etc/arvados/ws/ws.yml"
244
245 // update config using values from an crunch-dispatch-slurm config file.
246 func (ldr *Loader) loadOldWebsocketConfig(cfg *arvados.Config) error {
247         if ldr.WebsocketPath == "" {
248                 return nil
249         }
250         var oc oldWsConfig
251         err := ldr.loadOldConfigHelper("arvados-ws", ldr.WebsocketPath, &oc)
252         if os.IsNotExist(err) && ldr.WebsocketPath == defaultWebsocketConfigPath {
253                 return nil
254         } else if err != nil {
255                 return err
256         }
257
258         cluster, err := cfg.GetCluster("")
259         if err != nil {
260                 return err
261         }
262
263         loadOldClientConfig(cluster, oc.Client)
264
265         if oc.Postgres != nil {
266                 cluster.PostgreSQL.Connection = *oc.Postgres
267         }
268         if oc.PostgresPool != nil {
269                 cluster.PostgreSQL.ConnectionPool = *oc.PostgresPool
270         }
271         if oc.Listen != nil {
272                 cluster.Services.Websocket.InternalURLs[arvados.URL{Host: *oc.Listen, Path: "/"}] = arvados.ServiceInstance{}
273         }
274         if oc.LogLevel != nil {
275                 cluster.SystemLogs.LogLevel = *oc.LogLevel
276         }
277         if oc.LogFormat != nil {
278                 cluster.SystemLogs.Format = *oc.LogFormat
279         }
280         if oc.PingTimeout != nil {
281                 cluster.API.SendTimeout = *oc.PingTimeout
282         }
283         if oc.ClientEventQueue != nil {
284                 cluster.API.WebsocketClientEventQueue = *oc.ClientEventQueue
285         }
286         if oc.ServerEventQueue != nil {
287                 cluster.API.WebsocketServerEventQueue = *oc.ServerEventQueue
288         }
289         if oc.ManagementToken != nil {
290                 cluster.ManagementToken = *oc.ManagementToken
291         }
292
293         cfg.Clusters[cluster.ClusterID] = *cluster
294         return nil
295 }
296
297 type oldKeepProxyConfig struct {
298         Client          *arvados.Client
299         Listen          *string
300         DisableGet      *bool
301         DisablePut      *bool
302         DefaultReplicas *int
303         Timeout         *arvados.Duration
304         PIDFile         *string
305         Debug           *bool
306         ManagementToken *string
307 }
308
309 const defaultKeepproxyConfigPath = "/etc/arvados/keepproxy/keepproxy.yml"
310
311 func (ldr *Loader) loadOldKeepproxyConfig(cfg *arvados.Config) error {
312         if ldr.KeepproxyPath == "" {
313                 return nil
314         }
315         var oc oldKeepProxyConfig
316         err := ldr.loadOldConfigHelper("keepproxy", ldr.KeepproxyPath, &oc)
317         if os.IsNotExist(err) && ldr.KeepproxyPath == defaultKeepproxyConfigPath {
318                 return nil
319         } else if err != nil {
320                 return err
321         }
322
323         cluster, err := cfg.GetCluster("")
324         if err != nil {
325                 return err
326         }
327
328         loadOldClientConfig(cluster, oc.Client)
329
330         if oc.Listen != nil {
331                 cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: *oc.Listen, Path: "/"}] = arvados.ServiceInstance{}
332         }
333         if oc.DefaultReplicas != nil {
334                 cluster.Collections.DefaultReplication = *oc.DefaultReplicas
335         }
336         if oc.Timeout != nil {
337                 cluster.API.KeepServiceRequestTimeout = *oc.Timeout
338         }
339         if oc.Debug != nil {
340                 if *oc.Debug && cluster.SystemLogs.LogLevel != "debug" {
341                         cluster.SystemLogs.LogLevel = "debug"
342                 } else if !*oc.Debug && cluster.SystemLogs.LogLevel != "info" {
343                         cluster.SystemLogs.LogLevel = "info"
344                 }
345         }
346         if oc.ManagementToken != nil {
347                 cluster.ManagementToken = *oc.ManagementToken
348         }
349
350         // The following legacy options are no longer supported. If they are set to
351         // true or PIDFile has a value, error out and notify the user
352         unsupportedEntry := func(cfgEntry string) error {
353                 return fmt.Errorf("the keepproxy %s configuration option is no longer supported, please remove it from your configuration file", cfgEntry)
354         }
355         if oc.DisableGet != nil && *oc.DisableGet {
356                 return unsupportedEntry("DisableGet")
357         }
358         if oc.DisablePut != nil && *oc.DisablePut {
359                 return unsupportedEntry("DisablePut")
360         }
361         if oc.PIDFile != nil && *oc.PIDFile != "" {
362                 return unsupportedEntry("PIDFile")
363         }
364
365         cfg.Clusters[cluster.ClusterID] = *cluster
366         return nil
367 }
368
369 const defaultKeepWebConfigPath = "/etc/arvados/keep-web/keep-web.yml"
370
371 type oldKeepWebConfig struct {
372         Client *arvados.Client
373
374         Listen *string
375
376         AnonymousTokens    *[]string
377         AttachmentOnlyHost *string
378         TrustAllContent    *bool
379
380         Cache struct {
381                 TTL                  *arvados.Duration
382                 UUIDTTL              *arvados.Duration
383                 MaxCollectionEntries *int
384                 MaxCollectionBytes   *int64
385                 MaxPermissionEntries *int
386                 MaxUUIDEntries       *int
387         }
388
389         // Hack to support old command line flag, which is a bool
390         // meaning "get actual token from environment".
391         deprecatedAllowAnonymous *bool
392
393         // Authorization token to be included in all health check requests.
394         ManagementToken *string
395 }
396
397 func (ldr *Loader) loadOldKeepWebConfig(cfg *arvados.Config) error {
398         if ldr.KeepWebPath == "" {
399                 return nil
400         }
401         var oc oldKeepWebConfig
402         err := ldr.loadOldConfigHelper("keep-web", ldr.KeepWebPath, &oc)
403         if os.IsNotExist(err) && ldr.KeepWebPath == defaultKeepWebConfigPath {
404                 return nil
405         } else if err != nil {
406                 return err
407         }
408
409         cluster, err := cfg.GetCluster("")
410         if err != nil {
411                 return err
412         }
413
414         loadOldClientConfig(cluster, oc.Client)
415
416         if oc.Listen != nil {
417                 cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: *oc.Listen, Path: "/"}] = arvados.ServiceInstance{}
418                 cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: *oc.Listen, Path: "/"}] = arvados.ServiceInstance{}
419         }
420         if oc.AttachmentOnlyHost != nil {
421                 cluster.Services.WebDAVDownload.ExternalURL = arvados.URL{Host: *oc.AttachmentOnlyHost, Path: "/"}
422         }
423         if oc.ManagementToken != nil {
424                 cluster.ManagementToken = *oc.ManagementToken
425         }
426         if oc.TrustAllContent != nil {
427                 cluster.Collections.TrustAllContent = *oc.TrustAllContent
428         }
429         if oc.Cache.TTL != nil {
430                 cluster.Collections.WebDAVCache.TTL = *oc.Cache.TTL
431         }
432         if oc.Cache.UUIDTTL != nil {
433                 cluster.Collections.WebDAVCache.UUIDTTL = *oc.Cache.UUIDTTL
434         }
435         if oc.Cache.MaxCollectionEntries != nil {
436                 cluster.Collections.WebDAVCache.MaxCollectionEntries = *oc.Cache.MaxCollectionEntries
437         }
438         if oc.Cache.MaxCollectionBytes != nil {
439                 cluster.Collections.WebDAVCache.MaxCollectionBytes = *oc.Cache.MaxCollectionBytes
440         }
441         if oc.Cache.MaxPermissionEntries != nil {
442                 cluster.Collections.WebDAVCache.MaxPermissionEntries = *oc.Cache.MaxPermissionEntries
443         }
444         if oc.Cache.MaxUUIDEntries != nil {
445                 cluster.Collections.WebDAVCache.MaxUUIDEntries = *oc.Cache.MaxUUIDEntries
446         }
447         if oc.AnonymousTokens != nil {
448                 if len(*oc.AnonymousTokens) > 0 {
449                         cluster.Users.AnonymousUserToken = (*oc.AnonymousTokens)[0]
450                         if len(*oc.AnonymousTokens) > 1 {
451                                 ldr.Logger.Warn("More than 1 anonymous tokens configured, using only the first and discarding the rest.")
452                         }
453                 }
454         }
455
456         cfg.Clusters[cluster.ClusterID] = *cluster
457         return nil
458 }
459
460 const defaultGitHttpdConfigPath = "/etc/arvados/git-httpd/git-httpd.yml"
461
462 type oldGitHttpdConfig struct {
463         Client          *arvados.Client
464         Listen          *string
465         GitCommand      *string
466         GitoliteHome    *string
467         RepoRoot        *string
468         ManagementToken *string
469 }
470
471 func (ldr *Loader) loadOldGitHttpdConfig(cfg *arvados.Config) error {
472         if ldr.GitHttpdPath == "" {
473                 return nil
474         }
475         var oc oldGitHttpdConfig
476         err := ldr.loadOldConfigHelper("arv-git-httpd", ldr.GitHttpdPath, &oc)
477         if os.IsNotExist(err) && ldr.GitHttpdPath == defaultGitHttpdConfigPath {
478                 return nil
479         } else if err != nil {
480                 return err
481         }
482
483         cluster, err := cfg.GetCluster("")
484         if err != nil {
485                 return err
486         }
487
488         loadOldClientConfig(cluster, oc.Client)
489
490         if oc.Listen != nil {
491                 cluster.Services.GitHTTP.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
492         }
493         if oc.ManagementToken != nil {
494                 cluster.ManagementToken = *oc.ManagementToken
495         }
496         if oc.GitCommand != nil {
497                 cluster.Git.GitCommand = *oc.GitCommand
498         }
499         if oc.GitoliteHome != nil {
500                 cluster.Git.GitoliteHome = *oc.GitoliteHome
501         }
502         if oc.RepoRoot != nil {
503                 cluster.Git.Repositories = *oc.RepoRoot
504         }
505
506         cfg.Clusters[cluster.ClusterID] = *cluster
507         return nil
508 }
509
510 const defaultKeepBalanceConfigPath = "/etc/arvados/keep-balance/keep-balance.yml"
511
512 type oldKeepBalanceConfig struct {
513         Client              *arvados.Client
514         Listen              *string
515         KeepServiceTypes    *[]string
516         KeepServiceList     *arvados.KeepServiceList
517         RunPeriod           *arvados.Duration
518         CollectionBatchSize *int
519         CollectionBuffers   *int
520         RequestTimeout      *arvados.Duration
521         LostBlocksFile      *string
522         ManagementToken     *string
523 }
524
525 func (ldr *Loader) loadOldKeepBalanceConfig(cfg *arvados.Config) error {
526         if ldr.KeepBalancePath == "" {
527                 return nil
528         }
529         var oc oldKeepBalanceConfig
530         err := ldr.loadOldConfigHelper("keep-balance", ldr.KeepBalancePath, &oc)
531         if os.IsNotExist(err) && ldr.KeepBalancePath == defaultKeepBalanceConfigPath {
532                 return nil
533         } else if err != nil {
534                 return err
535         }
536
537         cluster, err := cfg.GetCluster("")
538         if err != nil {
539                 return err
540         }
541
542         loadOldClientConfig(cluster, oc.Client)
543
544         if oc.Listen != nil {
545                 cluster.Services.Keepbalance.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
546         }
547         if oc.ManagementToken != nil {
548                 cluster.ManagementToken = *oc.ManagementToken
549         }
550         if oc.RunPeriod != nil {
551                 cluster.Collections.BalancePeriod = *oc.RunPeriod
552         }
553         if oc.LostBlocksFile != nil {
554                 cluster.Collections.BlobMissingReport = *oc.LostBlocksFile
555         }
556         if oc.CollectionBatchSize != nil {
557                 cluster.Collections.BalanceCollectionBatch = *oc.CollectionBatchSize
558         }
559         if oc.CollectionBuffers != nil {
560                 cluster.Collections.BalanceCollectionBuffers = *oc.CollectionBuffers
561         }
562         if oc.RequestTimeout != nil {
563                 cluster.API.KeepServiceRequestTimeout = *oc.RequestTimeout
564         }
565
566         msg := "The %s configuration option is no longer supported. Please remove it from your configuration file. See the keep-balance upgrade notes at https://doc.arvados.org/admin/upgrading.html for more details."
567
568         // If the keep service type provided is "disk" silently ignore it, since
569         // this is what ends up being done anyway.
570         if oc.KeepServiceTypes != nil {
571                 numTypes := len(*oc.KeepServiceTypes)
572                 if numTypes != 0 && !(numTypes == 1 && (*oc.KeepServiceTypes)[0] == "disk") {
573                         return fmt.Errorf(msg, "KeepServiceTypes")
574                 }
575         }
576
577         if oc.KeepServiceList != nil {
578                 return fmt.Errorf(msg, "KeepServiceList")
579         }
580
581         cfg.Clusters[cluster.ClusterID] = *cluster
582         return nil
583 }
584
585 func (ldr *Loader) loadOldEnvironmentVariables(cfg *arvados.Config) error {
586         if os.Getenv("ARVADOS_API_TOKEN") == "" && os.Getenv("ARVADOS_API_HOST") == "" {
587                 return nil
588         }
589         cluster, err := cfg.GetCluster("")
590         if err != nil {
591                 return err
592         }
593         if tok := os.Getenv("ARVADOS_API_TOKEN"); tok != "" && cluster.SystemRootToken == "" {
594                 ldr.Logger.Warn("SystemRootToken missing from cluster config, falling back to ARVADOS_API_TOKEN environment variable")
595                 cluster.SystemRootToken = tok
596         }
597         if apihost := os.Getenv("ARVADOS_API_HOST"); apihost != "" && cluster.Services.Controller.ExternalURL.Host == "" {
598                 ldr.Logger.Warn("Services.Controller.ExternalURL missing from cluster config, falling back to ARVADOS_API_HOST(_INSECURE) environment variables")
599                 u, err := url.Parse("https://" + apihost)
600                 if err != nil {
601                         return fmt.Errorf("cannot parse ARVADOS_API_HOST: %s", err)
602                 }
603                 cluster.Services.Controller.ExternalURL = arvados.URL(*u)
604                 if i := os.Getenv("ARVADOS_API_HOST_INSECURE"); i != "" && i != "0" {
605                         cluster.TLS.Insecure = true
606                 }
607         }
608         cfg.Clusters[cluster.ClusterID] = *cluster
609         return nil
610 }