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

[CHEF-18291] Fix the kitchen failures on windows #4

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/kitchen/driver/dokken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def make_dokken_network
debug "driver - error :#{e}:"
end
ensure
lockfile.unlock
lockfile.unlock if lockfile.locked?
end
end

Expand Down
42 changes: 37 additions & 5 deletions lib/kitchen/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Helpers
require "socket" unless defined?(Socket)
require "timeout" unless defined?(Timeout)
require "resolv" unless defined?(Resolv)
require "rubygems/package" unless defined?(Gem::Package)
require "stringio" unless defined?(StringIO)

def port_open?(ip, port)
begin
Expand Down Expand Up @@ -91,17 +93,45 @@ def data_dockerfile(registry)
def create_data_image(registry)
return if ::Docker::Image.exist?(data_image)

docker_file_content = data_dockerfile(registry)
keys_content = insecure_ssh_public_key
i = if remote_docker_host?
build_docker_image_from_tar(docker_file_content, keys_content)
else
build_docker_image_from_dir(docker_file_content, keys_content)
end

i.tag("repo" => repo(data_image), "tag" => tag(data_image), "force" => true)
end

def build_docker_image_from_tar(docker_file_content, keys_content)
tar_io = StringIO.new
Gem::Package::TarWriter.new(tar_io) do |tar|
tar.add_file_simple("Dockerfile", 0644, docker_file_content.bytesize) do |io|
io.write docker_file_content
end

tar.add_file_simple("authorized_keys", 0644, keys_content.bytesize) do |io|
io.write keys_content
end
end

tar_io.rewind

Docker::Image.build_from_tar(tar_io)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will be the size of tar_io? Everything is handled in memory. As tar_io is a StringIO, it may lead to high memory usage if memory is low in the system

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tar will contain only the Dockerfile and the authorized_keys file. 3584 is the length of the tar_io string created here.

end

def build_docker_image_from_dir(docker_file_content, keys_content)
tmpdir = Dir.tmpdir
FileUtils.mkdir_p "#{tmpdir}/dokken"
File.write("#{tmpdir}/dokken/Dockerfile", data_dockerfile(registry))
File.write("#{tmpdir}/dokken/authorized_keys", insecure_ssh_public_key)
File.write("#{tmpdir}/dokken/Dockerfile", docker_file_content)
File.write("#{tmpdir}/dokken/authorized_keys", keys_content)

i = ::Docker::Image.build_from_dir(
::Docker::Image.build_from_dir(
"#{tmpdir}/dokken",
"nocache" => true,
"rm" => true
)
i.tag("repo" => repo(data_image), "tag" => tag(data_image), "force" => true)
end

def default_docker_host
Expand Down Expand Up @@ -156,7 +186,9 @@ def home_dir
# refs:
# https://github.com/docker/machine/issues/1814
# https://github.com/docker/toolbox/issues/607
return Dir.home.sub "C:/Users", "/c/Users" if Dir.home =~ /^C:/ && remote_docker_host?
#
# home_dir method is no longer used in other than the lockfiles and sandbox dir.
# return Dir.home.sub "C:/Users", "/c/Users" if Dir.home =~ /^C:/ && remote_docker_host?

Dir.home
end
Expand Down
4 changes: 2 additions & 2 deletions lib/kitchen/transport/dokken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ def upload(locals, remote)
remote,
recursive: true,
ssh: { port: ssh_port, keys: ["#{tmpdir}/id_rsa"] })
debug "Copied #{local} to #{remote}"
end
end
end

def login_command
@runner = options[:instance_name].to_s
cols = `tput cols`
lines = `tput lines`
lines, cols = IO.console.winsize
args = ["exec", "-e", "COLUMNS=#{cols}", "-e", "LINES=#{lines}", "-it", @runner, "/bin/bash", "-login", "-i"]
LoginCommand.new(options[:login_command], args)
end
Expand Down
Loading