Class: OsCtld::Commands::Container::Delete

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::SwitchUser
Defined in:
lib/osctld/commands/container/delete.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctld/commands/container/delete.rb', line 16

def execute(ct)
  if !opts[:force]
    ct.exclusively do
      error!('the container is running') if ct.running?
    end
  end

  # Remove monitor _before_ acquiring exclusive lock, because monitor
  # uses inclusive lock, which would result in a deadlock
  Monitor::Master.demonitor(ct)

  manipulate(ct) do
    progress('Stopping container')
    call_cmd!(
      Commands::Container::Stop,
      pool: ct.pool.name,
      id: ct.id,
      manipulation_lock: opts[:manipulation_lock],
      progress: opts[:progress],
    )

    progress('Disconnecting console')
    Console.remove(ct)

    progress('Removing shared mount directory')
    ct.mounts.shared_dir.remove

    progress('Destroying dataset')
    zfs(:destroy, '-r', ct.dataset)

    progress('Removing LXC configuration and script hooks')
    syscmd("rm -rf #{ct.lxc_dir} #{ct.user_hook_script_dir}")
    File.unlink(ct.log_path) if File.exist?(ct.log_path)
    File.unlink(ct.config_path)

    progress('Unregistering container')
    DB::Containers.remove(ct)

    progress('Removing cgroups')
    begin
      if ct.group.has_containers?(ct.user)
        CGroup.rmpath_all(ct.base_cgroup_path)

      else
        CGroup.rmpath_all(ct.group.full_cgroup_path(ct.user))
      end
    rescue SystemCallError
      # If some of the cgroups are busy, just leave them be
    end

    progress('Removing AppArmor profile')
    ct.apparmor.destroy_namespace
    ct.apparmor.destroy_profile

    bashrc = File.join(ct.lxc_dir, '.bashrc')
    File.unlink(bashrc) if File.exist?(bashrc)

    unless ct.group.has_containers?(ct.user)
      Dir.rmdir(ct.group.userdir(ct.user))
    end
  end

  progress('Reconfiguring LXC usernet')
  call_cmd(Commands::User::LxcUsernet)

  ok
end

#findObject



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

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