Class: OsVm::MachineConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/osvm/machine_config.rb

Direct Known Subclasses

NixosMachineConfig, VpsadminosMachineConfig

Defined Under Namespace

Classes: BridgeNetwork, Cpu, Disk, Network, SocketNetwork, UserNetwork

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, allocate_resources: true) ⇒ MachineConfig

Returns a new instance of MachineConfig.

Parameters:

  • cfg (Hash)


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/osvm/machine_config.rb', line 324

def initialize(cfg, allocate_resources: true)
  @spin = cfg.fetch('spin', 'vpsadminos')
  @qemu = cfg.fetch('qemu')
  @boot_mode = cfg.fetch('bootMode', 'direct')
  @boot_order = cfg['bootOrder']
  @iso = cfg['iso']
  @extra_qemu_options = cfg.fetch('extraQemuOptions', [])
  @test_shells = cfg.fetch('testShells', 1)
  # Older JSON describes guests whose shell service still uses /dev/hvcN.
  @test_shell_transport = cfg.fetch('testShellTransport', 'virtconsole')
  @shell_names = cfg.fetch('shells', [])
  @virtiofsd = cfg.fetch('virtiofsd')
  @kernel = cfg['kernel']
  @initrd = cfg['initrd']
  @kernel_params = cfg.fetch('kernelParams', [])
  @toplevel = cfg['toplevel']
  @disks = cfg.fetch('disks', []).map { |disk_cfg| Disk.new(disk_cfg) }
  @memory = cfg.fetch('memory')
  @cpus = cfg.fetch('cpus')
  @cpu = Cpu.new(cfg.fetch('cpu'))
  topology_cpus = @cpu.cores * @cpu.threads * @cpu.sockets
  if @cpus != topology_cpus
    raise ArgumentError,
          "CPU topology mismatch: cpus=#{@cpus.inspect}, cores=#{@cpu.cores.inspect}, " \
          "threads=#{@cpu.threads.inspect}, sockets=#{@cpu.sockets.inspect}, product=#{topology_cpus.inspect}"
  end
  @shared_filesystems = cfg.fetch('sharedFileSystems', {})
  @networks = cfg.fetch('networks', [{ 'type' => 'user' }]).each_with_index.map do |net_cfg, i|
    Network.from_config(i, net_cfg, allocate_resources:)
  end
  @tags = cfg.fetch('tags', [])
  @labels = cfg.fetch('labels', {})

  unless %w[direct firmware].include?(@boot_mode)
    raise ArgumentError, "unsupported boot mode #{@boot_mode.inspect}"
  end

  unless @iso.nil? || (@iso.is_a?(String) && !@iso.empty?)
    raise ArgumentError, 'iso must be a non-empty string'
  end

  unless @test_shells.is_a?(Integer) && @test_shells >= 1
    raise ArgumentError, 'testShells must be an integer greater than or equal to 1'
  end

  unless %w[virtconsole virtserialport].include?(@test_shell_transport)
    raise ArgumentError, "unsupported test shell transport #{@test_shell_transport.inspect}"
  end

  unless @shell_names.is_a?(Array) && @shell_names.all? { |v| v.is_a?(String) && !v.empty? }
    raise ArgumentError, 'shells must be an array of non-empty strings'
  end

  if @shell_names.uniq.length != @shell_names.length
    raise ArgumentError, 'shell names must be unique'
  end

  if @test_shells <= @shell_names.length
    raise ArgumentError, 'testShells must be greater than the number of named shells'
  end

  return unless @boot_mode == 'direct'

  %w[kernel initrd toplevel].each do |v|
    next if cfg[v]

    raise ArgumentError, "missing #{v.inspect} for direct boot machine"
  end
end

Instance Attribute Details

#boot_mode ⇒ 'direct', 'firmware' (readonly)

Returns:

  • ('direct', 'firmware')


258
259
260
# File 'lib/osvm/machine_config.rb', line 258

def boot_mode
  @boot_mode
end

#boot_order ⇒ String? (readonly)

Returns:

  • (String, nil)


261
262
263
# File 'lib/osvm/machine_config.rb', line 261

def boot_order
  @boot_order
end

#cpu ⇒ Cpu (readonly)

Returns:



309
310
311
# File 'lib/osvm/machine_config.rb', line 309

def cpu
  @cpu
end

#cpus ⇒ Integer (readonly)

Returns:

  • (Integer)


306
307
308
# File 'lib/osvm/machine_config.rb', line 306

def cpus
  @cpus
end

#disks ⇒ Array<Disk> (readonly)

Returns:



294
295
296
# File 'lib/osvm/machine_config.rb', line 294

def disks
  @disks
end

#extra_qemu_options ⇒ Array<String> (readonly)

Returns:

  • (Array<String>)


267
268
269
# File 'lib/osvm/machine_config.rb', line 267

def extra_qemu_options
  @extra_qemu_options
end

#initrd ⇒ String (readonly)

Returns path to initrd.

Returns:

  • (String) —

    path to initrd



285
286
287
# File 'lib/osvm/machine_config.rb', line 285

def initrd
  @initrd
end

#iso ⇒ String? (readonly)

Returns path to a bootable ISO image.

Returns:

  • (String, nil) —

    path to a bootable ISO image



264
265
266
# File 'lib/osvm/machine_config.rb', line 264

def iso
  @iso
end

#kernel ⇒ String (readonly)

Returns path to kernel bzImage.

Returns:

  • (String) —

    path to kernel bzImage



282
283
284
# File 'lib/osvm/machine_config.rb', line 282

def kernel
  @kernel
end

#kernel_params ⇒ Array<String> (readonly)

Returns kernel parameters.

Returns:

  • (Array<String>) —

    kernel parameters



288
289
290
# File 'lib/osvm/machine_config.rb', line 288

def kernel_params
  @kernel_params
end

#labels ⇒ Hash<String, String> (readonly)

Returns:

  • (Hash<String, String>)


321
322
323
# File 'lib/osvm/machine_config.rb', line 321

def labels
  @labels
end

#memory ⇒ Integer (readonly)

Returns system memory in MiB.

Returns:

  • (Integer) —

    system memory in MiB



303
304
305
# File 'lib/osvm/machine_config.rb', line 303

def memory
  @memory
end

#networks ⇒ Array<Network> (readonly)

Returns:



315
316
317
# File 'lib/osvm/machine_config.rb', line 315

def networks
  @networks
end

#qemu ⇒ String (readonly)

Returns path to qemu package.

Returns:

  • (String) —

    path to qemu package



255
256
257
# File 'lib/osvm/machine_config.rb', line 255

def qemu
  @qemu
end

#shared_filesystems ⇒ Hash<String, String> (readonly)

Returns fs name => host directory.

Returns:

  • (Hash<String, String>) —

    fs name => host directory



312
313
314
# File 'lib/osvm/machine_config.rb', line 312

def shared_filesystems
  @shared_filesystems
end

#shell_names ⇒ Array<String> (readonly)

Returns:

  • (Array<String>)


276
277
278
# File 'lib/osvm/machine_config.rb', line 276

def shell_names
  @shell_names
end

#spin ⇒ String (readonly)

Returns:

  • (String)


252
253
254
# File 'lib/osvm/machine_config.rb', line 252

def spin
  @spin
end

#tags ⇒ Array<String> (readonly)

Returns:

  • (Array<String>)


318
319
320
# File 'lib/osvm/machine_config.rb', line 318

def tags
  @tags
end

#test_shell_transport ⇒ 'virtconsole', 'virtserialport' (readonly)

Returns:

  • ('virtconsole', 'virtserialport')


273
274
275
# File 'lib/osvm/machine_config.rb', line 273

def test_shell_transport
  @test_shell_transport
end

#test_shells ⇒ Integer (readonly)

Returns:

  • (Integer)


270
271
272
# File 'lib/osvm/machine_config.rb', line 270

def test_shells
  @test_shells
end

#toplevel ⇒ String (readonly)

Returns path to system top level.

Returns:

  • (String) —

    path to system top level



291
292
293
# File 'lib/osvm/machine_config.rb', line 291

def toplevel
  @toplevel
end

#virtiofsd ⇒ String (readonly)

Returns path to virtiofsd package.

Returns:

  • (String) —

    path to virtiofsd package



279
280
281
# File 'lib/osvm/machine_config.rb', line 279

def virtiofsd
  @virtiofsd
end

Class Method Details

.from_config(cfg, allocate_resources: true) ⇒ MachineConfig

Build machine config from hash

Parameters:

  • cfg (Hash)

Returns:



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/osvm/machine_config.rb', line 229

def self.from_config(cfg, allocate_resources: true)
  spin = cfg.fetch('spin', 'vpsadminos')

  case spin
  when 'vpsadminos'
    VpsadminosMachineConfig.new(cfg, allocate_resources:)
  when 'nixos'
    NixosMachineConfig.new(cfg, allocate_resources:)
  else
    raise ArgumentError, "Unknown machine spin #{spin.inspect}"
  end
end

.load_file(path) ⇒ MachineConfig

Load machine config from file

Parameters:

  • path (String)

Returns:



221
222
223
224
# File 'lib/osvm/machine_config.rb', line 221

def self.load_file(path)
  cfg = JSON.parse(File.read(path))
  from_config(cfg)
end

.validate_config!(cfg) ⇒ Object

Check the canonical parser and deferred network fields without reserving global MAC addresses/ports or constructing a machine. Discard this object: host availability and allocation collisions remain runtime checks.



245
246
247
248
249
# File 'lib/osvm/machine_config.rb', line 245

def self.validate_config!(cfg)
  parsed = from_config(cfg, allocate_resources: false)
  parsed.networks.each(&:qemu_options)
  true
end

Instance Method Details

#all_disks ⇒ Array<Disk>

Disks in attachment order, including a root disk when present.

Returns:



298
299
300
# File 'lib/osvm/machine_config.rb', line 298

def all_disks
  disks
end