From 7cb4b89883d75978fd55a51d1820906305af9d16 Mon Sep 17 00:00:00 2001 From: carlad Date: Mon, 1 Aug 2016 16:36:08 +0200 Subject: [PATCH] add s3 log search --- lib/travis/api/v3/queries/log.rb | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/travis/api/v3/queries/log.rb b/lib/travis/api/v3/queries/log.rb index 9c7035ce..e799c9ed 100644 --- a/lib/travis/api/v3/queries/log.rb +++ b/lib/travis/api/v3/queries/log.rb @@ -1,16 +1,36 @@ module Travis::API::V3 class Queries::Log < Query + require 'net/http' + require 'uri' + def find(job) - #TODO check for the log in the DB, if it's not there then fetch it from S3. + #check for the log in the DB log = Models::Log.find_by_job_id - #if the log exists and has not bee archived yet, then collect the log_parts and return the contents + #if the log exists and has not been archived yet, then collect the log_parts and return the contents unless log.nil? || !log.archived_at.nil? log_parts = Models::Log::Part.where(log_id: log.id) - log_parts.each { |log_part| contents << log_part.content } - contents - else - #go look in S3 + log_data = [] + log_parts.each { |log_part| log_data << log_part.content } + log_data + elsif log.archived_at? + # if it's not there then fetch it from S3. + archived_log_path = archive_url("/jobs/#{params[:job.id]}/log.txt") + + content = open(Net::HTTP.get(URI.parse(archived_log_path))) + archived_log_data = [] + content.each_line do |line| + archived_log_data << line.chop + end + archived_log_data end end + + def archive_url(path) + "https://s3.amazonaws.com/#{hostname('archive')}#{path}" + end + + def hostname(name) + "#{name}#{'-staging' if Travis.env == 'staging'}.#{Travis.config.host.split('.')[-2, 2].join('.')}" + end end end