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

def execute(ct)
  grp = DB::Groups.find(opts[:group], ct.pool)
  return error('group not found') unless grp

  return error("already in group #{grp.name}") if ct.group == grp
  return error('container has to be stopped first') if ct.state != :stopped

  old_grp = ct.group

  grp.inclusively do
    ct.exclusively do
      # Double check state while having exclusive lock
      next 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!(grp)

        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 grp.setup_for?(ct.user)
        dir = grp.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: grp)}")

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

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

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

      ok
    end
  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