Class: OsCtld::Commands::Container::Stop
- Includes:
- OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::Container, Utils::SwitchUser
- Defined in:
- lib/osctld/commands/container/stop.rb
Direct Known Subclasses
Instance Method Summary collapse
Methods included from Utils::SwitchUser
Methods included from Utils::Container
#format_unavailable_repositories, #get_image_path, #get_image_path!, #get_repositories, #image_not_found_message, #image_spec, #image_unavailable_message, #remove_accounting_cgroups, #with_image_path, #with_repository_image_path!
Methods inherited from Logged
Instance Method Details
#execute(ct) ⇒ Object
17 18 19 20 21 22 23 24 25 26 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/osctld/commands/container/stop.rb', line 17 def execute(ct) manipulate(ct) do progress('Stopping container') # Remove the container from autostart queue ct.pool.autostart_plan.stop_ct(ct) mode = case opts.fetch(:method, 'shutdown_or_kill') when 'shutdown_or_kill' :stop when 'shutdown_or_fail' :shutdown when 'kill' :kill else error!("unknown stop method '#{opts[:method]}'") end if %i[freezing frozen].include?(ct.state) if mode == :stop mode = :kill elsif mode == :shutdown error!('The container is frozen, unable to shutdown') end end begin Hook.run(ct, :pre_stop) rescue HookFailed => e error!(e.) end # Give the container more memory, so that the shutdown does not hang # in low memory situations. ct.cgparams. if ct.running? promise = ct.get_exit_promise ct.log(:debug, 'No started or retiring run, skipping exit promise') unless promise begin DistConfig.run( ct.get_run_conf, :stop, mode:, message: opts[:message], timeout: opts[:timeout] || Container::DEFAULT_STOP_TIMEOUT ) rescue ContainerControl::UserRunnerError ct.log(:warn, 'Unable to stop, killing by force') progress('Unable to stop, killing by force') unless force_kill(ct) ct.log(:warn, 'Unable to kill or cleanup') error!('Unable to kill or cleanup') end rescue ContainerControl::Error => e error!(e.) end if promise if promise.wait ct.log(:debug, 'Exit promise fulfilled') else ct.log(:warn, 'Timeout while waiting for exit promise') error!('Container stop cleanup has not finished') end end remove_accounting_cgroups(ct) if ct.ephemeral? && !indirect? call_cmd!( Commands::Container::Delete, pool: ct.pool.name, id: ct.id, force: true ) end ok end end |
#find ⇒ Object
12 13 14 15 |
# File 'lib/osctld/commands/container/stop.rb', line 12 def find ct = DB::Containers.find(opts[:id], opts[:pool]) ct || error!('container not found') end |
#force_kill(ct) ⇒ Boolean (protected)
104 105 106 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 |
# File 'lib/osctld/commands/container/stop.rb', line 104 def force_kill(ct) recovery = Container::Recovery.new(ct) run_conf = ct.get_run_conf run_conf.nfs_cancellation.capture(run_conf.init_pid) # Freeze all processes before the kill CGroup.freeze_tree(ct.cgroup_path) begin run_conf.nfs_cancellation.abort CGroup.wait_frozen(ct.cgroup_path) run_conf.nfs_cancellation.abort # Send SIGKILL to all processes progress('Killing container processes') recovery.kill_all ensure CGroup.thaw_tree(ct.cgroup_path) end # Give the system some time to kill the processes sleep(10) progress('Recovering container state') recovery.recover_state progress('Cleaning up') recovery.cleanup_or_taint end |