Class: OsCtl::Image::Operations::Builder::ControlledExec
- Inherits:
-
OsCtl::Image::Operations::Base
- Object
- OsCtl::Image::Operations::Base
- OsCtl::Image::Operations::Builder::ControlledExec
- Includes:
- Lib::Utils::Log, Lib::Utils::System
- Defined in:
- lib/osctl/image/operations/builder/controlled_exec.rb
Instance Attribute Summary collapse
- #builder ⇒ Builder readonly
-
#client ⇒ Object
readonly
protected
Returns the value of attribute client.
- #command ⇒ Array<String> readonly
-
#env ⇒ Object
readonly
protected
Returns the value of attribute env.
-
#id ⇒ Object
readonly
protected
Returns the value of attribute id.
Instance Method Summary collapse
- #cgroup_name ⇒ Object protected
- #clear_cgroup ⇒ Object protected
-
#execute ⇒ Integer
Exit status.
-
#initialize(builder, command, id: nil, client: nil, env: {}) ⇒ ControlledExec
constructor
A new instance of ControlledExec.
- #inner_cgroup_path ⇒ Object protected
- #kill_all(cgroup, signal) ⇒ Object protected
- #outer_cgroup_path ⇒ Object protected
- #start_script ⇒ Object protected
Methods inherited from OsCtl::Image::Operations::Base
Constructor Details
#initialize(builder, command, id: nil, client: nil, env: {}) ⇒ ControlledExec
Returns a new instance of ControlledExec.
22 23 24 25 26 27 28 29 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 22 def initialize(builder, command, id: nil, client: nil, env: {}) super() @builder = builder @command = command @id = id || SecureRandom.hex(10) @client = client || OsCtldClient.new @env = env end |
Instance Attribute Details
#builder ⇒ Builder (readonly)
12 13 14 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 12 def builder @builder end |
#client ⇒ Object (readonly, protected)
Returns the value of attribute client.
48 49 50 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 48 def client @client end |
#command ⇒ Array<String> (readonly)
15 16 17 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 15 def command @command end |
#env ⇒ Object (readonly, protected)
Returns the value of attribute env.
48 49 50 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 48 def env @env end |
#id ⇒ Object (readonly, protected)
Returns the value of attribute id.
48 49 50 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 48 def id @id end |
Instance Method Details
#cgroup_name ⇒ Object (protected)
100 101 102 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 100 def cgroup_name "osctl-image.exec.#{id}" end |
#clear_cgroup ⇒ Object (protected)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 64 def clear_cgroup cgroup = outer_cgroup_path unless Dir.exist?(cgroup) log(:info, 'cgroup not found, nothing to kill') return end log(:info, "clearing cgroup #{cgroup}") if kill_all(cgroup, 'TERM') sleep(5) kill_all(cgroup, 'KILL') end Dir.rmdir(cgroup) rescue Errno::ENOENT # ignore end |
#execute ⇒ Integer
Returns exit status.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 32 def execute begin rc = Operations::Builder::RunscriptFromString.run( builder, start_script, client: ) ensure clear_cgroup end rc || 1 end |
#inner_cgroup_path ⇒ Object (protected)
104 105 106 107 108 109 110 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 104 def inner_cgroup_path if OsCtl::Lib::CGroup.v2? File.join('/sys/fs/cgroup', cgroup_name) else File.join('/sys/fs/cgroup/systemd', cgroup_name) end end |
#kill_all(cgroup, signal) ⇒ Object (protected)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 84 def kill_all(cgroup, signal) killed = false File.open(File.join(cgroup, 'cgroup.procs'), 'r') do |f| f.each_line do |line| pid = line.strip.to_i log(:info, "kill -SIG#{signal} #{pid}") Process.kill(signal, pid) killed = true end end killed end |
#outer_cgroup_path ⇒ Object (protected)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 112 def outer_cgroup_path if OsCtl::Lib::CGroup.v2? File.join( '/sys/fs/cgroup', builder.attrs[:group_path], "lxc.payload.#{builder.ctid}", cgroup_name ) else File.join( '/sys/fs/cgroup/systemd', builder.attrs[:group_path], "lxc.payload.#{builder.ctid}", cgroup_name ) end end |
#start_script ⇒ Object (protected)
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/osctl/image/operations/builder/controlled_exec.rb', line 50 def start_script exec_cmd = ['env'] exec_cmd.concat(env.map { |k, v| "#{k}=#{v}" }) unless env.empty? exec_cmd.concat(command) <<~EOF #!/bin/sh cgroup="#{inner_cgroup_path}" mkdir "$cgroup" echo $$ >> "$cgroup/cgroup.procs" exec #{Shellwords.join(exec_cmd)} EOF end |