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 Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods inherited from Logged

#base_execute

Instance Method Details

#dataset_missing?(error, dataset) ⇒ Boolean (protected)

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/osctld/commands/container/delete.rb', line 123

def dataset_missing?(error, dataset)
  output = error.output.to_s

  output.include?(dataset.to_s) && output.include?('dataset does not exist')
end

#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
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/osctld/commands/container/delete.rb', line 17

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

  manipulate(ct) do
    progress('Stopping container')
    stop_opts = {
      pool: ct.pool.name,
      id: ct.id,
      manipulation_lock: opts[:manipulation_lock],
      progress: opts[:progress],
      message: opts[:message]
    }
    stop_opts[:method] = 'kill' if opts[:force] && ct.running?

    call_cmd!(Commands::Container::Stop, **stop_opts)

    progress('Verifying recovery cleanup')
    unless Container::Recovery.new(ct).cleanup_or_taint
      error!('Unable to safely complete container cleanup')
    end

    if ct.send_log
      SendReceive.stopped_using_key(ct.pool, ct.send_log.opts.key_name)
    end

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

    progress('Removing shared mount directory')
    ct.clear_start_menu
    ct.mounts.shared_dir.remove
    BpfFs.remove_ct(ct.pool.name, ct.id)

    progress('Moving dataset to trash')
    trash_dataset(ct)

    progress('Removing LXC configuration and script hooks')
    Monitor::Master.demonitor(ct)
    syscmd("rm -rf #{ct.lxc_dir} #{ct.user_hook_script_dir}")

    if File.exist?(ct.log_path)
      File.rename(ct.log_path, "#{ct.log_path}.destroyed")
    end

    File.unlink(ct.config_path)

    progress('Unregistering container')
    DB::Containers.remove(ct)
    ct.pool.autostart_plan.clear_ct(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

    if AppArmor.enabled?
      progress('Removing AppArmor profile')
      ct.apparmor.destroy_namespace
      ct.apparmor.destroy_profile
    end

    bashrc = File.join(ct.lxc_dir, '.bashrc')
    FileUtils.rm_f(bashrc)

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

  if !ct.user.standalone && !ct.user.has_containers?
    call_cmd!(
      Commands::User::Delete,
      pool: ct.user.pool.name,
      name: ct.user.name
    )
  end

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

  ct.pool.trash_bin.prune if opts[:prune]

  ok
end

#find ⇒ Object



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

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

#trash_dataset(ct) ⇒ Object (protected)



115
116
117
118
119
120
121
# File 'lib/osctld/commands/container/delete.rb', line 115

def trash_dataset(ct)
  TrashBin.add_dataset(ct.pool, ct.dataset)
rescue SystemCommandFailed => e
  raise unless dataset_missing?(e, ct.dataset)

  ct.log(:warn, "Unable to move dataset #{ct.dataset} to trash: dataset does not exist")
end