Class: OsCtld::ContainerControl::Commands::Exec::Runner

Inherits:
Runner
  • Object
show all
Includes:
Utils::Runscript::Runner
Defined in:
lib/osctld/container_control/commands/exec.rb

Instance Attribute Summary

Attributes inherited from Runner

#ctid, #log_file, #lxc_home, #stderr, #stdin, #stdout, #user_home

Instance Method Summary collapse

Methods included from Utils::Runscript::Runner

#runscript_run, #with_configured_network

Methods inherited from Runner

#error, #initialize, #lxc_ct, #ok, #setup_exec_env, #setup_exec_run_env, #system_path

Constructor Details

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

Instance Method Details

#exec_run(opts) ⇒ Object (protected)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/osctld/container_control/commands/exec.rb', line 95

def exec_run(opts)
  pid = Process.fork do
    STDIN.reopen(stdin)
    STDOUT.reopen(stdout)
    STDERR.reopen(stderr)

    setup_exec_run_env

    cmd = [
      'lxc-execute',
      '-P', lxc_home,
      '-n', ctid,
      '-o', log_file,
      '-s', "lxc.environment=PATH=#{system_path.join(':')}",
      '-s', "lxc.environment=HOME=/root",
      '-s', "lxc.environment=USER=root",
      '--',
      opts[:cmd],
    ].flatten

    Process.exec(*cmd)
  end

  _, status = Process.wait2(pid)
  ok(status.exitstatus)
end

#exec_run_network(opts) ⇒ Object (protected)



122
123
124
125
126
127
# File 'lib/osctld/container_control/commands/exec.rb', line 122

def exec_run_network(opts)
  with_configured_network(
    init_script: opts[:init_script],
    net_config: opts[:net_config],
  ) { exec_running(opts) }
end

#exec_running(opts) ⇒ Object (protected)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctld/container_control/commands/exec.rb', line 79

def exec_running(opts)
  pid = lxc_ct.attach(
    stdin: stdin,
    stdout: stdout,
    stderr: stderr,
  ) do
    setup_exec_env
    ENV['HOME'] = '/root'
    ENV['USER'] = 'root'
    LXC.run_command(opts[:cmd])
  end

  _, status = Process.wait2(pid)
  ok(status.exitstatus)
end

#execute(mode, opts) ⇒ Integer

Returns exit status.

Parameters:

  • mode (:running, :run_network, :run)
  • opts (Hash)

Options Hash (opts):

  • :cmd (Array<String>)

    command to execute

  • :stdin (IO)
  • :stdout (IO)
  • :stderr (IO)
  • :run (Boolean)

    run the container if it is stopped?

  • :network (Boolean)

    setup network if the container is run?

  • :init_script (String)

    path to the script used to control the container

  • :net_config (Hash)

Returns:

  • (Integer)

    exit status



74
75
76
# File 'lib/osctld/container_control/commands/exec.rb', line 74

def execute(mode, opts)
  send(:"exec_#{mode}", opts)
end