Skip to content

Commit 7ceb451

Browse files
committed
Try to use docker buildx ls output again now that it seems to have been fixed
1 parent 7154dff commit 7ceb451

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

cookbooks/boxcutter_docker/libraries/default.rb

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,24 @@ def self.context_rm(name, user, group)
5959
end
6060

6161
# buildkits
62-
def self.buildx_ls(home)
63-
# Currently the output of `docker buildx ls --format ls` is essentially
64-
# unparseable in an automated way. Work is being done to remedy this but
65-
# doesn't seem like it will land anytime soon, so instead look where the
66-
# config files are stored in ~/.docker/buildx
67-
# https://github.com/docker/buildx/pull/830
68-
buildx_instances_path = ::File.join(home, '.docker/buildx/instances')
69-
config_map = {}
70-
return config_map unless Dir.exist?(buildx_instances_path)
71-
Dir.foreach(buildx_instances_path) do |filename|
72-
next if ['.', '..'].include?(filename)
73-
74-
file_path = ::File.join(buildx_instances_path, filename)
75-
if ::File.file?(file_path)
76-
begin
77-
json_content = ::File.read(file_path)
78-
config_map[filename] = JSON.parse(json_content)
79-
rescue JSON::ParserError => e
80-
puts "Error parsing JSON in file #{filename}: #{e.message}"
81-
rescue StandardError => e
82-
puts "Error reading file #{filename}: #{e.message}"
83-
end
84-
end
62+
def self.buildx_ls(user, group)
63+
# https://github.com/docker/buildx/pull/1787
64+
# https://github.com/docker/buildx/pull/2138
65+
cmd = Mixlib::ShellOut.new(
66+
'docker buildx ls --no-trunc --format json',
67+
login: true,
68+
user: user,
69+
group: group,
70+
).run_command
71+
cmd.error!
72+
builder_instances = []
73+
# `docker buildx ls` outputs multiple JSON objects, each on a new line.
74+
# So we need to parse the output line by line and store each in an array
75+
cmd.stdout.each_line do |line|
76+
builder_instance = JSON.parse(line)
77+
builder_instances.push(builder_instance)
8578
end
86-
config_map
79+
builder_instances
8780
end
8881

8982
def self.buildx_create_command(name, config)

cookbooks/boxcutter_docker/resources/default.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ class Helpers
77
action :configure do
88
# buildx
99
node['boxcutter_docker']['buildx'].each do |user, user_config|
10-
current_builders = Boxcutter::Docker::Helpers.buildx_ls(user_config['home'])
10+
current_builders = Boxcutter::Docker::Helpers.buildx_ls(user_config['user'], user_config['group'])
1111
puts "MISCHA: current_builders=#{current_builders}"
12-
current_builder_names = current_builders.values.map { |builder| builder['Name'] }.compact
12+
docker_container_builders = current_builders.select { |builder| builder["Driver"] == 'docker-container' }
13+
current_builder_names = docker_container_builders.values.map { |builder| builder['Name'] }.compact
14+
# current_builder_names = current_builders.values.map { |builder| builder['Name'] }.compact
1315
puts "MISCHA: current_builder_names=#{current_builder_names}"
1416
desired_builder_names = user_config['builders'].values.map { |builder| builder['name'] }.compact
1517
puts "MISCHA: desired_builder_names=#{desired_builder_names}"

0 commit comments

Comments
 (0)