Skip to content

Commit

Permalink
generate most of bhyve config
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Fisher committed Jul 22, 2024
1 parent 3038724 commit 9a09ced
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 12 deletions.
71 changes: 71 additions & 0 deletions lib/oozone/brand_config_decorators/bhyve.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

module Oozone
module ConfigDecorator
# The bhyve installer is strongly opinionated. Here it fills in most of
# the zone config file for you.
class Bhyve
def initialize(config, metadata)
@config = config
@metadata = metadata
@bootdisk = File.join('rpool', 'zones', 'bhyve', @metadata[:zone_name])
@cloud_init_iso = '/tmp/cloud-init.iso' # THIS WILL BE DYNAMIC
end

def decorate!
ret = add_zvol_device(@config)
ret = add_cloudinit_cdrom(ret) if @metadata[:cloudinit]
add_boot_attrs(ret)
end

# rubocop:disable Metrics/MethodLength
def add_boot_attrs(config)
to_add = [
['add attr',
['set name=bootrom'],
['set type=string'],
['set value=BHYVE_RELEASE'],
'end'],
['add attr',
['set name=bootdisk'],
['set type=string'],
["set value=#{@bootdisk}"],
'end'],
['add attr',
['set name=acpi'],
['set type=string'],
['set value=false'],
'end']
]

if @metadata[:cloudinit]
to_add << ['add attr',
['set name=cdrom'],
['set type=string'],
["set value=#{@cloud_init_iso}"],
'end']
end

config << to_add
end
# rubocop:enable Metrics/MethodLength

def add_cloudinit_cdrom(config)
config << [
['add fs',
["set dir=#{@cloud_init_iso}"],
["set special=#{@cloud_init_iso}"],
['set type=lofs'],
['set options=ro'],
'end']
]
end

def add_zvol_device(config)
config << [
['add device', ["set match=/dev/zvol/rdsk/#{@bootdisk}"], 'end']
]
end
end
end
end
33 changes: 31 additions & 2 deletions lib/oozone/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'yaml'
require 'pathname'
require_relative 'dataset_manager'
require_relative 'constants'

module Oozone
#
Expand All @@ -28,6 +29,14 @@ def write!(_target = zone_config_file)

private

def decorator_class
"Oozone::ConfigDecorator::#{@raw[:brand].capitalize}"
end

def decorator_class_path
File.join('brand_config_decorators', @raw[:brand])
end

def zone_config_file
ZCONF_DIR.join(@file.basename.to_s.sub(/.yaml/, '.zone'))
end
Expand All @@ -44,11 +53,26 @@ def raw_config(zone_file)
exit 1
end

def load_decorator
require_relative decorator_class_path
Object.const_get(decorator_class)
rescue LoadError
nil
end

def decorated_input(parsed_input)
decorator = load_decorator

return parsed_input if decorator.nil?

decorator.new(parsed_input, @metadata).decorate!
end

def parsed_config
"#{(config_prelude + parse_input).compact.join("\n")}\n"
"#{(config_prelude + decorated_input(parsed_input)).compact.join("\n")}\n"
end

def parse_input
def parsed_input
@raw.map { |k, v| respond_to?(k, true) ? send(k, v) : simple_conv(k, v) }
end

Expand Down Expand Up @@ -131,6 +155,11 @@ def run_ssh(cmds)
nil
end

def cloudinit(defns)
@metadata[:cloudinit] = defns
nil
end

# Used to specify a bhyve volume
def volume_size(defns)
@metadata[:volume_size] = defns
Expand Down
26 changes: 16 additions & 10 deletions spec/oozone/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
# Test the config loader via its public interface.
#
class ConfigLoaderTest < Minitest::Test
def setup
def test_01_zone_file_is_created_correctly
spy = Spy.on_instance_method(Oozone::ConfigLoader, :create_dataset)
@t1 = Oozone::ConfigLoader.new(RES_DIR.join('test_zone_01.yaml'))
@t2 = Oozone::ConfigLoader.new(RES_DIR.join('test_zone_02.yaml'))
sut = Oozone::ConfigLoader.new(RES_DIR.join('test_zone_01.yaml'))
assert_equal(contents_of('test_zone_01.zone'), sut.config)
spy.unhook
end

def test_01_zone_file_is_created_correctly
assert_equal(contents_of('test_zone_01.zone'), @t1.config)
end

def test_01_metadata
m = @t1.metadata
spy = Spy.on_instance_method(Oozone::ConfigLoader, :create_dataset)
sut = Oozone::ConfigLoader.new(RES_DIR.join('test_zone_01.yaml'))
m = sut.metadata
assert_instance_of(Hash, m)
assert_equal('test_zone_01', m[:zone_name])
assert_equal({ domain: 'localnet',
Expand All @@ -32,14 +30,17 @@ def test_01_metadata
assert_equal({ '/etc/release': '/var/tmp/etc/release',
'/etc/passwd': '/passwd' }, m[:upload])
assert_equal(Pathname.new('/zones/wavefront/root'), m[:root])
spy.unhook
end

def test_02_zone_file_is_created_correctly
assert_equal(contents_of('test_zone_02.zone'), @t2.config)
sut = Oozone::ConfigLoader.new(RES_DIR.join('test_zone_02.yaml'))
assert_equal(contents_of('test_zone_02.zone'), sut.config)
end

def test_02_metadata
m = @t2.metadata
sut = Oozone::ConfigLoader.new(RES_DIR.join('test_zone_02.yaml'))
m = sut.metadata
assert_instance_of(Hash, m)
assert_equal('test_zone_02', m[:zone_name])
assert_equal(Pathname.new('/zones/test02/root'), m[:root])
Expand All @@ -50,6 +51,11 @@ def test_02_metadata
refute m.key?(:upload)
end

def test_bhyve_zone_file_is_created_correctly
sut = Oozone::ConfigLoader.new(RES_DIR.join('test-bhyve.yaml'))
assert_equal(sut.config, contents_of('test-bhyve.zone'))
end

# FIXME
def _test_write!
File.stub(:write)
Expand Down
11 changes: 11 additions & 0 deletions spec/resources/test-bhyve.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
brand: bhyve
zonepath: /zones/test-bhyve
autoboot: true
cloudinit: true
net:
- physical: bhyve_net0
'global-nic': e1000g0
allowed-address: 192.168.1.80/24
volume_size: 30G
raw_image: /home/rob/work/noble-server-cloudimg-amd64.img.raw
38 changes: 38 additions & 0 deletions spec/resources/test-bhyve.zone
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
create -b
set brand=bhyve
set zonepath=/zones/test-bhyve
set autoboot=true
add net
set physical=bhyve_net0
set global-nic=e1000g0
set allowed-address=192.168.1.80/24
end
add device
set match=/dev/zvol/rdsk/rpool/zones/bhyve/test-bhyve
end
add fs
set dir=/tmp/cloud-init.iso
set special=/tmp/cloud-init.iso
set type=lofs
set options=ro
end
add attr
set name=bootrom
set type=string
set value=BHYVE_RELEASE
end
add attr
set name=bootdisk
set type=string
set value=rpool/zones/bhyve/test-bhyve
end
add attr
set name=acpi
set type=string
set value=false
end
add attr
set name=cdrom
set type=string
set value=/tmp/cloud-init.iso
end

0 comments on commit 9a09ced

Please sign in to comment.