Class: Jets::Remote::Tailer

Inherits:
Base
  • Object
show all
Includes:
Util::Pretty
Defined in:
lib/jets/remote/tailer.rb

Direct Known Subclasses

CLI::Ci::Tailer

Constant Summary collapse

@@delay =

initial delay

2

Instance Method Summary collapse

Methods included from Util::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

#initialize(options, build_id) ⇒ Tailer

Returns a new instance of Tailer.



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

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

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

Instance Method Details

#build_phase_started?(build) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/jets/remote/tailer.rb', line 54

def build_phase_started?(build)
  @build_phase_started ||= build.phases.any? do |phase|
    phase.phase_type == "BUILD"
  end
end

#build_time(build) ⇒ Object



267
268
269
270
# File 'lib/jets/remote/tailer.rb', line 267

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

#cloudwatch_tailObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jets/remote/tailer.rb', line 118

def cloudwatch_tail
  since = @options[:since] || "24h" # by default, search only 24h in the past
  AwsLogs::Tail.new(
    log_group_name: @log_group_name,
    log_stream_names: [@log_stream_name],
    since: since,
    follow: true,
    format: "plain",
    show_if: show_if
  )
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)


164
165
166
167
# File 'lib/jets/remote/tailer.rb', line 164

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

#display_failed_phases(build) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jets/remote/tailer.rb', line 60

def display_failed_phases(build)
  status = build.build_status.to_s # in case nil
  status = (status != "SUCCEEDED") ? status.color(:red) : status.color(:green)
  return if status == "SUCCEEDED"

  failed_phases = build.phases.select do |phase|
    phase.phase_status != "SUCCEEDED" && phase.phase_status.to_s != ""
  end
  return if failed_phases.empty?

  puts "Failed Phases:"
  failed_phases.each do |phase|
    puts "#{phase.phase_type}: #{phase.phase_status.color(:red)}"
    next unless phase.contexts # can be nil
    context = phase.contexts.last
    if context # show error details: Unable to pull customer's container image https://gist.github.com/tongueroo/22e4ca3d4cde002108ff506eba9062f6
      message = context.message
      puts message
      if message.include?("CannotPullContainerError") && message.include?("access denied")
        puts "See: https://docs.aws.amazon.com/codebuild/latest/userguide/sample-ecr.html"
      end
    end
  end
end

#display_phase(details) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/jets/remote/tailer.rb', line 189

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

  return if filter_phase_type?(details)

  phases = [
    "Phase:", details[:phase_type]
  ]

  if details[:phase_type] == "COMPLETED"
    # nothing to add
  elsif details[:phase_status].nil?
    phases << "Pending"
  else
    phases += [
      phase_status(details[:phase_status]),
      phase_duration(details[:duration_in_seconds])
    ]
  end

  phases = phases.flatten.compact.join(" ")

  say phases
end

#display_time_took(build) ⇒ Object



85
86
87
# File 'lib/jets/remote/tailer.rb', line 85

def display_time_took(build)
  puts "Remote runner took #{build_time(build)} to complete"
end

#filter_phase_type?(details) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/jets/remote/tailer.rb', line 220

def filter_phase_type?(details)
  return false unless Jets.bootstrap.config.codebuild.logging.show == "filtered"

  # Phase: SUBMITTED Status: SUCCEEDED Duration: 0s
  # Phase: QUEUED Pending
  # Phase: QUEUED Status: SUCCEEDED Duration: 0s
  # Phase: PROVISIONING Pending
  # Phase: PROVISIONING Status: SUCCEEDED Duration: 5s
  # Phase: DOWNLOAD_SOURCE Status: SUCCEEDED Duration: 0s
  # Phase: INSTALL Status: SUCCEEDED Duration: 0s
  # Phase: PRE_BUILD Status: SUCCEEDED Duration: 0s
  # Phase: BUILD Pending
  # [Container] 2024/04/06 15:51:12.262049 Running command ...
  #
  # Becomes
  #
  # Phase: SUBMITTED Status: SUCCEEDED Duration: 0s
  # Phase: QUEUED Pending
  # Phase: QUEUED Status: SUCCEEDED Duration: 0s
  # Phase: PROVISIONING Pending
  # Phase: PROVISIONING Status: SUCCEEDED Duration: 5s
  # [Container] 2024/04/06 15:51:12.262049 Running command ...
  filtered_phase_types = %w[DOWNLOAD_SOURCE INSTALL PRE_BUILD BUILD]
  filtered_phase_types.include?(details[:phase_type])
end

#find_buildObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jets/remote/tailer.rb', line 89

def find_build
  resp = codebuild.batch_get_builds(ids: [@build_id])
  resp.builds.first
rescue Aws::CodeBuild::Errors::ThrottlingException => e
  log_arch "WARN: find_build codebuild.batch_get_builds ThrottlingException: #{e.message}"
  @@delay = 10 # increase global @@delay also

  # Also exponential backoff delay
  # 2, 4, 8, 16, 32
  @retries ||= 1
  raise if @retries > 5 # give up after 5 retries
  delay = 2**@retries
  log_arch "Retrying in #{delay}s..."
  sleep delay
  @retries += 1
  retry
end

#logs_command?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/jets/remote/tailer.rb', line 151

def logs_command?
  ARGV.join(" ").include?("logs")
end

#outputObject



263
264
265
# File 'lib/jets/remote/tailer.rb', line 263

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

#phase_duration(duration) ⇒ Object



253
254
255
256
257
# File 'lib/jets/remote/tailer.rb', line 253

def phase_duration(duration)
  return unless duration # can be nil

  ["Duration:", pretty_time(duration)]
end

#phase_status(status) ⇒ Object



246
247
248
249
250
251
# File 'lib/jets/remote/tailer.rb', line 246

def phase_status(status)
  return unless status # can be nil

  text = (status == "SUCCEEDED") ? status : status.color(:red)
  ["Status:", text]
end


176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/jets/remote/tailer.rb', line 176

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets/remote/tailer.rb', line 16

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

    # CodeBuild AWS Lambda Compute Type take slightly a few milliseconds to report phases.
    # But return the build already. So we have to wait until the phases are available.
    while build.phases.nil?
      build = find_build # refresh
      sleep 1
    end

    print_phases(build) unless @stop_printing_phases # once cloudwatch logs starts, stop printing phases
    set_log_group_name(build)

    complete = build.build_complete

    next if ENV["JETS_TEST"]
    if build_phase_started?(build)
      start_cloudwatch_tail
      @stop_printing_phases = true
      @@delay = 5 # increase @@delay also
    end

    sleep @@delay
  end

  stop_cloudwatch_tail(build)
  if Jets.bootstrap.config.codebuild.logging.final_phases
    print_phases(build) # print final phases
  end
  display_failed_phases(build)
end

#say(text) ⇒ Object



259
260
261
# File 'lib/jets/remote/tailer.rb', line 259

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

#set_log_group_name(build) ⇒ Object

Setting enables start_cloudwatch_tail



170
171
172
173
174
# File 'lib/jets/remote/tailer.rb', line 170

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

#show_ifObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jets/remote/tailer.rb', line 130

def show_if
  return true unless Jets.bootstrap.config.codebuild.logging.show == "filtered"

  start_marker = "./jets"
  end_marker = "Phase complete: BUILD"
  proc do |event|
    @display_showing ||= event.message.include?(start_marker)
    if @display_showing && event.message.include?(end_marker)
      @display_showing = false
    end
    @display_showing
  end
end

#start_cloudwatch_tailObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/jets/remote/tailer.rb', line 107

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

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

#stop_cloudwatch_tail(build) ⇒ Object



145
146
147
148
149
# File 'lib/jets/remote/tailer.rb', line 145

def stop_cloudwatch_tail(build)
  return if ENV["JETS_TEST"]
  @cw_tail&.stop_follow!
  @thread&.join
end