Class: OsCtld::Commands::Container::MigrateSync

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_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!, 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
# File 'lib/osctld/commands/container/migrate_sync.rb', line 11

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

  manipulate(ct) do
    ct.exclusively do
      if !ct.migration_log || !ct.migration_log.can_continue?(:base)
        error!('invalid migration sequence')
      end
    end

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

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

    send_dataset(ct, ct.dataset, snap)
    ct.dataset.descendants.each do |ds|
      send_dataset(ct, ds, snap)
    end

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

  ok
end

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



45
46
47
48
49
50
51
# File 'lib/osctld/commands/container/migrate_sync.rb', line 45

def send_dataset(ct, ds, base_snap)
  progress("Syncing #{ds.relative_name}")

  snaps = ds.snapshots
  send_snapshot(ct, ds, base_snap, snaps.first)
  send_snapshot(ct, ds, base_snap, snaps.last, snaps.first) if snaps.count > 1
end

#send_snapshot(ct, ds, base_snap, snap, from_snap = nil) ⇒ Object (protected)



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
# File 'lib/osctld/commands/container/migrate_sync.rb', line 53

def send_snapshot(ct, ds, base_snap, snap, from_snap = nil)
  stream = OsCtl::Lib::Zfs::Stream.new(ds, snap.snapshot, from_snap && from_snap.snapshot)
  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', from_snap ? 'incremental' : 'base',
        ct.id, ds.relative_name
      ] + (base_snap == snap.snapshot ? [base_snap] : [])
    ),
    in: r
  )
  r.close
  stream.monitor(send)

  _, status = Process.wait2(pid)

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