Skip to content

Commit

Permalink
Ensure ruby 2.5 syntax and that temp files are cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
David Heyer committed Feb 20, 2024
1 parent 82c4a02 commit ebbed24
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/taste_tester/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def gen_file_list(path, destination)
while (prefix = look_at.pop)
Dir.glob(File.join("#{prefix}**", '*'), File::FNM_DOTMATCH) do |p|
sep_index = p.index(File::SEPARATOR)
minus_first = sep_index.nil? ? '' : p.slice(sep_index+1..)
minus_first = sep_index.nil? ? '' : p[sep_index+1..-1]

if File.directory?(p)
# we don't store directories in the tar, but we do want to follow
Expand All @@ -162,15 +162,15 @@ def gen_file_list(path, destination)
targets
end

def generate_intermediate_tar(bucket, i)
stream = Tempfile.create([i.to_s, '.tar'], @server.bundle_dir)
def generate_intermediate_tar(bucket, i, prefix)
stream = Tempfile.create([prefix, i.to_s, '.tar'], @server.bundle_dir)
Minitar::Writer.open(stream) do |writer|
bucket.each do |file_entry|
file_path = File.join(file_entry[BASE_PATH_INDEX], file_entry[RELATIVE_PATH_INDEX])
name = File.join(file_entry[DESTINATION_PATH_INDEX], file_entry[RELATIVE_PATH_INDEX])

sep_index = file_entry[RELATIVE_PATH_INDEX].index(File::SEPARATOR)
minus_first = sep_index.nil? ? '' : file_entry[RELATIVE_PATH_INDEX].slice(sep_index+1..)
minus_first = sep_index.nil? ? '' : file_entry[RELATIVE_PATH_INDEX][sep_index+1..]

chefignores = Chef::Cookbook::Chefignore.new(file_path)
next if chefignores.ignored?(file_entry[RELATIVE_PATH_INDEX]) ||
Expand Down Expand Up @@ -221,7 +221,6 @@ def assemble_bundle(files)
# don't copy end blocks
IO.copy_stream(chunk, stream, (chunk.size - 1024))
chunk.close
File.unlink(chunk.path)
end
stream.write("\0" * 1024)
stream.close
Expand All @@ -236,6 +235,7 @@ def assemble_bundle(files)
end

def bundle_upload
puts "Running bundle upload"
src_dirs = {
TasteTester::Config.relative_role_dir => 'roles',
TasteTester::Config.relative_databag_dir => 'data_bags',
Expand All @@ -247,16 +247,22 @@ def bundle_upload
file_list = []
src_dirs.each { |path, dest| file_list += gen_file_list(path, dest) }

processes = TasteTester::Config.bundle_generation_processes || Etc.nprocessors
if processes > 1
buckets = file_list.each_slice((file_list.length/processes.to_f).round.to_i).to_a
chunks = Parallel.map((0...buckets.length), in_processes: buckets.length) do |i|
generate_intermediate_tar(buckets[i], i)
chunks = []
prefix = Time.now.to_i.to_s
begin
processes = TasteTester::Config.bundle_generation_processes || Etc.nprocessors
if processes > 1
buckets = file_list.each_slice((file_list.length/processes.to_f).round.to_i).to_a
chunks = Parallel.map((0...buckets.length), in_processes: buckets.length) do |i|
generate_intermediate_tar(buckets[i], i, prefix)
end
else
chunks = [generate_intermediate_tar(file_list, 0, prefix)]
end
else
chunks = [generate_intermediate_tar(file_list, 0)]
assemble_bundle(chunks)
ensure
Dir.glob("#{@server.bundle_dir}/#{prefix}*") { |f| File.unlink(f) }
end
assemble_bundle(chunks)
end

def full
Expand Down

0 comments on commit ebbed24

Please sign in to comment.