Skip to content

Commit

Permalink
These methods should never have been in logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
relistan committed Apr 14, 2015
1 parent 32e87c1 commit 2621ba3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 58 deletions.
9 changes: 5 additions & 4 deletions lib/centurion/docker_via_cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pty'
require_relative 'logging'
require_relative 'shell'

module Centurion; end

Expand All @@ -14,16 +15,16 @@ def initialize(hostname, port, docker_path, tls_args = {})

def pull(image, tag='latest')
info 'Using CLI to pull'
echo(build_command(:pull, "#{image}:#{tag}"))
Centurion::Shell.echo(build_command(:pull, "#{image}:#{tag}"))
end

def tail(container_id)
info "Tailing the logs on #{container_id}"
echo(build_command(:logs, container_id))
Centurion::Shell.echo(build_command(:logs, container_id))
end

def attach(container_id)
echo(build_command(:attach, container_id))
Centurion::Shell.echo(build_command(:attach, container_id))
end

private
Expand All @@ -37,7 +38,7 @@ def all_tls_path_available?
end

def tls_parameters
return '' if @tls_args.nil? || @tls_args == {}
return '' if @tls_args.nil? || @tls_args.empty?

tls_flags = ''

Expand Down
4 changes: 2 additions & 2 deletions lib/centurion/dogestry.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'logging'
require_relative 'shell'
require 'fileutils'

module Centurion; end
Expand Down Expand Up @@ -72,7 +73,6 @@ def pull(repo, pull_hosts)
hosts = pull_hosts.join(",")
flags = "-pullhosts #{hosts}"

echo(exec_command('pull', repo, flags))
Centurion::Shell.echo(exec_command('pull', repo, flags))
end

end
43 changes: 0 additions & 43 deletions lib/centurion/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,6 @@ def debug(*args)
log.debug args.join(' ')
end

def echo(command)
if Thread.list.find_all { |t| t.status == 'run' }.count > 1
run_without_echo(command)
else
run_with_echo(command)
end
end

def run_without_echo(command)
output = Queue.new
output_thread = Thread.new do
while true do
begin
puts output.pop
rescue => e
info "Rescuing... #{e.message}"
end
end
end

IO.popen(command) do |io|
io.each_line { |line| output << line }
end

output_thread.kill
validate_status(command)
end

def run_with_echo(command)
$stdout.sync = true
$stderr.sync = true
IO.popen(command) do |io|
io.each_char { |char| print char }
end
validate_status(command)
end

def validate_status(command)
unless $?.success?
raise "The command failed with a non-zero exit status: #{$?.exitstatus}. Command: '#{command}'"
end
end

private

def log(*args)
Expand Down
46 changes: 46 additions & 0 deletions lib/centurion/shell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Centurion; end

module Centurion::Shell
def self.echo(command)
if Thread.list.find_all { |t| t.status == 'run' }.count > 1
run_without_echo(command)
else
run_with_echo(command)
end
end

def self.run_without_echo(command)
output = Queue.new
output_thread = Thread.new do
while true do
begin
puts output.pop
rescue => e
info "Rescuing... #{e.message}"
end
end
end

IO.popen(command) do |io|
io.each_line { |line| output << line }
end

output_thread.kill
validate_status(command)
end

def self.run_with_echo(command)
$stdout.sync = true
$stderr.sync = true
IO.popen(command) do |io|
io.each_char { |char| print char }
end
validate_status(command)
end

def self.validate_status(command)
unless $?.success?
raise "The command failed with a non-zero exit status: #{$?.exitstatus}. Command: '#{command}'"
end
end
end
18 changes: 9 additions & 9 deletions spec/docker_via_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
context 'without TLS certificates' do
let(:docker_via_cli) { Centurion::DockerViaCli.new('host1', 2375, docker_path) }
it 'pulls the latest image given its name' do
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with("docker -H=tcp://host1:2375 pull foo:latest")
docker_via_cli.pull('foo')
end

it 'pulls an image given its name & tag' do
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with("docker -H=tcp://host1:2375 pull foo:bar")
docker_via_cli.pull('foo', 'bar')
end

it 'tails logs on a container' do
id = '12345abcdef'
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with("docker -H=tcp://host1:2375 logs -f #{id}")
docker_via_cli.tail(id)
end

it 'should print all chars when one thread is running' do
expect(docker_via_cli).to receive(:run_with_echo)
expect(Centurion::Shell).to receive(:run_with_echo)

allow(Thread).to receive(:list) {[double(:status => 'run')]}

docker_via_cli.pull('foo')
end

it 'should only print lines when multiple threads are running' do
expect(docker_via_cli).to receive(:run_without_echo)
expect(Centurion::Shell).to receive(:run_without_echo)

allow(Thread).to receive(:list) {[double(:status => 'run'), double(:status => 'run')]}

Expand All @@ -47,7 +47,7 @@
let(:docker_via_cli) { Centurion::DockerViaCli.new('host1', 2375,
docker_path, tls_args) }
it 'pulls the latest image given its name' do
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with('docker -H=tcp://host1:2375 ' \
'--tlsverify ' \
'--tlscacert=/certs/ca.pem ' \
Expand All @@ -57,7 +57,7 @@
end

it 'pulls an image given its name & tag' do
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with('docker -H=tcp://host1:2375 ' \
'--tlsverify ' \
'--tlscacert=/certs/ca.pem ' \
Expand All @@ -68,7 +68,7 @@

it 'tails logs on a container' do
id = '12345abcdef'
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with('docker -H=tcp://host1:2375 ' \
'--tlsverify ' \
'--tlscacert=/certs/ca.pem ' \
Expand All @@ -79,7 +79,7 @@

it 'attach to a container' do
id = '12345abcdef'
expect(docker_via_cli).to receive(:echo).
expect(Centurion::Shell).to receive(:echo).
with('docker -H=tcp://host1:2375 ' \
'--tlsverify ' \
'--tlscacert=/certs/ca.pem ' \
Expand Down

0 comments on commit 2621ba3

Please sign in to comment.