Class: DockerPier::LogStream

Inherits:
Object
  • Object
show all
Defined in:
lib/docker-pier/log-stream.rb,
lib/docker-pier/log-stream/palette.rb

Defined Under Namespace

Classes: Palette

Instance Method Summary collapse

Constructor Details

#initialize(pier) ⇒ LogStream

Returns a new instance of LogStream.



17
18
19
20
21
# File 'lib/docker-pier/log-stream.rb', line 17

def initialize(pier)
  @pier = pier
  @palette = DockerPier::LogStream::Palette.new
  @task_colors = Hash.new{ |h,k| h[k] = @palette.draw! }
end

Instance Method Details

#get_path!Object



27
28
29
30
31
32
33
34
35
# File 'lib/docker-pier/log-stream.rb', line 27

def get_path!
  logs_dir = Pathname.new '/var/log/fluentd'
  logfile_link = logs_dir + 'docker.log'

  # fluentd creates the symlink with an absolute path from inside a container,
  # so the dir the link points to is wrong. We assume the logs dir instead.
  logs_link_dest = Pathname.new(@pier.ssh.exec!("readlink #{logfile_link.to_shell}").chomp)
  logs_dir + logs_link_dest.basename
end

#parse_log_ln(ln) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/docker-pier/log-stream.rb', line 37

def parse_log_ln(ln)
  ts, fluent_source_id, event_json = ln.split(/\s+/, 3)

  ts = DateTime.parse(ts)
  event = JSON.parse(event_json)
  event_type = (event.delete('source') == 'stderr') ? :error : :output

  container_id = event.delete('container_name')[1..-1]
  msg = event.delete('log')

  if msg[0] == '{'
    msg_parts = JSON.parse(msg)
    msg_lns = msg_parts.delete('lines').map{ |ln| ln.chomp }
    msg_parts = OpenStruct.new(msg_parts)
  else
    msg_lns = msg.split("\n")
  end

  service_parts = container_id.match(/^(\w+)\.(\d+)\.(\w+)$/)
  service = service_parts ? [service_parts[1], service_parts[2].to_i] : nil

  task_handle = service ? ("%s[%02d]" % service) : container_id
  task_color = @task_colors[task_handle]

  OpenStruct.new(
    time: ts,
    type: event_type,
    service: service,
    task: OpenStruct.new(name: task_handle, color: task_color),
    location: [:some_node, container_id],
    message: msg_parts,
    lines: msg_lns
  )
end

#pathObject



23
24
25
# File 'lib/docker-pier/log-stream.rb', line 23

def path
  @path ||= get_path!
end

#stream!Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/docker-pier/log-stream.rb', line 72

def stream!
  ssh_session = @pier.ssh

  channel = ssh_session.open_channel do |ch|
		ch.exec "tail -f #{self.path.to_shell}" do |ch, success|
      raise "could not tail logs on remote" unless success

      ch.on_data do |_, data|
        data.split("\n").each do |ln|
          event = parse_log_ln(ln)
          yield(event)
        end
      end
    end
  end

  int_pressed = false
  trap("INT") { int_pressed = true }
  ssh_session.loop(0.1) { not int_pressed }

  ssh_session.shutdown!
end