87bfd93fcad3ac72773ba2fcb0b199a519d68c33
[arvados.git] / services / api / lib / config_loader.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 module Psych
6   module Visitors
7     class YAMLTree < Psych::Visitors::Visitor
8       def visit_ActiveSupport_Duration o
9         seconds = o.to_i
10         outstr = ""
11         if seconds / 3600 > 0
12           outstr += "#{seconds / 3600}h"
13           seconds = seconds % 3600
14         end
15         if seconds / 60 > 0
16           outstr += "#{seconds / 60}m"
17           seconds = seconds % 60
18         end
19         if seconds > 0
20           outstr += "#{seconds}s"
21         end
22         if outstr == ""
23           outstr = "0s"
24         end
25         @emitter.scalar outstr, nil, nil, true, false, Nodes::Scalar::ANY
26       end
27
28       def visit_URI_Generic o
29         @emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
30       end
31
32       def visit_URI_HTTP o
33         @emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
34       end
35
36       def visit_Pathname o
37         @emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
38       end
39     end
40   end
41 end
42
43
44 module Boolean; end
45 class TrueClass; include Boolean; end
46 class FalseClass; include Boolean; end
47
48 class NonemptyString < String
49 end
50
51 class ConfigLoader
52   def initialize
53     @config_migrate_map = {}
54     @config_types = {}
55   end
56
57   def declare_config(assign_to, configtype, migrate_from=nil, migrate_fn=nil)
58     if migrate_from
59       @config_migrate_map[migrate_from] = migrate_fn || ->(cfg, k, v) {
60         ConfigLoader.set_cfg cfg, assign_to, v
61       }
62     end
63     @config_types[assign_to] = configtype
64   end
65
66
67   def migrate_config from_config, to_config
68     remainders = {}
69     from_config.each do |k, v|
70       if @config_migrate_map[k.to_sym]
71         @config_migrate_map[k.to_sym].call to_config, k, v
72       else
73         remainders[k] = v
74       end
75     end
76     remainders
77   end
78
79   def coercion_and_check check_cfg, check_nonempty: true
80     @config_types.each do |cfgkey, cfgtype|
81       cfg = check_cfg
82       k = cfgkey
83       ks = k.split '.'
84       k = ks.pop
85       ks.each do |kk|
86         cfg = cfg[kk]
87         if cfg.nil?
88           break
89         end
90       end
91
92       if cfg.nil?
93         raise "missing #{cfgkey}"
94       end
95
96       if cfgtype == String and !cfg[k]
97         cfg[k] = ""
98       end
99
100       if cfgtype == String and cfg[k].is_a? Symbol
101         cfg[k] = cfg[k].to_s
102       end
103
104       if cfgtype == Pathname and cfg[k].is_a? String
105
106         if cfg[k] == ""
107           cfg[k] = Pathname.new("")
108         else
109           cfg[k] = Pathname.new(cfg[k])
110           if !cfg[k].exist?
111             raise "#{cfgkey} path #{cfg[k]} does not exist"
112           end
113         end
114       end
115
116       if cfgtype == NonemptyString
117         if (!cfg[k] || cfg[k] == "") && check_nonempty
118           raise "#{cfgkey} cannot be empty"
119         end
120         if cfg[k].is_a? String
121           next
122         end
123       end
124
125       if cfgtype == ActiveSupport::Duration
126         if cfg[k].is_a? Integer
127           cfg[k] = cfg[k].seconds
128         elsif cfg[k].is_a? String
129           cfg[k] = ConfigLoader.parse_duration cfg[k]
130         end
131       end
132
133       if cfgtype == URI
134         cfg[k] = URI(cfg[k])
135       end
136
137       if !cfg[k].is_a? cfgtype
138         raise "#{cfgkey} expected #{cfgtype} but was #{cfg[k].class}"
139       end
140     end
141   end
142
143   def self.set_cfg cfg, k, v
144     # "foo.bar = baz" --> { cfg["foo"]["bar"] = baz }
145     ks = k.split '.'
146     k = ks.pop
147     ks.each do |kk|
148       cfg = cfg[kk]
149       if cfg.nil?
150         break
151       end
152     end
153     if !cfg.nil?
154       cfg[k] = v
155     end
156   end
157
158   def self.parse_duration durstr
159     duration_re = /(\d+(\.\d+)?)(s|m|h)/
160     dursec = 0
161     while durstr != ""
162       mt = duration_re.match durstr
163       if !mt
164         raise "#{cfgkey} not a valid duration: '#{cfg[k]}', accepted suffixes are s, m, h"
165       end
166       multiplier = {s: 1, m: 60, h: 3600}
167       dursec += (Float(mt[1]) * multiplier[mt[3].to_sym])
168       durstr = durstr[mt[0].length..-1]
169     end
170     return dursec.seconds
171   end
172
173   def self.copy_into_config src, dst
174     src.each do |k, v|
175       dst.send "#{k}=", self.to_OrderedOptions(v)
176     end
177   end
178
179   def self.to_OrderedOptions confs
180     if confs.is_a? Hash
181       opts = ActiveSupport::OrderedOptions.new
182       confs.each do |k,v|
183         opts[k] = self.to_OrderedOptions(v)
184       end
185       opts
186     elsif confs.is_a? Array
187       confs.map { |v| self.to_OrderedOptions v }
188     else
189       confs
190     end
191   end
192
193   def self.load path
194     yaml = ERB.new(IO.read path).result(binding)
195     YAML.load(yaml, deserialize_symbols: false)
196   end
197
198 end