Class: OsCtld::Commands::User::Create

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/user/create.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

#block_index_with_custom_mapping(u) ⇒ Object (protected)

Custom mapping on an existing or a new block on a specific position



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/osctld/commands/user/create.rb', line 112

def block_index_with_custom_mapping(u)
  range = find_id_range
  allocation = range.export_at(opts[:block_index])

  if allocation[:type] == :free
    allocation = range.allocate(
      1,
      block_index: opts[:block_index],
      owner: u.id_range_allocation_owner
    )
    progress("Allocated block ##{allocation[:block_index]} from ID range #{range.name}")
  else
    progress("Using block ##{allocation[:block_index]} from ID range #{range.name}")
  end

  uid_map = IdMap.from_string_list(opts[:uid_map])
  gid_map = IdMap.from_string_list(opts[:gid_map])

  # Check that the maps fit within the allocation
  { uid_map:, gid_map: }.each do |name, map|
    map.each do |entry|
      if entry.host_id < allocation[:first_id] \
         || (entry.host_id + entry.id_count - 1) > allocation[:last_id]
        error!("#{name} does not fit within the ID range allocation")
      end
    end
  end

  [uid_map, gid_map]
end

#block_index_with_default_mapping(u) ⇒ Object (protected)

Use an existing or a new block at a specific position, create a default mapping



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/osctld/commands/user/create.rb', line 88

def block_index_with_default_mapping(u)
  range = find_id_range
  allocation = range.export_at(opts[:block_index])

  if allocation[:type] == :free
    allocation = range.allocate(
      1,
      block_index: opts[:block_index],
      owner: u.id_range_allocation_owner
    )
    progress("Allocated block ##{allocation[:block_index]} from ID range #{range.name}")
  else
    progress("Using block ##{allocation[:block_index]} from ID range #{range.name}")
  end

  create_default_mapping(allocation)
end

#check_mappings!(uid_map, gid_map) ⇒ Object (protected)



148
149
150
151
152
153
154
155
# File 'lib/osctld/commands/user/create.rb', line 148

def check_mappings!(uid_map, gid_map)
  if !uid_map.valid?
    error!('UID map is not valid')

  elsif !gid_map.valid?
    error!('GID map is not valid')
  end
end

#create_default_mapping(allocation) ⇒ Object (protected)



143
144
145
146
# File 'lib/osctld/commands/user/create.rb', line 143

def create_default_mapping(allocation)
  s = "0:#{allocation[:first_id]}:#{allocation[:id_count]}"
  [s, s].map { |v| IdMap.from_string_list([v]) }
end

#execute(u) ⇒ Object



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
# File 'lib/osctld/commands/user/create.rb', line 27

def execute(u)
  # Possibilities:
  # 1) user new [--id-range *range*] kkt
  #    -> allocate new block, add default map
  #
  # 2) user new [--id-range *range*] --id-range-block-index *n* kkt
  #    -> use existing block, or allocate at index
  #
  # 3) user new --map 0:123000:65536 kkt
  #    -> do not allocate anything, use custom map as is
  #
  # 4) user new [--id-range *range*] --id-range-block-index *n* --map 0:123000:65536 kkt
  #    -> do not allocate anything, use custom map, but check that it fits
  #       in that allocation

  uid_map, gid_map =
    if !opts[:block_index] && !opts[:uid_map] && !opts[:gid_map]
      new_block_with_default_mapping(u)

    elsif opts[:block_index] && !opts[:uid_map] && !opts[:gid_map]
      block_index_with_default_mapping(u)

    elsif !opts[:range] && !opts[:block_index] && opts[:uid_map] && opts[:gid_map]
      no_block_with_custom_mapping(u)

    elsif opts[:block_index] && opts[:uid_map] && opts[:gid_map]
      block_index_with_custom_mapping(u)

    else
      error!('unsupported flag combination')
    end

  check_mappings!(uid_map, gid_map)

  manipulate(u) do
    u.configure(
      uid_map,
      gid_map,
      ugid: opts[:ugid],
      standalone: opts[:standalone]
    )

    call_cmd!(Commands::User::Setup, user: u)
    call_cmd!(Commands::User::Register, name: u.name, pool: u.pool.name)
    call_cmd!(Commands::User::SubUGIds)
  end

  ok
end

#findObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/osctld/commands/user/create.rb', line 12

def find
  pool = DB::Pools.get_or_default(opts[:pool])
  error!('pool not found') unless pool

  rx = /^[a-z0-9_-]{1,#{32 - 1 - pool.name.length}}$/

  if rx !~ opts[:name]
    error!("invalid name, allowed format: #{rx.source}")
  end

  u = User.new(pool, opts[:name], load: false)
  error!('user already exists') if DB::Users.contains?(u.name, pool)
  u
end

#find_id_rangeObject (protected)



157
158
159
160
161
162
163
164
165
# File 'lib/osctld/commands/user/create.rb', line 157

def find_id_range
  range = if opts[:id_range]
            DB::IdRanges.find(opts[:id_range], opts[:pool])
          else
            DB::IdRanges.find('default', opts[:pool])
          end

  range || error!('ID range not found')
end

#new_block_with_default_mapping(u) ⇒ Object (protected)

Allocate a new block, create a default mapping



80
81
82
83
84
85
# File 'lib/osctld/commands/user/create.rb', line 80

def new_block_with_default_mapping(u)
  range = find_id_range
  allocation = range.allocate(1, owner: u.id_range_allocation_owner)
  progress("Allocated block ##{allocation[:block_index]} from ID range #{range.name}")
  create_default_mapping(allocation)
end

#no_block_with_custom_mapping(_u) ⇒ Object (protected)

Do not allocate anything, use a custom map



107
108
109
# File 'lib/osctld/commands/user/create.rb', line 107

def no_block_with_custom_mapping(_u)
  [opts[:uid_map], opts[:gid_map]].map { |v| IdMap.from_string_list(v) }
end