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_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) ⇒ MachineConfig
Build machine config from hash.
-
.load_file(path) ⇒ MachineConfig
Load machine config from file.
Instance Method Summary collapse
-
#initialize(cfg) ⇒ MachineConfig
constructor
A new instance of MachineConfig.
Constructor Details
#initialize(cfg) ⇒ MachineConfig
Returns a new instance of MachineConfig.
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)
219 220 221 |
# File 'lib/osvm/machine_config.rb', line 219 def boot_mode @boot_mode end |
#boot_order ⇒ String? (readonly)
222 223 224 |
# File 'lib/osvm/machine_config.rb', line 222 def boot_order @boot_order end |
#cpus ⇒ Integer (readonly)
258 259 260 |
# File 'lib/osvm/machine_config.rb', line 258 def cpus @cpus end |
#disks ⇒ Array<Disk> (readonly)
252 253 254 |
# File 'lib/osvm/machine_config.rb', line 252 def disks @disks end |
#extra_qemu_options ⇒ Array<String> (readonly)
228 229 230 |
# File 'lib/osvm/machine_config.rb', line 228 def @extra_qemu_options end |
#initrd ⇒ String (readonly)
Returns path to initrd.
243 244 245 |
# File 'lib/osvm/machine_config.rb', line 243 def initrd @initrd end |
#iso ⇒ String? (readonly)
Returns path to a bootable ISO image.
225 226 227 |
# File 'lib/osvm/machine_config.rb', line 225 def iso @iso end |
#kernel ⇒ String (readonly)
Returns path to kernel bzImage.
240 241 242 |
# File 'lib/osvm/machine_config.rb', line 240 def kernel @kernel end |
#kernel_params ⇒ Array<String> (readonly)
Returns kernel parameters.
246 247 248 |
# File 'lib/osvm/machine_config.rb', line 246 def kernel_params @kernel_params end |
#labels ⇒ Hash<String, String> (readonly)
273 274 275 |
# File 'lib/osvm/machine_config.rb', line 273 def labels @labels end |
#memory ⇒ Integer (readonly)
Returns system memory in MiB.
255 256 257 |
# File 'lib/osvm/machine_config.rb', line 255 def memory @memory end |
#networks ⇒ Array<Network> (readonly)
267 268 269 |
# File 'lib/osvm/machine_config.rb', line 267 def networks @networks end |
#qemu ⇒ String (readonly)
Returns path to qemu package.
216 217 218 |
# File 'lib/osvm/machine_config.rb', line 216 def qemu @qemu end |
#shared_filesystems ⇒ Hash<String, String> (readonly)
Returns fs name => host directory.
264 265 266 |
# File 'lib/osvm/machine_config.rb', line 264 def shared_filesystems @shared_filesystems end |
#shell_names ⇒ Array<String> (readonly)
234 235 236 |
# File 'lib/osvm/machine_config.rb', line 234 def shell_names @shell_names end |
#spin ⇒ String (readonly)
213 214 215 |
# File 'lib/osvm/machine_config.rb', line 213 def spin @spin end |
#tags ⇒ Array<String> (readonly)
270 271 272 |
# File 'lib/osvm/machine_config.rb', line 270 def @tags end |
#test_shells ⇒ Integer (readonly)
231 232 233 |
# File 'lib/osvm/machine_config.rb', line 231 def test_shells @test_shells end |
#toplevel ⇒ String (readonly)
Returns path to system top level.
249 250 251 |
# File 'lib/osvm/machine_config.rb', line 249 def toplevel @toplevel end |
#virtiofsd ⇒ String (readonly)
Returns 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
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
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 |