lib/kitchen/driver/vsphere.rb in chef-provisioning-vsphere-0.5.8 vs lib/kitchen/driver/vsphere.rb in chef-provisioning-vsphere-0.6.0.dev.1
- old
+ new
@@ -5,10 +5,12 @@
module Kitchen
module Driver
class Vsphere < Kitchen::Driver::Base
+ @@chef_zero_server = false
+
default_config :machine_options,
:start_timeout => 600,
:create_timeout => 600,
:ready_timeout => 90,
:bootstrap_options => {
@@ -23,20 +25,21 @@
:domain => 'local'
}
}
def create(state)
- config[:server_name] ||= "kitchen-#{SecureRandom.hex(4)}"
+ state[:vsphere_name] ||= "kitchen-#{SecureRandom.hex(4)}"
state[:username] = config[:machine_options][:bootstrap_options][:ssh][:user]
state[:password] = config[:machine_options][:bootstrap_options][:ssh][:password]
-
- machine = with_provisioning_driver(config[:server_name]) do | action_handler, driver, machine_spec|
+ config[:server_name] = state[:vsphere_name]
+
+ machine = with_provisioning_driver(state) 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[:hostname] = machine_spec.location['ipaddress']
- state[:vsphere_name] = config[:server_name]
+ machine_spec.save(action_handler)
end
node_dir = File.join(instance.verifier[:test_base_path], "nodes")
Dir.mkdir(node_dir) unless Dir.exist?(node_dir)
node_file = File.join(node_dir, "#{instance.suite.name}.json")
@@ -47,17 +50,16 @@
}
}
File.open(node_file, 'w') do |out|
out << JSON.pretty_generate(node)
end
-
end
def destroy(state)
return if state[:server_id].nil?
- with_provisioning_driver(state[:vsphere_name]) do | action_handler, driver, machine_spec|
+ with_provisioning_driver(state) 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
@@ -67,18 +69,46 @@
node_file = File.join(instance.verifier[:test_base_path], "nodes/#{instance.suite.name}.json")
File.delete(node_file) if File.exist?(node_file)
end
- def with_provisioning_driver(name, &block)
- Cheffish.honor_local_mode do
- chef_server = Cheffish.default_chef_server
- config[:machine_options][:convergence_options] = {:chef_server => chef_server}
- machine_spec = Chef::Provisioning.chef_managed_entry_store(chef_server).new_entry(:machine, name)
- url = URI::VsphereUrl.from_config(@config[:driver_options]).to_s
- driver = Chef::Provisioning.driver_for_url(url, config)
- action_handler = Chef::Provisioning::ActionHandler.new
- block.call(action_handler, driver, machine_spec)
+ def with_provisioning_driver(state, &block)
+ config[:machine_options][:convergence_options] = {:chef_server => chef_server}
+ machine_spec = Chef::Provisioning.chef_managed_entry_store(chef_server).get(:machine, state[:vsphere_name])
+ if machine_spec.nil?
+ machine_spec = Chef::Provisioning.chef_managed_entry_store(chef_server)
+ .new_entry(:machine, state[:vsphere_name])
+ end
+ url = URI::VsphereUrl.from_config(@config[:driver_options]).to_s
+ driver = Chef::Provisioning.driver_for_url(url, config)
+ action_handler = Chef::Provisioning::ActionHandler.new
+ block.call(action_handler, driver, machine_spec)
+ end
+
+ def chef_server
+ if !@@chef_zero_server
+ vsphere_mutex.synchronize do
+ if !@@chef_zero_server
+ Chef::Config.local_mode = true
+ Chef::Config.chef_repo_path = Chef::Config.find_chef_repo_path(Dir.pwd)
+ require 'chef/local_mode'
+ Chef::LocalMode.setup_server_connectivity
+ @@chef_zero_server = true
+ end
+ end
+ end
+
+ Cheffish.default_chef_server
+ end
+
+ def vsphere_mutex
+ @@vsphere_mutex ||= begin
+ Kitchen.mutex.synchronize do
+ instance.class.mutexes ||= Hash.new
+ instance.class.mutexes[self.class] = Mutex.new
+ end
+
+ instance.class.mutexes[self.class]
end
end
end
end
end