Class: OsCtld::ContainerControl::Commands::Syscmd::Frontend

Inherits:
Frontend
  • Object
show all
Defined in:
lib/osctld/container_control/commands/syscmd.rb

Instance Attribute Summary

Attributes inherited from Frontend

#command_class, #ct

Instance Method Summary collapse

Methods inherited from Frontend

#exec_runner, #fork_runner, #initialize

Constructor Details

This class inherits a constructor from OsCtld::ContainerControl::Frontend

Instance Method Details

#execute(cmd, opts = {}) ⇒ OsCtl::Lib::SystemCommandResult

Parameters:

  • cmd (Array<String>)

    command to execute

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :stdin (IO, String)
  • :run (Boolean)

    run the container if it is stopped?

  • :network (Boolean)

    setup network if the container is run?

  • :valid_rcs (Array<Integer>, Symbol)

Returns:

  • (OsCtl::Lib::SystemCommandResult)

Raises:



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
# File 'lib/osctld/container_control/commands/syscmd.rb', line 18

def execute(cmd, opts = {})
  opts[:valid_rcs] ||= []

  in_r, in_w = nil
  out_r, out_w = IO.pipe

  if opts[:stdin].is_a?(String)
    in_r, in_w = IO.pipe
    in_w.write(opts[:stdin])
    in_w.close

  elsif opts[:stdin]
    in_r = opts[:stdin]
  end

  if !ct.running? && !opts[:run]
    raise OsCtld::SystemCommandFailed.new(cmd, 1, 'Container is not running')
  end

  begin
    ret = ContainerControl::Commands::Exec.run!(
      ct,
      cmd:,
      stdin: in_r,
      stdout: out_w,
      stderr: out_w,
      run: opts[:run],
      network: opts[:network]
    )
  rescue ContainerControl::Error => e
    out_r.close
    raise OsCtld::SystemCommandFailed.new(
      cmd,
      1,
      "Command '#{cmd}' within CT #{ct.id} failed: #{e.message}"
    )
  ensure
    in_r.close if in_r && opts[:stdin].is_a?(String)
    out_w.close
  end

  out = out_r.read
  out_r.close

  if ret != 0 &&
     opts[:valid_rcs] != :all &&
     !opts[:valid_rcs].include?(ret)
    raise OsCtld::SystemCommandFailed.new(
      cmd,
      1,
      "Command '#{cmd}' within CT #{ct.id} failed with exit code " \
      "#{ret}: #{out}"
    )
  end

  OsCtl::Lib::SystemCommandResult.new(ret, out)
end