Class: OsCtld::Pool

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Assets::Definition, Lockable, Manipulable
Defined in:
lib/osctld/pool.rb

Overview

This class represents a data pool

Data pool contains users, groups and containers, both data and configuration. Each user/group/ct belongs to exactly one pool.

Constant Summary collapse

PROPERTY_ACTIVE =
'org.vpsadminos.osctl:active'
PROPERTY_DATASET =
'org.vpsadminos.osctl:dataset'
USER_DS =
'user'
CT_DS =
'ct'
CONF_DS =
'conf'
HOOK_DS =
'hook'
LOG_DS =
'log'
REPOSITORY_DS =
'repository'
OPTIONS =
%i(parallel_start parallel_stop)

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(name, dataset) ⇒ Pool

Returns a new instance of Pool



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osctld/pool.rb', line 33

def initialize(name, dataset)
  init_lock
  init_manipulable

  @name = name
  @dataset = dataset || name
  @state = :active
  @attrs = Attributes.new

  load_config

  @migration_key_chain = Migration::KeyChain.new(self)
  @autostart_plan = AutoStart::Plan.new(self)
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs



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

def attrs
  @attrs
end

#autostart_planObject (readonly)

Returns the value of attribute autostart_plan



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

def autostart_plan
  @autostart_plan
end

#datasetObject (readonly)

Returns the value of attribute dataset



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

def dataset
  @dataset
end

#migration_key_chainObject (readonly)

Returns the value of attribute migration_key_chain



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

def migration_key_chain
  @migration_key_chain
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state



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

def state
  @state
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/osctld/pool.rb', line 301

def active?
  state == :active
end

#apparmor_dirObject



357
358
359
# File 'lib/osctld/pool.rb', line 357

def apparmor_dir
  File.join(run_dir, 'apparmor')
end

#assetsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/osctld/pool.rb', line 56

def assets
  define_assets do |add|
    # Datasets
    add.dataset(
      ds(USER_DS),
      desc: 'Contains user homes and LXC configuration',
      user: 0,
      group: 0,
      mode: 0511
    )
    add.dataset(
      ds(CT_DS),
      desc: 'Contains container root filesystems',
      user: 0,
      group: 0,
      mode: 0511
    )
    add.dataset(
      ds(CONF_DS),
      desc: 'Configuration files',
      user: 0,
      group: 0,
      mode: 0500
    )
    add.dataset(
      ds(HOOK_DS),
      desc: 'User supplied script hooks',
      user: 0,
      group: 0,
      mode: 0500
    )
    add.dataset(
      ds(LOG_DS),
      desc: 'Container log files, pool history',
      user: 0,
      group: 0,
      mode: 0511
    )
    add.dataset(
      ds(REPOSITORY_DS),
      desc: 'Local template repository cache',
      user: Repository::UID,
      group: 0,
      mode: 0500
    )

    # Configs
    add.directory(
      File.join(conf_path, 'pool'),
      desc: 'Pool configuration files for osctld',
      user: 0,
      group: 0,
      mode: 0500
    )
    add.file(
      config_path,
      desc: 'Pool configuration file for osctld',
      optional: true
    )
    add.directory(
      File.join(conf_path, 'user'),
      desc: 'User configuration files for osctld',
      user: 0,
      group: 0,
      mode: 0500
    )
    add.directory(
      File.join(conf_path, 'group'),
      desc: 'Group configuration files for osctld',
      user: 0,
      group: 0,
      mode: 0500
    )
    add.directory(
      File.join(conf_path, 'ct'),
      desc: 'Container configuration files for osctld',
      user: 0,
      group: 0,
      mode: 0500
    )
    add.directory(
      File.join(conf_path, 'migration'),
      desc: 'Identity and authorized keys for migrations',
      user: 0,
      group: 0,
      mode: 0500
    )

    # Logs
    add.directory(
      File.join(log_path, 'ct'),
      desc: 'Container log files',
      user: 0,
      group: 0
    )

    # Hooks
    add.directory(
      File.join(user_hook_script_dir, 'ct'),
      desc: 'User supplied container script hooks',
      user: 0,
      group: 0
    )

    # Pool history
    History.assets(pool, add)

    # Migration
    migration_key_chain.assets(add)

    # Runstate
    add.directory(
      run_dir,
      desc: 'Runtime configuration',
      user: 0,
      group: 0,
      mode: 0711
    )
    add.directory(
      console_dir,
      desc: 'Sockets for container consoles',
      user: 0,
      group: 0,
      mode: 0711
    )
    add.directory(
      hook_dir,
      desc: 'Container hooks',
      user: 0,
      group: 0,
      mode: 0711
    )
    add.directory(
      mount_dir,
      desc: 'Mount helper directories for containers',
      user: 0,
      group: 0,
      mode: 0711
    )
    add.directory(
      apparmor_dir,
      desc: 'AppArmor files',
      user: 0,
      group: 0,
      mode: 0700
    )

    AppArmor.assets(add, pool)
  end
end

#autostartObject



293
294
295
# File 'lib/osctld/pool.rb', line 293

def autostart
  autostart_plan.start
end

#check_user_conflict(user) ⇒ Object (protected)



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/osctld/pool.rb', line 540

def check_user_conflict(user)
  DB::Users.sync do
    if u = DB::Users.by_name(user.name)
      log(
        :warn,
        "Unable to load user '#{user.name}': "+
        "name already taken by pool '#{u.pool.name}'"
      )
      return false

    elsif u = DB::Users.by_ugid(user.ugid)
      log(
        :warn,
        "Unable to load user '#{user.name}': "+
        "user/group ID #{user.ugid} already taken by pool '#{u.pool.name}'"
      )
      return false
    end
  end

  true
end

#conf_pathObject



317
318
319
# File 'lib/osctld/pool.rb', line 317

def conf_path
  path(CONF_DS)
end

#config_pathObject



361
362
363
# File 'lib/osctld/pool.rb', line 361

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

#console_dirObject



349
350
351
# File 'lib/osctld/pool.rb', line 349

def console_dir
  File.join(run_dir, 'console')
end

#ct_dsObject



309
310
311
# File 'lib/osctld/pool.rb', line 309

def ct_ds
  ds(CT_DS)
end

#default_optsObject (protected)



384
385
386
387
388
389
# File 'lib/osctld/pool.rb', line 384

def default_opts
  {
    parallel_start: 2,
    parallel_stop: 4,
  }
end

#disableObject



305
306
307
# File 'lib/osctld/pool.rb', line 305

def disable
  @state = :disabled
end

#ds(path) ⇒ Object (protected)



569
570
571
# File 'lib/osctld/pool.rb', line 569

def ds(path)
  File.join(dataset, path)
end

#dump_optsObject (protected)



391
392
393
394
395
396
397
398
399
400
# File 'lib/osctld/pool.rb', line 391

def dump_opts
  ret = {}

  OPTIONS.each do |k|
    v = instance_variable_get("@#{k}")
    ret[k.to_s] = v unless v.nil?
  end

  ret
end

#ensure_limits(ct) ⇒ Object (protected)



563
564
565
566
567
# File 'lib/osctld/pool.rb', line 563

def ensure_limits(ct)
  if ct.prlimits.contains?('nofile')
    SystemLimits.ensure_nofile(ct.prlimits['nofile'].hard)
  end
end

#hook_dirObject



345
346
347
# File 'lib/osctld/pool.rb', line 345

def hook_dir
  File.join(run_dir, 'hooks')
end

#idObject



48
49
50
# File 'lib/osctld/pool.rb', line 48

def id
  name
end

#load_configObject (protected)



374
375
376
377
378
379
380
381
382
# File 'lib/osctld/pool.rb', line 374

def load_config
  return unless File.exist?(config_path)

  cfg = YAML.load_file(config_path)

  @parallel_start = cfg['parallel_start']
  @parallel_stop = cfg['parallel_stop']
  @attrs = Attributes.load(cfg['attrs'] || {})
end

#load_ctsObject (protected)



478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/osctld/pool.rb', line 478

def load_cts
  log(:info, "Loading containers")

  Dir.glob(File.join(conf_path, 'ct', '*.yml')).each do |f|
    ctid = File.basename(f)[0..(('.yml'.length+1) * -1)]

    ct = Container.new(self, ctid)
    ensure_limits(ct)
    ct.lxc_config.configure
    Monitor::Master.monitor(ct)
    Console.reconnect_tty0(ct) if ct.current_state == :running
    DB::Containers.add(ct)
  end
end

#load_groupsObject (protected)



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/osctld/pool.rb', line 453

def load_groups
  log(:info, "Loading groups")
  DB::Groups.setup(self)

  rx = /^#{Regexp.escape(File.join(conf_path, 'group'))}(.*)\/config\.yml$/

  Dir.glob(File.join(conf_path, 'group', '**', 'config.yml')).each do |file|
    next unless rx =~ file
    name = $1
    next if ['', '/default'].include?(name)

    DB::Groups.add(Group.new(self, name, devices: false))
  end

  # The devices in the root group have to be configured as soon as possible,
  # because `echo a > devices.deny` will not work when the root cgroup has
  # any children.
  root = DB::Groups.root(self)

  # Initialize devices of all groups, from the root group down
  root.descendants.each do |grp|
    grp.devices.init
  end
end

#load_repositoriesObject (protected)



493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/osctld/pool.rb', line 493

def load_repositories
  log(:info, "Loading repositories")
  DB::Repositories.setup(self)

  Dir.glob(File.join(conf_path, 'repository', '*.yml')).each do |f|
    name = File.basename(f)[0..(('.yml'.length+1) * -1)]
    next if name == 'default'

    repo = Repository.new(self, name)
    DB::Repositories.add(repo)
  end
end

#load_usersObject (protected)



440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/osctld/pool.rb', line 440

def load_users
  log(:info, "Loading users")

  Dir.glob(File.join(conf_path, 'user', '*.yml')).each do |f|
    name = File.basename(f)[0..(('.yml'.length+1) * -1)]
    u = User.new(self, name)
    next unless check_user_conflict(u)

    DB::Users.add(u)
    UserControl::Supervisor.start_server(u)
  end
end

#log_pathObject



321
322
323
# File 'lib/osctld/pool.rb', line 321

def log_path
  path(LOG_DS)
end

#log_typeObject



333
334
335
# File 'lib/osctld/pool.rb', line 333

def log_type
  "pool=#{name}"
end

#manipulation_resourceObject



337
338
339
# File 'lib/osctld/pool.rb', line 337

def manipulation_resource
  ['pool', name]
end

#mkdatasetsObject (protected)



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/osctld/pool.rb', line 408

def mkdatasets
  log(:info, "Ensuring presence of base datasets and directories")
  zfs(:create, '-p', ds(USER_DS))
  zfs(:create, '-p', ds(CT_DS))
  zfs(:create, '-p', ds(CONF_DS))
  zfs(:create, '-p', ds(HOOK_DS))
  zfs(:create, '-p', ds(LOG_DS))
  zfs(:create, '-p', ds(REPOSITORY_DS))

  File.chmod(0511, path(USER_DS))
  File.chmod(0511, path(CT_DS))
  File.chmod(0500, path(CONF_DS))
  File.chmod(0500, path(HOOK_DS))
  File.chmod(0511, path(LOG_DS))

  File.chown(Repository::UID, 0, path(REPOSITORY_DS))
  File.chmod(0500, path(REPOSITORY_DS))

  # Configuration directories
  %w(pool ct group user migration repository).each do |dir|
    path = File.join(conf_path, dir)
    Dir.mkdir(path, 0500) unless Dir.exist?(path)
  end

  [
    File.join(user_hook_script_dir, 'ct'),
    File.join(log_path, 'ct'),
  ].each do |path|
    Dir.mkdir(path) unless Dir.exist?(path)
  end
end

#mount_dirObject



353
354
355
# File 'lib/osctld/pool.rb', line 353

def mount_dir
  File.join(run_dir, 'mounts')
end

#path(ds = '') ⇒ Object (protected)



573
574
575
# File 'lib/osctld/pool.rb', line 573

def path(ds = '')
  File.join('/', dataset, ds)
end

#poolObject



52
53
54
# File 'lib/osctld/pool.rb', line 52

def pool
  self
end

#repo_pathObject



329
330
331
# File 'lib/osctld/pool.rb', line 329

def repo_path
  path(REPOSITORY_DS)
end

#run_dirObject



341
342
343
# File 'lib/osctld/pool.rb', line 341

def run_dir
  File.join(RunState::POOL_DIR, name)
end

#runstateObject (protected)



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/osctld/pool.rb', line 506

def runstate
  Dir.mkdir(run_dir, 0711) unless Dir.exist?(run_dir)

  [console_dir, hook_dir, mount_dir].each do |dir|
    Dir.mkdir(dir, 0711) unless Dir.exist?(dir)
  end

  [apparmor_dir].each do |dir|
    Dir.mkdir(dir, 0700) unless Dir.exist?(dir)
  end

  %w(
    ct-pre-start
    ct-pre-mount
    ct-post-mount
    ct-autodev
    ct-on-start
    ct-post-stop
  ).each do |hook|
    symlink = OsCtld.hook_run(hook, self)
    hook_src = OsCtld::hook_src(hook)

    if File.symlink?(symlink)
      if File.readlink(symlink) == hook_src
        next
      else
        File.unlink(symlink)
      end
    end

    File.symlink(hook_src, symlink)
  end
end

#save_configObject (protected)



402
403
404
405
406
# File 'lib/osctld/pool.rb', line 402

def save_config
  regenerate_file(config_path, 0400) do |f|
    f.write(YAML.dump(dump_opts.merge(attrs.dump)))
  end
end

#set(opts) ⇒ Object

Set pool options

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :parallel_start (Integer)
  • :parallel_stop (Integer)
  • :attrs (Hash)


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/osctld/pool.rb', line 250

def set(opts)
  opts.each do |k, v|
    case k
    when :parallel_start
      instance_variable_set(:"@#{k}", opts[k])
      pool.autostart_plan.resize(opts[k])

    when :parallel_stop
      instance_variable_set(:"@#{k}", opts[k])

    when :attrs
      attrs.update(v)

    else
      fail "unsupported option '#{k}'"
    end
  end

  save_config
end

#setupObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/osctld/pool.rb', line 207

def setup
  # Ensure needed datasets are present
  mkdatasets

  # Setup run state, i.e. hooks
  runstate

  # Load users from zpool
  load_users

  # Register loaded users into the system
  Commands::User::Register.run(all: true)

  # Generate /etc/subuid and /etc/subgid
  Commands::User::SubUGIds.run

  # Load groups
  load_groups

  # Load containers from zpool
  load_cts

  # Setup AppArmor profiles
  AppArmor.setup_pool(pool)

  # Allow containers to create veth interfaces
  Commands::User::LxcUsernet.run

  # Load migration keys
  migration_key_chain.setup

  # Load repositories
  load_repositories

  # Open history
  History.open(self)
end

#stopObject



297
298
299
# File 'lib/osctld/pool.rb', line 297

def stop
  autostart_plan.stop
end

#unset(opts) ⇒ Object

Reset pool options

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :options (Array<Symbol>)
  • :attrs (Array<String>)


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/osctld/pool.rb', line 275

def unset(opts)
  opts.each do |k, v|
    case k
    when :options
      OPTIONS.each do |opt|
        next unless v.include?(opt)

        remove_instance_variable(:"@#{opt}")
      end

    when :attrs
      v.each { |attr| attrs.unset(attr) }
    end
  end

  save_config
end

#user_dsObject



313
314
315
# File 'lib/osctld/pool.rb', line 313

def user_ds
  ds(USER_DS)
end

#user_hook_script_dirObject



325
326
327
# File 'lib/osctld/pool.rb', line 325

def user_hook_script_dir
  path(HOOK_DS)
end