Class: ChefMetalVagrant::VagrantDriver
- Inherits:
-
ChefMetal::Driver
- Object
- ChefMetal::Driver
- ChefMetalVagrant::VagrantDriver
- Includes:
- Chef::Mixin::ShellOut
- Defined in:
- lib/chef_metal_vagrant/vagrant_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) ⇒ VagrantDriver
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) ⇒ VagrantDriver
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.
24 25 26 27 28 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 24 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.
30 31 32 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 30 def cluster_path @cluster_path end |
Class Method Details
.canonicalize_url(driver_url, config) ⇒ Object
36 37 38 39 40 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 36 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
32 33 34 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 32 def self.from_url(driver_url, config) VagrantDriver.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
191 192 193 194 195 196 197 198 199 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 191 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 45 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' => ChefMetalVagrant::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
76 77 78 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 76 def connect_to_machine(machine_spec, ) machine_for(machine_spec, ) end |
#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 80 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
131 132 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 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 131 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
201 202 203 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 201 def driver_url "vagrant:#{cluster_path}" end |
#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
70 71 72 73 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 70 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
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 120 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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 105 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/chef_metal_vagrant/vagrant_driver.rb', line 168 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 |