Class: OsCtld::Commands::Container::SendState

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_state.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



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
47
48
49
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
# File 'lib/osctld/commands/container/send_state.rb', line 12

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

      running = ct.state == :running
    end

    if !opts[:clone] || opts[:consistent]
      call_cmd(Commands::Container::Stop, id: ct.id, pool: ct.pool.name)
    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

    if opts[:clone] && opts[:restart] && running
      call_cmd(Commands::Container::Start, id: ct.id, pool: ct.pool.name)
    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

      unless ct.send_log.can_send_continue?(:transfer)
        error!('invalid send sequence')
      end
    end

    progress('Starting on the target node')
    ret = system(
      *send_ssh_cmd(
        ct.pool.send_receive_key_chain,
        ct.send_log.opts,
        ['receive', 'transfer', ct.send_log.token] + \
        (running && opts[:start] ? ['start'] : [])
      )
    )

    error!('transfer failed') if ret.nil? || $?.exitstatus != 0

    ct.exclusively do
      ct.send_log.state = :transfer
      ct.send_log.opts.cloned = opts[:clone]
      ct.save_config
    end

    ok
  end
end

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/osctld/commands/container/send_state.rb', line 83

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