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!, #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
87
88
89
90
91
# File 'lib/osctld/commands/container/chown.rb', line 16

def execute(ct)
  new_user = DB::Users.find(opts[:user], ct.pool)

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

  Monitor::Master.demonitor(ct)

  old_user = ct.user

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

    progress('Moving LXC configuration')

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

      FileUtils.mkdir_p(dir, mode: 0o751)
      File.chown(0, new_user.ugid, dir)
    end

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

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

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

    # Configure datasets
    if new_user.uid_map != old_user.uid_map \
       || new_user.gid_map != old_user.gid_map
      datasets = ct.datasets

      datasets.reverse_each do |ds|
        progress("Unmounting dataset #{ds.relative_name}")
        zfs(:unmount, nil, ds, valid_rcs: [1])
      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
    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

  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