Class: OsCtld::Commands::Container::SendSync

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::Send, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/container/send_sync.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osctld/commands/container/send_sync.rb', line 11

def execute
  ct = DB::Containers.find(opts[:id], opts[:pool])
  error!('container not found') unless ct
  running = false

  manipulate(ct) do
    ct.exclusively do
      if !ct.send_log || !ct.send_log.can_send_continue?(:incremental)
        error!('invalid send sequence')
      end
    end

    snap = "osctl-send-incr-#{Time.now.to_i}"
    zfs(:snapshot, '-r', "#{ct.dataset}@#{snap}")

    ct.exclusively do
      ct.send_log.snapshots << snap
      ct.save_config
    end

    progress("Syncing #{ct.dataset.relative_name}")
    send_dataset(ct, ct.dataset, snap)

    ct.dataset.descendants.each do |ds|
      progress("Syncing #{ds.relative_name}")
      send_dataset(ct, ds, snap)
    end

    ct.exclusively do
      ct.send_log.state = :incremental
      ct.save_config
    end

    ok
  end
end

#send_dataset(ct, ds, snap) ⇒ Object (protected)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctld/commands/container/send_sync.rb', line 50

def send_dataset(ct, ds, snap)
  stream = OsCtl::Lib::Zfs::Stream.new(
    ds,
    snap,
    ct.send_log.snapshots[-2],
    intermediary: ct.send_log.opts.snapshots
  )
  stream.progress do |total, _transfered, changed|
    progress(type: :progress, data: {
      time: Time.now.to_i,
      size: stream.size,
      transfered: total,
      changed:
    })
  end

  mbuf_opts = Daemon.get.config.send_receive.send_mbuffer.as_hash_options
  r, send = stream.spawn_with_mbuffer(**mbuf_opts)
  pid = Process.spawn(
    *send_ssh_cmd(
      ct.pool.send_receive_key_chain,
      ct.send_log.opts,
      ['receive', 'incremental', ct.send_log.token, ds.relative_name, snap]
    ),
    in: r
  )
  r.close
  stream.monitor(send)

  _, status = Process.wait2(pid)

  return unless status.exitstatus != 0

  error!('sync failed')
end