Class: Travis::Artifacts::Uploader

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/travis/artifacts/uploader.rb

Instance Attribute Summary collapse

Attributes included from Logger

#logger

Instance Method Summary collapse

Constructor Details

#initialize(paths, options = {}) ⇒ Uploader

Returns a new instance of Uploader.


10
11
12
13
14
15
16
17
# File 'lib/travis/artifacts/uploader.rb', line 10

def initialize(paths, options = {})
  @paths  = paths
  @test   = Test.new
  @public = ! (options[:private]||false)
  @target_path = options[:target_path] || "artifacts/#{@test.build_number}/#{@test.job_number}"
  @clone_path = options[:clone_path]
  @cache_control = !@public ? 'private' : options[:cache_control] || 'public, max-age=315360000'
end

Instance Attribute Details

#clone_pathObject (readonly)

Returns the value of attribute clone_path


8
9
10
# File 'lib/travis/artifacts/uploader.rb', line 8

def clone_path
  @clone_path
end

#pathsObject (readonly)

Returns the value of attribute paths


8
9
10
# File 'lib/travis/artifacts/uploader.rb', line 8

def paths
  @paths
end

#target_pathObject (readonly)

Returns the value of attribute target_path


8
9
10
# File 'lib/travis/artifacts/uploader.rb', line 8

def target_path
  @target_path
end

Instance Method Details

#_clone(s3_source_path, filename) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/travis/artifacts/uploader.rb', line 80

def _clone(s3_source_path, filename)
  clone_destination = File.join(clone_path, filename).sub(/^\//, '')
  logger.info "Cloning to #{clone_destination}, public: #{@public}"

  bucket.files.create({
      :key => clone_destination,
      :public => @public,
      :body => '',
      :metadata => { 'etag'=>'', 'x-amz-copy-source' => "#{Travis::Artifacts.bucket_name}/#{s3_source_path}" }
    })
end

#_upload(file) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/travis/artifacts/uploader.rb', line 93

def _upload(file)
  destination = File.join(target_path, file.destination).sub(/^\//, '')
  logger.info "Uploading file #{file.source} to #{destination}, public: #{@public}"

  bucket.files.create({
    :key => destination,
    :public => @public,
    :body => file.read,
    :content_type => file.content_type,
    :metadata => { "Cache-Control" => @cache_control }
  })
  _clone(destination, file.destination) unless clone_path.nil?
end

#filesObject


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
53
54
# File 'lib/travis/artifacts/uploader.rb', line 25

def files
  files = []

  paths.each do |artifact_path|
    to   = artifact_path.to
    from = artifact_path.from
    root = artifact_path.root

    if artifact_path.directory?
      root = File.join(root, from)
      root << '/' unless root =~ /\/$/
    end

    Find.find(artifact_path.fullpath) do |path|
      next unless File.file? path

      relative = path.sub(/#{root}\/?/, '')

      destination = if to
        artifact_path.directory? ? File.join(to, relative) : to
      else
        relative
      end

      files << Artifact.new(path, destination)
    end
  end

  files
end

#uploadObject


19
20
21
22
23
# File 'lib/travis/artifacts/uploader.rb', line 19

def upload
  files.each do |file|
    upload_file(file)
  end
end

#upload_file(file) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/travis/artifacts/uploader.rb', line 57

def upload_file(file)
  retries = 0

  begin
    _upload(file)
  rescue StandardError => e
    if retries < 2
      logger.info "Attempt to upload failed, retrying"
      retries += 1
      retry
    else
      if e.respond_to?(:request)
        # we don't want to display sensitive data, make the error message simpler
        request  = e.request
        response = e.response
        raise e.class.new("Expected(#{request[:expects].inspect}) <=> Actual(#{response.status})")
      else
        raise
      end
    end
  end
end