15467: Adding KeepServices WIP
[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.curoverse.com/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}] = arvados.ServiceInstance{}
104 }
105
106 const defaultKeepstoreConfigPath = "/etc/arvados/keepstore/keepstore.yml"
107
108 type oldKeepstoreConfig struct {
109         Debug *bool
110 }
111
112 func (ldr *Loader) loadOldConfigHelper(component, path string, target interface{}) error {
113         if path == "" {
114                 return nil
115         }
116         buf, err := ioutil.ReadFile(path)
117         if err != nil {
118                 return err
119         }
120
121         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)
122
123         err = yaml.Unmarshal(buf, target)
124         if err != nil {
125                 return fmt.Errorf("%s: %s", path, err)
126         }
127         return nil
128 }
129
130 // update config using values from an old-style keepstore config file.
131 func (ldr *Loader) loadOldKeepstoreConfig(cfg *arvados.Config) error {
132         if ldr.KeepstorePath == "" {
133                 return nil
134         }
135         var oc oldKeepstoreConfig
136         err := ldr.loadOldConfigHelper("keepstore", ldr.KeepstorePath, &oc)
137         if os.IsNotExist(err) && (ldr.KeepstorePath == defaultKeepstoreConfigPath) {
138                 return nil
139         } else if err != nil {
140                 return err
141         }
142
143         cluster, err := cfg.GetCluster("")
144         if err != nil {
145                 return err
146         }
147
148         if v := oc.Debug; v == nil {
149         } else if *v && cluster.SystemLogs.LogLevel != "debug" {
150                 cluster.SystemLogs.LogLevel = "debug"
151         } else if !*v && cluster.SystemLogs.LogLevel != "info" {
152                 cluster.SystemLogs.LogLevel = "info"
153         }
154
155         cfg.Clusters[cluster.ClusterID] = *cluster
156         return nil
157 }
158
159 type oldCrunchDispatchSlurmConfig struct {
160         Client *arvados.Client
161
162         SbatchArguments *[]string
163         PollPeriod      *arvados.Duration
164         PrioritySpread  *int64
165
166         // crunch-run command to invoke. The container UUID will be
167         // appended. If nil, []string{"crunch-run"} will be used.
168         //
169         // Example: []string{"crunch-run", "--cgroup-parent-subsystem=memory"}
170         CrunchRunCommand *[]string
171
172         // Extra RAM to reserve (in Bytes) for SLURM job, in addition
173         // to the amount specified in the container's RuntimeConstraints
174         ReserveExtraRAM *int64
175
176         // Minimum time between two attempts to run the same container
177         MinRetryPeriod *arvados.Duration
178
179         // Batch size for container queries
180         BatchSize *int64
181 }
182
183 const defaultCrunchDispatchSlurmConfigPath = "/etc/arvados/crunch-dispatch-slurm/crunch-dispatch-slurm.yml"
184
185 func loadOldClientConfig(cluster *arvados.Cluster, client *arvados.Client) {
186         if client == nil {
187                 return
188         }
189         if client.APIHost != "" {
190                 cluster.Services.Controller.ExternalURL.Host = client.APIHost
191         }
192         if client.Scheme != "" {
193                 cluster.Services.Controller.ExternalURL.Scheme = client.Scheme
194         } else {
195                 cluster.Services.Controller.ExternalURL.Scheme = "https"
196         }
197         if client.AuthToken != "" {
198                 cluster.SystemRootToken = client.AuthToken
199         }
200         cluster.TLS.Insecure = client.Insecure
201         for _, r := range client.KeepServiceURIs {
202                 if cluster.Containers.SLURM.KeepServices == nil {
203                         cluster.Containers.SLURM.KeepServices = make(map[string]arvados.Service)
204                 }
205                 if cluster.Containers.SLURM.KeepServices["00000-bi6l4-000000000000000"].InternalURLs == nil {
206                         cluster.Containers.SLURM.KeepServices["00000-bi6l4-000000000000000"].InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
207                 }
208                 p, err := url.Parse(r)
209                 cluster.Containers.SLURM.KeepServices["00000-bi6l4-000000000000000"].InternalURLs[arvados.URL(p)] = struct{}{}
210         }
211 }
212
213 // update config using values from an crunch-dispatch-slurm config file.
214 func (ldr *Loader) loadOldCrunchDispatchSlurmConfig(cfg *arvados.Config) error {
215         if ldr.CrunchDispatchSlurmPath == "" {
216                 return nil
217         }
218         var oc oldCrunchDispatchSlurmConfig
219         err := ldr.loadOldConfigHelper("crunch-dispatch-slurm", ldr.CrunchDispatchSlurmPath, &oc)
220         if os.IsNotExist(err) && (ldr.CrunchDispatchSlurmPath == defaultCrunchDispatchSlurmConfigPath) {
221                 return nil
222         } else if err != nil {
223                 return err
224         }
225
226         cluster, err := cfg.GetCluster("")
227         if err != nil {
228                 return err
229         }
230
231         loadOldClientConfig(cluster, oc.Client)
232
233         if oc.SbatchArguments != nil {
234                 cluster.Containers.SLURM.SbatchArgumentsList = *oc.SbatchArguments
235         }
236         if oc.PollPeriod != nil {
237                 cluster.Containers.CloudVMs.PollInterval = *oc.PollPeriod
238         }
239         if oc.PrioritySpread != nil {
240                 cluster.Containers.SLURM.PrioritySpread = *oc.PrioritySpread
241         }
242         if oc.CrunchRunCommand != nil {
243                 if len(*oc.CrunchRunCommand) >= 1 {
244                         cluster.Containers.CrunchRunCommand = (*oc.CrunchRunCommand)[0]
245                 }
246                 if len(*oc.CrunchRunCommand) >= 2 {
247                         cluster.Containers.CrunchRunArgumentsList = (*oc.CrunchRunCommand)[1:]
248                 }
249         }
250         if oc.ReserveExtraRAM != nil {
251                 cluster.Containers.ReserveExtraRAM = arvados.ByteSize(*oc.ReserveExtraRAM)
252         }
253         if oc.MinRetryPeriod != nil {
254                 cluster.Containers.MinRetryPeriod = *oc.MinRetryPeriod
255         }
256         if oc.BatchSize != nil {
257                 cluster.API.MaxItemsPerResponse = int(*oc.BatchSize)
258         }
259
260         cfg.Clusters[cluster.ClusterID] = *cluster
261         return nil
262 }
263
264 type oldWsConfig struct {
265         Client       *arvados.Client
266         Postgres     *arvados.PostgreSQLConnection
267         PostgresPool *int
268         Listen       *string
269         LogLevel     *string
270         LogFormat    *string
271
272         PingTimeout      *arvados.Duration
273         ClientEventQueue *int
274         ServerEventQueue *int
275
276         ManagementToken *string
277 }
278
279 const defaultWebsocketConfigPath = "/etc/arvados/ws/ws.yml"
280
281 // update config using values from an crunch-dispatch-slurm config file.
282 func (ldr *Loader) loadOldWebsocketConfig(cfg *arvados.Config) error {
283         if ldr.WebsocketPath == "" {
284                 return nil
285         }
286         var oc oldWsConfig
287         err := ldr.loadOldConfigHelper("arvados-ws", ldr.WebsocketPath, &oc)
288         if os.IsNotExist(err) && ldr.WebsocketPath == defaultWebsocketConfigPath {
289                 return nil
290         } else if err != nil {
291                 return err
292         }
293
294         cluster, err := cfg.GetCluster("")
295         if err != nil {
296                 return err
297         }
298
299         loadOldClientConfig(cluster, oc.Client)
300
301         if oc.Postgres != nil {
302                 cluster.PostgreSQL.Connection = *oc.Postgres
303         }
304         if oc.PostgresPool != nil {
305                 cluster.PostgreSQL.ConnectionPool = *oc.PostgresPool
306         }
307         if oc.Listen != nil {
308                 cluster.Services.Websocket.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
309         }
310         if oc.LogLevel != nil {
311                 cluster.SystemLogs.LogLevel = *oc.LogLevel
312         }
313         if oc.LogFormat != nil {
314                 cluster.SystemLogs.Format = *oc.LogFormat
315         }
316         if oc.PingTimeout != nil {
317                 cluster.API.SendTimeout = *oc.PingTimeout
318         }
319         if oc.ClientEventQueue != nil {
320                 cluster.API.WebsocketClientEventQueue = *oc.ClientEventQueue
321         }
322         if oc.ServerEventQueue != nil {
323                 cluster.API.WebsocketServerEventQueue = *oc.ServerEventQueue
324         }
325         if oc.ManagementToken != nil {
326                 cluster.ManagementToken = *oc.ManagementToken
327         }
328
329         cfg.Clusters[cluster.ClusterID] = *cluster
330         return nil
331 }