Class: Kitchen::Driver::Vsphere

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/vsphere.rb

Overview

vSphere kitchen class extends the Kitchen::Driver::Base

Constant Summary collapse

@@chef_zero_server =
false

Instance Method Summary collapse

Instance Method Details

#chef_serverObject

Uses either a remote Chef server or Chef Zero



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kitchen/driver/vsphere.rb', line 100

def chef_server
  unless @@chef_zero_server
    vsphere_mutex.synchronize do
      unless @@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

#create(state) ⇒ Object

Creates VM on the vSphere cluster

Parameters:

  • state (Object)

    Uses state of the machine from Chef provisioning.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kitchen/driver/vsphere.rb', line 41

def create(state)
  state[:vsphere_name] ||= config[:vsphere_name]
  state[:username] = config[:machine_options][:bootstrap_options][:ssh][:user]
  state[:password] = config[:machine_options][:bootstrap_options][:ssh][:password]
  config[:server_name] = state[:vsphere_name]

  with_provisioning_driver(state) do |action_handler, driver, machine_spec|
    begin
      driver.allocate_machine(action_handler, machine_spec, config[:machine_options])
      driver.ready_machine(action_handler, machine_spec, config[:machine_options])
    rescue
      raise
    ensure
      if machine_spec
        if machine_spec.location
          state[:server_id] = machine_spec.location["server_id"]
          state[:hostname] = machine_spec.location["ipaddress"]
        end
        machine_spec.save(action_handler)
      end
    end
  end
end

#destroy(state) ⇒ Object

Destroys VM on the vSphere cluster

Parameters:

  • state (Object)

    Uses state of the machine from Chef provisioning.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kitchen/driver/vsphere.rb', line 68

def destroy(state)
  return if state[:server_id].nil?

  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

  state.delete(:server_id)
  state.delete(:hostname)
  state.delete(:vsphere_name)
end

#vsphere_mutexObject

Creates a new mutex object for Kitchen



118
119
120
121
122
123
124
125
126
127
# File 'lib/kitchen/driver/vsphere.rb', line 118

def vsphere_mutex
  @@vsphere_mutex ||= begin
    Kitchen.mutex.synchronize do
      instance.class.mutexes ||= {}
      instance.class.mutexes[self.class] = Mutex.new
    end

    instance.class.mutexes[self.class]
  end
end

#with_provisioning_driver(state) {|action_handler, driver, machine_spec| ... } ⇒ Object

Leverages the chef provisioning driver options

Parameters:

  • state (Object)

    Uses state of the machine from Chef provisioning.

Yields:

  • (action_handler, driver, machine_spec)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kitchen/driver/vsphere.rb', line 85

def with_provisioning_driver(state)
  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
  yield(action_handler, driver, machine_spec)
end