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!, 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
# 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

  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: 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: ctid,
      port: opts[:port] || 22,
      dst: opts[:dst],
    }

    recv_opts = [
      'receive', 'skel',
      opts[:to_pool] ? 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(16*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)


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_config.rb', line 76

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') if !opts[:network_interfaces]
    ct_cfg['user'] = opts[:user]
    ct_cfg['group'] = opts[:group]
    dump.container(YAML.dump(ct_cfg))
  end
  exporter.dump_user_hook_scripts(Container::Hook.hooks)
  exporter.close
end