Class: OsCtld::Commands::Container::MigrateTransfer

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::Migration, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/container/migrate_transfer.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!, 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
# File 'lib/osctld/commands/container/migrate_transfer.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.migration_log || !ct.migration_log.can_continue?(:incremental)
        error!('invalid migration sequence')
      end

      running = ct.state == :running
      call_cmd(Commands::Container::Stop, id: ct.id, pool: ct.pool.name)
    end

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

    ct.exclusively do
      ct.migration_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.migration_log.state = :incremental
      ct.save_config

      if !ct.migration_log.can_continue?(:transfer)
        error!('invalid migration sequence')
      end
    end

    progress('Starting on the target node')
    ret = system(
      *migrate_ssh_cmd(
        ct.pool.migration_key_chain,
        ct.migration_log.opts,
        ['receive', 'transfer', ct.id] + (running ? ['start'] : [])
      )
    )

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

    ct.exclusively do
      ct.migration_log.state = :transfer
      ct.save_config
    end

    ok
  end
end

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/osctld/commands/container/migrate_transfer.rb', line 73

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

  r, send = stream.spawn
  pid = Process.spawn(
    *migrate_ssh_cmd(
      ct.pool.migration_key_chain,
      ct.migration_log.opts,
      ['receive', 'incremental', ct.id, ds.relative_name, snap]
    ),
    in: r
  )
  r.close
  stream.monitor(send)

  _, status = Process.wait2(pid)

  if status.exitstatus != 0
    error!('sync failed')
  end
end