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

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, 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 inherited from Logged

#base_execute

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, 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



16
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
# File 'lib/osctld/commands/container/stop.rb', line 16

def execute(ct)
  manipulate(ct) do
    progress('Stopping container')

    mode =
      case (opts[: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

    begin
      Container::Hook.run(ct, :pre_stop)

    rescue HookFailed => e
      error!(e.message)
    end

    begin
      ContainerControl::Commands::Stop.run!(
        ct,
        mode,
        timeout: opts[:timeout] || 60,
      )
    rescue ContainerControl::Error => e
      error!(e.message)
    end

    remove_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



11
12
13
14
# File 'lib/osctld/commands/container/stop.rb', line 11

def find
  ct = DB::Containers.find(opts[:id], opts[:pool])
  ct || error!('container not found')
end

#remove_cgroups(ct) ⇒ Object (protected)

Remove accounting cgroups to reset counters



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/osctld/commands/container/stop.rb', line 66

def remove_cgroups(ct)
  tries = 0

  begin
    %w(blkio cpuacct memory).each do |subsys|
      CGroup.rmpath(CGroup.real_subsystem(subsys), ct.base_cgroup_path)
    end
  rescue SystemCallError => e
    ct.log(:warn, "Error occurred while pruning cgroups: #{e.message}")

    return if tries >= 5
    tries += 1
    sleep(0.5)
    retry
  end
end