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

#clear_failed_state_snapshot(ct) ⇒ Object (protected)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/osctld/commands/container/send_state.rb', line 100

def clear_failed_state_snapshot(ct)
  snap = ct.send_log.state_snapshot
  return if snap.nil?

  progress("Removing failed cutover snapshot #{snap}")

  ct.each_dataset do |ds|
    zfs(:destroy, nil, "#{ds}@#{snap}", valid_rcs: [1])
  end

  ct.exclusively do
    ct.send_log.state_snapshot = nil
    ct.save_config
  end
end

#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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

      if ct.send_log.state_running.nil?
        ct.send_log.state_running = ct.state == :running
        ct.save_config
      end

      running = ct.send_log.state_running
    end

    if !opts[:clone] || opts[:consistent]
      call_cmd!(Commands::Container::Stop, id: ct.id, pool: ct.pool.name)

      # Force write-out of dirtied pages
      if Daemon.get.config.writeout_dirtied_pages?
        begin
          ct.unmount(force: true)
        rescue SystemCommandFailed => e
          log(:warn, ct, "Unable to unmount dataset for writeback: #{e.message}")
          ct.mount(force: true)
        end
      end
    end

    clear_failed_state_snapshot(ct)

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

    ct.exclusively do
      ct.send_log.state_snapshot = 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)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/osctld/commands/container/send_state.rb', line 116

def send_dataset(ct, ds, snap)
  stream = OsCtl::Lib::Zfs::Stream.new(
    ds,
    snap,
    ct.send_log.snapshots.last,
    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