commands/docker-logs: parallelize for multiple containers

This commit is contained in:
Mitchell Hashimoto 2014-04-15 17:36:48 -07:00
parent 2add94ee28
commit cb95e8aaee
1 changed files with 50 additions and 42 deletions

View File

@ -30,58 +30,66 @@ module VagrantPlugins
argv = parse_options(opts) argv = parse_options(opts)
return if !argv return if !argv
output_options = {}
output_options[:prefix] = false if !options[:prefix]
# TODO: exit with exit status != 0 if all machines are unknown # TODO: exit with exit status != 0 if all machines are unknown
# or not created. # or not created.
# Go through each machine and execute the client on it # Go through each machine and execute the client on it
with_target_vms(argv) do |machine| @env.batch do |batch|
if machine.provider_name != :docker with_target_vms(argv) do |machine|
machine.ui.output(I18n.t("docker_provider.not_docker_provder")) if machine.provider_name != :docker
next machine.ui.output(I18n.t("docker_provider.not_docker_provder"))
end next
end
state = machine.state state = machine.state
if state == :host_state_unknown if state == :host_state_unknown
machine.ui.output(I18n.t("docker_provider.logs_host_state_unknown")) machine.ui.output(I18n.t("docker_provider.logs_host_state_unknown"))
next next
elsif state == :not_created elsif state == :not_created
machine.ui.output(I18n.t("docker_provider.not_created_skip")) machine.ui.output(I18n.t("docker_provider.not_created_skip"))
next next
end end
command = ["docker", "logs"] batch.custom(machine) do |m|
command << "--follow" if options[:follow] execute_single(m, options)
command << machine.id
data_acc = ""
machine.provider.driver.execute(*command) do |type, data|
# Accumulate the data so we only output lines at a time
data_acc << data
# If we have a newline, then output all the lines we have so far
if data_acc.include?("\n")
lines = data_acc.split("\n")
if !data_acc.end_with?("\n")
data_acc = lines.pop.chomp
else
data_acc = ""
end
lines.each do |line|
line = " " if line == ""
machine.ui.output(line, **output_options)
end
end end
end end
# Output any remaining data
machine.ui.output(data_acc, **output_options) if !data_acc.empty?
end end
end end
def execute_single(machine, options)
command = ["docker", "logs"]
command << "--follow" if options[:follow]
command << machine.id
output_options = {}
output_options[:prefix] = false if !options[:prefix]
data_acc = ""
machine.provider.driver.execute(*command) do |type, data|
# Accumulate the data so we only output lines at a time
data_acc << data
# If we have a newline, then output all the lines we have so far
if data_acc.include?("\n")
lines = data_acc.split("\n")
if !data_acc.end_with?("\n")
data_acc = lines.pop.chomp
else
data_acc = ""
end
lines.each do |line|
line = " " if line == ""
machine.ui.output(line, **output_options)
end
end
end
# Output any remaining data
machine.ui.output(data_acc, **output_options) if !data_acc.empty?
end
end end
end end
end end