Class: Jets::Remote::Tailer
Constant Summary
collapse
- @@delay =
2
Instance Method Summary
collapse
#pretty_home, #pretty_path, #pretty_time
#log
#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2
#output_value, #stack_exists?
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 = [] @shown_phases = []
@thread = nil
end
|
Instance Method Details
#build_phase_started?(build) ⇒ 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_tail ⇒ Object
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" 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.
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 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 context = phase.contexts.last
if context 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"
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
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"
filtered_phase_types = %w[DOWNLOAD_SOURCE INSTALL PRE_BUILD BUILD]
filtered_phase_types.include?(details[:phase_type])
end
|
#find_build ⇒ Object
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
@retries ||= 1
raise if @retries > 5 delay = 2**@retries
log_arch "Retrying in #{delay}s..."
sleep delay
@retries += 1
retry
end
|
#logs_command? ⇒ Boolean
151
152
153
|
# File 'lib/jets/remote/tailer.rb', line 151
def logs_command?
ARGV.join(" ").include?("logs")
end
|
#output ⇒ Object
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
["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
text = (status == "SUCCEEDED") ? status : status.color(:red)
["Status:", text]
end
|
#print_phases(build) ⇒ Object
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
|
#run ⇒ Object
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
while build.phases.nil?
build = find_build sleep 1
end
print_phases(build) unless @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 end
sleep @@delay
end
stop_cloudwatch_tail(build)
if Jets.bootstrap.config.codebuild.logging.final_phases
print_phases(build) 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_if ⇒ Object
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_tail ⇒ Object
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
|