Skip to content

Commit

Permalink
Add support for BIOS serial
Browse files Browse the repository at this point in the history
Resolves #85
  • Loading branch information
myoung34 committed Mar 31, 2017
1 parent 828d602 commit 5f5faaa
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .kitchen.yml
Expand Up @@ -41,3 +41,15 @@ suites:
runcmd:
- ifdown eth0
- service network restart
- name: bios_serial
driver_config:
vm_hostname: bios-serial
network:
- ["private_network", {ovirt__network_name: 'ovirtmgmt'}]
customize:
template: vagrant-centos7
bios_serial: 'banana-hammock'
cloud_init: |
runcmd:
- ifdown eth0
- service network restart
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -78,6 +78,7 @@ Vagrant.configure("2") do |config|
ovirt.cpu_cores = 2
ovirt.cpu_sockets = 2
ovirt.cpu_threads = 2
ovirt.bios_serial = aaabbbb-ccc-dddd
ovirt.cloud_init =<<EOF
write_files:
- content: |
Expand Down Expand Up @@ -119,6 +120,7 @@ end
1. `cloud_init` => The cloud-init data to pass. Must be properly formatted as yaml. [Docs here](http://cloudinit.readthedocs.io/en/latest/topics/examples.html)
1. `affinity` => The affinity to use. [See this for possible uses](http://www.rubydoc.info/gems/ovirt-engine-sdk/OvirtSDK4/VmAffinity). Optional. Invalid will cause a `RuntimeError`
1. `placement_host` => The host to start the VM on. Optional.
1. `bios_serial` => The BIOS serial number to assign. Optional.


## Testing
Expand Down
24 changes: 24 additions & 0 deletions lib/vagrant-ovirt4/action/create_vm.rb
Expand Up @@ -25,6 +25,7 @@ def call(env)
env[:ui].info(" -- Cluster: #{config.cluster}")
env[:ui].info(" -- Template: #{config.template}")
env[:ui].info(" -- Console Type: #{config.console}")
env[:ui].info(" -- BIOS Serial: #{config.bios_serial}")
env[:ui].info(" -- Memory: ")
env[:ui].info(" ---- Memory: #{Filesize.from("#{config.memory_size} B").to_f('MB').to_i} MB")
env[:ui].info(" ---- Maximum: #{Filesize.from("#{config.memory_maximum} B").to_f('MB').to_i} MB")
Expand Down Expand Up @@ -103,6 +104,29 @@ def call(env)
raise Errors::WaitForReadyVmTimeout
end

begin
if config.bios_serial
vm_service = env[:vms_service].vm_service(env[:machine].id)
vm_service.update(
serial_number: {
policy: OvirtSDK4::SerialNumberPolicy::CUSTOM,
value: config.bios_serial,
}
)
end
rescue OvirtSDK4::Error => e
fault_message = /Fault detail is \"\[?(.+?)\]?\".*/.match(e.message)[1] rescue e.message
retry if e.message =~ /Related operation is currently in progress/

if config.debug
raise e
else
raise Errors::UpdateBiosError,
:error_message => fault_message
end
end


@app.call(env)
end

Expand Down
3 changes: 3 additions & 0 deletions lib/vagrant-ovirt4/config.rb
Expand Up @@ -23,6 +23,7 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :cloud_init
attr_accessor :affinity
attr_accessor :placement_host
attr_accessor :bios_serial

def initialize
@url = UNSET_VALUE
Expand All @@ -42,6 +43,7 @@ def initialize
@cloud_init = UNSET_VALUE
@affinity = UNSET_VALUE
@placement_host = UNSET_VALUE
@bios_serial = UNSET_VALUE

end

Expand All @@ -63,6 +65,7 @@ def finalize!
@cloud_init = nil if @cloud_init == UNSET_VALUE
@affinity = nil if @affinity == UNSET_VALUE
@placement_host = nil if @placement_host == UNSET_VALUE
@bios_serial = nil if @bios_serial == UNSET_VALUE

unless affinity.nil?
raise "Invalid affinity. Must be one of #{OvirtSDK4::VmAffinity.constants.map { |s| "'#{s.downcase}'" }.join(' ')}" unless OvirtSDK4::VmAffinity.constants.include? affinity.upcase.to_sym
Expand Down
4 changes: 4 additions & 0 deletions lib/vagrant-ovirt4/errors.rb
Expand Up @@ -50,6 +50,10 @@ class RemoveSnapshotError < VagrantOVirtError
class RemoveVMError < VagrantOVirtError
error_key(:remove_vm_error)
end

class UpdateBiosError < VagrantOVirtError
error_key(:update_bios_error)
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions locales/en.yml
Expand Up @@ -60,6 +60,8 @@ en:
No VM found with id '%{vm_id}'
create_vm_error: |-
Creation failed. oVirt error message was '%{error_message}'
update_bios_error: |-
BIOS update failed. oVirt error message was '%{error_message}'
start_vm_error: |-
Unable to start VM: %{error_message}
no_network_error: |-
Expand Down
3 changes: 2 additions & 1 deletion spec/vagrant-ovirt4/config_spec.rb
Expand Up @@ -45,11 +45,12 @@
its("cloud_init") { should be_nil }
its("affinity") { should be_nil }
its("placement_host") { should be_nil }
its("bios_serial") { should be_nil }

end

describe "overriding defaults" do
[:url, :username, :password, :insecure, :debug, :cpu_cores, :cpu_sockets, :cpu_threads, :cluster, :console, :template, :cloud_init, :placement_host].each do |attribute|
[:url, :username, :password, :insecure, :debug, :cpu_cores, :cpu_sockets, :cpu_threads, :cluster, :console, :template, :cloud_init, :placement_host, :bios_serial].each do |attribute|

it "should not default #{attribute} if overridden" do
instance.send("#{attribute}=".to_sym, "foo")
Expand Down
6 changes: 6 additions & 0 deletions test/integration/bios_serial/bios_serial_spec.rb
@@ -0,0 +1,6 @@
describe command('dmidecode -s system-serial-number') do
its(:exit_status) { should eq 0 }
its(:stderr) { should be_empty }
its(:stdout) { should match(/^banana-hammock$/) }
end

0 comments on commit 5f5faaa

Please sign in to comment.