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!, 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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/osctld/commands/user/create.rb', line 106

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.new(opts[:uid_map])
  gid_map = IdMap.new(opts[:gid_map])

  # Check that the maps fit within the allocation
  {uid_map: uid_map, gid_map: gid_map}.each do |name, map|
    map.each do |entry|
      if entry.host_id < allocation[:first_id] \
         || (entry.host_id + entry.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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/osctld/commands/user/create.rb', line 82

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)



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

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)



137
138
139
140
# File 'lib/osctld/commands/user/create.rb', line 137

def create_default_mapping(allocation)
  v = "0:#{allocation[:first_id]}:#{allocation[:id_count]}"
  [v, v].map { |v| IdMap.new([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
# 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])

    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)



151
152
153
154
155
156
157
158
159
# File 'lib/osctld/commands/user/create.rb', line 151

def find_id_range
  if opts[:id_range]
    range = DB::IdRanges.find(opts[:id_range], opts[:pool])
  else
    range = 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



74
75
76
77
78
79
# File 'lib/osctld/commands/user/create.rb', line 74

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



101
102
103
# File 'lib/osctld/commands/user/create.rb', line 101

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