Class: Chef::Provisioning::VagrantDriver::Driver
- Inherits:
-
ChefMetal::Driver
- Object
- ChefMetal::Driver
- Chef::Provisioning::VagrantDriver::Driver
- Includes:
- Mixin::ShellOut
- Defined in:
- lib/chef/provisioning/vagrant_driver/driver.rb
Overview
Provisions machines in vagrant.
Instance Attribute Summary collapse
-
#cluster_path ⇒ Object
readonly
Returns the value of attribute cluster_path.
Class Method Summary collapse
- .canonicalize_url(driver_url, config) ⇒ Object
- .from_url(driver_url, config) ⇒ Object
-
.vagrant_config_string(vagrant_config, variable, line_prefix) ⇒ Object
Used by vagrant_cluster and machine to get the string used to configure vagrant.
Instance Method Summary collapse
-
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
Acquire a machine, generally by provisioning it.
-
#connect_to_machine(machine_spec, machine_options) ⇒ Object
Connect to machine without acquiring it.
- #destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #destroy_machines(action_handler, specs_and_options, parallelizer) ⇒ Object
- #driver_url ⇒ Object
-
#initialize(driver_url, config) ⇒ Driver
constructor
Create a new vagrant driver.
- #ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #ready_machines(action_handler, specs_and_options, parallelizer) ⇒ Object
- #stop_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #stop_machines(action_handler, specs_and_options, parallelizer) ⇒ Object
Constructor Details
#initialize(driver_url, config) ⇒ Driver
Create a new vagrant driver.
## Parameters cluster_path - path to the directory containing the vagrant files, which
should have been created with the vagrant_cluster resource.
26 27 28 29 30 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 26 def initialize(driver_url, config) super scheme, cluster_path = driver_url.split(':', 2) @cluster_path = cluster_path end |
Instance Attribute Details
#cluster_path ⇒ Object (readonly)
Returns the value of attribute cluster_path.
32 33 34 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 32 def cluster_path @cluster_path end |
Class Method Details
.canonicalize_url(driver_url, config) ⇒ Object
38 39 40 41 42 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 38 def self.canonicalize_url(driver_url, config) scheme, cluster_path = driver_url.split(':', 2) cluster_path = File.(cluster_path || File.join(Chef::Config.config_dir, 'vms')) "vagrant:#{cluster_path}" end |
.from_url(driver_url, config) ⇒ Object
34 35 36 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 34 def self.from_url(driver_url, config) Driver.new(driver_url, config) end |
.vagrant_config_string(vagrant_config, variable, line_prefix) ⇒ Object
Used by vagrant_cluster and machine to get the string used to configure vagrant
193 194 195 196 197 198 199 200 201 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 193 def self.vagrant_config_string(vagrant_config, variable, line_prefix) hostname = name.gsub(/[^A-Za-z0-9\-]/, '-') result = '' vagrant_config.each_pair do |key, value| result += "#{line_prefix}#{variable}.#{key} = #{value.inspect}\n" end result end |
Instance Method Details
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
Acquire a machine, generally by provisioning it. Returns a Machine object pointing at the machine, allowing useful actions like setup, converge, execute, file and directory.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 47 def allocate_machine(action_handler, machine_spec, ) ensure_vagrant_cluster(action_handler) vm_name = machine_spec.name vm_file_path = File.join(cluster_path, "#{machine_spec.name}.vm") vm_file_updated = create_vm_file(action_handler, vm_name, vm_file_path, ) if vm_file_updated || !machine_spec.location old_location = machine_spec.location machine_spec.location = { 'driver_url' => driver_url, 'driver_version' => Chef::Provisioning::VagrantDriver::VERSION, 'vm_name' => vm_name, 'vm_file_path' => vm_file_path, 'allocated_at' => Time.now.utc.to_s, 'host_node' => action_handler.host_node } machine_spec.location['needs_reload'] = true if vm_file_updated if [:vagrant_options] %w(vm.guest winrm.host winrm.port winrm.username winrm.password).each do |key| machine_spec.location[key] = [:vagrant_options][key] if [:vagrant_options][key] end end machine_spec.location['chef_client_timeout'] = [:chef_client_timeout] if [:chef_client_timeout] end end |
#connect_to_machine(machine_spec, machine_options) ⇒ Object
Connect to machine without acquiring it
78 79 80 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 78 def connect_to_machine(machine_spec, ) machine_for(machine_spec, ) end |
#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 82 def destroy_machine(action_handler, machine_spec, ) if machine_spec.location vm_name = machine_spec.location['vm_name'] current_status = vagrant_status(vm_name) if current_status != 'not created' action_handler.perform_action "run vagrant destroy -f #{vm_name} (status was '#{current_status}')" do result = shell_out("vagrant destroy -f #{vm_name}", :cwd => cluster_path) if result.exitstatus != 0 raise "vagrant destroy failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end end end convergence_strategy_for(machine_spec, ). cleanup_convergence(action_handler, machine_spec) vm_file_path = machine_spec.location['vm_file_path'] ChefMetal.inline_resource(action_handler) do file vm_file_path do action :delete end end end end |
#destroy_machines(action_handler, specs_and_options, parallelizer) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 133 def destroy_machines(action_handler, , parallelizer) all_names = [] all_status = [] all_outputs = {} .each_key do |spec| if spec.location vm_name = spec.location['vm_name'] current_status = vagrant_status(vm_name) if current_status != 'not created' all_names.push(vm_name) all_status.push(current_status) end end end if all_names.length > 0 names = all_names.join(" ") statuses = all_status.join(", ") action_handler.perform_action "run vagrant destroy -f #{names} (status was '#{statuses}')" do result = shell_out("vagrant destroy -f #{names}", :cwd => cluster_path) if result.exitstatus != 0 raise "vagrant destroy failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end end end .each_pair do |spec, | convergence_strategy_for(spec, ).cleanup_convergence(action_handler, spec) vm_file_path = spec.location['vm_file_path'] ChefMetal.inline_resource(action_handler) do file vm_file_path do action :delete end end yield spec if block_given? end end |
#driver_url ⇒ Object
203 204 205 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 203 def driver_url "vagrant:#{cluster_path}" end |
#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
72 73 74 75 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 72 def ready_machine(action_handler, machine_spec, ) start_machine(action_handler, machine_spec, ) machine_for(machine_spec, ) end |
#ready_machines(action_handler, specs_and_options, parallelizer) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 122 def ready_machines(action_handler, , parallelizer) start_machines(action_handler, ) machines = [] .each_pair do |spec, | machine = machine_for(spec, ) machines << machine yield machine if block_given? end machines end |
#stop_machine(action_handler, machine_spec, machine_options) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 107 def stop_machine(action_handler, machine_spec, ) if machine_spec.location vm_name = machine_spec.location['vm_name'] current_status = vagrant_status(vm_name) if current_status == 'running' action_handler.perform_action "run vagrant halt #{vm_name} (status was '#{current_status}')" do result = shell_out("vagrant halt #{vm_name}", :cwd => cluster_path) if result.exitstatus != 0 raise "vagrant halt failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end end end end end |
#stop_machines(action_handler, specs_and_options, parallelizer) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/chef/provisioning/vagrant_driver/driver.rb', line 170 def stop_machines(action_handler, , parallelizer) all_names = [] .each_key do |spec| if spec.location vm_name = spec.location['vm_name'] current_status = vagrant_status(vm_name) if current_status == 'running' all_names.push(vm_name) end end end if all_names.length > 0 names = all_names.join(" ") action_handler.perform_action "run vagrant halt #{names} (status was 'running')" do result = shell_out("vagrant halt #{names}", :cwd => cluster_path) if result.exitstatus != 0 raise "vagrant halt failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end end end end |