Class: OsCtld::ContainerControl::Runner

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

Overview

Runner is run in a forked&execed process and under the container’s user

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • opts (Hash)

    container options

Options Hash (**opts):

  • :id (String)
  • :lxc_home (String)
  • :user_home (String)
  • :log_file (String)
  • :stdin (IO, nil)
  • :stdout (IO)
  • :stderr (IO)


16
17
18
19
20
21
22
23
24
# File 'lib/osctld/container_control/runner.rb', line 16

def initialize(**opts)
  @ctid = opts[:id]
  @lxc_home = opts[:lxc_home]
  @user_home = opts[:user_home]
  @log_file = opts[:log_file]
  @stdin = opts[:stdin]
  @stdout = opts[:stdout]
  @stderr = opts[:stderr]
end

Instance Attribute Details

#ctidObject (readonly)

Returns the value of attribute ctid.



6
7
8
# File 'lib/osctld/container_control/runner.rb', line 6

def ctid
  @ctid
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



6
7
8
# File 'lib/osctld/container_control/runner.rb', line 6

def log_file
  @log_file
end

#lxc_homeObject (readonly)

Returns the value of attribute lxc_home.



6
7
8
# File 'lib/osctld/container_control/runner.rb', line 6

def lxc_home
  @lxc_home
end

#stderrObject (readonly, protected)

Returns the value of attribute stderr.



36
37
38
# File 'lib/osctld/container_control/runner.rb', line 36

def stderr
  @stderr
end

#stdinObject (readonly, protected)

Returns the value of attribute stdin.



36
37
38
# File 'lib/osctld/container_control/runner.rb', line 36

def stdin
  @stdin
end

#stdoutObject (readonly, protected)

Returns the value of attribute stdout.



36
37
38
# File 'lib/osctld/container_control/runner.rb', line 36

def stdout
  @stdout
end

#user_homeObject (readonly)

Returns the value of attribute user_home.



6
7
8
# File 'lib/osctld/container_control/runner.rb', line 6

def user_home
  @user_home
end

Instance Method Details

#error(msg) ⇒ Object (protected)



42
43
44
# File 'lib/osctld/container_control/runner.rb', line 42

def error(msg)
  { status: false, message: msg }
end

#execute(*args, **kwargs) ⇒ Hash

Implement this method

Parameters:

  • args (Array)

    command arguments

  • kwargs (Array)

    command arguments

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/osctld/container_control/runner.rb', line 30

def execute(*args, **kwargs)
  raise NotImplementedError
end

#lxc_ctObject (protected)



46
47
48
# File 'lib/osctld/container_control/runner.rb', line 46

def lxc_ct
  @lxc_ct ||= LXC::Container.new(ctid, lxc_home)
end

#ok(out = nil) ⇒ Object (protected)



38
39
40
# File 'lib/osctld/container_control/runner.rb', line 38

def ok(out = nil)
  { status: true, output: out }
end

#setup_exec_envObject (protected)



54
55
56
57
58
# File 'lib/osctld/container_control/runner.rb', line 54

def setup_exec_env
  ENV.delete_if { |k, _| k != 'TERM' }
  ENV['PATH'] = system_path.join(':')
  ENV['HOME'] = user_home
end

#setup_exec_run_envObject (protected)



60
61
62
63
# File 'lib/osctld/container_control/runner.rb', line 60

def setup_exec_run_env
  setup_exec_env
  ENV['PATH'] = ['/run/wrappers/bin', ENV.fetch('PATH', nil)].join(':')
end

#system_pathObject (protected)



50
51
52
# File 'lib/osctld/container_control/runner.rb', line 50

def system_path
  SwitchUser::SYSTEM_PATH
end