Class: OsCtld::IdRange
Defined Under Namespace
Classes: AllocationError, AllocationTable
Instance Attribute Summary collapse
Instance Method Summary
collapse
#define_assets
#acquire_manipulation_lock, #init_manipulable, #is_being_manipulated?, #manipulate, #manipulated_by, #release_manipulation_lock
Methods included from Lockable
#exclusively, included, #inclusively, #init_lock, #lock, #unlock
Constructor Details
#initialize(pool, name, load: true) ⇒ IdRange
Returns a new instance of IdRange.
17
18
19
20
21
22
23
24
|
# File 'lib/osctld/id_range.rb', line 17
def initialize(pool, name, load: true)
init_lock
init_manipulable
@pool = pool
@name = name
@attrs = Attributes.new
load_config if load
end
|
Instance Attribute Details
#allocations ⇒ Object
Returns the value of attribute allocations.
187
188
189
|
# File 'lib/osctld/id_range.rb', line 187
def allocations
@allocations
end
|
#attrs ⇒ Object
Returns the value of attribute attrs.
15
16
17
|
# File 'lib/osctld/id_range.rb', line 15
def attrs
@attrs
end
|
#block_count ⇒ Object
Returns the value of attribute block_count.
15
16
17
|
# File 'lib/osctld/id_range.rb', line 15
def block_count
@block_count
end
|
#block_size ⇒ Object
Returns the value of attribute block_size.
15
16
17
|
# File 'lib/osctld/id_range.rb', line 15
def block_size
@block_size
end
|
#name ⇒ Object
Returns the value of attribute name.
15
16
17
|
# File 'lib/osctld/id_range.rb', line 15
def name
@name
end
|
#pool ⇒ Object
Returns the value of attribute pool.
15
16
17
|
# File 'lib/osctld/id_range.rb', line 15
def pool
@pool
end
|
#start_id ⇒ Object
Returns the value of attribute start_id.
15
16
17
|
# File 'lib/osctld/id_range.rb', line 15
def start_id
@start_id
end
|
Instance Method Details
#add_block_ids(block) ⇒ Object
189
190
191
192
193
194
195
196
|
# File 'lib/osctld/id_range.rb', line 189
def add_block_ids(block)
first_id = start_id + (block[:block_index] * block_size)
block.merge(
first_id:,
last_id: first_id + (block_size * block[:block_count]) - 1,
id_count: block_size * block[:block_count]
)
end
|
#allocate(block_count, opts = {}) ⇒ Hash
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/osctld/id_range.rb', line 45
def allocate(block_count, opts = {})
ret = nil
exclusively do
if opts[:block_index]
begin
ret = allocations.allocate_at(opts[:block_index], block_count, opts[:owner])
rescue ArgumentError => e
raise AllocationError, e.message
end
else
unless (ret = allocations.allocate(block_count, opts[:owner]))
raise AllocationError, 'no free space found'
end
end
save_config
end
add_block_ids(ret)
end
|
#assets ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/osctld/id_range.rb', line 89
def assets
define_assets do |add|
add.file(
config_path,
desc: 'Configuration file',
user: 0,
group: 0,
mode: 0o400
)
end
end
|
#can_delete? ⇒ Boolean
85
86
87
|
# File 'lib/osctld/id_range.rb', line 85
def can_delete?
allocations.empty?
end
|
#config_path ⇒ Object
177
178
179
|
# File 'lib/osctld/id_range.rb', line 177
def config_path
File.join(pool.conf_path, 'id-range', "#{name}.yml")
end
|
30
31
32
33
34
35
36
37
38
|
# File 'lib/osctld/id_range.rb', line 30
def configure(start_id, block_size, block_count)
exclusively do
@start_id = start_id
@block_size = block_size
@block_count = block_count
@allocations = AllocationTable.new(block_count)
save_config
end
end
|
#export ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/osctld/id_range.rb', line 137
def export
inclusively do
{
pool: pool.name,
name:,
start_id:,
last_id:,
block_size:,
block_count:,
allocated: allocations.count_allocated_blocks,
free: allocations.count_free_blocks
}.merge!(attrs.export)
end
end
|
#export_all ⇒ Object
152
153
154
155
156
|
# File 'lib/osctld/id_range.rb', line 152
def export_all
inclusively do
allocations.export_all.map { |v| add_block_ids(v) }
end
end
|
#export_allocated ⇒ Object
158
159
160
161
162
|
# File 'lib/osctld/id_range.rb', line 158
def export_allocated
inclusively do
allocations.export_allocated.map { |v| add_block_ids(v) }
end
end
|
#export_at(block_index) ⇒ Object
171
172
173
174
175
|
# File 'lib/osctld/id_range.rb', line 171
def export_at(block_index)
inclusively do
add_block_ids(allocations.export_at(block_index))
end
end
|
#export_free ⇒ Object
164
165
166
167
168
|
# File 'lib/osctld/id_range.rb', line 164
def export_free
inclusively do
allocations.export_free.map { |v| add_block_ids(v) }
end
end
|
#free_at(block_index) ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/osctld/id_range.rb', line 67
def free_at(block_index)
exclusively do
unless allocations.free_at(block_index)
raise AllocationError, "block at index #{block_index} not found"
end
save_config
end
end
|
#free_by(owner) ⇒ Object
78
79
80
81
82
83
|
# File 'lib/osctld/id_range.rb', line 78
def free_by(owner)
exclusively do
allocations.free_by(owner)
save_config
end
end
|
#id ⇒ Object
26
27
28
|
# File 'lib/osctld/id_range.rb', line 26
def id
name
end
|
#last_id ⇒ Object
133
134
135
|
# File 'lib/osctld/id_range.rb', line 133
def last_id
start_id + (block_size * block_count) - 1
end
|
#load_config ⇒ Object
198
199
200
201
202
203
204
205
206
|
# File 'lib/osctld/id_range.rb', line 198
def load_config
cfg = OsCtl::Lib::ConfigFile.load_yaml_file(config_path)
@start_id = cfg['start_id']
@block_size = cfg['block_size']
@block_count = cfg['block_count']
@allocations = AllocationTable.load(block_count, cfg['allocations'])
@attrs = Attributes.load(cfg['attrs'] || {})
end
|
#manipulation_resource ⇒ Object
181
182
183
|
# File 'lib/osctld/id_range.rb', line 181
def manipulation_resource
['id-range', "#{pool.name}:#{name}"]
end
|
#save_config ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/osctld/id_range.rb', line 208
def save_config
regenerate_file(config_path, 0o400) do |f|
f.write(OsCtl::Lib::ConfigFile.dump_yaml({
'start_id' => start_id,
'block_size' => block_size,
'block_count' => block_count,
'allocations' => allocations.dump,
'attrs' => attrs.dump
}))
end
File.chown(0, 0, config_path)
end
|
#set(opts) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/osctld/id_range.rb', line 103
def set(opts)
opts.each do |k, v|
case k
when :attrs
attrs.update(v)
else
raise "unsupported option '#{k}'"
end
end
save_config
end
|
#unset(opts) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/osctld/id_range.rb', line 119
def unset(opts)
opts.each do |k, v|
case k
when :attrs
v.each { |attr| attrs.unset(attr) }
else
raise "unsupported option '#{k}'"
end
end
save_config
end
|