Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Busser and transport enhancements #18

Open
wants to merge 9 commits into
base: Transport
Choose a base branch
from
59 changes: 57 additions & 2 deletions lib/kitchen/busser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def initialize(suite_name, opts = {})
@config[:root_path] = opts.fetch(:root_path, DEFAULT_ROOT_PATH)
@config[:version] = opts.fetch(:version, "busser")
@config[:busser_bin] = opts.fetch(:busser_bin, File.join(@config[:root_path], "bin/busser"))
@config[:remote_transfer_path] = opts.fetch(:remote_transfer_path, File.join(@config[:root_path], "transfer"))
@config[:local_transfer_path] = nil
end

# Returns the name of this busser, suitable for display in a CLI.
Expand Down Expand Up @@ -89,6 +91,53 @@ def diagnose
result
end

# Clears local and constructs a remote command to clean-up busser transfer directory
#
# @return [string] a shell transfer specific directory delete command
def cleanup_cmd
#Empty the previous local transfer folder
FileUtils.rm_rf(config[:local_transfer_path]) if config[:local_transfer_path]

case shell
when "bourne"
cmd = [
sudo("rm -rf #{config[:remote_transfer_path]}"),
sudo("mkdir -p #{config[:remote_transfer_path]}"),
sudo("chmod 777 #{config[:remote_transfer_path]}")
].join("\n").concat("\n").gsub(/^ {10}/, '')

when "powershell"
cmd = <<-CMD.gsub(/^ {10}/, "")
rmdir -Recurse -Force -ea 0 #{config[:remote_transfer_path]} | out-null
exit 0
CMD
else
raise "[#{self}] Unsupported shell: #{shell}"
end
Util.wrap_command(cmd, shell)
end

# Prepare busser test files to be transferred to the target system
#
# @return [Hash] a hash list of local transfer files with its remote target directory
def sync_files
# Initialize a new transfer folder
config[:local_transfer_path] = Dir.mktmpdir("#{instance.name}-busser-transfer")

transfer_list = []
local_suite_files.each do |f|
raw_content = IO.read(f)
md5 = Digest::MD5.hexdigest(raw_content)
remote_dir = config[:remote_transfer_path]
temp_file = File.join(config[:local_transfer_path], md5)
encoded_content = Base64.encode64(raw_content).gsub("\n", '')
IO.binwrite(temp_file, encoded_content)

transfer_list.push({local: temp_file , remote: remote_dir})
end
transfer_list
end

# Returns a command string which installs Busser, and installs all
# required Busser plugins for the suite.
#
Expand Down Expand Up @@ -119,7 +168,10 @@ def setup_cmd
when "powershell"
cmd = <<-CMD.gsub(/^ {10}/, "")
#{busser_setup_env}
echo 'Cleaning up busser...'
gem uninstall #{plugins.join(" ")}

echo 'Setting up busser...'
if ((gem list busser -i) -eq \"false\") {
gem install #{gem_install_args}
}
Expand Down Expand Up @@ -312,8 +364,11 @@ def remote_file(file, dir)
# @api private
def stream_file(local_path, remote_path)
local_file = IO.read(local_path)
encoded_file = Base64.encode64(local_file).gsub("\n", "")

#encoded_file = Base64.encode64(local_file).gsub("\n", "")
md5 = Digest::MD5.hexdigest(local_file)
transfer_path = File.join(File.join(config[:root_path],'transfer'),md5)

perms = format("%o", File.stat(local_path).mode)[2, 4]
stream_cmd = [
sudo(config[:busser_bin]),
Expand All @@ -325,7 +380,7 @@ def stream_file(local_path, remote_path)

[
%{echo "Uploading #{remote_path} (mode=#{perms})"},
%{echo "#{encoded_file}" | #{stream_cmd}}
%{cat "#{transfer_path}" | #{stream_cmd}}
].join("\n").concat("\n")
end

Expand Down
1 change: 1 addition & 0 deletions lib/kitchen/data_munger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def transport_data_for(suite, platform)
set_kitchen_config_at!(tdata, :kitchen_root)
set_kitchen_config_at!(tdata, :test_base_path)
set_kitchen_config_at!(tdata, :log_level)
tdata[:ssh_key] = data[:driver][:ssh_key]
combine_arrays!(tdata, :run_list, :platform, :suite)
end
end
Expand Down
12 changes: 11 additions & 1 deletion lib/kitchen/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

require "kitchen/lazy_hash"


module Kitchen

module Driver
Expand Down Expand Up @@ -86,14 +87,23 @@ def setup(state)
end
end

# Verifies a converged instance.
# Uploads latest test files and verifies a converged instance.
#
# @param state [Hash] mutable instance and driver state
# @raise [ActionFailed] if the action could not be completed
def verify(state)
transport.connection(state) do |conn|

conn.execute(busser.cleanup_cmd)

busser.sync_files.each do |file|
conn.upload!(file[:local], file[:remote])
end

conn.execute(busser.sync_cmd)
conn.execute(busser.run_cmd)

conn.execute(busser.cleanup_cmd)
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/kitchen/transport/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def execute(command)

# (see Base#upload!)
def upload!(local, remote, options = {}, &progress)
return if local.nil? || local.empty?

options = { :recursive => true }.merge(options)

if progress.nil?
Expand All @@ -56,6 +58,8 @@ def upload!(local, remote, options = {}, &progress)
}
end


local = Array.new(1) { local } if local.is_a? String
local.each do |path|
session.scp.upload!(path, remote, options, &progress)
end
Expand Down
14 changes: 10 additions & 4 deletions lib/kitchen/transport/winrm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
end

require "logger"

require "kitchen/errors"
require "kitchen/login_command"

module Kitchen

module Transport
Expand Down Expand Up @@ -78,6 +78,8 @@ def wql(query)

# (see Base#upload!)
def upload!(local, remote)
return if local.nil? || local.empty?

logger.info("Concurrent threads set to :max_threads => #{config[:max_threads]}")
logger.debug("Upload: #{local} -> #{remote}")
local = Array.new(1) { local } if local.is_a? String
Expand Down Expand Up @@ -351,12 +353,16 @@ def should_upload_file?(local, remote)
$file.Dispose()
}
if ($guest_md5 -eq '#{local_md5}') {
exit 0
[Environment]::ExitCode = 0
return 0
}
}
exit 1
[Environment]::ExitCode = 1
return 1
EOH
powershell(command)[:exitcode] == 1
result = powershell(command)
should = (result[:data][0][:stdout] == "1")
should
end

# Uploads the given file to a new temp file on the guest
Expand Down