Class: OsCtld::Commands::Container::RecoverCleanup

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/container/recover_cleanup.rb

Defined Under Namespace

Classes: RouteList

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

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

#cleanup_cgroups(ct) ⇒ Object (protected)



33
34
35
36
37
38
# File 'lib/osctld/commands/container/recover_cleanup.rb', line 33

def cleanup_cgroups(ct)
  progress('Removing leftover cgroups')
  CGroup.rmpath_all(File.join(ct.cgroup_path, 'lxc.payload'))
  CGroup.rmpath_all(File.join(ct.cgroup_path, 'lxc.monitor'))
  CGroup.rmpath_all(File.join(ct.cgroup_path, 'lxc.pivot'))
end

#cleanup_netifs(ct) ⇒ Object (protected)



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/commands/container/recover_cleanup.rb', line 40

def cleanup_netifs(ct)
  progress('Searching for stray network interfaces')
  veths = []

  [4, 6].each do |ip_v|
    routes = RouteList.new(ip_v)

    ct.netifs.each do |netif|
      next if netif.type != :routed

      netif.routes.each_version(ip_v) do |route|
        veth = routes.veth_of(route)

        if veth
          progress("Found route #{route.addr.to_string} on #{veth}")
          veths << veth unless veths.include?(veth)
        end
      end
    end
  end

  veths.each do |veth|
    found = DB::Containers.get.detect do |ct|
      n = ct.netifs.detect do |netif|
        netif.respond_to?(:veth) && netif.veth == veth
      end
      n && ct
    end

    if found
      progress("Interface #{veth} is used by container #{found.ident}")
    else
      progress("Removing #{veth}")
      syscmd("ip link delete #{veth}")
    end
  end
end

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/osctld/commands/container/recover_cleanup.rb', line 11

def execute
  ct = DB::Containers.find(opts[:id], opts[:pool])
  return error('container not found') unless ct

  manipulate(ct) do
    if ct.state != :stopped && !opts[:force]
      error!('the container has to be stopped')
    end

    if opts[:cleanup] == 'all' || opts[:cleanup].include?('cgroups')
      cleanup_cgroups(ct)
    end

    if opts[:cleanup] == 'all' || opts[:cleanup].include?('netifs')
      cleanup_netifs(ct)
    end

    ok
  end
end