Class: KnifeWsFusion::BaseCommand
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- KnifeWsFusion::BaseCommand
- Defined in:
- lib/chef/knife/base_command.rb
Direct Known Subclasses
Constant Summary collapse
- @@fusion_search_paths =
[ File.join('/', 'Applications', 'VMware Fusion Tech Preview.app'), File.join('/', 'Applications', 'VMware Fusion.app'), ]
Instance Method Summary collapse
- #clone_vm(src_vmx_path, dest_vmx_path, clone_type, clone_name, snapshot_name = nil) ⇒ Object
- #create_vm_directory(vms_dir, vm_name) ⇒ Object
- #get_exe_from_path(exe) ⇒ Object
- #get_guest_ip_address(vmx_path) ⇒ Object
- #get_tools_state(vmx_path) ⇒ Object
- #get_vmrun_path ⇒ Object
- #is_fusion? ⇒ Boolean
- #locate_config_value(key) ⇒ Object
- #normalize_vmx_path(path) ⇒ Object
- #power_on_vm(vmx_path) ⇒ Object
- #run_vmrun(args) ⇒ Object
- #set_vmx_variable(vmx_path, key, value) ⇒ Object
- #wait_for_ssh(ip_address, port) ⇒ Object
Instance Method Details
#clone_vm(src_vmx_path, dest_vmx_path, clone_type, clone_name, snapshot_name = nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/chef/knife/base_command.rb', line 140 def clone_vm(src_vmx_path, dest_vmx_path, clone_type, clone_name, snapshot_name=nil) args = [ 'clone', src_vmx_path, dest_vmx_path, clone_type, #"-cloneName=#{clone_name}", ] if snapshot_name args.push("-snapshot=#{snapshot_name}") end exit_status, out, err = run_vmrun(args) return exit_status == 0 end |
#create_vm_directory(vms_dir, vm_name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chef/knife/base_command.rb', line 59 def create_vm_directory(vms_dir, vm_name) if is_fusion? vm_name += '.vmwarevm' end vm_dir = File.join(vms_dir, vm_name) Dir.mkdir(vm_dir, 0755) return vm_dir end |
#get_exe_from_path(exe) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/chef/knife/base_command.rb', line 47 def get_exe_from_path(exe) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe_path = File.join(path, "#{exe}#{ext}") return exe_path if File.executable?(exe_path) end end return nil end |
#get_guest_ip_address(vmx_path) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/chef/knife/base_command.rb', line 191 def get_guest_ip_address(vmx_path) exit_status, out, err = \ run_vmrun(['getGuestIPAddress', vmx_path, '-wait']) if exit_status == 0 return out.strip() else return '' end end |
#get_tools_state(vmx_path) ⇒ Object
185 186 187 188 189 |
# File 'lib/chef/knife/base_command.rb', line 185 def get_tools_state(vmx_path) exit_status, out, err = run_vmrun(['checkToolsState', vmx_path]) return out.strip() end |
#get_vmrun_path ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/chef/knife/base_command.rb', line 22 def get_vmrun_path if config[:vmrun_path] return config[:vmrun_path] end path = get_exe_from_path('vmrun') if path return path end if is_fusion? @@fusion_search_paths.each do |search_path| vmrun_path = File.join(search_path, 'Contents', 'Library', 'vmrun') if File.executable?(vmrun_path) return vmrun_path end end end return nil end |
#is_fusion? ⇒ Boolean
12 13 14 15 16 17 18 19 20 |
# File 'lib/chef/knife/base_command.rb', line 12 def is_fusion? @@fusion_search_paths.each do |path| if File.directory?(path) return true end end return false end |
#locate_config_value(key) ⇒ Object
202 203 204 205 |
# File 'lib/chef/knife/base_command.rb', line 202 def locate_config_value(key) key = key.to_sym return Chef::Config[:knife][key] || config[key] end |
#normalize_vmx_path(path) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/chef/knife/base_command.rb', line 70 def normalize_vmx_path(path) path = File.(path) if not File.exists?(path) return nil end if path.end_with?('.vmx') return path end if not File.directory?(path) # Well, it had better be a VMX file then. return path end Dir.foreach(path) do |filename| if filename.end_with?('.vmx') return File.join(path, filename) end end return nil end |
#power_on_vm(vmx_path) ⇒ Object
179 180 181 182 183 |
# File 'lib/chef/knife/base_command.rb', line 179 def power_on_vm(vmx_path) exit_status, out, err = run_vmrun(['start', vmx_path, 'nogui']) return exit_status == 0 end |
#run_vmrun(args) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/chef/knife/base_command.rb', line 95 def run_vmrun(args) vmrun_path = get_vmrun_path() cmd = [vmrun_path] + args out = nil err = nil exit_status = nil Chef::Log.debug("Running #{cmd.join(' ')}") Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr| wait_thr.join() out = stdout.read() err = stderr.read() exit_status = wait_thr.value end Chef::Log.debug("... stdout: {#{out}}") Chef::Log.debug("... stderr: {#{err}}") Chef::Log.debug("... exit code: {#{exit_status}}") return exit_status, out, err end |
#set_vmx_variable(vmx_path, key, value) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/chef/knife/base_command.rb', line 120 def set_vmx_variable(vmx_path, key, value) lines = [] File.open(vmx_path, 'r') do |file| lines = file.readlines() end line_re = Regexp.new("^#{key}\s*=") File.open(vmx_path, 'w') do |file| lines.each do |line| if line_re.match(line) file.write("#{key} = \"#{value}\"\n") else file.write(line) end end end end |
#wait_for_ssh(ip_address, port) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/chef/knife/base_command.rb', line 159 def wait_for_ssh(ip_address, port) tcp_socket = TCPSocket.new(ip_address, port) readable = IO.select([tcp_socket], nil, nil, 5) if readable Chef::Log.debug("Connected to sshd on #{ip_address}") yield return true else return false end rescue Errno::ETIMEDOUT, Errno::EPERM return false rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH sleep 2 return false ensure tcp_socket && tcp_socket.close end |