Skip to content

Instantly share code, notes, and snippets.

@mwrock
Last active August 29, 2015 14:04
Show Gist options
  • Save mwrock/a6a84ec104534e170876 to your computer and use it in GitHub Desktop.
Save mwrock/a6a84ec104534e170876 to your computer and use it in GitHub Desktop.
Make your Chef-Metal driver a Test-Kitchen driver
require 'kitchen'
require 'chef_metal_vsphere/vsphere_driver'
require 'chef_metal/chef_machine_spec'
module Kitchen
module Driver
class Vsphere < Kitchen::Driver::SSHBase
def create(state)
config[:server_name] ||= "kitchen-#{SecureRandom.hex}-#{instance.name}"
state[:username] = config[:machine_options][:bootstrap_options][:ssh][:user]
state[:password] = config[:machine_options][:bootstrap_options][:ssh][:password]
machine = with_metal_driver(config[:server_name]) do | action_handler, driver, machine_spec|
driver.allocate_machine(action_handler, machine_spec, config[:machine_options])
driver.ready_machine(action_handler, machine_spec, config[:machine_options])
state[:server_id] = machine_spec.location['server_id']
state[:vm_name] = config[:server_name]
end
state[:hostname] = machine.transport.host
end
def destroy(state)
return if state[:server_id].nil?
with_metal_driver(state[:vm_name]) do | action_handler, driver, machine_spec|
machine_spec.location = { 'driver_url' => driver.driver_url,
'server_id' => state[:server_id]}
driver.destroy_machine(action_handler, machine_spec, config[:machine_options])
end
state.delete(:server_id)
state.delete(:hostname)
state.delete(:vm_name)
end
def with_metal_driver(name, &block)
Cheffish.honor_local_mode do
chef_server = Cheffish.default_chef_server(Cheffish.profiled_config)
config[:machine_options][:convergence_options] = {:chef_server => chef_server}
machine_spec = ChefMetal::ChefMachineSpec.new({'name' => name}, chef_server)
driver = ChefMetal.driver_for_url("vsphere://#{config[:driver_options][:host]}", config)
action_handler = ChefMetal::ActionHandler.new
block.call(action_handler, driver, machine_spec)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment