Class: OsCtld::Commands::Container::Chgrp

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

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

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

def execute(ct)
  new_group = DB::Groups.find(opts[:group], ct.pool)

  if new_group.nil?
    error!('group not found')
  elsif ct.group == new_group
    error!("already in group #{new_group.name}")
  elsif ct.state != :stopped
    error!('container has to be stopped first')
  end

  old_group = ct.group

  manipulate([ct, new_group, old_group]) do
    # Double check state
    error!('container has to be stopped first') if ct.state != :stopped

    # Check that devices are available in the new group
    unless %w(provide remove).include?(opts[:missing_devices])
      begin
        ct.devices.check_all_available!(group: new_group)

      rescue DeviceNotAvailable, DeviceModeInsufficient => e
        error!(e.message)
      end
    end

    # Stop monitor for old user/group
    Monitor::Master.demonitor(ct)

    progress('Moving LXC configuration')

    # Ensure LXC home
    unless new_group.setup_for?(ct.user)
      dir = new_group.userdir(ct.user)

      FileUtils.mkdir_p(dir, mode: 0751)
      File.chown(0, ct.user.ugid, dir)
    end

    # Move CT dir
    syscmd("mv #{ct.lxc_dir} #{ct.lxc_dir(group: new_group)}")

    # Switch group, regenerate configs
    progress('Reconfiguring container')
    ct.chgrp(new_group, missing_devices: opts[:missing_devices] || 'check')

    # Restart monitor
    Monitor::Master.monitor(ct)

    # Clear old LXC home if possible
    unless old_group.has_containers?(ct.user)
      progress('Cleaning up original LXC home')
      Dir.rmdir(old_group.userdir(ct.user))
    end

    ok
  end
end

#findObject



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

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