zfs_mgmt 0.7.3 → 0.8.0

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.
data/lib/zfs_mgmt/zfs.rb CHANGED
@@ -1,10 +1,38 @@
1
1
  # coding: utf-8
2
2
  require 'easy_talk'
3
+ require 'set'
4
+
5
+ module ZfsMgmt
6
+ class TypedHashBuilder
7
+ def initialize(_name, type, constraints = {})
8
+ @type = type
9
+ @constraints = constraints || {}
10
+ end
11
+
12
+ def build
13
+ schema = { type: 'object' }
14
+ schema[:description] = @constraints[:description] if @constraints[:description]
15
+ schema[:default] = @constraints[:default] if @constraints.key?(:default)
16
+ value_type = @type.respond_to?(:values) ? @type.values : nil
17
+ value_type = value_type.raw_type if value_type.respond_to?(:raw_type)
18
+ if value_type
19
+ schema[:additionalProperties] = EasyTalk::Property.new(:_value, value_type, {}).build
20
+ end
21
+ schema
22
+ end
23
+ end
24
+ end
25
+
26
+ unless EasyTalk::Builders::Registry.registered?('T::Types::TypedHash')
27
+ EasyTalk::Builders::Registry.register('T::Types::TypedHash', ZfsMgmt::TypedHashBuilder, collection: true)
28
+ end
3
29
 
4
30
  module ZfsMgmt
5
31
  class Zfs
6
32
  include EasyTalk::Model
7
33
 
34
+ attr_reader :provided_attributes, :destination_base, :destination_explicit_remote
35
+
8
36
  SEND_BOOL_PROPS = %i[
9
37
  send_intermediary
10
38
  send_backup
@@ -30,12 +58,18 @@ module ZfsMgmt
30
58
  snapshot snap_prefix snap_timestamp
31
59
  send remote destination send_replicate
32
60
  recv_state
61
+ excludes overrides
33
62
  restic_backup restic_repository
34
63
  ] +
35
64
  SEND_BOOL_PROPS.map(&:to_s) +
36
65
  RECV_BOOL_PROPS.map(&:to_s)
37
66
  ).map { |p| "zfsmgmt:#{p}" }.freeze
38
67
 
68
+ VALID_CONFIG_KEYS = (
69
+ VALID_ZFS_PROPS.map { |p| p.sub(/\Azfsmgmt:/, '') } +
70
+ %w[name type snapshot_recursive send_enabled]
71
+ ).uniq.freeze
72
+
39
73
  define_schema do
40
74
  title 'Zfs'
41
75
  description 'Encapsulates zfsmgmt:* user properties for a ZFS dataset'
@@ -75,9 +109,9 @@ module ZfsMgmt
75
109
  property :send_enabled, T::Boolean, optional: true, default: false, as: :send,
76
110
  description: 'Whether zfs send is enabled for this dataset'
77
111
  property :remote, String, optional: true,
78
- description: 'Remote SSH target for zfs recv (e.g. user@host)'
112
+ description: 'DEPRECATED: Remote SSH target for zfs recv (specify remote in destination instead, e.g. user@host:pool/dataset)'
79
113
  property :destination, String, optional: true,
80
- description: 'Resolved destination ZFS dataset path for zfs send/recv'
114
+ description: 'Destination ZFS dataset path(s) for zfs send/recv, optionally prefixed with [user@]host: (string or array of strings, e.g. backup/tank or ["backup/tank", "user@host:backup/tank"])'
81
115
 
82
116
  property :send_intermediary, T::Boolean, optional: true, default: false,
83
117
  description: 'Pass -I (intermediary snapshots) to zfs send'
@@ -110,6 +144,10 @@ module ZfsMgmt
110
144
  description: 'Pass -d (discard first path element) to zfs recv'
111
145
  property :recv_force, T::Boolean, optional: true, default: false,
112
146
  description: 'Pass -F (force rollback) to zfs recv'
147
+ property :excludes, T::Array[String], optional: true,
148
+ description: 'ZFS properties to exclude on zfs recv (-x)'
149
+ property :overrides, T::Hash[String, String], optional: true,
150
+ description: 'ZFS property overrides to set on zfs recv (-o property=value)'
113
151
 
114
152
  # Restic backup properties
115
153
  property :restic_backup, T::Boolean, optional: true, default: false,
@@ -118,17 +156,88 @@ module ZfsMgmt
118
156
  description: 'Target restic repository for backups'
119
157
  end
120
158
 
159
+ validate :validate_destination
160
+ validate :validate_excludes
161
+ validate :validate_overrides
162
+
121
163
  def initialize(attributes = {})
122
164
  attrs = attributes.transform_keys(&:to_sym)
123
165
  @snapshots = attrs.delete(:snapshots) if attrs.key?(:snapshots)
124
166
  @destination_relative_path = attrs.delete(:destination_relative_path) if attrs.key?(:destination_relative_path)
125
167
  @destination_base = attrs.delete(:destination_base) if attrs.key?(:destination_base)
168
+ @destination_entries = attrs.delete(:destination_entries) if attrs.key?(:destination_entries)
169
+ explicit_remote_provided = attrs.key?(:destination_explicit_remote)
170
+ @destination_explicit_remote = attrs.delete(:destination_explicit_remote) if explicit_remote_provided
171
+ @config_merged = attrs.key?(:config_merged) ? !!attrs.delete(:config_merged) : false
126
172
  if attrs.key?(:send) && !attrs.key?(:send_enabled)
127
173
  attrs[:send_enabled] = attrs.delete(:send)
128
174
  end
175
+ @provided_attributes = attrs.keys.to_set
176
+
177
+ raw_dest = attrs[:destination]
178
+ valid_dest_input = raw_dest.is_a?(String) || (raw_dest.is_a?(Array) && raw_dest.all? { |item| item.is_a?(String) })
179
+ if @destination_entries.nil? && valid_dest_input
180
+ parsed_dests = ZfsMgmt.parse_destinations(raw_dest)
181
+ @destination_entries = parsed_dests.map do |dest|
182
+ {
183
+ resolved: dest,
184
+ base: dest,
185
+ explicit_remote: ZfsMgmt.parse_destination(dest).first
186
+ }
187
+ end
188
+ end
189
+
190
+ unless explicit_remote_provided
191
+ @destination_explicit_remote = (@destination_entries && @destination_entries.length == 1) ? @destination_entries.first[:explicit_remote] : nil
192
+ end
193
+
194
+ if attrs.key?(:remote) && !attrs[:remote].nil? && !attrs[:remote].to_s.empty?
195
+ remote_val = attrs[:remote].to_s
196
+ ctx = attrs[:name].nil? || attrs[:name].to_s.empty? ? nil : attrs[:name].to_s
197
+ ZfsMgmt.warn_deprecated_remote(ctx)
198
+ if @destination_entries && !@destination_entries.empty?
199
+ @destination_entries = @destination_entries.map do |entry|
200
+ if entry[:explicit_remote].nil?
201
+ _, resolved_path = ZfsMgmt.parse_destination(entry[:resolved])
202
+ _, base_path = ZfsMgmt.parse_destination(entry[:base])
203
+ {
204
+ resolved: ZfsMgmt.format_destination(remote_val, resolved_path),
205
+ base: ZfsMgmt.format_destination(remote_val, base_path),
206
+ explicit_remote: nil
207
+ }
208
+ else
209
+ entry
210
+ end
211
+ end
212
+ resolved_list = @destination_entries.map { |e| e[:resolved] }
213
+ attrs[:destination] = (raw_dest.is_a?(Array) || resolved_list.length > 1) ? resolved_list : resolved_list.first
214
+ if @destination_base
215
+ base_list = @destination_entries.map { |e| e[:base] }
216
+ @destination_base = (raw_dest.is_a?(Array) || base_list.length > 1) ? base_list : base_list.first
217
+ end
218
+ end
219
+ end
220
+ attrs[:excludes] = [] unless attrs.key?(:excludes)
221
+ if attrs.key?(:overrides)
222
+ if attrs[:overrides].is_a?(Hash)
223
+ attrs[:overrides] = attrs[:overrides].each_with_object({}) { |(k, v), h| h[k.to_s] = v }
224
+ end
225
+ else
226
+ attrs[:overrides] = {}
227
+ end
129
228
  super(attrs)
130
229
  end
131
230
 
231
+ def config_merged?
232
+ @config_merged
233
+ end
234
+
235
+ def override_options
236
+ return [] unless overrides.is_a?(Hash)
237
+
238
+ overrides.reject { |_k, v| v == ZfsMgmt::KNOCKOUT_PREFIX }.map { |k, v| "#{k}=#{v}" }
239
+ end
240
+
132
241
  def send=(value)
133
242
  self.send_enabled = value
134
243
  end
@@ -164,11 +273,38 @@ module ZfsMgmt
164
273
  snapshots
165
274
  end
166
275
 
276
+ def destinations
277
+ return @destination_entries.map { |e| e[:resolved] } if @destination_entries
278
+
279
+ ZfsMgmt.parse_destinations(destination)
280
+ end
281
+
282
+ def destination_entries
283
+ return @destination_entries if @destination_entries
284
+
285
+ ZfsMgmt.parse_destinations(destination).map do |dest|
286
+ {
287
+ resolved: dest,
288
+ base: dest,
289
+ explicit_remote: ZfsMgmt.parse_destination(dest).first
290
+ }
291
+ end
292
+ end
293
+
294
+ def destination_remote
295
+ ZfsMgmt.parse_destination(destinations.first).first
296
+ end
297
+
298
+ def destination_path
299
+ ZfsMgmt.parse_destination(destinations.first).last
300
+ end
301
+
167
302
  def resolved_destination(override = nil)
168
303
  return destination unless override
169
304
 
305
+ ov_remote, ov_path = ZfsMgmt.parse_destination(override)
170
306
  rel = @destination_relative_path || File.basename(name)
171
- File.join(override, rel)
307
+ ZfsMgmt.format_destination(ov_remote, File.join(ov_path, rel))
172
308
  end
173
309
 
174
310
  def base_destination(override = nil)
@@ -177,12 +313,24 @@ module ZfsMgmt
177
313
  @destination_base || destination
178
314
  end
179
315
 
316
+ def discard_first_for(base)
317
+ return nil unless base
318
+
319
+ base_remote, base_path = ZfsMgmt.parse_destination(base)
320
+ rel = name.include?('/') ? name.split('/', 2).last : nil
321
+ joined = rel && !rel.empty? ? File.join(base_path, rel) : base_path
322
+ ZfsMgmt.format_destination(base_remote, joined)
323
+ end
324
+
180
325
  def discard_first_destination(override = nil)
181
326
  base = base_destination(override)
182
327
  return nil unless base
183
328
 
184
- rel = name.include?('/') ? name.split('/', 2).last : nil
185
- rel && !rel.empty? ? File.join(base, rel) : base
329
+ if base.is_a?(Array)
330
+ base.map { |b| discard_first_for(b) }
331
+ else
332
+ discard_first_for(base)
333
+ end
186
334
  end
187
335
 
188
336
  def self.warn_unknown_zfs_props(name, props)
@@ -197,10 +345,10 @@ module ZfsMgmt
197
345
  end
198
346
  end
199
347
 
200
- def self.from_zfs_props(name, props = {})
348
+ def self.from_zfs_props(name, props = {}, config_merged: false)
201
349
  props ||= {}
202
350
  warn_unknown_zfs_props(name, props)
203
- attrs = { name: name }
351
+ attrs = { name: name, config_merged: config_merged }
204
352
  attrs[:type] = props['type'] if props.key?('type')
205
353
 
206
354
  attrs[:manage] = ZfsMgmt.prop_on?(props['zfsmgmt:manage'])
@@ -252,18 +400,32 @@ module ZfsMgmt
252
400
 
253
401
  # Interpret zfsmgmt:destination taking @source into account
254
402
  if props.key?('zfsmgmt:destination')
255
- dest_base = props['zfsmgmt:destination']
403
+ raw_dest_prop = props['zfsmgmt:destination']
404
+ dest_bases = ZfsMgmt.parse_destinations(raw_dest_prop)
256
405
  dest_src = props['zfsmgmt:destination@source']
257
- if dest_src == 'local'
258
- rel = File.basename(name)
259
- attrs[:destination] = File.join(dest_base, rel)
406
+ prop_remote = props['zfsmgmt:remote']
407
+ prop_remote = nil if prop_remote && prop_remote.to_s.empty?
408
+ rel = if dest_src == 'local'
409
+ File.basename(name)
410
+ elsif dest_src && (m = /\Ainherited from (.+)\z/.match(dest_src))
411
+ File.join(File.basename(m[1]), name.sub(m[1], ''))
412
+ end
413
+ if rel && !dest_bases.empty?
414
+ entries = dest_bases.map do |dest_base|
415
+ explicit_remote, base_path = ZfsMgmt.parse_destination(dest_base)
416
+ effective_remote = explicit_remote || prop_remote
417
+ {
418
+ resolved: ZfsMgmt.format_destination(effective_remote, File.join(base_path, rel)),
419
+ base: ZfsMgmt.format_destination(effective_remote, base_path),
420
+ explicit_remote: explicit_remote
421
+ }
422
+ end
423
+ as_array = raw_dest_prop.is_a?(Array) || entries.length > 1
424
+ attrs[:destination] = as_array ? entries.map { |e| e[:resolved] } : entries.first[:resolved]
425
+ attrs[:destination_base] = as_array ? entries.map { |e| e[:base] } : entries.first[:base]
426
+ attrs[:destination_explicit_remote] = entries.length == 1 ? entries.first[:explicit_remote] : nil
427
+ attrs[:destination_entries] = entries
260
428
  attrs[:destination_relative_path] = rel
261
- attrs[:destination_base] = dest_base
262
- elsif dest_src && (m = /\Ainherited from (.+)\z/.match(dest_src))
263
- rel = File.join(File.basename(m[1]), name.sub(m[1], ''))
264
- attrs[:destination] = File.join(dest_base, rel)
265
- attrs[:destination_relative_path] = rel
266
- attrs[:destination_base] = dest_base
267
429
  end
268
430
  end
269
431
 
@@ -279,10 +441,58 @@ module ZfsMgmt
279
441
  attrs[prop] = ZfsMgmt.prop_on?(props[key]) if props.key?(key)
280
442
  end
281
443
 
444
+ if props.key?('zfsmgmt:excludes')
445
+ attrs[:excludes] = ZfsMgmt.parse_excludes(props['zfsmgmt:excludes'])
446
+ end
447
+
448
+ if props.key?('zfsmgmt:overrides')
449
+ ctx = (name.nil? || name.empty?) ? nil : name
450
+ attrs[:overrides] = ZfsMgmt.parse_overrides(props['zfsmgmt:overrides'], context: ctx)
451
+ end
452
+
282
453
  attrs[:restic_backup] = ZfsMgmt.prop_on?(props['zfsmgmt:restic_backup']) if props.key?('zfsmgmt:restic_backup')
283
454
  attrs[:restic_repository] = props['zfsmgmt:restic_repository'] if props.key?('zfsmgmt:restic_repository')
284
455
 
285
456
  new(attrs)
286
457
  end
458
+
459
+ private
460
+
461
+ def validate_destination
462
+ return if destination.nil?
463
+
464
+ valid = destination.is_a?(String) || (destination.is_a?(Array) && destination.all? { |item| item.is_a?(String) })
465
+ return if valid
466
+
467
+ errors.add(:destination, 'must be a string or an array of strings')
468
+ end
469
+
470
+ def validate_excludes
471
+ return if excludes.nil?
472
+
473
+ unless excludes.is_a?(Array) && excludes.all? { |item| item.is_a?(String) }
474
+ errors.add(:excludes, 'must be an array of strings')
475
+ end
476
+ end
477
+
478
+ def validate_overrides
479
+ return if overrides.nil?
480
+
481
+ if overrides.is_a?(Array)
482
+ errors.add(:overrides, "must be a hash of property=value pairs (use [zfs.\"#{name}\".overrides], not [[zfs.\"#{name}\".overrides]])")
483
+ return
484
+ end
485
+
486
+ unless overrides.is_a?(Hash)
487
+ errors.add(:overrides, 'must be a hash of property=value pairs')
488
+ return
489
+ end
490
+
491
+ overrides.each do |k, v|
492
+ unless k.is_a?(String) && v.is_a?(String)
493
+ errors.add(:overrides, "#{k}: property and value must be strings")
494
+ end
495
+ end
496
+ end
287
497
  end
288
498
  end
@@ -9,6 +9,7 @@ class ZfsMgmt::ZfsMgr::List < Thor
9
9
  def stale()
10
10
  ZfsMgmt.set_log_level(options[:loglevel])
11
11
  ZfsMgmt.global_options = options
12
+ ZfsMgmt.load_config
12
13
  cutoff = Time.at(Time.now.to_i - ZfsMgmt.timespec_to_seconds(options[:age]))
13
14
  table = Text::Table.new
14
15
  table.head = ['zfs','snapshot','age']
@@ -35,6 +36,7 @@ class ZfsMgmt::ZfsMgr::List < Thor
35
36
  def holds()
36
37
  ZfsMgmt.set_log_level(options[:loglevel])
37
38
  ZfsMgmt.global_options = options
39
+ ZfsMgmt.load_config
38
40
  table = Text::Table.new
39
41
  table.head = ['snapshot','userrefs','holds']
40
42
  table.rows = []
@@ -21,6 +21,7 @@ class ZfsMgmt::ZfsMgr::Backup < Thor
21
21
  def incremental()
22
22
  ZfsMgmt.set_log_level(options[:loglevel])
23
23
  ZfsMgmt.global_options = options
24
+ ZfsMgmt.load_config
24
25
  ZfsMgmt::Restic.backup(backup_level: options[:level], options: options)
25
26
  end
26
27
  desc "differential", "perform differential backup"
@@ -28,12 +29,14 @@ class ZfsMgmt::ZfsMgr::Backup < Thor
28
29
  def differential()
29
30
  ZfsMgmt.set_log_level(options[:loglevel])
30
31
  ZfsMgmt.global_options = options
32
+ ZfsMgmt.load_config
31
33
  ZfsMgmt::Restic.backup(backup_level: 1, options: options)
32
34
  end
33
35
  desc "full", "perform full backup"
34
36
  def full()
35
37
  ZfsMgmt.set_log_level(options[:loglevel])
36
38
  ZfsMgmt.global_options = options
39
+ ZfsMgmt.load_config
37
40
  ZfsMgmt::Restic.backup(backup_level: 0, options: options)
38
41
  end
39
42
  end
@@ -5,9 +5,7 @@ class ZfsMgmt::ZfsMgr::Send < Thor
5
5
  :desc => 'only act on zfs matching this regexp'
6
6
  desc "all", "send all zfs configured via user properties"
7
7
  method_option :remote, :type => :string,
8
- :desc => 'remote specification like root@otherhost or localhost'
9
- method_option :destination, :type => :string,
10
- :desc => 'destination path like otherpool/ourpool'
8
+ :desc => 'DEPRECATED: remote specification like root@otherhost or localhost (specify in destination property or config instead)'
11
9
  method_option :verbose, :type => :string, :aliases => :'-v', :enum => ['send','receive','recv','mbuffer','pv'],
12
10
  :desc => 'enable verbose output on the specified element of the pipe'
13
11
  method_option :initial_snapshot, :type => :string, :enum => ['oldest','newest'], :default => 'oldest',
@@ -26,8 +24,8 @@ class ZfsMgmt::ZfsMgr::Send < Thor
26
24
  method_option :noop, :aliases => :'-n', :desc => "pass -n (noop) option to zfs send", :type => :boolean
27
25
  method_option :unmount, :aliases => :'-u', :desc => "pass -u (unmount) option to zfs receive", :type => :boolean
28
26
  method_option :force, :aliases => :'-F', :desc => "pass -F (force) option to zfs receive", :type => :boolean
29
- method_option :exclude, :aliases => :'-x', :desc => "passed to -x option of receive side", :type => :string, repeatable: true
30
- method_option :option, :aliases => :'-o', :desc => "passed to -o option of receive side", :type => :string, repeatable: true
27
+ method_option :exclude, :aliases => :'-x', :desc => "passed to -x option of receive side (merged with config and zfsmgmt:excludes; prefix with ** to knock out)", :type => :string, repeatable: true
28
+ method_option :option, :aliases => :'-o', :desc => "passed to -o option of receive side (merged with config and zfsmgmt:overrides; use key=** or ** to knock out)", :type => :string, repeatable: true
31
29
  method_option :drop_holds, :desc => "pass the -h option to zfs recv, indicating holds should be ignored", :type => :boolean
32
30
  method_option :discard_first, :desc => "pass the -d option to zfs recv, discarding the first element of the send snapshot's file system name", :type => :boolean
33
31
  method_option :recv_state, :aliases => :'-s', :desc => "pass the -s option to zfs recv to save partial receive state for resuming (defaults to true unless disabled)", :type => :boolean
@@ -37,6 +35,7 @@ class ZfsMgmt::ZfsMgr::Send < Thor
37
35
  def all()
38
36
  ZfsMgmt.set_log_level(options[:loglevel])
39
37
  ZfsMgmt.global_options = options
38
+ ZfsMgmt.load_config
40
39
  lock = ZfsMgmt.lock(options)
41
40
 
42
41
  ZfsMgmt.zfs_send_all(options)
@@ -57,11 +56,15 @@ class ZfsMgmt::ZfsMgr::Send < Thor
57
56
  ZfsMgmt.send_state_show(check: options[:check], filter: options[:filter])
58
57
  end
59
58
 
60
- desc "delete ZFS", "delete a key from the state db"
61
- def delete(zfs)
59
+ desc "delete SOURCE [DESTINATION]", "delete a key (or all entries for a source) from the state db"
60
+ def delete(zfs, destination = nil)
62
61
  ZfsMgmt.set_log_level(options[:loglevel])
63
62
  ZfsMgmt.global_options = options
64
- ZfsMgmt.send_state_delete(zfs)
63
+ if destination
64
+ ZfsMgmt.send_state_delete(zfs, destination)
65
+ else
66
+ ZfsMgmt.send_state_delete(zfs)
67
+ end
65
68
  end
66
69
 
67
70
  desc "wipe", "remove all keys in state db"
@@ -11,6 +11,7 @@ class ZfsMgmt::ZfsMgr::Snapshot < Thor
11
11
  def destroy()
12
12
  ZfsMgmt.set_log_level(options[:loglevel])
13
13
  ZfsMgmt.global_options = options
14
+ ZfsMgmt.load_config
14
15
  lock = ZfsMgmt.lock(options)
15
16
  ZfsMgmt.snapshot_destroy(noop: options[:noop], verbose: options[:verbose], filter: options[:filter])
16
17
  ZfsMgmt.unlock(lock)
@@ -21,6 +22,7 @@ class ZfsMgmt::ZfsMgr::Snapshot < Thor
21
22
  def policy()
22
23
  ZfsMgmt.set_log_level(options[:loglevel])
23
24
  ZfsMgmt.global_options = options
25
+ ZfsMgmt.load_config
24
26
  ZfsMgmt.snapshot_policy(filter: options[:filter], show_destroy: options[:show_destroy])
25
27
  end
26
28
  desc "create", "execute zfs snapshot based on zfs properties"
@@ -29,6 +31,7 @@ class ZfsMgmt::ZfsMgr::Snapshot < Thor
29
31
  def create()
30
32
  ZfsMgmt.set_log_level(options[:loglevel])
31
33
  ZfsMgmt.global_options = options
34
+ ZfsMgmt.load_config
32
35
  lock = ZfsMgmt.lock(options)
33
36
  ZfsMgmt.snapshot_create(noop: options[:noop], filter: options[:filter])
34
37
  end