Class: Cody::Tailer

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/cody/tailer.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #codebuild

Methods included from AwsServices::Helpers

#are_you_sure?, #inferred_project_name, #inferred_stack_name, #project_name_convention, #stack_exists?

Constructor Details

#initialize(options, build_id) ⇒ Tailer

Returns a new instance of Tailer.



7
8
9
10
11
12
13
14
# File 'lib/cody/tailer.rb', line 7

def initialize(options, build_id)
  @options, @build_id = options, build_id

  @output = [] # for specs
  @shown_phases = []
  @thread = nil
  set_trap
end

Instance Method Details

#build_time(build) ⇒ Object



150
151
152
153
# File 'lib/cody/tailer.rb', line 150

def build_time(build)
  duration = build.phases.inject(0) { |sum,p| sum + p.duration_in_seconds.to_i }
  pretty_time(duration)
end

#cloudwatch_tailObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cody/tailer.rb', line 55

def cloudwatch_tail
  since = @options[:since] || "7d" # by default, search only 7 days in the past
  cw_tail = AwsLogs::Tail.new(
    log_group_name: @log_group_name,
    log_stream_names: [@log_stream_name],
    since: since,
    follow: true,
    format: "simple",
  )
  cw_tail.run
end

#complete_failed?(build) ⇒ Boolean

build.build_status : The current status of the build. Valid values include:

FAILED : The build failed.
FAULT : The build faulted.
IN_PROGRESS : The build is still in progress.
STOPPED : The build stopped.
SUCCEEDED : The build succeeded.
TIMED_OUT : The build timed out.

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/cody/tailer.rb', line 91

def complete_failed?(build)
  return if ENV["CODY_TEST"]
  build.build_complete && build.build_status != "SUCCEEDED"
end

#display_phase(details) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cody/tailer.rb', line 116

def display_phase(details)
  already_shown = @shown_phases.detect do |p|
    p[:phase_type] == details[:phase_type] &&
    p[:phase_status] == details[:phase_status] &&
    p[:start_time] == details[:start_time] &&
    p[:duration_in_seconds] == details[:duration_in_seconds]
  end
  return if already_shown

  status = details[:phase_status]
  status = status&.include?("FAILED") ? status.color(:red) : status
  say [
    "Phase:".color(:green), details[:phase_type],
    "Status:".color(:purple), status,
    # "Time: ".color(:purple), details[:start_time],
    "Duration:".color(:purple), details[:duration_in_seconds],
  ].join(" ")
end

#find_buildObject



40
41
42
43
# File 'lib/cody/tailer.rb', line 40

def find_build
  resp = codebuild.batch_get_builds(ids: [@build_id])
  resp.builds.first
end

#outputObject



139
140
141
# File 'lib/cody/tailer.rb', line 139

def output
  @output.join("\n") + "\n"
end

#pretty_time(total_seconds) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/cody/tailer.rb', line 156

def pretty_time(total_seconds)
  minutes = (total_seconds / 60) % 60
  seconds = total_seconds % 60
  if total_seconds < 60
    "#{seconds.to_i}s"
  else
    "#{minutes.to_i}m #{seconds.to_i}s"
  end
end


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cody/tailer.rb', line 103

def print_phases(build)
  build.phases.each do |phase|
    details = {
      phase_type: phase.phase_type,
      phase_status: phase.phase_status,
      start_time: phase.start_time,
      duration_in_seconds: phase.duration_in_seconds,
    }
    display_phase(details)
    @shown_phases << details
  end
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cody/tailer.rb', line 16

def run
  puts "Showing logs for build #{@build_id}"

  complete = false
  until complete do
    build = find_build
    unless build
      puts "ERROR: Build id not found: #{@build_id}".color(:red)
      return
    end
    print_phases(build)
    set_log_group_name(build)

    complete = build.build_complete

    next if ENV["CODY_TEST"]
    start_cloudwatch_tail
    sleep 5
  end

  stop_cloudwatch_tail(build)
  puts "The build took #{build_time(build)} to complete."
end

#say(text) ⇒ Object



135
136
137
# File 'lib/cody/tailer.rb', line 135

def say(text)
  ENV["CODY_TEST"] ? @output << text : puts(text)
end

#set_log_group_name(build) ⇒ Object

Setting enables start_cloudwatch_tail



97
98
99
100
101
# File 'lib/cody/tailer.rb', line 97

def set_log_group_name(build)
  logs = build.logs
  @log_group_name = logs.group_name if logs.group_name
  @log_stream_name = logs.stream_name if logs.stream_name
end

#set_trapObject



143
144
145
146
147
148
# File 'lib/cody/tailer.rb', line 143

def set_trap
  Signal.trap("INT") {
    puts "\nCtrl-C detected. Exiting..."
    exit # immediate exit
  }
end

#start_cloudwatch_tailObject



45
46
47
48
49
50
51
52
53
# File 'lib/cody/tailer.rb', line 45

def start_cloudwatch_tail
  return if @cloudwatch_tail_started
  return unless @log_group_name && @log_stream_name

  @thread = Thread.new do
    cloudwatch_tail
  end
  @cloudwatch_tail_started = true
end

#stop_cloudwatch_tail(build) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cody/tailer.rb', line 67

def stop_cloudwatch_tail(build)
  return if ENV["CODY_TEST"]

  # The AwsLogs::Tail.stop_follow! results in a little waiting because it signals to break the polling loop.
  # Since it's in the middle of the loop process, the loop will finish the sleep 5 first.
  # So it can pause from 0-5 seconds.
  #
  # However, this is sometimes not enough of a pause for CloudWatch to receive and send the logs back to us.
  # So additionally pause on a failed build so we can receive the final logs at the end.
  #
  sleep 10 if complete_failed?(build) # provide extra time for cw tail to report error
  AwsLogs::Tail.stop_follow!
  @thread.join if @thread
end