Class: OsCtld::Commands::Container::Chown

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/container/chown.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
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osctld/commands/container/chown.rb', line 16

def execute(ct)
  user = DB::Users.find(opts[:user], ct.pool)
  error!('user not found') unless user

  error!("already owned by #{user.name}") if ct.user == user

  error!('container has to be stopped first') if ct.state != :stopped
  Monitor::Master.demonitor(ct)

  old_user = ct.user

  user.inclusively do
    ct.exclusively do
      # Double check state while having exclusive lock
      error!('container has to be stopped first') if ct.state != :stopped

      progress('Moving LXC configuration')

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

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

      # Move CT dir
      syscmd("mv #{ct.lxc_dir} #{ct.lxc_dir(user: user)}")
      File.chown(0, user.ugid, ct.lxc_dir(user: user))

      # Chown assets
      File.chown(0, user.ugid, ct.log_path) if File.exist?(ct.log_path)

      # Switch user, regenerate configs
      ct.chown(user)

      # Configure datasets
      datasets = ct.datasets

      datasets.reverse_each do |ds|
        progress("Unmounting dataset #{ds.relative_name}")
        zfs(:unmount, nil, ds)
      end

      datasets.each do |ds|
        progress("Setting UID/GID mapping of #{ds.relative_name}")
        zfs(
          :set,
          "uidmap=\"#{ct.uid_map.map(&:to_s).join(',')}\" "+
          "gidmap=\"#{ct.gid_map.map(&:to_s).join(',')}\"",
          ds
        )

        progress("Remounting dataset #{ds.relative_name}")
        zfs(:mount, nil, ds)
      end

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

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

  call_cmd(Commands::User::LxcUsernet)
  ok
end

#findObject



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

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