Class: Hookit::Resource::Zfs

Inherits:
Base
  • Object
show all
Defined in:
lib/hookit/resource/zfs.rb

Instance Attribute Summary

Attributes inherited from Base

#dict

Instance Method Summary collapse

Methods inherited from Base

#action, actions, #can_run?, default_action, #default_action, field, #not_if, #only_if

Constructor Details

#initialize(name) ⇒ Zfs

Returns a new instance of Zfs.



16
17
18
19
# File 'lib/hookit/resource/zfs.rb', line 16

def initialize(name)
  snapshot(name) unless snapshot
  super
end

Instance Method Details

#clone!Object



72
73
74
# File 'lib/hookit/resource/zfs.rb', line 72

def clone!
  run_command! "zfs clone #{snapshot} #{dataset}"
end

#destroy!Object



61
62
63
64
65
66
# File 'lib/hookit/resource/zfs.rb', line 61

def destroy!
  `zfs list -t snapshot | grep #{snapshot}`
  if $?.exitstatus == 0
    run_command! "zfs destroy #{snapshot}"
  end 
end

#receive!Object



44
45
46
# File 'lib/hookit/resource/zfs.rb', line 44

def receive!
  run_command! "#{source.to_s.strip} | zfs receive -F #{dataset}"
end

#rollback!Object



68
69
70
# File 'lib/hookit/resource/zfs.rb', line 68

def rollback!
  run_command! "zfs rollback -r #{snapshot}"
end

#run(action) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hookit/resource/zfs.rb', line 21

def run(action)
  case action
  when :send
    send!
  when :receive
    receive!
  when :transmit
    transmit!
  when :snapshot
    snapshot!
  when :destroy
    destroy!
  when :rollback
    rollback!
  when :clone
    clone!
  end
end

#run_command!(cmd, expect_code = 0) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/hookit/resource/zfs.rb', line 88

def run_command!(cmd, expect_code=0)
  res = `#{cmd}`
  code = $?.exitstatus
  if code != expect_code
    raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
  end
  return validate!(res)
end

#send!Object



40
41
42
# File 'lib/hookit/resource/zfs.rb', line 40

def send!
  run_command! "zfs send #{snapshot} | #{destination}"
end

#snapshot!Object



56
57
58
59
# File 'lib/hookit/resource/zfs.rb', line 56

def snapshot!
  destroy!
  run_command! "zfs snapshot #{snapshot}"
end

#transmit!Object



48
49
50
51
52
53
54
# File 'lib/hookit/resource/zfs.rb', line 48

def transmit!
  if ssh_host
    run_command! "zfs send #{options} #{snapshot} | ssh -o stricthostkeychecking=no #{ssh_host} zfs receive -F #{dataset}"
  else
    run_command! "zfs send #{options} #{snapshot} | zfs receive -F #{dataset}"
  end
end

#validate!(res) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hookit/resource/zfs.rb', line 76

def validate!(res)
  if validator.is_a? Proc
    if validator.call(res)
      res
    else
      raise "ERROR: execute resource \"#{name}\" failed validation!"
    end
  else
    res
  end
end