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) ⇒ MachineConfig

Returns a new instance of MachineConfig.

Parameters:

  • cfg (Hash)


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/osvm/machine_config.rb', line 276

def initialize(cfg)
  @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)
  @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'))
  @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)
  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 @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')


219
220
221
# File 'lib/osvm/machine_config.rb', line 219

def boot_mode
  @boot_mode
end

#boot_orderString? (readonly)

Returns:

  • (String, nil)


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

def boot_order
  @boot_order
end

#cpuCpu (readonly)

Returns:



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

def cpu
  @cpu
end

#cpusInteger (readonly)

Returns:

  • (Integer)


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

def cpus
  @cpus
end

#disksArray<Disk> (readonly)

Returns:



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

def disks
  @disks
end

#extra_qemu_optionsArray<String> (readonly)

Returns:

  • (Array<String>)


228
229
230
# File 'lib/osvm/machine_config.rb', line 228

def extra_qemu_options
  @extra_qemu_options
end

#initrdString (readonly)

Returns path to initrd.

Returns:

  • (String)

    path to initrd



243
244
245
# File 'lib/osvm/machine_config.rb', line 243

def initrd
  @initrd
end

#isoString? (readonly)

Returns path to a bootable ISO image.

Returns:

  • (String, nil)

    path to a bootable ISO image



225
226
227
# File 'lib/osvm/machine_config.rb', line 225

def iso
  @iso
end

#kernelString (readonly)

Returns path to kernel bzImage.

Returns:

  • (String)

    path to kernel bzImage



240
241
242
# File 'lib/osvm/machine_config.rb', line 240

def kernel
  @kernel
end

#kernel_paramsArray<String> (readonly)

Returns kernel parameters.

Returns:

  • (Array<String>)

    kernel parameters



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

def kernel_params
  @kernel_params
end

#labelsHash<String, String> (readonly)

Returns:

  • (Hash<String, String>)


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

def labels
  @labels
end

#memoryInteger (readonly)

Returns system memory in MiB.

Returns:

  • (Integer)

    system memory in MiB



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

def memory
  @memory
end

#networksArray<Network> (readonly)

Returns:



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

def networks
  @networks
end

#qemuString (readonly)

Returns path to qemu package.

Returns:

  • (String)

    path to qemu package



216
217
218
# File 'lib/osvm/machine_config.rb', line 216

def qemu
  @qemu
end

#shared_filesystemsHash<String, String> (readonly)

Returns fs name => host directory.

Returns:

  • (Hash<String, String>)

    fs name => host directory



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

def shared_filesystems
  @shared_filesystems
end

#shell_namesArray<String> (readonly)

Returns:

  • (Array<String>)


234
235
236
# File 'lib/osvm/machine_config.rb', line 234

def shell_names
  @shell_names
end

#spinString (readonly)

Returns:

  • (String)


213
214
215
# File 'lib/osvm/machine_config.rb', line 213

def spin
  @spin
end

#tagsArray<String> (readonly)

Returns:

  • (Array<String>)


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

def tags
  @tags
end

#test_shellsInteger (readonly)

Returns:

  • (Integer)


231
232
233
# File 'lib/osvm/machine_config.rb', line 231

def test_shells
  @test_shells
end

#toplevelString (readonly)

Returns path to system top level.

Returns:

  • (String)

    path to system top level



249
250
251
# File 'lib/osvm/machine_config.rb', line 249

def toplevel
  @toplevel
end

#virtiofsdString (readonly)

Returns path to virtiofsd package.

Returns:

  • (String)

    path to virtiofsd package



237
238
239
# File 'lib/osvm/machine_config.rb', line 237

def virtiofsd
  @virtiofsd
end

Class Method Details

.from_config(cfg) ⇒ MachineConfig

Build machine config from hash

Parameters:

  • cfg (Hash)

Returns:



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/osvm/machine_config.rb', line 199

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

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

.load_file(path) ⇒ MachineConfig

Load machine config from file

Parameters:

  • path (String)

Returns:



191
192
193
194
# File 'lib/osvm/machine_config.rb', line 191

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