Class: Bricolage::LogLocator
- Inherits:
-
Object
- Object
- Bricolage::LogLocator
- Defined in:
- lib/bricolage/loglocator.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .empty ⇒ Object
-
.slice_last_stderr(re, nth = 0) ⇒ Object
CLUDGE: FIXME: We redirect stderr to the file, we can find error messages from there.
Instance Method Summary collapse
-
#cleanup_local_dirs(path) ⇒ Object
Removes empty directories recursively.
-
#initialize(path, s3_writer) ⇒ LogLocator
constructor
A new instance of LogLocator.
- #redirect_stdouts ⇒ Object
- #s3_url ⇒ Object
- #upload ⇒ Object
Constructor Details
#initialize(path, s3_writer) ⇒ LogLocator
Returns a new instance of LogLocator.
9 10 11 12 |
# File 'lib/bricolage/loglocator.rb', line 9 def initialize(path, s3_writer) @path = path @s3_writer = s3_writer end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/bricolage/loglocator.rb', line 14 def path @path end |
Class Method Details
.empty ⇒ Object
5 6 7 |
# File 'lib/bricolage/loglocator.rb', line 5 def LogLocator.empty new(nil, nil) end |
.slice_last_stderr(re, nth = 0) ⇒ Object
CLUDGE: FIXME: We redirect stderr to the file, we can find error messages from there. Using a temporary file or Ruby SQL driver is MUCH better.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bricolage/loglocator.rb', line 43 def self.slice_last_stderr(re, nth = 0) return unless $stderr.stat.file? $stderr.flush f = $stderr.dup matched = nil begin f.seek(0) f.each do |line| m = line.slice(re, nth) matched = m if m end ensure f.close end matched = matched.to_s.strip matched.empty? ? nil : matched end |
Instance Method Details
#cleanup_local_dirs(path) ⇒ Object
Removes empty directories recursively
79 80 81 82 83 84 85 86 87 |
# File 'lib/bricolage/loglocator.rb', line 79 def cleanup_local_dirs(path) dir_path = path until dir_path == '/' or dir_path == '.' Dir.rmdir(dir_path) dir_path = File.dirname(dir_path) end rescue SystemCallError return # ignore end |
#redirect_stdouts ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bricolage/loglocator.rb', line 21 def redirect_stdouts return yield unless @path FileUtils.mkdir_p File.dirname(@path) @original_stdout = $stdout.dup @original_stderr = $stderr.dup begin # Use 'w+' to make readable for retrieve_last_match_from_stderr File.open(@path, 'w+') {|f| f.sync = true $stdout.reopen f $stderr.reopen f } return yield ensure $stdout.reopen @original_stdout; @original_stdout.close $stderr.reopen @original_stderr; @original_stderr.close upload end end |
#s3_url ⇒ Object
16 17 18 19 |
# File 'lib/bricolage/loglocator.rb', line 16 def s3_url return nil unless @s3_writer @s3_writer.url end |
#upload ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bricolage/loglocator.rb', line 61 def upload return unless @path return unless @s3_writer # FIXME: Shows HTTP URL? puts "bricolage: S3 log: #{s3_url}" begin @s3_writer.upload(path) # tmp: Prints & removes local file if S3 upload is succeeded. # It seems leaving local files causes unexpected Docker failure, I try to remove this. puts File.read(path) FileUtils.rm_f(path) cleanup_local_dirs(File.dirname(path)) rescue => ex $stderr.puts "warning: S3 upload failed: #{ex.class} #{ex.}: #{s3_url}" end end |