Class: OsCtld::Group
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, config: nil, devices: true, root: false) ⇒ Group
Returns a new instance of Group.
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/osctld/group.rb', line 14
def initialize(pool, name, load: true, config: nil, devices: true, root: false)
init_lock
init_manipulable
@pool = pool
@name = name
@root = root
@cgparams = nil
@devices = nil
@attrs = Attributes.new
load_config(config) if load
devices.init if load && devices
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
12
13
14
|
# File 'lib/osctld/group.rb', line 12
def attrs
@attrs
end
|
#cgparams ⇒ Object
Returns the value of attribute cgparams.
12
13
14
|
# File 'lib/osctld/group.rb', line 12
def cgparams
@cgparams
end
|
#devices ⇒ Object
Returns the value of attribute devices.
12
13
14
|
# File 'lib/osctld/group.rb', line 12
def devices
@devices
end
|
#name ⇒ Object
Returns the value of attribute name.
12
13
14
|
# File 'lib/osctld/group.rb', line 12
def name
@name
end
|
#pool ⇒ Object
Returns the value of attribute pool.
12
13
14
|
# File 'lib/osctld/group.rb', line 12
def pool
@pool
end
|
Instance Method Details
#abs_cgroup_path(subsystem) ⇒ Object
125
126
127
|
# File 'lib/osctld/group.rb', line 125
def abs_cgroup_path(subsystem)
CGroup.abs_cgroup_path(subsystem, cgroup_path)
end
|
#abs_full_cgroup_path(subsystem, user) ⇒ Object
129
130
131
|
# File 'lib/osctld/group.rb', line 129
def abs_full_cgroup_path(subsystem, user)
CGroup.abs_cgroup_path(subsystem, full_cgroup_path(user))
end
|
#any_container_running? ⇒ Boolean
Return `true` if any container from this or any descendant group is running.
233
234
235
236
237
238
239
240
241
|
# File 'lib/osctld/group.rb', line 233
def any_container_running?
groups = [self] + descendants
DB::Containers.get.each do |ct|
return true if ct.pool == pool && groups.include?(self) && ct.running?
end
false
end
|
#assets ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/osctld/group.rb', line 47
def assets
define_assets do |add|
add.file(
config_path,
desc: "osctld's group config",
user: 0,
group: 0,
mode: 0400
)
users.each do |u|
add.directory(
userdir(u),
desc: "LXC path for #{u.name}:#{name}",
user: 0,
group: u.ugid,
mode: 0751
)
end
end
end
|
#cgroup_path ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/osctld/group.rb', line 109
def cgroup_path
if root?
path
else
File.join(
DB::Groups.root(pool).path,
*name.split('/').drop(1).map { |v| "group.#{v}" }
)
end
end
|
#children ⇒ Array<Group>
Return all groups that are direct descendants
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/osctld/group.rb', line 181
def children
DB::Groups.get.select do |grp|
next if grp.pool != pool || grp.name == name
if root?
s = '/'
else
s = "#{name}/"
end
grp.name.start_with?(s) && grp.name[s.size..-1].index('/').nil?
end.sort! { |a, b| a.name <=> b.name }
end
|
#config_dir ⇒ Object
101
102
103
|
# File 'lib/osctld/group.rb', line 101
def config_dir
File.join(pool.conf_path, 'group', id)
end
|
#config_path ⇒ Object
105
106
107
|
# File 'lib/osctld/group.rb', line 105
def config_path
File.join(pool.conf_path, 'group', id, 'config.yml')
end
|
39
40
41
42
43
44
45
|
# File 'lib/osctld/group.rb', line 39
def configure(path: nil, devices: true)
@path = path if root?
@cgparams = CGroup::Params.new(self)
@devices = Devices::GroupManager.new(self)
@devices.init if devices
save_config
end
|
#containers ⇒ Object
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/osctld/group.rb', line 220
def containers
ret = []
DB::Containers.get.each do |ct|
next if ct.pool != pool || ct.group != self || ret.include?(ct)
ret << ct
end
ret
end
|
#descendants ⇒ Array<Group>
Return all groups below the current group's path
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/osctld/group.rb', line 198
def descendants
groups = DB::Groups.get.select { |grp| grp.pool == pool }
if root?
groups.drop(1)
else
groups.select { |grp| grp.name.start_with?("#{name}/") }
end.sort! { |a, b| a.name <=> b.name }
end
|
#full_cgroup_path(user) ⇒ Object
121
122
123
|
# File 'lib/osctld/group.rb', line 121
def full_cgroup_path(user)
File.join(cgroup_path, "user.#{user.name}")
end
|
#groups_in_path ⇒ Array<Group>
Return all groups leading to this group's path, i.e. all parents and the group itself.
175
176
177
|
# File 'lib/osctld/group.rb', line 175
def groups_in_path
parents + [self]
end
|
#has_containers?(user = nil) ⇒ Boolean
210
211
212
213
214
215
216
217
218
|
# File 'lib/osctld/group.rb', line 210
def has_containers?(user = nil)
ct = DB::Containers.get.detect do |ct|
ct.pool.name == pool.name \
&& ct.group.name == name \
&& (user.nil? || ct.user.name == user.name)
end
ct ? true : false
end
|
#id ⇒ Object
27
28
29
|
# File 'lib/osctld/group.rb', line 27
def id
@name
end
|
#load_config(config = nil) ⇒ Object
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/osctld/group.rb', line 281
def load_config(config = nil)
if config
cfg = OsCtl::Lib::ConfigFile.load_yaml(config)
else
cfg = OsCtl::Lib::ConfigFile.load_yaml_file(config_path)
end
@path = cfg['path'] if root?
@cgparams = CGroup::Params.load(self, cfg['cgparams'])
@devices = Devices::GroupManager.load(self, cfg['devices'] || [])
@attrs = Attributes.load(cfg['attrs'] || {})
end
|
#log_type ⇒ Object
254
255
256
|
# File 'lib/osctld/group.rb', line 254
def log_type
"group=#{pool.name}:#{name}"
end
|
#manipulation_resource ⇒ Object
258
259
260
|
# File 'lib/osctld/group.rb', line 258
def manipulation_resource
['group', "#{pool.name}:#{name}"]
end
|
#parent ⇒ Group?
Return the closest parent group
167
168
169
170
|
# File 'lib/osctld/group.rb', line 167
def parent
return if root?
parents.last
end
|
#parents ⇒ Array<Group>
Return all parent groups, from the root group to the closest parent
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/osctld/group.rb', line 147
def parents
return [] if root?
ret = []
t = ''
name.split('/')[0..-2].each do |n|
t = File.join('/', t, n)
g = DB::Groups.by_path(pool, t)
raise GroupNotFound, "group '#{t}' not found" if g.nil?
ret << g
end
ret
end
|
#path ⇒ Object
35
36
37
|
# File 'lib/osctld/group.rb', line 35
def path
root? ? @path : File.join(DB::Groups.root(pool).path, name)
end
|
#root? ⇒ Boolean
31
32
33
|
# File 'lib/osctld/group.rb', line 31
def root?
@root
end
|
#save_config ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/osctld/group.rb', line 262
def save_config
Dir.mkdir(config_dir) unless Dir.exist?(config_dir)
cfg = {
'cgparams' => cgparams.dump,
'devices' => devices.dump,
'attrs' => attrs.dump,
}
cfg['path'] = path if root?
File.open(config_path, 'w', 0400) do |f|
f.write(OsCtl::Lib::ConfigFile.dump_yaml(cfg))
end
File.chown(0, 0, config_path)
end
|
#set(opts) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/osctld/group.rb', line 71
def set(opts)
opts.each do |k, v|
case k
when :attrs
attrs.update(v)
else
fail "unsupported option '#{k}'"
end
end
save_config
end
|
#setup_for?(user) ⇒ Boolean
141
142
143
|
# File 'lib/osctld/group.rb', line 141
def setup_for?(user)
Dir.exist?(userdir(user))
end
|
#unset(opts) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/osctld/group.rb', line 87
def unset(opts)
opts.each do |k, v|
case k
when :attrs
v.each { |attr| attrs.unset(attr) }
else
fail "unsupported option '#{k}'"
end
end
save_config
end
|
#userdir(user) ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/osctld/group.rb', line 133
def userdir(user)
File.join(
user.userdir,
*name.split('/').drop(1).map { |v| "group.#{v}" },
'cts'
)
end
|
#users ⇒ Object
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/osctld/group.rb', line 243
def users
ret = []
DB::Containers.get.each do |ct|
next if ct.pool != pool || ct.group != self || ret.include?(ct.user)
ret << ct.user
end
ret
end
|