Class: OsCtld::Commands::Container::Stop

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::Container, Utils::SwitchUser
Defined in:
lib/osctld/commands/container/stop.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods included from Utils::Container

#get_image_path, #get_repositories, #remove_accounting_cgroups

Methods inherited from Logged

#base_execute

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

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
# 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.message)
    end

    # Give the container more memory, so that the shutdown does not hang
    # in low memory situations.
    ct.cgparams.temporarily_expand_memory if ct.running?

    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.message)
    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

#findObject



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)

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/osctld/commands/container/stop.rb', line 92

def force_kill(ct)
  recovery = Container::Recovery.new(ct)

  # Freeze all processes before the kill
  CGroup.freeze_tree(ct.cgroup_path)

  # Send SIGKILL to all processes
  progress('Killing container processes')
  recovery.kill_all

  # Thaw all processes
  CGroup.thaw_tree(ct.cgroup_path)

  # 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