zfs_mgmt 0.7.2 → 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.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +10 -4
- data/README.md +137 -0
- data/Rakefile +18 -0
- data/bin/zfsmgr +3 -0
- data/examples/config.toml +105 -0
- data/lib/zfs_mgmt/config.rb +405 -0
- data/lib/zfs_mgmt/version.rb +1 -1
- data/lib/zfs_mgmt/zfs.rb +252 -17
- data/lib/zfs_mgmt/zfs_mgr/list.rb +2 -0
- data/lib/zfs_mgmt/zfs_mgr/restic.rb +3 -0
- data/lib/zfs_mgmt/zfs_mgr/send.rb +11 -8
- data/lib/zfs_mgmt/zfs_mgr/snapshot.rb +3 -0
- data/lib/zfs_mgmt.rb +356 -44
- data/schema/config.schema.json +246 -0
- data/zfs_mgmt.gemspec +2 -0
- metadata +32 -1
data/lib/zfs_mgmt.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
require "zfs_mgmt/version"
|
|
3
3
|
require "thor"
|
|
4
4
|
require "zfs_mgmt/zfs"
|
|
5
|
+
require "zfs_mgmt/config"
|
|
5
6
|
require "zfs_mgmt/restic"
|
|
6
7
|
require "zfs_mgmt/zfs_mgr"
|
|
7
8
|
require 'pp'
|
|
@@ -46,8 +47,232 @@ $properties_xlate = {
|
|
|
46
47
|
$lock = nil
|
|
47
48
|
|
|
48
49
|
module ZfsMgmt
|
|
50
|
+
KNOCKOUT_PREFIX = '**'.freeze
|
|
51
|
+
DESTINATION_REMOTE_RE = /\A((?:[^@:\/\s]+@)?[^:\/\s]+):(.+)\z/
|
|
52
|
+
|
|
49
53
|
class << self
|
|
50
|
-
attr_accessor :global_options
|
|
54
|
+
attr_accessor :global_options, :config
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.parse_destination(dest)
|
|
58
|
+
return [nil, nil] if dest.nil?
|
|
59
|
+
|
|
60
|
+
str = dest.to_s
|
|
61
|
+
if (m = DESTINATION_REMOTE_RE.match(str))
|
|
62
|
+
[m[1], m[2]]
|
|
63
|
+
else
|
|
64
|
+
[nil, str]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.format_destination(remote, path)
|
|
69
|
+
return path if remote.nil? || remote.to_s.empty? || path.nil? || path.to_s.empty?
|
|
70
|
+
|
|
71
|
+
"#{remote}:#{path}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.parse_destinations(value)
|
|
75
|
+
case value
|
|
76
|
+
when nil
|
|
77
|
+
[]
|
|
78
|
+
when String
|
|
79
|
+
value.split(',').map(&:strip).reject(&:empty?).uniq
|
|
80
|
+
when Array
|
|
81
|
+
value.flat_map { |v| v.is_a?(String) ? v.split(',').map(&:strip) : [] }.reject(&:empty?).uniq
|
|
82
|
+
else
|
|
83
|
+
[]
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.warn_deprecated_remote(context = nil)
|
|
88
|
+
suffix = context ? " (#{context})" : ''
|
|
89
|
+
$logger&.warn("remote option is deprecated#{suffix}; include the remote host in destination instead (e.g. user@host:pool/dataset)")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def self.parse_excludes(value)
|
|
93
|
+
case value
|
|
94
|
+
when nil
|
|
95
|
+
[]
|
|
96
|
+
when String
|
|
97
|
+
value.split(',').map(&:strip).reject(&:empty?)
|
|
98
|
+
when Array
|
|
99
|
+
value.map { |v| v.to_s.strip }.reject(&:empty?)
|
|
100
|
+
else
|
|
101
|
+
[]
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.parse_override_entry(entry, context: nil)
|
|
106
|
+
str = entry.to_s.strip
|
|
107
|
+
return nil if str.empty?
|
|
108
|
+
return [KNOCKOUT_PREFIX, KNOCKOUT_PREFIX] if str == KNOCKOUT_PREFIX
|
|
109
|
+
|
|
110
|
+
if str.include?('=')
|
|
111
|
+
k, v = str.split('=', 2)
|
|
112
|
+
k = k.strip
|
|
113
|
+
v = v.strip
|
|
114
|
+
if k.empty?
|
|
115
|
+
target = context ? " on #{context}" : ''
|
|
116
|
+
$logger&.warn("ignoring malformed override '#{str}'#{target}")
|
|
117
|
+
return nil
|
|
118
|
+
end
|
|
119
|
+
return [k, v]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
if str.start_with?(KNOCKOUT_PREFIX)
|
|
123
|
+
k = str[KNOCKOUT_PREFIX.length..-1].strip
|
|
124
|
+
return [KNOCKOUT_PREFIX, KNOCKOUT_PREFIX] if k.empty?
|
|
125
|
+
return [k, KNOCKOUT_PREFIX]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
target = context ? " on #{context}" : ''
|
|
129
|
+
$logger&.warn("ignoring malformed override '#{str}'#{target}")
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def self.parse_overrides(value, context: nil)
|
|
134
|
+
case value
|
|
135
|
+
when nil
|
|
136
|
+
{}
|
|
137
|
+
when Hash
|
|
138
|
+
value.each_with_object({}) do |(k, v), h|
|
|
139
|
+
key = k.to_s.strip
|
|
140
|
+
next if key.empty?
|
|
141
|
+
|
|
142
|
+
h[key] = v.to_s.strip
|
|
143
|
+
end
|
|
144
|
+
when String
|
|
145
|
+
result = {}
|
|
146
|
+
value.split(',').each do |part|
|
|
147
|
+
parsed = parse_override_entry(part, context: context)
|
|
148
|
+
next unless parsed
|
|
149
|
+
|
|
150
|
+
result[parsed[0]] = parsed[1]
|
|
151
|
+
end
|
|
152
|
+
result
|
|
153
|
+
when Array
|
|
154
|
+
result = {}
|
|
155
|
+
value.each do |entry|
|
|
156
|
+
parsed = parse_override_entry(entry, context: context)
|
|
157
|
+
next unless parsed
|
|
158
|
+
|
|
159
|
+
result[parsed[0]] = parsed[1]
|
|
160
|
+
end
|
|
161
|
+
result
|
|
162
|
+
else
|
|
163
|
+
{}
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def self.merge_excludes(*layers, strip_knockouts: true)
|
|
168
|
+
result = []
|
|
169
|
+
layers.each do |layer|
|
|
170
|
+
next if layer.nil?
|
|
171
|
+
|
|
172
|
+
parse_excludes(layer).each do |item|
|
|
173
|
+
if item == KNOCKOUT_PREFIX
|
|
174
|
+
result.clear
|
|
175
|
+
result << KNOCKOUT_PREFIX unless strip_knockouts
|
|
176
|
+
elsif item.start_with?(KNOCKOUT_PREFIX)
|
|
177
|
+
target = item[KNOCKOUT_PREFIX.length..-1].strip
|
|
178
|
+
next if target.empty?
|
|
179
|
+
|
|
180
|
+
result.delete(target)
|
|
181
|
+
unless strip_knockouts
|
|
182
|
+
knockout_token = "#{KNOCKOUT_PREFIX}#{target}"
|
|
183
|
+
result << knockout_token unless result.include?(knockout_token)
|
|
184
|
+
end
|
|
185
|
+
else
|
|
186
|
+
result.delete("#{KNOCKOUT_PREFIX}#{item}")
|
|
187
|
+
result << item unless result.include?(item)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
result
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def self.merge_overrides(*layers, strip_knockouts: true)
|
|
195
|
+
result = {}
|
|
196
|
+
layers.each do |layer|
|
|
197
|
+
next if layer.nil?
|
|
198
|
+
|
|
199
|
+
parse_overrides(layer).each do |k, v|
|
|
200
|
+
if k == KNOCKOUT_PREFIX
|
|
201
|
+
result.clear
|
|
202
|
+
result[KNOCKOUT_PREFIX] = KNOCKOUT_PREFIX unless strip_knockouts
|
|
203
|
+
elsif v == KNOCKOUT_PREFIX
|
|
204
|
+
if strip_knockouts
|
|
205
|
+
result.delete(k)
|
|
206
|
+
else
|
|
207
|
+
result[k] = KNOCKOUT_PREFIX
|
|
208
|
+
end
|
|
209
|
+
else
|
|
210
|
+
result[k] = v
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
result
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def self.resolve_excludes(zfs_obj, cli_exclude = nil)
|
|
218
|
+
layers = [zfs_obj.excludes]
|
|
219
|
+
if config && !zfs_obj.config_merged?
|
|
220
|
+
layers << config.excludes
|
|
221
|
+
layers.concat(config.zfs_excludes_layers_for(zfs_obj.name))
|
|
222
|
+
end
|
|
223
|
+
layers << cli_exclude unless cli_exclude.nil?
|
|
224
|
+
merge_excludes(*layers, strip_knockouts: true)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def self.resolve_overrides(zfs_obj, cli_option = nil)
|
|
228
|
+
layers = [zfs_obj.overrides]
|
|
229
|
+
if config && !zfs_obj.config_merged?
|
|
230
|
+
layers << config.overrides
|
|
231
|
+
layers.concat(config.zfs_overrides_layers_for(zfs_obj.name))
|
|
232
|
+
end
|
|
233
|
+
layers << cli_option unless cli_option.nil?
|
|
234
|
+
merge_overrides(*layers, strip_knockouts: true)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def self.build_zfs_from_props(zfs_name, props)
|
|
238
|
+
ZfsMgmt::Zfs.from_zfs_props(zfs_name, merge_config_props(zfs_name, props), config_merged: !config.nil?)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def self.config_dir
|
|
242
|
+
if global_options && global_options[:config_dir]
|
|
243
|
+
return global_options[:config_dir]
|
|
244
|
+
end
|
|
245
|
+
if Process.euid == 0
|
|
246
|
+
'/etc/zfsmgr'
|
|
247
|
+
elsif ENV['XDG_CONFIG_HOME'] && !ENV['XDG_CONFIG_HOME'].empty?
|
|
248
|
+
File.join(ENV['XDG_CONFIG_HOME'], 'zfsmgr')
|
|
249
|
+
else
|
|
250
|
+
File.join(Dir.home, '.config', 'zfsmgr')
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def self.load_config(dir = config_dir)
|
|
255
|
+
self.config = ZfsMgmt::Config.load_dir(dir)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def self.merge_config_props(zfs_name, props)
|
|
259
|
+
base = props || {}
|
|
260
|
+
return base unless config && zfs_name && !zfs_name.empty?
|
|
261
|
+
|
|
262
|
+
merged = base.merge(config.zfs_props_for(zfs_name))
|
|
263
|
+
excludes = merge_excludes(base['zfsmgmt:excludes'], config.excludes, *config.zfs_excludes_layers_for(zfs_name), strip_knockouts: true)
|
|
264
|
+
overrides = merge_overrides(base['zfsmgmt:overrides'], config.overrides, *config.zfs_overrides_layers_for(zfs_name), strip_knockouts: true)
|
|
265
|
+
if excludes.empty?
|
|
266
|
+
merged.delete('zfsmgmt:excludes')
|
|
267
|
+
else
|
|
268
|
+
merged['zfsmgmt:excludes'] = excludes
|
|
269
|
+
end
|
|
270
|
+
if overrides.empty?
|
|
271
|
+
merged.delete('zfsmgmt:overrides')
|
|
272
|
+
else
|
|
273
|
+
merged['zfsmgmt:overrides'] = overrides
|
|
274
|
+
end
|
|
275
|
+
merged
|
|
51
276
|
end
|
|
52
277
|
class ZfsGetError < StandardError
|
|
53
278
|
end
|
|
@@ -69,6 +294,8 @@ module ZfsMgmt
|
|
|
69
294
|
'remote',
|
|
70
295
|
'destination',
|
|
71
296
|
'recv_state',
|
|
297
|
+
'excludes',
|
|
298
|
+
'overrides',
|
|
72
299
|
].map do |p|
|
|
73
300
|
['zfsmgmt',p].join(':')
|
|
74
301
|
end
|
|
@@ -182,7 +409,7 @@ module ZfsMgmt
|
|
|
182
409
|
zfs_obj = zfs
|
|
183
410
|
snaps ||= zfs_obj.snapshots
|
|
184
411
|
else
|
|
185
|
-
zfs_obj =
|
|
412
|
+
zfs_obj = build_zfs_from_props(zfs, props)
|
|
186
413
|
end
|
|
187
414
|
|
|
188
415
|
minage = 0
|
|
@@ -267,11 +494,12 @@ module ZfsMgmt
|
|
|
267
494
|
unless match_filter?(zfs_name, filter)
|
|
268
495
|
next
|
|
269
496
|
end
|
|
270
|
-
|
|
497
|
+
merged_props = merge_config_props(zfs_name, props)
|
|
498
|
+
zfs = ZfsMgmt::Zfs.from_zfs_props(zfs_name, merged_props, config_merged: !config.nil?)
|
|
271
499
|
managed = true
|
|
272
500
|
property_match.each do |k,v|
|
|
273
501
|
matched = if k.to_s.start_with?('zfsmgmt:')
|
|
274
|
-
key_comp?(
|
|
502
|
+
key_comp?(merged_props, k, v)
|
|
275
503
|
else
|
|
276
504
|
attr_name = (k.to_sym == :send ? :send_enabled : k.to_sym)
|
|
277
505
|
match_value?(zfs.public_send(attr_name), v)
|
|
@@ -417,26 +645,45 @@ module ZfsMgmt
|
|
|
417
645
|
zfs_obj = zfs
|
|
418
646
|
snaps ||= zfs_obj.snapshots
|
|
419
647
|
else
|
|
420
|
-
zfs_obj =
|
|
648
|
+
zfs_obj = build_zfs_from_props(zfs, props)
|
|
421
649
|
end
|
|
422
650
|
zfs_name = zfs_obj.name
|
|
423
651
|
sorted = snaps.keys.sort { |a,b| snaps[a]['creation'] <=> snaps[b]['creation'] }
|
|
424
652
|
discard_first = recv_discard_first?(options, zfs_obj)
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
else
|
|
428
|
-
zfs_obj.resolved_destination(options[:destination])
|
|
429
|
-
end
|
|
430
|
-
recv_target = discard_first ? zfs_obj.base_destination(options[:destination]) : destination_path
|
|
431
|
-
unless destination_path
|
|
653
|
+
entries = zfs_obj.destination_entries
|
|
654
|
+
if entries.empty?
|
|
432
655
|
$logger.error("fatal error: unable to determine destination path for #{zfs_name}")
|
|
433
656
|
exit(1)
|
|
434
657
|
end
|
|
658
|
+
opt_remote = options.key?(:remote) ? options[:remote] : options['remote']
|
|
659
|
+
opt_remote = nil if opt_remote.respond_to?(:empty?) && opt_remote.empty?
|
|
660
|
+
warn_deprecated_remote('CLI --remote') if opt_remote
|
|
661
|
+
|
|
662
|
+
entries.each do |entry|
|
|
663
|
+
zfs_send_to_destination(options, zfs_obj, snaps, sorted, entry, discard_first: discard_first, opt_remote: opt_remote)
|
|
664
|
+
end
|
|
665
|
+
end
|
|
666
|
+
|
|
667
|
+
def self.zfs_send_to_destination(options, zfs_obj, snaps, sorted, entry, discard_first:, opt_remote:)
|
|
668
|
+
zfs_name = zfs_obj.name
|
|
669
|
+
raw_destination = discard_first ? zfs_obj.discard_first_for(entry[:base]) : entry[:resolved]
|
|
670
|
+
raw_recv_target = discard_first ? entry[:base] : raw_destination
|
|
671
|
+
_, destination_path = parse_destination(raw_destination)
|
|
672
|
+
_, recv_target = parse_destination(raw_recv_target)
|
|
673
|
+
if destination_path.nil? || destination_path.empty?
|
|
674
|
+
$logger.error("fatal error: unable to determine destination path for #{zfs_name}")
|
|
675
|
+
return
|
|
676
|
+
end
|
|
677
|
+
entry_remote = entry[:explicit_remote] || opt_remote || zfs_obj.remote
|
|
678
|
+
entry_remote = nil if entry_remote.respond_to?(:empty?) && entry_remote.empty?
|
|
679
|
+
cmd_prefix = entry_remote ? ['ssh', entry_remote] : []
|
|
680
|
+
state_destination = format_destination(entry_remote, destination_path)
|
|
681
|
+
|
|
435
682
|
# does the destination zfs already exist?
|
|
436
683
|
remote_zfs_state = ''
|
|
437
684
|
begin
|
|
438
685
|
recv_zfs = zfsget(zfs: destination_path,
|
|
439
|
-
command_prefix:
|
|
686
|
+
command_prefix: cmd_prefix,
|
|
440
687
|
#properties: ['receive_resume_token'],
|
|
441
688
|
)
|
|
442
689
|
rescue ZfsDatasetNotFoundError
|
|
@@ -470,10 +717,10 @@ module ZfsMgmt
|
|
|
470
717
|
e = zfs_send_estimate(com) if options[:verbose] == 'pv'
|
|
471
718
|
com += mbuffer_command(options) if options[:mbuffer]
|
|
472
719
|
com += pv_command(options,e) if options[:verbose] == 'pv'
|
|
473
|
-
com += zfs_recv_com(options,[],zfs_obj,recv_target)
|
|
720
|
+
com += zfs_recv_com(options,[],zfs_obj,recv_target, warn_deprecated: false, command_prefix: cmd_prefix)
|
|
474
721
|
|
|
475
722
|
status = system_com(com)
|
|
476
|
-
record_send_state(zfs_name, status) unless options[:noop]
|
|
723
|
+
record_send_state(zfs_name, status, destination: state_destination) unless options[:noop]
|
|
477
724
|
unless status && status.success?
|
|
478
725
|
return
|
|
479
726
|
end
|
|
@@ -488,10 +735,10 @@ module ZfsMgmt
|
|
|
488
735
|
com += mbuffer_command(options) if options[:mbuffer]
|
|
489
736
|
com += pv_command(options,e) if options[:verbose] == 'pv'
|
|
490
737
|
|
|
491
|
-
com += zfs_recv_com(options,[],zfs_obj,recv_target)
|
|
738
|
+
com += zfs_recv_com(options,[],zfs_obj,recv_target, warn_deprecated: false, command_prefix: cmd_prefix)
|
|
492
739
|
|
|
493
740
|
status = system_com(com)
|
|
494
|
-
record_send_state(zfs_name, status) unless options[:noop]
|
|
741
|
+
record_send_state(zfs_name, status, destination: state_destination) unless options[:noop]
|
|
495
742
|
unless status && status.success?
|
|
496
743
|
return
|
|
497
744
|
end
|
|
@@ -501,7 +748,7 @@ module ZfsMgmt
|
|
|
501
748
|
begin
|
|
502
749
|
remote_snaps = zfsget(zfs: destination_path,
|
|
503
750
|
types: ['snapshot'],
|
|
504
|
-
command_prefix:
|
|
751
|
+
command_prefix: cmd_prefix,
|
|
505
752
|
properties: ['creation','userrefs'],
|
|
506
753
|
)
|
|
507
754
|
rescue ZfsGetError
|
|
@@ -533,10 +780,10 @@ module ZfsMgmt
|
|
|
533
780
|
e = zfs_send_estimate(com) if options[:verbose] == 'pv'
|
|
534
781
|
com += mbuffer_command(options) if options[:mbuffer]
|
|
535
782
|
com += pv_command(options,e) if options[:verbose] == 'pv'
|
|
536
|
-
com += zfs_recv_com(options,[],zfs_obj,recv_target)
|
|
783
|
+
com += zfs_recv_com(options,[],zfs_obj,recv_target, warn_deprecated: false, command_prefix: cmd_prefix)
|
|
537
784
|
|
|
538
785
|
status = system_com(com)
|
|
539
|
-
record_send_state(zfs_name, status) unless options[:noop]
|
|
786
|
+
record_send_state(zfs_name, status, destination: state_destination) unless options[:noop]
|
|
540
787
|
return
|
|
541
788
|
end
|
|
542
789
|
$logger.debug("skipping remote snapshot #{rsnap} because the same snapshot doesn't exist locally #{rsnap.sub(destination_path,zfs_name)}")
|
|
@@ -604,7 +851,7 @@ module ZfsMgmt
|
|
|
604
851
|
VOLUME_UNSUPPORTED_RECV_PROPERTIES.include?(prop_name)
|
|
605
852
|
end
|
|
606
853
|
|
|
607
|
-
def self.zfs_recv_com(options,extra_opts,props,target)
|
|
854
|
+
def self.zfs_recv_com(options,extra_opts,props,target, warn_deprecated: true, command_prefix: nil)
|
|
608
855
|
zfs_obj = props.is_a?(ZfsMgmt::Zfs) ? props : ZfsMgmt::Zfs.from_zfs_props('', props)
|
|
609
856
|
zfs_recv_com = [ ZfsMgmt.global_options[:zfs_binary], 'recv' ]
|
|
610
857
|
zfs_recv_com.push('-s') if recv_state_enabled?(options, zfs_obj)
|
|
@@ -625,7 +872,11 @@ module ZfsMgmt
|
|
|
625
872
|
end
|
|
626
873
|
zfs_recv_com.push('-v') if key_comp?(options, 'verbose', ['receive', 'recv'])
|
|
627
874
|
volume = zfs_obj.volume?
|
|
628
|
-
|
|
875
|
+
cli_exclude = options.key?(:exclude) ? options[:exclude] : options['exclude']
|
|
876
|
+
cli_option = options.key?(:option) ? options[:option] : options['option']
|
|
877
|
+
excludes = resolve_excludes(zfs_obj, cli_exclude)
|
|
878
|
+
overrides = resolve_overrides(zfs_obj, cli_option).map { |k, v| "#{k}=#{v}" }
|
|
879
|
+
[['-x', excludes], ['-o', overrides]].each do |flag, values|
|
|
629
880
|
next unless values
|
|
630
881
|
|
|
631
882
|
values.each do |val|
|
|
@@ -637,19 +888,51 @@ module ZfsMgmt
|
|
|
637
888
|
end
|
|
638
889
|
end
|
|
639
890
|
zfs_recv_com += extra_opts
|
|
640
|
-
|
|
891
|
+
_, target_path = parse_destination(target)
|
|
892
|
+
zfs_recv_com.push(dq(target_path))
|
|
641
893
|
|
|
642
|
-
|
|
894
|
+
prefix = command_prefix.nil? ? recv_command_prefix(options, zfs_obj, target, warn_deprecated: warn_deprecated) : command_prefix
|
|
895
|
+
unless prefix.empty?
|
|
643
896
|
if options[:mbuffer]
|
|
644
897
|
zfs_recv_com = mbuffer_command(options) + zfs_recv_com
|
|
645
898
|
end
|
|
646
|
-
zfs_recv_com =
|
|
899
|
+
zfs_recv_com = prefix + [ sq(zfs_recv_com.join(' ')) ]
|
|
647
900
|
end
|
|
648
901
|
zfs_recv_com
|
|
649
902
|
end
|
|
650
|
-
def self.
|
|
651
|
-
|
|
652
|
-
remote ? [
|
|
903
|
+
def self.effective_remote(options, props, target = nil, warn_deprecated: true)
|
|
904
|
+
opts = options || {}
|
|
905
|
+
opt_remote = opts.key?(:remote) ? opts[:remote] : opts['remote']
|
|
906
|
+
opt_remote = nil if opt_remote.respond_to?(:empty?) && opt_remote.empty?
|
|
907
|
+
|
|
908
|
+
if warn_deprecated && opt_remote
|
|
909
|
+
warn_deprecated_remote('CLI --remote')
|
|
910
|
+
end
|
|
911
|
+
|
|
912
|
+
if target
|
|
913
|
+
target_remote, = parse_destination(target)
|
|
914
|
+
return target_remote if target_remote
|
|
915
|
+
end
|
|
916
|
+
|
|
917
|
+
if props.is_a?(ZfsMgmt::Zfs)
|
|
918
|
+
return props.destination_explicit_remote if props.destinations.length <= 1 && props.destination_explicit_remote
|
|
919
|
+
return opt_remote if opt_remote
|
|
920
|
+
return props.remote
|
|
921
|
+
elsif props.is_a?(Hash)
|
|
922
|
+
raw_dest = props['zfsmgmt:destination']
|
|
923
|
+
if raw_dest && !raw_dest.is_a?(Array) && !raw_dest.to_s.include?(',')
|
|
924
|
+
prop_dest_remote, = parse_destination(raw_dest)
|
|
925
|
+
return prop_dest_remote if prop_dest_remote
|
|
926
|
+
end
|
|
927
|
+
return opt_remote if opt_remote
|
|
928
|
+
return props['zfsmgmt:remote']
|
|
929
|
+
end
|
|
930
|
+
|
|
931
|
+
opt_remote
|
|
932
|
+
end
|
|
933
|
+
def self.recv_command_prefix(options, props, target = nil, warn_deprecated: true)
|
|
934
|
+
remote = effective_remote(options, props, target, warn_deprecated: warn_deprecated)
|
|
935
|
+
remote && !remote.empty? ? [ 'ssh', remote ] : []
|
|
653
936
|
end
|
|
654
937
|
def self.zfs_send_estimate(com)
|
|
655
938
|
lcom = com.dup
|
|
@@ -743,8 +1026,8 @@ module ZfsMgmt
|
|
|
743
1026
|
$logger.debug("skipping filesystem with send disabled: #{zfs.name}")
|
|
744
1027
|
next
|
|
745
1028
|
end
|
|
746
|
-
|
|
747
|
-
$logger.error("#{zfs.name}: you must specify a destination zfs path via the user property zfsmgmt:destination,
|
|
1029
|
+
if zfs.destinations.empty?
|
|
1030
|
+
$logger.error("#{zfs.name}: you must specify a destination zfs path via the user property zfsmgmt:destination, skipping")
|
|
748
1031
|
next
|
|
749
1032
|
end
|
|
750
1033
|
if zfs.snapshots.empty?
|
|
@@ -800,6 +1083,7 @@ module ZfsMgmt
|
|
|
800
1083
|
|
|
801
1084
|
SEND_WARN_SECONDS = 36 * 60 * 60
|
|
802
1085
|
SEND_CRIT_SECONDS = 72 * 60 * 60
|
|
1086
|
+
SEND_STATE_KEY_SEPARATOR = ' -> '.freeze
|
|
803
1087
|
|
|
804
1088
|
def self.send_state_db_path
|
|
805
1089
|
if global_options && global_options[:state_db]
|
|
@@ -820,7 +1104,13 @@ module ZfsMgmt
|
|
|
820
1104
|
PStore.new(path)
|
|
821
1105
|
end
|
|
822
1106
|
|
|
823
|
-
def self.
|
|
1107
|
+
def self.send_state_key(source, destination = nil)
|
|
1108
|
+
return source.to_s if destination.nil? || destination.to_s.empty?
|
|
1109
|
+
|
|
1110
|
+
"#{source}#{SEND_STATE_KEY_SEPARATOR}#{destination}"
|
|
1111
|
+
end
|
|
1112
|
+
|
|
1113
|
+
def self.record_send_state(zfs, status, destination: nil, timestamp: Time.now.to_i)
|
|
824
1114
|
exit_code = if status.nil?
|
|
825
1115
|
1
|
|
826
1116
|
elsif status.respond_to?(:exitstatus)
|
|
@@ -828,9 +1118,16 @@ module ZfsMgmt
|
|
|
828
1118
|
else
|
|
829
1119
|
status.to_i
|
|
830
1120
|
end
|
|
1121
|
+
key = send_state_key(zfs, destination)
|
|
1122
|
+
payload = {
|
|
1123
|
+
'source' => zfs.to_s,
|
|
1124
|
+
'destination' => destination,
|
|
1125
|
+
'timestamp' => timestamp.to_i,
|
|
1126
|
+
'exit' => exit_code
|
|
1127
|
+
}
|
|
831
1128
|
store = send_state_store
|
|
832
1129
|
store.transaction do
|
|
833
|
-
store[
|
|
1130
|
+
store[key] = JSON.generate(payload)
|
|
834
1131
|
end
|
|
835
1132
|
end
|
|
836
1133
|
|
|
@@ -840,20 +1137,34 @@ module ZfsMgmt
|
|
|
840
1137
|
store = send_state_store(path)
|
|
841
1138
|
entries = {}
|
|
842
1139
|
store.transaction(true) do
|
|
843
|
-
store.roots.sort.each do |
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
1140
|
+
store.roots.sort.each do |key|
|
|
1141
|
+
key_str = key.to_s
|
|
1142
|
+
next unless match_filter?(key_str, filter)
|
|
1143
|
+
raw = store[key]
|
|
1144
|
+
data = raw.is_a?(String) ? JSON.parse(raw) : raw.dup
|
|
1145
|
+
unless data.key?('source')
|
|
1146
|
+
src, dst = key_str.split(SEND_STATE_KEY_SEPARATOR, 2)
|
|
1147
|
+
data['source'] = src
|
|
1148
|
+
data['destination'] = dst if dst && !data.key?('destination')
|
|
1149
|
+
end
|
|
1150
|
+
entries[key_str] = data
|
|
848
1151
|
end
|
|
849
1152
|
end
|
|
850
1153
|
entries
|
|
851
1154
|
end
|
|
852
1155
|
|
|
853
|
-
def self.send_state_delete(zfs)
|
|
1156
|
+
def self.send_state_delete(zfs, destination = nil)
|
|
854
1157
|
store = send_state_store
|
|
855
1158
|
store.transaction do
|
|
856
|
-
|
|
1159
|
+
if destination && !destination.to_s.empty?
|
|
1160
|
+
store.delete(send_state_key(zfs, destination))
|
|
1161
|
+
else
|
|
1162
|
+
store.delete(zfs)
|
|
1163
|
+
prefix = "#{zfs}#{SEND_STATE_KEY_SEPARATOR}"
|
|
1164
|
+
store.roots.each do |k|
|
|
1165
|
+
store.delete(k) if k.to_s.start_with?(prefix)
|
|
1166
|
+
end
|
|
1167
|
+
end
|
|
857
1168
|
end
|
|
858
1169
|
end
|
|
859
1170
|
|
|
@@ -870,7 +1181,7 @@ module ZfsMgmt
|
|
|
870
1181
|
entries = send_state_entries(filter: filter)
|
|
871
1182
|
if check
|
|
872
1183
|
now = Time.now.to_i
|
|
873
|
-
entries.each do |
|
|
1184
|
+
entries.each do |key, data|
|
|
874
1185
|
ts = data['timestamp'].to_i
|
|
875
1186
|
exit_code = data['exit'].to_i
|
|
876
1187
|
age = [now - ts, 0].max
|
|
@@ -882,14 +1193,15 @@ module ZfsMgmt
|
|
|
882
1193
|
else
|
|
883
1194
|
0
|
|
884
1195
|
end
|
|
885
|
-
|
|
1196
|
+
label = send_state_key(data['source'] || key, data['destination'])
|
|
1197
|
+
puts "#{state} \"zfs_send #{label}\" age=#{age};#{SEND_WARN_SECONDS};#{SEND_CRIT_SECONDS} last send #{time_str} exit #{exit_code}"
|
|
886
1198
|
end
|
|
887
1199
|
else
|
|
888
1200
|
return if entries.empty?
|
|
889
1201
|
table = Text::Table.new
|
|
890
|
-
table.head = ['
|
|
891
|
-
table.rows = entries.map do |
|
|
892
|
-
[
|
|
1202
|
+
table.head = ['source', 'destination', 'timestamp', 'exit']
|
|
1203
|
+
table.rows = entries.map do |key, data|
|
|
1204
|
+
[data['source'] || key, data['destination'].to_s, local_epoch_to_datetime(data['timestamp'].to_i), data['exit']]
|
|
893
1205
|
end
|
|
894
1206
|
print table.to_s
|
|
895
1207
|
end
|