zfs_mgmt 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,9 @@
1
- # coding: utf-8
2
- require 'easy_talk'
3
- require 'json_schemer'
4
- require 'tomlrb'
5
- require 'deep_merge/core'
6
- require 'json'
7
- require 'zfs_mgmt/zfs'
1
+ require "easy_talk"
2
+ require "json_schemer"
3
+ require "tomlrb"
4
+ require "deep_merge/core"
5
+ require "json"
6
+ require "zfs_mgmt/zfs"
8
7
 
9
8
  module ZfsMgmt
10
9
  class ConfigError < StandardError; end
@@ -13,80 +12,81 @@ module ZfsMgmt
13
12
  include EasyTalk::Model
14
13
 
15
14
  TOP_LEVEL_KEYS = %w[excludes overrides ssh_config mbuffer mbuffer_size hold hold_tag zfs].freeze
16
- JSON_SCHEMA_DRAFT = 'http://json-schema.org/draft-07/schema#'
17
- JSON_SCHEMA_URL = 'https://raw.githubusercontent.com/aranc23/zfs_mgmt/master/schema/config.schema.json'
15
+ JSON_SCHEMA_DRAFT = "http://json-schema.org/draft-07/schema#"
16
+ JSON_SCHEMA_URL = "https://raw.githubusercontent.com/aranc23/zfs_mgmt/master/schema/config.schema.json"
18
17
 
19
18
  define_schema do
20
- title 'Config'
21
- description 'Top-level zfsmgr configuration loaded from TOML files'
19
+ title "Config"
20
+ description "Top-level zfsmgr configuration loaded from TOML files"
22
21
 
23
22
  property :excludes, T::Array[String], optional: true,
24
- description: 'ZFS properties to exclude on zfs recv (-x)'
23
+ description: "ZFS properties to exclude on zfs recv (-x)"
25
24
  property :overrides, T::Hash[String, String], optional: true,
26
- description: 'ZFS property overrides to set on zfs recv (-o property=value)'
25
+ description: "ZFS property overrides to set on zfs recv (-o property=value)"
27
26
  property :ssh_config, T::Hash[String, String], optional: true,
28
- description: 'SSH options passed via -o option=value (merged with BatchMode=yes default)'
27
+ description: "SSH options passed via -o option=value (merged with BatchMode=yes default)"
29
28
  property :mbuffer, T::Boolean, optional: true,
30
- description: 'Whether to insert mbuffer between zfs send and zfs recv (defaults to true)'
29
+ description: "Whether to insert mbuffer between zfs send and zfs recv (defaults to true)"
31
30
  property :mbuffer_size, String, optional: true,
32
- description: 'Buffer size passed to mbuffer -m option (e.g. 512M, 1G)'
31
+ description: "Buffer size passed to mbuffer -m option (e.g. 512M, 1G)"
33
32
  property :hold, T::Boolean, optional: true,
34
- description: 'Whether to automatically manage ZFS snapshot holds during zfs send/recv (defaults to false)'
33
+ description: "Whether to automatically manage ZFS snapshot holds during zfs send/recv (defaults to false)"
35
34
  property :hold_tag, String, optional: true, pattern: '\A\S+\z',
36
- description: 'ZFS hold tag prefix used for automatic snapshot hold management (defaults to "zfsmgmt"; scoped per transfer leg as <hold_tag>:send:<host>:<dataset> or <hold_tag>:recv:<host>:<dataset>)'
35
+ description: 'ZFS hold tag prefix used for automatic snapshot hold management (defaults to "zfsmgmt"; scoped per transfer leg as <hold_tag>:send:<host>:<dataset> or <hold_tag>:recv:<host>:<dataset>)'
37
36
  property :zfs, T::Hash[String, ZfsMgmt::Zfs], optional: true,
38
- description: 'Per-dataset ZFS configuration keyed by dataset name'
37
+ description: "Per-dataset ZFS configuration keyed by dataset name"
39
38
  end
40
39
 
41
40
  validate :validate_with_json_schemer
42
41
  validate :validate_hold_mutual_exclusion
43
42
 
44
- def initialize(attributes = {})
43
+ def initialize(attributes = {}, file_sources: nil)
45
44
  @raw_zfs = {}
45
+ @file_sources = file_sources
46
46
  @raw_attributes = normalize_raw_attributes(attributes || {})
47
47
  raw_attrs = (attributes || {}).each_with_object({}) { |(k, v), h| h[k.to_s] = v }
48
48
 
49
49
  raw_attrs.each_key do |k|
50
50
  next if TOP_LEVEL_KEYS.include?(k)
51
51
 
52
- $logger&.warn("unknown config key '#{k}'")
52
+ ZfsMgmt.logger&.warn("unknown config key '#{k}'")
53
53
  end
54
54
 
55
55
  super_attrs = {}
56
- super_attrs[:excludes] = raw_attrs.key?('excludes') ? raw_attrs['excludes'] : []
57
- if raw_attrs.key?('overrides')
58
- raw_overrides = raw_attrs['overrides']
56
+ super_attrs[:excludes] = raw_attrs.key?("excludes") ? raw_attrs["excludes"] : []
57
+ if raw_attrs.key?("overrides")
58
+ raw_overrides = raw_attrs["overrides"]
59
59
  super_attrs[:overrides] = if raw_overrides.is_a?(Hash)
60
- raw_overrides.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
61
- else
62
- raw_overrides
63
- end
60
+ raw_overrides.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
61
+ else
62
+ raw_overrides
63
+ end
64
64
  else
65
65
  super_attrs[:overrides] = {}
66
66
  end
67
67
 
68
- if raw_attrs.key?('ssh_config')
69
- raw_ssh_config = raw_attrs['ssh_config']
68
+ if raw_attrs.key?("ssh_config")
69
+ raw_ssh_config = raw_attrs["ssh_config"]
70
70
  super_attrs[:ssh_config] = if raw_ssh_config.is_a?(Hash)
71
- raw_ssh_config.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
72
- else
73
- raw_ssh_config
74
- end
71
+ raw_ssh_config.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
72
+ else
73
+ raw_ssh_config
74
+ end
75
75
  else
76
76
  super_attrs[:ssh_config] = {}
77
77
  end
78
78
 
79
- super_attrs[:mbuffer] = raw_attrs['mbuffer'] if raw_attrs.key?('mbuffer')
80
- super_attrs[:mbuffer_size] = raw_attrs['mbuffer_size'] if raw_attrs.key?('mbuffer_size')
81
- super_attrs[:hold] = raw_attrs['hold'] if raw_attrs.key?('hold')
82
- super_attrs[:hold_tag] = raw_attrs['hold_tag'] if raw_attrs.key?('hold_tag')
79
+ super_attrs[:mbuffer] = raw_attrs["mbuffer"] if raw_attrs.key?("mbuffer")
80
+ super_attrs[:mbuffer_size] = raw_attrs["mbuffer_size"] if raw_attrs.key?("mbuffer_size")
81
+ super_attrs[:hold] = raw_attrs["hold"] if raw_attrs.key?("hold")
82
+ super_attrs[:hold_tag] = raw_attrs["hold_tag"] if raw_attrs.key?("hold_tag")
83
83
 
84
- if raw_attrs.key?('zfs')
85
- raw_zfs_input = raw_attrs['zfs']
86
- if raw_zfs_input.is_a?(Hash)
87
- super_attrs[:zfs] = normalize_zfs_entries(raw_zfs_input)
84
+ if raw_attrs.key?("zfs")
85
+ raw_zfs_input = raw_attrs["zfs"]
86
+ super_attrs[:zfs] = if raw_zfs_input.is_a?(Hash)
87
+ normalize_zfs_entries(raw_zfs_input)
88
88
  else
89
- super_attrs[:zfs] = raw_zfs_input
89
+ raw_zfs_input
90
90
  end
91
91
  else
92
92
  super_attrs[:zfs] = {}
@@ -105,9 +105,9 @@ module ZfsMgmt
105
105
  layers = []
106
106
  dataset_ancestors(dataset_name).each do |ancestor|
107
107
  raw_entry = @raw_zfs[ancestor]
108
- next unless raw_entry && raw_entry.key?('excludes')
108
+ next unless raw_entry&.key?("excludes")
109
109
 
110
- val = raw_entry['excludes']
110
+ val = raw_entry["excludes"]
111
111
  if val.is_a?(Array) && val.all? { |item| item.is_a?(String) }
112
112
  layers << val
113
113
  end
@@ -116,11 +116,88 @@ module ZfsMgmt
116
116
  end
117
117
 
118
118
  def zfs_overrides_layers_for(dataset_name)
119
- zfs_hash_layers_for(dataset_name, 'overrides')
119
+ zfs_hash_layers_for(dataset_name, "overrides")
120
120
  end
121
121
 
122
122
  def zfs_ssh_config_layers_for(dataset_name)
123
- zfs_hash_layers_for(dataset_name, 'ssh_config')
123
+ zfs_hash_layers_for(dataset_name, "ssh_config")
124
+ end
125
+
126
+ def top_level_origin_for(key)
127
+ key_str = key.to_s
128
+ return nil unless @raw_attributes.is_a?(Hash) && @raw_attributes.key?(key_str)
129
+
130
+ val = @raw_attributes[key_str]
131
+ if %w[excludes overrides ssh_config].include?(key_str)
132
+ return nil if val.nil? || (val.respond_to?(:empty?) && val.empty?)
133
+ elsif %w[mbuffer_size hold_tag].include?(key_str)
134
+ return nil if val.nil? || val.to_s.empty?
135
+ elsif val.nil?
136
+ return nil
137
+ end
138
+
139
+ src = @file_sources&.dig(:top_level, key_str)
140
+ if src.is_a?(Hash)
141
+ paths = src.values.uniq
142
+ paths.empty? ? "config" : paths.join(", ")
143
+ else
144
+ src || "config"
145
+ end
146
+ end
147
+
148
+ def zfs_prop_origin_for(dataset_name, prop_key)
149
+ key_str = prop_key.to_s
150
+ winner = nil
151
+
152
+ dataset_ancestors(dataset_name).each do |ancestor|
153
+ raw_entry = @raw_zfs[ancestor]
154
+ next unless raw_entry
155
+
156
+ matched_key = match_zfs_entry_key(raw_entry, ancestor, dataset_name, key_str)
157
+ next unless matched_key
158
+
159
+ origin = @file_sources&.dig(:zfs, ancestor, matched_key) || "config"
160
+ winner = {
161
+ source: (ancestor == dataset_name) ? "local" : "inherited from #{ancestor}",
162
+ origin: origin,
163
+ ancestor: ancestor
164
+ }
165
+ end
166
+
167
+ winner
168
+ end
169
+
170
+ def zfs_layer_sources_for(dataset_name, key)
171
+ key_str = key.to_s
172
+ layers = []
173
+
174
+ dataset_ancestors(dataset_name).each do |ancestor|
175
+ raw_entry = @raw_zfs[ancestor]
176
+ next unless raw_entry
177
+
178
+ val = raw_entry[key_str]
179
+ next if val.nil?
180
+ if key_str == "excludes"
181
+ next unless val.is_a?(Array) && !val.empty? && val.all? { |item| item.is_a?(String) }
182
+ else
183
+ next unless val.is_a?(Hash) && !val.empty? && val.all? { |ok, ov| ok.is_a?(String) && ov.is_a?(String) }
184
+ end
185
+
186
+ src_entry = @file_sources&.dig(:zfs, ancestor, key_str)
187
+ origin = if src_entry.is_a?(Hash)
188
+ paths = src_entry.values.uniq
189
+ paths.empty? ? "config" : paths.join(", ")
190
+ else
191
+ src_entry || "config"
192
+ end
193
+
194
+ layers << {
195
+ source: (ancestor == dataset_name) ? "local" : "inherited from #{ancestor}",
196
+ origin: origin
197
+ }
198
+ end
199
+
200
+ layers
124
201
  end
125
202
 
126
203
  def zfs_props_for(dataset_name)
@@ -139,17 +216,17 @@ module ZfsMgmt
139
216
  raw_entry = @raw_zfs[ancestor]
140
217
  next unless raw_entry
141
218
 
142
- source = (ancestor == dataset_name ? 'local' : "inherited from #{ancestor}")
219
+ source = ((ancestor == dataset_name) ? "local" : "inherited from #{ancestor}")
143
220
  raw_entry.each do |k, v|
144
221
  next if v.nil?
145
222
 
146
- if k == 'type'
147
- result['type'] = v.to_s if ancestor == dataset_name
223
+ if k == "type"
224
+ result["type"] = v.to_s if ancestor == dataset_name
148
225
  next
149
226
  end
150
- next if k == 'name'
227
+ next if k == "name"
151
228
 
152
- if k == 'excludes'
229
+ if k == "excludes"
153
230
  if v.is_a?(Array) && v.all? { |item| item.is_a?(String) }
154
231
  excludes_layers << v
155
232
  excludes_source = source
@@ -157,7 +234,7 @@ module ZfsMgmt
157
234
  next
158
235
  end
159
236
 
160
- if k == 'overrides'
237
+ if k == "overrides"
161
238
  if v.is_a?(Hash) && v.all? { |ok, ov| ok.is_a?(String) && ov.is_a?(String) }
162
239
  overrides_layers << v
163
240
  overrides_source = source
@@ -165,7 +242,7 @@ module ZfsMgmt
165
242
  next
166
243
  end
167
244
 
168
- if k == 'ssh_config'
245
+ if k == "ssh_config"
169
246
  if v.is_a?(Hash) && v.all? { |sk, sv| sk.is_a?(String) && sv.is_a?(String) }
170
247
  ssh_config_layers << v
171
248
  ssh_config_source = source
@@ -173,63 +250,63 @@ module ZfsMgmt
173
250
  next
174
251
  end
175
252
 
176
- if k == 'snapshot_recursive'
253
+ if k == "snapshot_recursive"
177
254
  if v == true
178
- result['zfsmgmt:snapshot'] = 'recursive'
179
- result['zfsmgmt:snapshot@source'] = source
255
+ result["zfsmgmt:snapshot"] = "recursive"
256
+ result["zfsmgmt:snapshot@source"] = source
180
257
  end
181
258
  next
182
259
  end
183
260
 
184
- if k == 'destination'
261
+ if k == "destination"
185
262
  if v.is_a?(String)
186
- result['zfsmgmt:destination'] = v
187
- result['zfsmgmt:destination@source'] = source
263
+ result["zfsmgmt:destination"] = v
264
+ result["zfsmgmt:destination@source"] = source
188
265
  elsif v.is_a?(Array) && v.all? { |item| item.is_a?(String) }
189
- result['zfsmgmt:destination'] = v.dup
190
- result['zfsmgmt:destination@source'] = source
266
+ result["zfsmgmt:destination"] = v.dup
267
+ result["zfsmgmt:destination@source"] = source
191
268
  end
192
269
  next
193
270
  end
194
271
 
195
- if k == 'source'
272
+ if k == "source"
196
273
  if v.is_a?(String)
197
- result['zfsmgmt:source'] = v
198
- result['zfsmgmt:source@source'] = source
274
+ result["zfsmgmt:source"] = v
275
+ result["zfsmgmt:source@source"] = source
199
276
  end
200
277
  next
201
278
  end
202
279
 
203
280
  prop_key = case k
204
- when 'send_enabled' then 'send'
205
- when 'recv_enabled' then 'recv'
206
- else k
207
- end
281
+ when "send_enabled" then "send"
282
+ when "recv_enabled" then "recv"
283
+ else k
284
+ end
208
285
  next unless ZfsMgmt::Zfs::VALID_ZFS_PROPS.include?("zfsmgmt:#{prop_key}")
209
286
 
210
287
  str_val = case v
211
- when true then 'true'
212
- when false then 'false'
213
- else v.to_s
214
- end
288
+ when true then "true"
289
+ when false then "false"
290
+ else v.to_s
291
+ end
215
292
  result["zfsmgmt:#{prop_key}"] = str_val
216
293
  result["zfsmgmt:#{prop_key}@source"] = source
217
294
  end
218
295
  end
219
296
 
220
297
  unless excludes_layers.empty?
221
- result['zfsmgmt:excludes'] = ZfsMgmt.merge_excludes(*excludes_layers, strip_knockouts: false)
222
- result['zfsmgmt:excludes@source'] = excludes_source
298
+ result["zfsmgmt:excludes"] = ZfsMgmt.merge_excludes(*excludes_layers, strip_knockouts: false)
299
+ result["zfsmgmt:excludes@source"] = excludes_source
223
300
  end
224
301
 
225
302
  unless overrides_layers.empty?
226
- result['zfsmgmt:overrides'] = ZfsMgmt.merge_overrides(*overrides_layers, strip_knockouts: false)
227
- result['zfsmgmt:overrides@source'] = overrides_source
303
+ result["zfsmgmt:overrides"] = ZfsMgmt.merge_overrides(*overrides_layers, strip_knockouts: false)
304
+ result["zfsmgmt:overrides@source"] = overrides_source
228
305
  end
229
306
 
230
307
  unless ssh_config_layers.empty?
231
- result['zfsmgmt:ssh_config'] = ZfsMgmt.merge_ssh_config(*ssh_config_layers, strip_knockouts: false)
232
- result['zfsmgmt:ssh_config@source'] = ssh_config_source
308
+ result["zfsmgmt:ssh_config"] = ZfsMgmt.merge_ssh_config(*ssh_config_layers, strip_knockouts: false)
309
+ result["zfsmgmt:ssh_config@source"] = ssh_config_source
233
310
  end
234
311
 
235
312
  result
@@ -240,64 +317,64 @@ module ZfsMgmt
240
317
  end
241
318
 
242
319
  def self.schema_path
243
- File.expand_path('../../schema/config.schema.json', __dir__)
320
+ File.expand_path("../../schema/config.schema.json", __dir__)
244
321
  end
245
322
 
246
323
  def self.config_json_schema
247
324
  base = JSON.parse(JSON.generate(json_schema))
248
325
  schema = {
249
- '$schema' => JSON_SCHEMA_DRAFT,
250
- '$id' => JSON_SCHEMA_URL
326
+ "$schema" => JSON_SCHEMA_DRAFT,
327
+ "$id" => JSON_SCHEMA_URL
251
328
  }.merge(base)
252
329
 
253
- zfs_schema = schema.dig('properties', 'zfs', 'additionalProperties')
330
+ zfs_schema = schema.dig("properties", "zfs", "additionalProperties")
254
331
  if zfs_schema.is_a?(Hash)
255
- if zfs_schema['required'].is_a?(Array)
256
- zfs_schema['required'].delete('name')
257
- zfs_schema.delete('required') if zfs_schema['required'].empty?
332
+ if zfs_schema["required"].is_a?(Array)
333
+ zfs_schema["required"].delete("name")
334
+ zfs_schema.delete("required") if zfs_schema["required"].empty?
258
335
  end
259
336
 
260
- zfs_props = zfs_schema['properties']
337
+ zfs_props = zfs_schema["properties"]
261
338
  if zfs_props.is_a?(Hash)
262
- zfs_props['snapshot'] = {
263
- 'description' => 'Whether snapshots should be created for this dataset (true, false, "recursive", or "local")',
264
- 'oneOf' => [
265
- { 'type' => 'boolean' },
266
- { 'type' => 'string', 'enum' => %w[recursive local] }
339
+ zfs_props["snapshot"] = {
340
+ "description" => 'Whether snapshots should be created for this dataset (true, false, "recursive", or "local")',
341
+ "oneOf" => [
342
+ {"type" => "boolean"},
343
+ {"type" => "string", "enum" => %w[recursive local]}
267
344
  ],
268
- 'default' => false
345
+ "default" => false
269
346
  }
270
- zfs_props['send'] = {
271
- 'description' => 'Whether zfs send is enabled for this dataset (true, false, or "local")',
272
- 'oneOf' => [
273
- { 'type' => 'boolean' },
274
- { 'type' => 'string', 'enum' => %w[local] }
347
+ zfs_props["send"] = {
348
+ "description" => 'Whether zfs send is enabled for this dataset (true, false, or "local")',
349
+ "oneOf" => [
350
+ {"type" => "boolean"},
351
+ {"type" => "string", "enum" => %w[local]}
275
352
  ],
276
- 'default' => false
353
+ "default" => false
277
354
  }
278
- zfs_props['destination'] = {
279
- 'description' => 'Destination ZFS dataset path(s) for zfs send/recv, optionally prefixed with [user@]host: (e.g. backup/tank, user@host:backup/tank, or an array of destinations)',
280
- 'oneOf' => [
281
- { 'type' => 'string' },
282
- { 'type' => 'array', 'items' => { 'type' => 'string' } }
355
+ zfs_props["destination"] = {
356
+ "description" => "Destination ZFS dataset path(s) for zfs send/recv, optionally prefixed with [user@]host: (e.g. backup/tank, user@host:backup/tank, or an array of destinations)",
357
+ "oneOf" => [
358
+ {"type" => "string"},
359
+ {"type" => "array", "items" => {"type" => "string"}}
283
360
  ]
284
361
  }
285
- zfs_props['recv'] = {
286
- 'description' => 'Whether zfs recv is enabled for this dataset (true, false, or "local")',
287
- 'oneOf' => [
288
- { 'type' => 'boolean' },
289
- { 'type' => 'string', 'enum' => %w[local] }
362
+ zfs_props["recv"] = {
363
+ "description" => 'Whether zfs recv is enabled for this dataset (true, false, or "local")',
364
+ "oneOf" => [
365
+ {"type" => "boolean"},
366
+ {"type" => "string", "enum" => %w[local]}
290
367
  ],
291
- 'default' => false
368
+ "default" => false
292
369
  }
293
370
  end
294
371
 
295
- zfs_schema['not'] = {
296
- 'properties' => {
297
- 'hold' => { 'const' => true },
298
- 'send_holds' => { 'const' => true }
372
+ zfs_schema["not"] = {
373
+ "properties" => {
374
+ "hold" => {"const" => true},
375
+ "send_holds" => {"const" => true}
299
376
  },
300
- 'required' => %w[hold send_holds]
377
+ "required" => %w[hold send_holds]
301
378
  }
302
379
  end
303
380
 
@@ -306,30 +383,32 @@ module ZfsMgmt
306
383
 
307
384
  def self.load_files(paths)
308
385
  merged = {}
386
+ file_sources = {top_level: {}, zfs: {}}
309
387
  paths.sort.each do |path|
310
388
  begin
311
389
  parsed = Tomlrb.load_file(path)
312
390
  rescue Tomlrb::ParseError => e
313
- $logger&.error("failed to parse config file #{path}: #{e.message}")
391
+ ZfsMgmt.logger&.error("failed to parse config file #{path}: #{e.message}")
314
392
  raise ConfigError, "failed to parse config file #{path}: #{e.message}"
315
393
  rescue SystemCallError => e
316
- $logger&.error("failed to read config file #{path}: #{e.message}")
394
+ ZfsMgmt.logger&.error("failed to read config file #{path}: #{e.message}")
317
395
  raise ConfigError, "failed to read config file #{path}: #{e.message}"
318
396
  end
319
397
 
320
398
  next if parsed.nil?
321
399
  unless parsed.is_a?(Hash)
322
- $logger&.error("invalid config file #{path}: expected top-level table")
400
+ ZfsMgmt.logger&.error("invalid config file #{path}: expected top-level table")
323
401
  raise ConfigError, "invalid config file #{path}: expected top-level table"
324
402
  end
325
403
 
404
+ record_file_sources(file_sources, parsed, path)
326
405
  DeepMerge.deep_merge!(parsed, merged, overwrite_arrays: true)
327
406
  end
328
407
 
329
- config = from_hash(merged)
408
+ config = new(merged, file_sources: file_sources)
330
409
  unless config.valid?
331
- msg = "invalid configuration: #{config.errors.full_messages.join('; ')}"
332
- $logger&.error(msg)
410
+ msg = "invalid configuration: #{config.errors.full_messages.join("; ")}"
411
+ ZfsMgmt.logger&.error(msg)
333
412
  raise ConfigError, msg
334
413
  end
335
414
  config
@@ -338,27 +417,80 @@ module ZfsMgmt
338
417
  def self.load_dir(dir)
339
418
  return new if dir.nil? || !Dir.exist?(dir)
340
419
 
341
- files = Dir.glob(File.join(dir, '*.toml')).sort
420
+ files = Dir.glob(File.join(dir, "*.toml")).sort
342
421
  load_files(files)
343
422
  end
344
423
 
345
424
  def self.validate_file(path)
346
425
  load_files([path])
347
- { path: path, valid: true, error: nil }
426
+ {path: path, valid: true, error: nil}
348
427
  rescue ConfigError => e
349
- { path: path, valid: false, error: e.message }
428
+ {path: path, valid: false, error: e.message}
350
429
  end
351
430
 
352
431
  def self.json_schemer
353
- @json_schemer ||= JSONSchemer.schema(config_json_schema, regexp_resolver: 'ecma')
432
+ @json_schemer ||= JSONSchemer.schema(config_json_schema, regexp_resolver: "ecma")
354
433
  end
355
434
 
356
435
  def self.validate_schema(data)
357
- json_schemer.validate(data).map { |err| err['error'] || JSONSchemer::Errors.pretty(err) }
436
+ json_schemer.validate(data).map { |err| err["error"] || JSONSchemer::Errors.pretty(err) }
437
+ end
438
+
439
+ def self.record_file_sources(file_sources, parsed, path)
440
+ parsed.each do |k, v|
441
+ k_str = k.to_s
442
+ if k_str == "zfs" && v.is_a?(Hash)
443
+ v.each do |ds_name, ds_hash|
444
+ next unless ds_hash.is_a?(Hash)
445
+
446
+ ds_sources = (file_sources[:zfs][ds_name.to_s] ||= {})
447
+ ds_hash.each do |dk, dv|
448
+ dk_str = dk.to_s
449
+ if (dk_str == "overrides" || dk_str == "ssh_config") && dv.is_a?(Hash)
450
+ sub_sources = ds_sources[dk_str].is_a?(Hash) ? ds_sources[dk_str] : (ds_sources[dk_str] = {})
451
+ dv.each_key { |subk| sub_sources[subk.to_s] = path }
452
+ else
453
+ ds_sources[dk_str] = path
454
+ end
455
+ end
456
+ end
457
+ elsif (k_str == "overrides" || k_str == "ssh_config") && v.is_a?(Hash)
458
+ sub_sources = file_sources[:top_level][k_str].is_a?(Hash) ? file_sources[:top_level][k_str] : (file_sources[:top_level][k_str] = {})
459
+ v.each_key { |subk| sub_sources[subk.to_s] = path }
460
+ else
461
+ file_sources[:top_level][k_str] = path
462
+ end
463
+ end
358
464
  end
465
+ private_class_method :record_file_sources
359
466
 
360
467
  private
361
468
 
469
+ def match_zfs_entry_key(raw_entry, ancestor, dataset_name, key_str)
470
+ case key_str
471
+ when "type"
472
+ (ancestor == dataset_name && !raw_entry["type"].nil?) ? "type" : nil
473
+ when "send"
474
+ %w[send send_enabled].find { |k| !raw_entry[k].nil? }
475
+ when "recv"
476
+ %w[recv recv_enabled].find { |k| !raw_entry[k].nil? }
477
+ when "snapshot"
478
+ if !raw_entry["snapshot"].nil?
479
+ "snapshot"
480
+ elsif raw_entry["snapshot_recursive"] == true
481
+ "snapshot_recursive"
482
+ end
483
+ when "snapshot_recursive"
484
+ if !raw_entry["snapshot_recursive"].nil?
485
+ "snapshot_recursive"
486
+ elsif raw_entry["snapshot"] == "recursive"
487
+ "snapshot"
488
+ end
489
+ else
490
+ raw_entry[key_str].nil? ? nil : key_str
491
+ end
492
+ end
493
+
362
494
  def normalize_raw_attributes(value)
363
495
  case value
364
496
  when Hash
@@ -374,17 +506,17 @@ module ZfsMgmt
374
506
 
375
507
  def validate_with_json_schemer
376
508
  unless @raw_attributes.is_a?(Hash)
377
- errors.add(:base, 'expected top-level table')
509
+ errors.add(:base, "expected top-level table")
378
510
  return
379
511
  end
380
512
 
381
513
  self.class.json_schemer.validate(@raw_attributes).each do |err|
382
- pointer = err['data_pointer'].to_s
383
- segments = pointer.sub(/\A\//, '').split('/').map { |s| s.gsub('~1', '/').gsub('~0', '~') }
514
+ pointer = err["data_pointer"].to_s
515
+ segments = pointer.sub(/\A\//, "").split("/").map { |s| s.gsub("~1", "/").gsub("~0", "~") }
384
516
  top_key = segments.first
385
- message = err['error'] || JSONSchemer::Errors.pretty(err)
517
+ message = err["error"] || JSONSchemer::Errors.pretty(err)
386
518
 
387
- if top_key == 'zfs' && segments[1]
519
+ if top_key == "zfs" && segments[1]
388
520
  errors.add(:zfs, "#{segments[1]}: #{message}")
389
521
  elsif TOP_LEVEL_KEYS.include?(top_key)
390
522
  errors.add(top_key.to_sym, message)
@@ -401,13 +533,13 @@ module ZfsMgmt
401
533
  next unless model.is_a?(ZfsMgmt::Zfs)
402
534
 
403
535
  cfg_props = zfs_props_for(ds_name)
404
- effective_hold = if cfg_props.key?('zfsmgmt:hold')
405
- ZfsMgmt.prop_on?(cfg_props['zfsmgmt:hold'])
406
- elsif !hold.nil?
407
- hold == true
408
- else
409
- model.hold == true
410
- end
536
+ effective_hold = if cfg_props.key?("zfsmgmt:hold")
537
+ ZfsMgmt.prop_on?(cfg_props["zfsmgmt:hold"])
538
+ elsif !hold.nil?
539
+ hold == true
540
+ else
541
+ model.hold == true
542
+ end
411
543
  if effective_hold && model.send_holds == true
412
544
  errors.add(:zfs, "#{ds_name}: hold and send_holds cannot both be enabled")
413
545
  end
@@ -418,7 +550,7 @@ module ZfsMgmt
418
550
  layers = []
419
551
  dataset_ancestors(dataset_name).each do |ancestor|
420
552
  raw_entry = @raw_zfs[ancestor]
421
- next unless raw_entry && raw_entry.key?(key)
553
+ next unless raw_entry&.key?(key)
422
554
 
423
555
  val = raw_entry[key]
424
556
  if val.is_a?(Hash) && val.all? { |k, v| k.is_a?(String) && v.is_a?(String) }
@@ -431,8 +563,8 @@ module ZfsMgmt
431
563
  def dataset_ancestors(dataset_name)
432
564
  return [] if dataset_name.nil? || dataset_name.empty? || @raw_zfs.empty?
433
565
 
434
- parts = dataset_name.to_s.split('/')
435
- (0...parts.length).map { |i| parts[0..i].join('/') }
566
+ parts = dataset_name.to_s.split("/")
567
+ (0...parts.length).map { |i| parts[0..i].join("/") }
436
568
  end
437
569
 
438
570
  def normalize_zfs_entries(raw_zfs_input)
@@ -464,13 +596,13 @@ module ZfsMgmt
464
596
  hash.each do |k, v|
465
597
  k_str = k.to_s
466
598
  unless ZfsMgmt::Zfs::VALID_CONFIG_KEYS.include?(k_str)
467
- $logger&.warn("unknown config property '#{k_str}' on #{ds_name}")
599
+ ZfsMgmt.logger&.warn("unknown config property '#{k_str}' on #{ds_name}")
468
600
  next
469
601
  end
470
- if (k_str == 'overrides' || k_str == 'ssh_config') && v.is_a?(Hash)
471
- filtered[k_str] = v.each_with_object({}) { |(ok, ov), oh| oh[ok.to_s] = ov }
602
+ filtered[k_str] = if (k_str == "overrides" || k_str == "ssh_config") && v.is_a?(Hash)
603
+ v.each_with_object({}) { |(ok, ov), oh| oh[ok.to_s] = ov }
472
604
  else
473
- filtered[k_str] = v
605
+ v
474
606
  end
475
607
  end
476
608
  filtered
@@ -481,14 +613,14 @@ module ZfsMgmt
481
613
  provided = model.provided_attributes || Set.new
482
614
  provided.each do |attr|
483
615
  key = case attr
484
- when :send_enabled then 'send'
485
- when :recv_enabled then 'recv'
486
- else attr.to_s
487
- end
616
+ when :send_enabled then "send"
617
+ when :recv_enabled then "recv"
618
+ else attr.to_s
619
+ end
488
620
  raw[key] = model.public_send(attr)
489
621
  end
490
- raw['destination'] = model.destination_base if model.destination_base
491
- raw['source'] = model.source_base if model.source_base
622
+ raw["destination"] = model.destination_base if model.destination_base
623
+ raw["source"] = model.source_base if model.source_base
492
624
  raw
493
625
  end
494
626
  end