Class: DeisInteractive::Rails::Logs

Inherits:
Base
  • Object
show all
Defined in:
lib/deis-interactive/rails/logs.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#app, #process

Instance Method Summary collapse

Methods inherited from Base

#deis_remote, #git_remote_response, #inferred_app, #pod_ids, #processes_pattern

Constructor Details

#initialize(app, process, follow: false, count: nil) ⇒ Logs

Returns a new instance of Logs.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/deis-interactive/rails/logs.rb', line 14

def initialize(app, process, follow: false, count: nil)
  super(app, process)
  @follow = follow
  @pids = Concurrent::Array.new
  @outputs = Concurrent::Array.new
  @count = count || 20

  at_exit do
    pids.each do |pid|
      Process.kill("KILL", pid) if pid_alive?(pid)
    end
  end
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



10
11
12
# File 'lib/deis-interactive/rails/logs.rb', line 10

def count
  @count
end

#followObject (readonly)

Returns the value of attribute follow.



9
10
11
# File 'lib/deis-interactive/rails/logs.rb', line 9

def follow
  @follow
end

#outputsObject (readonly)

Returns the value of attribute outputs.



12
13
14
# File 'lib/deis-interactive/rails/logs.rb', line 12

def outputs
  @outputs
end

#pidsObject (readonly)

Returns the value of attribute pids.



11
12
13
# File 'lib/deis-interactive/rails/logs.rb', line 11

def pids
  @pids
end

Instance Method Details

#any_pid_alive?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# File 'lib/deis-interactive/rails/logs.rb', line 47

def any_pid_alive?
  20.times {
    break if pids.count > 0
    sleep 0.1
  }

  return false if pids.count == 0
  pids.any? { |pid| pid_alive?(pid) }
end

#follow_optionObject



57
58
59
60
61
# File 'lib/deis-interactive/rails/logs.rb', line 57

def follow_option
  if follow
    "-f"
  end
end

#log_pod(pod_id) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/deis-interactive/rails/logs.rb', line 80

def log_pod(pod_id)
  Thread.new do
    cmd = "kubectl logs #{follow_option} --tail #{count} #{pod_id} --namespace #{app}"
    Open3.popen2e(cmd) do |_, out_err, wait_thr|
      pids << wait_thr.pid
      out_err.each { |line| outputs << line }
    end
  end
end

#log_podsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/deis-interactive/rails/logs.rb', line 63

def log_pods
  pod_ids.each do |pod_id|
    log_pod(pod_id)
  end

  loop do
    while (outputs.count > 0)
      puts outputs.shift
    end
    if any_pid_alive?
      sleep 0.01
    else
      break
    end
  end
end

#performObject



28
29
30
31
32
33
34
35
36
# File 'lib/deis-interactive/rails/logs.rb', line 28

def perform
  if process.nil?
    puts "Logging on all pods"
  else
    puts "Logging on pod of process #{process}"
  end

  log_pods
end

#pid_alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/deis-interactive/rails/logs.rb', line 38

def pid_alive?(pid)
  begin
    Process.getpgid( pid )
    true
  rescue Errno::ESRCH
    false
  end
end