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.
- checksums.yaml +4 -4
- data/.standard.yml +8 -0
- data/AGENTS.md +6 -1
- data/Gemfile +3 -2
- data/Gemfile.lock +46 -1
- data/README.md +21 -1
- data/Rakefile +4 -3
- data/bin/readsnaps +2 -2
- data/bin/zfsmgr +31 -27
- data/lib/zfs_mgmt/config.rb +283 -151
- data/lib/zfs_mgmt/restic.rb +89 -93
- data/lib/zfs_mgmt/version.rb +1 -1
- data/lib/zfs_mgmt/zfs.rb +128 -128
- data/lib/zfs_mgmt/zfs_mgr/config.rb +22 -4
- data/lib/zfs_mgmt/zfs_mgr/list.rb +26 -26
- data/lib/zfs_mgmt/zfs_mgr/recv.rb +3 -3
- data/lib/zfs_mgmt/zfs_mgr/restic.rb +21 -21
- data/lib/zfs_mgmt/zfs_mgr/send.rb +5 -5
- data/lib/zfs_mgmt/zfs_mgr/snapshot.rb +14 -14
- data/lib/zfs_mgmt/zfs_mgr/state.rb +6 -6
- data/lib/zfs_mgmt/zfs_mgr/transfer_options.rb +25 -25
- data/lib/zfs_mgmt/zfs_mgr.rb +0 -1
- data/lib/zfs_mgmt.rb +864 -567
- data/zfs_mgmt.gemspec +12 -14
- metadata +2 -1
data/lib/zfs_mgmt/config.rb
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
require
|
|
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 =
|
|
17
|
-
JSON_SCHEMA_URL =
|
|
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
|
|
21
|
-
description
|
|
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
|
-
|
|
23
|
+
description: "ZFS properties to exclude on zfs recv (-x)"
|
|
25
24
|
property :overrides, T::Hash[String, String], optional: true,
|
|
26
|
-
|
|
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
|
-
|
|
27
|
+
description: "SSH options passed via -o option=value (merged with BatchMode=yes default)"
|
|
29
28
|
property :mbuffer, T::Boolean, optional: true,
|
|
30
|
-
|
|
29
|
+
description: "Whether to insert mbuffer between zfs send and zfs recv (defaults to true)"
|
|
31
30
|
property :mbuffer_size, String, optional: true,
|
|
32
|
-
|
|
31
|
+
description: "Buffer size passed to mbuffer -m option (e.g. 512M, 1G)"
|
|
33
32
|
property :hold, T::Boolean, optional: true,
|
|
34
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
52
|
+
ZfsMgmt.logger&.warn("unknown config key '#{k}'")
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
super_attrs = {}
|
|
56
|
-
super_attrs[:excludes] = raw_attrs.key?(
|
|
57
|
-
if raw_attrs.key?(
|
|
58
|
-
raw_overrides = raw_attrs[
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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?(
|
|
69
|
-
raw_ssh_config = raw_attrs[
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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[
|
|
80
|
-
super_attrs[:mbuffer_size] = raw_attrs[
|
|
81
|
-
super_attrs[:hold] = raw_attrs[
|
|
82
|
-
super_attrs[:hold_tag] = raw_attrs[
|
|
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?(
|
|
85
|
-
raw_zfs_input = raw_attrs[
|
|
86
|
-
if raw_zfs_input.is_a?(Hash)
|
|
87
|
-
|
|
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
|
-
|
|
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
|
|
108
|
+
next unless raw_entry&.key?("excludes")
|
|
109
109
|
|
|
110
|
-
val = raw_entry[
|
|
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,
|
|
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,
|
|
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 ?
|
|
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 ==
|
|
147
|
-
result[
|
|
223
|
+
if k == "type"
|
|
224
|
+
result["type"] = v.to_s if ancestor == dataset_name
|
|
148
225
|
next
|
|
149
226
|
end
|
|
150
|
-
next if k ==
|
|
227
|
+
next if k == "name"
|
|
151
228
|
|
|
152
|
-
if k ==
|
|
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 ==
|
|
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 ==
|
|
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 ==
|
|
253
|
+
if k == "snapshot_recursive"
|
|
177
254
|
if v == true
|
|
178
|
-
result[
|
|
179
|
-
result[
|
|
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 ==
|
|
261
|
+
if k == "destination"
|
|
185
262
|
if v.is_a?(String)
|
|
186
|
-
result[
|
|
187
|
-
result[
|
|
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[
|
|
190
|
-
result[
|
|
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 ==
|
|
272
|
+
if k == "source"
|
|
196
273
|
if v.is_a?(String)
|
|
197
|
-
result[
|
|
198
|
-
result[
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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[
|
|
222
|
-
result[
|
|
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[
|
|
227
|
-
result[
|
|
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[
|
|
232
|
-
result[
|
|
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(
|
|
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
|
-
|
|
250
|
-
|
|
326
|
+
"$schema" => JSON_SCHEMA_DRAFT,
|
|
327
|
+
"$id" => JSON_SCHEMA_URL
|
|
251
328
|
}.merge(base)
|
|
252
329
|
|
|
253
|
-
zfs_schema = schema.dig(
|
|
330
|
+
zfs_schema = schema.dig("properties", "zfs", "additionalProperties")
|
|
254
331
|
if zfs_schema.is_a?(Hash)
|
|
255
|
-
if zfs_schema[
|
|
256
|
-
zfs_schema[
|
|
257
|
-
zfs_schema.delete(
|
|
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[
|
|
337
|
+
zfs_props = zfs_schema["properties"]
|
|
261
338
|
if zfs_props.is_a?(Hash)
|
|
262
|
-
zfs_props[
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
{
|
|
266
|
-
{
|
|
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
|
-
|
|
345
|
+
"default" => false
|
|
269
346
|
}
|
|
270
|
-
zfs_props[
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
{
|
|
274
|
-
{
|
|
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
|
-
|
|
353
|
+
"default" => false
|
|
277
354
|
}
|
|
278
|
-
zfs_props[
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
{
|
|
282
|
-
{
|
|
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[
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
{
|
|
289
|
-
{
|
|
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
|
-
|
|
368
|
+
"default" => false
|
|
292
369
|
}
|
|
293
370
|
end
|
|
294
371
|
|
|
295
|
-
zfs_schema[
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
372
|
+
zfs_schema["not"] = {
|
|
373
|
+
"properties" => {
|
|
374
|
+
"hold" => {"const" => true},
|
|
375
|
+
"send_holds" => {"const" => true}
|
|
299
376
|
},
|
|
300
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
408
|
+
config = new(merged, file_sources: file_sources)
|
|
330
409
|
unless config.valid?
|
|
331
|
-
msg = "invalid configuration: #{config.errors.full_messages.join(
|
|
332
|
-
|
|
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,
|
|
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
|
-
{
|
|
426
|
+
{path: path, valid: true, error: nil}
|
|
348
427
|
rescue ConfigError => e
|
|
349
|
-
{
|
|
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:
|
|
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[
|
|
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,
|
|
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[
|
|
383
|
-
segments = pointer.sub(/\A\//,
|
|
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[
|
|
517
|
+
message = err["error"] || JSONSchemer::Errors.pretty(err)
|
|
386
518
|
|
|
387
|
-
if top_key ==
|
|
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?(
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
|
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
|
-
|
|
599
|
+
ZfsMgmt.logger&.warn("unknown config property '#{k_str}' on #{ds_name}")
|
|
468
600
|
next
|
|
469
601
|
end
|
|
470
|
-
if (k_str ==
|
|
471
|
-
|
|
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
|
-
|
|
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
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
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[
|
|
491
|
-
raw[
|
|
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
|