Class: OsVm::MachineConfig
- Inherits:
-
Object
- Object
- OsVm::MachineConfig
- Defined in:
- lib/osvm/machine_config.rb
Direct Known Subclasses
Defined Under Namespace
Classes: BridgeNetwork, Cpu, Disk, Network, SocketNetwork, UserNetwork
Instance Attribute Summary collapse
- #boot_mode ⇒ 'direct', 'firmware' readonly
- #boot_order ⇒ String? readonly
- #cpu ⇒ Cpu readonly
- #cpus ⇒ Integer readonly
- #disks ⇒ Array<Disk> readonly
- #extra_qemu_options ⇒ Array<String> readonly
-
#initrd ⇒ String
readonly
Path to initrd.
-
#iso ⇒ String?
readonly
Path to a bootable ISO image.
-
#kernel ⇒ String
readonly
Path to kernel bzImage.
-
#kernel_params ⇒ Array<String>
readonly
Kernel parameters.
- #labels ⇒ Hash<String, String> readonly
-
#memory ⇒ Integer
readonly
System memory in MiB.
- #networks ⇒ Array<Network> readonly
-
#qemu ⇒ String
readonly
Path to qemu package.
-
#shared_filesystems ⇒ Hash<String, String>
readonly
Fs name => host directory.
- #shell_names ⇒ Array<String> readonly
- #spin ⇒ String readonly
- #tags ⇒ Array<String> readonly
- #test_shell_transport ⇒ 'virtconsole', 'virtserialport' readonly
- #test_shells ⇒ Integer readonly
-
#toplevel ⇒ String
readonly
Path to system top level.
-
#virtiofsd ⇒ String
readonly
Path to virtiofsd package.
Class Method Summary collapse
-
.from_config(cfg, allocate_resources: true) ⇒ MachineConfig
Build machine config from hash.
-
.load_file(path) ⇒ MachineConfig
Load machine config from file.
-
.validate_config!(cfg) ⇒ Object
Check the canonical parser and deferred network fields without reserving global MAC addresses/ports or constructing a machine.
Instance Method Summary collapse
-
#all_disks ⇒ Array<Disk>
Disks in attachment order, including a root disk when present.
-
#initialize(cfg, allocate_resources: true) ⇒ MachineConfig
constructor
A new instance of MachineConfig.
Constructor Details
#initialize(cfg, allocate_resources: true) ⇒ MachineConfig
Returns a new instance of MachineConfig.
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)
258 259 260 |
# File 'lib/osvm/machine_config.rb', line 258 def boot_mode @boot_mode end |
#boot_order ⇒ String? (readonly)
261 262 263 |
# File 'lib/osvm/machine_config.rb', line 261 def boot_order @boot_order end |
#cpus ⇒ Integer (readonly)
306 307 308 |
# File 'lib/osvm/machine_config.rb', line 306 def cpus @cpus end |
#disks ⇒ Array<Disk> (readonly)
294 295 296 |
# File 'lib/osvm/machine_config.rb', line 294 def disks @disks end |
#extra_qemu_options ⇒ Array<String> (readonly)
267 268 269 |
# File 'lib/osvm/machine_config.rb', line 267 def @extra_qemu_options end |
#initrd ⇒ String (readonly)
Returns 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.
264 265 266 |
# File 'lib/osvm/machine_config.rb', line 264 def iso @iso end |
#kernel ⇒ String (readonly)
Returns 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.
288 289 290 |
# File 'lib/osvm/machine_config.rb', line 288 def kernel_params @kernel_params end |
#labels ⇒ Hash<String, String> (readonly)
321 322 323 |
# File 'lib/osvm/machine_config.rb', line 321 def labels @labels end |
#memory ⇒ Integer (readonly)
Returns system memory in MiB.
303 304 305 |
# File 'lib/osvm/machine_config.rb', line 303 def memory @memory end |
#networks ⇒ Array<Network> (readonly)
315 316 317 |
# File 'lib/osvm/machine_config.rb', line 315 def networks @networks end |
#qemu ⇒ String (readonly)
Returns 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.
312 313 314 |
# File 'lib/osvm/machine_config.rb', line 312 def shared_filesystems @shared_filesystems end |
#shell_names ⇒ Array<String> (readonly)
276 277 278 |
# File 'lib/osvm/machine_config.rb', line 276 def shell_names @shell_names end |
#spin ⇒ String (readonly)
252 253 254 |
# File 'lib/osvm/machine_config.rb', line 252 def spin @spin end |
#tags ⇒ Array<String> (readonly)
318 319 320 |
# File 'lib/osvm/machine_config.rb', line 318 def @tags end |
#test_shell_transport ⇒ 'virtconsole', 'virtserialport' (readonly)
273 274 275 |
# File 'lib/osvm/machine_config.rb', line 273 def test_shell_transport @test_shell_transport end |
#test_shells ⇒ Integer (readonly)
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.
291 292 293 |
# File 'lib/osvm/machine_config.rb', line 291 def toplevel @toplevel end |
#virtiofsd ⇒ String (readonly)
Returns 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
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
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.
298 299 300 |
# File 'lib/osvm/machine_config.rb', line 298 def all_disks disks end |