Class: OsCtld::Cli::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/cli/runner.rb

Class Method Summary collapse

Class Method Details

.run ⇒ Object



8
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
# File 'lib/osctld/cli/runner.rb', line 8

def self.run
  ret = nil

  unless ARGV.empty?
    warn "Usage: #{$0}"
    exit(false)
  end

  begin
    cfg = JSON.parse($stdin.readline, symbolize_names: true)
    ret = IO.new(cfg[:return])
    ret.close_on_exec = true

    OsCtl::Lib::Logger.setup(:none)
    CGroup.init

    Process.setproctitle(
      "osctld: #{cfg[:pool]}:#{cfg[:id]} runner:#{cfg[:name].downcase}"
    )

    stdin = cfg[:stdin] && IO.new(cfg[:stdin])
    stdout = IO.new(cfg[:stdout])
    stderr = IO.new(cfg[:stderr])
    network_socket = cfg[:network_socket] && UNIXSocket.for_fd(cfg[:network_socket])
    network_socket.close_on_exec = true if network_socket

    [stdin, stdout, stderr].compact.each do |io|
      io.close_on_exec = true
    end

    runner = OsCtld::ContainerControl::Commands.const_get(cfg[:name])::Runner.new(
      pool: cfg[:pool],
      id: cfg[:id],
      lxc_home: cfg[:lxc_home],
      user_home: cfg[:user_home],
      log_file: cfg[:log_file],
      stdin:,
      stdout:,
      stderr:,
      network_socket:
    )
  rescue StandardError => e
    report_failure(ret, e, :setup)
  end

  begin
    val = runner.execute(*cfg[:args], **cfg[:kwargs])
  rescue StandardError => e
    report_failure(ret, e, :execution)
  end

  begin
    ret.puts(val.to_json)
  rescue StandardError => e
    report_failure(ret, e, :response)
  end
end