Class: OsCtld::SendReceive::Commands::ReceiveIncremental

Inherits:
Base show all
Includes:
Utils::Receive
Defined in:
lib/osctld/send_receive/commands/receive_incremental.rb

Instance Attribute Summary

Attributes inherited from Commands::Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods included from Utils::Receive

#check_auth_pubkey, #receive_pipeline_error

Methods inherited from Base

#base_execute, handle, #receive_pipeline_error, #validate_protocol_version!, #validate_send_log_protocol!

Methods inherited from Commands::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

#dataset_name(ct) ⇒ Object (protected)



73
74
75
76
77
78
79
# File 'lib/osctld/send_receive/commands/receive_incremental.rb', line 73

def dataset_name(ct)
  if opts[:dataset] == '/'
    ct.dataset.name
  else
    File.join(ct.dataset.name, opts[:dataset])
  end
end

#executeObject



9
10
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/osctld/send_receive/commands/receive_incremental.rb', line 9

def execute
  ct = SendReceive::Tokens.find_container(opts[:token])
  error!('container not found') unless ct

  ct.manipulate(self, block: true) do
    error!('this container is not staged') if ct.state != :staged

    ct.exclusively do
      if !ct.send_log || !ct.send_log.can_receive_continue?(:incremental)
        error!('invalid send sequence')
      elsif !check_auth_pubkey(opts[:key_pool], opts[:key_name], ct)
        error!('authentication key mismatch')
      end
    end

    validate_send_log_protocol!(ct)

    ds = OsCtl::Lib::Zfs::Dataset.new(dataset_name(ct), base: ct.dataset.name)
    # don't check its existence now to save time

    client.send("#{{ status: true, response: 'continue' }.to_json}\n", 0)
    io = client.recv_io

    r, w = IO.pipe

    mbuffer_cfg = Daemon.get.config.send_receive.receive_mbuffer

    mbuffer_pid = Process.spawn(
      mbuffer_cfg.command,
      '-q',
      *mbuffer_cfg.as_cli_options,
      in: io,
      out: w
    )

    recv_pid = Process.spawn(
      'zfs', 'recv',
      '-F', '-u',
      ds.name,
      in: r
    )

    io.close
    r.close
    w.close

    _, mbuffer_status = Process.wait2(mbuffer_pid)
    _, recv_status = Process.wait2(recv_pid)

    if mbuffer_status.exitstatus == 0 && recv_status.exitstatus == 0
      ct.exclusively do
        ct.send_log.state = :incremental
        ct.send_log.snapshots << [ds.name, opts[:snapshot]] if opts[:snapshot]
        ct.save_config
      end
      ok
    else
      error(receive_pipeline_error(mbuffer_status, recv_status))
    end
  end
end