Class: OsCtld::Commands::Container::SendConfig

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Send
Defined in:
lib/osctld/commands/container/send_config.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



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
70
71
72
73
74
75
76
77
# File 'lib/osctld/commands/container/send_config.rb', line 10

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

  from_snapshot = if opts[:from_snapshot]
                    if opts[:from_snapshot].start_with?('@')
                      opts[:from_snapshot][1..]
                    else
                      opts[:from_snapshot]
                    end
                  end

  manipulate(ct) do
    next error('this container is already being sent') if ct.send_log

    ctid = opts[:as_id] || ct.id

    f = Tempfile.open("ct-#{ct.id}-skel")
    export(
      ct,
      f,
      ctid:,
      user: opts[:as_user] || ct.user.name,
      group: opts[:as_group] || ct.group.name,
      network_interfaces: opts[:network_interfaces]
    )
    f.seek(0)

    m_opts = {
      ctid:,
      port: opts[:port] || 22,
      dst: opts[:dst],
      snapshots: opts.fetch(:snapshots, true),
      from_snapshot:,
      preexisting_datasets: opts.fetch(:preexisting_datasets, false)
    }

    recv_opts = [
      'receive', 'skel',
      opts[:to_pool] || '-'
    ]

    recv_opts << opts[:passphrase] if opts[:passphrase]

    ssh = send_ssh_cmd(
      ct.pool.send_receive_key_chain,
      m_opts,
      recv_opts
    )
    token = nil

    IO.popen("exec #{ssh.join(' ')}", 'r+') do |io|
      io.write(f.readpartial(32 * 1024)) until f.eof?
      io.close_write
      token = io.readline.strip
    end

    f.close
    f.unlink

    if $?.exitstatus == 0
      ct.open_send_log(:source, token, m_opts)
      ok
    else
      error('send config failed')
    end
  end
end

#export(ct, io, opts = {}) ⇒ Object (protected)

Parameters:

  • ct (Container)
  • io (IO)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :ctid (String)
  • :user (String)
  • :group (String)
  • :network_interfaces (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osctld/commands/container/send_config.rb', line 88

def export(ct, io, opts = {})
  exporter = OsCtl::Lib::Exporter::Zfs.new(ct, io)
  exporter.(
    'skel',
    id: opts[:ctid],
    user: opts[:user],
    group: opts[:group]
  )
  exporter.dump_configs do |dump|
    dump.user(File.read(ct.user.config_path))
    dump.group(File.read(ct.group.config_path))

    ct_cfg = ct.dump_config
    ct_cfg.delete('net_interfaces') unless opts[:network_interfaces]
    ct_cfg['user'] = opts[:user]
    ct_cfg['group'] = opts[:group]
    dump.container(OsCtl::Lib::ConfigFile.dump_yaml(ct_cfg))
  end
  exporter.dump_user_hook_scripts(Hook::Manager.list_all_scripts(ct))
  exporter.close
end