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



94
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
121
122
# File 'lib/osctld/container_control/commands/exec.rb', line 94

def exec_run(opts)
  pid = Process.fork do
    STDIN.reopen(opts[:stdin])
    STDOUT.reopen(opts[:stdout])
    STDERR.reopen(opts[: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],
    ]

    # opts[:cmd] can contain an arbitrary command with multiple arguments
    # and quotes, so the mapping to process arguments is not clear. We use
    # the shell to handle this.
    Process.exec("exec #{cmd.join(' ')}")
  end

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

#exec_run_network(opts) ⇒ Object (protected)



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

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)



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

def exec_running(opts)
  pid = lxc_ct.attach(
    stdin: opts[:stdin],
    stdout: opts[:stdout],
    stderr: opts[: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 (NetConfig)

Returns:

  • (Integer)

    exit status



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

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