Class: OsCtld::Group

Inherits:
Object
  • Object
show all
Includes:
Assets::Definition, Lockable, Manipulable
Defined in:
lib/osctld/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assets::Definition

#define_assets

Methods included from Manipulable

#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



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/osctld/group.rb', line 13

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

#attrsObject (readonly)

Returns the value of attribute attrs



11
12
13
# File 'lib/osctld/group.rb', line 11

def attrs
  @attrs
end

#cgparamsObject (readonly)

Returns the value of attribute cgparams



11
12
13
# File 'lib/osctld/group.rb', line 11

def cgparams
  @cgparams
end

#devicesObject (readonly)

Returns the value of attribute devices



11
12
13
# File 'lib/osctld/group.rb', line 11

def devices
  @devices
end

#nameObject (readonly)

Returns the value of attribute name



11
12
13
# File 'lib/osctld/group.rb', line 11

def name
  @name
end

#poolObject (readonly)

Returns the value of attribute pool



11
12
13
# File 'lib/osctld/group.rb', line 11

def pool
  @pool
end

Instance Method Details

#abs_cgroup_path(subsystem) ⇒ Object



124
125
126
# File 'lib/osctld/group.rb', line 124

def abs_cgroup_path(subsystem)
  File.join(CGroup::FS, CGroup.real_subsystem(subsystem), cgroup_path)
end

#abs_full_cgroup_path(subsystem, user) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/osctld/group.rb', line 128

def abs_full_cgroup_path(subsystem, user)
  File.join(
    CGroup::FS,
    CGroup.real_subsystem(subsystem),
    full_cgroup_path(user)
  )
end

#any_container_running?Boolean

Return `true` if any container from this or any descendant group is running.

Returns:

  • (Boolean)


236
237
238
239
240
241
242
243
244
# File 'lib/osctld/group.rb', line 236

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

#assetsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osctld/group.rb', line 46

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_pathObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/osctld/group.rb', line 108

def cgroup_path
  if root?
    path

  else
    File.join(
      DB::Groups.root(pool).path,
      *name.split('/').drop(1).map { |v| "group.#{v}" }
    )
  end
end

#childrenArray<Group>

Return all groups that are direct descendants

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/osctld/group.rb', line 184

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_dirObject



100
101
102
# File 'lib/osctld/group.rb', line 100

def config_dir
  File.join(pool.conf_path, 'group', id)
end

#config_pathObject



104
105
106
# File 'lib/osctld/group.rb', line 104

def config_path
  File.join(pool.conf_path, 'group', id, 'config.yml')
end

#configure(path: nil, devices: true) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/osctld/group.rb', line 38

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

#containersObject



223
224
225
226
227
228
229
230
231
232
# File 'lib/osctld/group.rb', line 223

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

#descendantsArray<Group>

Return all groups below the current group's path

Returns:



201
202
203
204
205
206
207
208
209
210
# File 'lib/osctld/group.rb', line 201

def descendants
  groups = DB::Groups.get.select { |grp| grp.pool == pool }

  if root?
    groups.drop(1) # remove the root group, which is first

  else
    groups.select { |grp| grp.name.start_with?("#{name}/") }
  end.sort! { |a, b| a.name <=> b.name }
end

#full_cgroup_path(user) ⇒ Object



120
121
122
# File 'lib/osctld/group.rb', line 120

def full_cgroup_path(user)
  File.join(cgroup_path, "user.#{user.name}")
end

#groups_in_pathArray<Group>

Return all groups leading to this group's path, i.e. all parents and the group itself.

Returns:



178
179
180
# File 'lib/osctld/group.rb', line 178

def groups_in_path
  parents + [self]
end

#has_containers?(user = nil) ⇒ Boolean

Parameters:

  • user (User, nil) (defaults to: nil)

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
# File 'lib/osctld/group.rb', line 213

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

#idObject



26
27
28
# File 'lib/osctld/group.rb', line 26

def id
  @name
end

#load_config(config = nil) ⇒ Object (protected)



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/osctld/group.rb', line 284

def load_config(config = nil)
  if config
    cfg = YAML.load(config)
  else
    cfg = YAML.load_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_typeObject



257
258
259
# File 'lib/osctld/group.rb', line 257

def log_type
  "group=#{pool.name}:#{name}"
end

#manipulation_resourceObject



261
262
263
# File 'lib/osctld/group.rb', line 261

def manipulation_resource
  ['group', "#{pool.name}:#{name}"]
end

#parentGroup?

Return the closest parent group

Returns:



170
171
172
173
# File 'lib/osctld/group.rb', line 170

def parent
  return if root?
  parents.last
end

#parentsArray<Group>

Return all parent groups, from the root group to the closest parent

Returns:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/osctld/group.rb', line 150

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

#pathObject



34
35
36
# File 'lib/osctld/group.rb', line 34

def path
  root? ? @path : File.join(DB::Groups.root(pool).path, name)
end

#root?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/osctld/group.rb', line 30

def root?
  @root
end

#save_configObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/osctld/group.rb', line 265

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(YAML.dump(cfg))
  end

  File.chown(0, 0, config_path)
end

#set(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Hash)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctld/group.rb', line 70

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

Returns:

  • (Boolean)


144
145
146
# File 'lib/osctld/group.rb', line 144

def setup_for?(user)
  Dir.exist?(userdir(user))
end

#unset(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Array<String>)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/osctld/group.rb', line 86

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



136
137
138
139
140
141
142
# File 'lib/osctld/group.rb', line 136

def userdir(user)
  File.join(
    user.userdir,
    *name.split('/').drop(1).map { |v| "group.#{v}" },
    'cts'
  )
end

#usersObject



246
247
248
249
250
251
252
253
254
255
# File 'lib/osctld/group.rb', line 246

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