Class: OsCtld::ContainerControl::Frontend

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

Overview

Frontend is run from osctld in daemon mode, when it is running as root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_class, ct) ⇒ Frontend

Returns a new instance of Frontend.

Parameters:



12
13
14
15
# File 'lib/osctld/container_control/frontend.rb', line 12

def initialize(command_class, ct)
  @command_class = command_class
  @ct = ct
end

Instance Attribute Details

#command_classClass (readonly)

Returns:

  • (Class)


5
6
7
# File 'lib/osctld/container_control/frontend.rb', line 5

def command_class
  @command_class
end

#ctContainer (readonly)

Returns:



8
9
10
# File 'lib/osctld/container_control/frontend.rb', line 8

def ct
  @ct
end

Instance Method Details

#execute(*args) ⇒ Object

Implement this method

Parameters:

  • args (Array)

    command arguments

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/osctld/container_control/frontend.rb', line 19

def execute(*args)
  raise NotImplementedError
end

#pipe_runner(args: [], keep_fds: []) ⇒ ContainerControl::Result (protected)

Parameters:

  • args (Array) (defaults to: [])

    command arguments

  • keep_fds (Array<IO, Integer>) (defaults to: [])

Returns:



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
75
76
# File 'lib/osctld/container_control/frontend.rb', line 27

def pipe_runner(args: [], keep_fds: [])
  r, w = IO.pipe

  runner_opts = {
    pool: ct.pool.name,
    id: ct.id,
    lxc_home: ct.lxc_home,
    user_home: ct.user.homedir,
    log_file: ct.log_path,
  }

  pid = SwitchUser.fork_and_switch_to(
    ct.user.sysusername,
    ct.user.ugid,
    ct.user.homedir,
    ct.cgroup_path,
    prlimits: ct.prlimits.export,
    keep_fds: [w] + keep_fds,
  ) do
    # Closed by SwitchUser.fork_and_switch_to
    # r.close

    Process.setproctitle(
      "osctld: #{runner_opts[:pool]}:#{runner_opts[:id]} "+
      "#{command_class.name.split('::').last.downcase} runner"
    )

    runner = command_class::Runner.new(runner_opts)
    ret = runner.execute(*args)
    w.write(ret.to_json + "\n")

    exit
  end

  w.close

  begin
    ret = JSON.parse(r.readline, symbolize_names: true)
    Process.wait(pid)
    ContainerControl::Result.from_runner(ret)

  rescue EOFError
    Process.wait(pid)
    ContainerControl::Result.new(
      false,
      message: 'user runner failed',
      user_runner: true,
    )
  end
end