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, #pool, #stderr, #stdin, #stdout, #user_home

Instance Method Summary collapse

Methods included from Utils::Runscript::Runner

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/osctld/container_control/commands/exec.rb', line 107

def exec_run(opts)
  pid = Process.fork do
    $stdin.reopen(stdin)
    $stdout.reopen(stdout)
    $stderr.reopen(stderr)

    setup_exec_run_env
    osctld_wrapper_callback

    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)



136
137
138
139
140
141
# File 'lib/osctld/container_control/commands/exec.rb', line 136

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)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/osctld/container_control/commands/exec.rb', line 91

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



85
86
87
# File 'lib/osctld/container_control/commands/exec.rb', line 85

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