Class: TravisBundleCache::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/travis_bundle_cache/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



6
7
8
9
10
11
12
13
14
# File 'lib/travis_bundle_cache/cache.rb', line 6

def initialize
  @bucket_name         = ENV["AWS_S3_BUCKET"]
  @architecture        = `uname -m`.strip
  @file_name           = "#{ENV['BUNDLE_ARCHIVE']}-#{@architecture}.tgz"
  @file_path           = File.expand_path("~/#{@file_name}")
  @lock_file           = File.join(File.expand_path(ENV["TRAVIS_BUILD_DIR"]), "Gemfile.lock")
  @digest_filename     = "#{@file_name}.sha2"
  @old_digest_filename = File.expand_path("~/remote_#{@digest_filename}")
end

Instance Method Details

#archive_and_upload_bundleObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/travis_bundle_cache/cache.rb', line 38

def archive_and_upload_bundle
  if @old_digest == ""
    puts "=> There was no existing digest, uploading a new version of the archive"
  else
    puts "=> There were changes, uploading a new version of the archive"
    puts "  => Old checksum: #{@old_digest}"
    puts "  => New checksum: #{@bundle_digest}"

    puts "=> Cleaning old gem versions from the bundle"
    run_command "bundle clean"
  end

  puts "=> Preparing bundle archive"
  `cd ~ && tar -cjf "#{@file_name}" .bundle && split -b 5m -a 3 "#{@file_name}" "#{@file_name}."`

  parts_pattern = File.expand_path(File.join("~", "#{@file_name}.*"))
  parts         = Dir.glob(parts_pattern).sort

  puts "=> Uploading the bundle"
  puts "  => Beginning multipart upload"
  response  = storage.initiate_multipart_upload(@bucket_name, @file_name, { "x-amz-acl" => "public-read" })
  upload_id = response.body['UploadId']
  puts "    => Upload ID: #{upload_id}"

  part_ids = []

  puts "  => Uploading #{parts.length} parts"
  parts.each_with_index do |part, index|
    part_number = (index + 1).to_s
    puts "    => Uploading #{part}"
    File.open(part) do |part_file|
      response = storage.upload_part(@bucket_name, @file_name, upload_id, part_number, part_file)
      part_ids << response.headers['ETag']
      puts "      => Uploaded"
    end
  end

  puts "  => Completing multipart upload"
  storage.complete_multipart_upload(@bucket_name, @file_name, upload_id, part_ids)

  puts "=> Uploading the digest file"
  bucket = storage.directories.new(key: @bucket_name)
  bucket.files.create({
    :body         => @bundle_digest,
    :key          => @digest_filename,
    :public       => true,
    :content_type => "text/plain"
  })

  puts "All done now."
end

#cache_bundleObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/travis_bundle_cache/cache.rb', line 22

def cache_bundle
  puts "Checking for changes"
  @bundle_digest = Digest::SHA2.file(@lock_file).hexdigest
  @old_digest    = File.exists?(@old_digest_filename) ? File.read(@old_digest_filename) : ""

  if ENV['TRAVIS_PULL_REQUEST'].to_i > 0
    puts "=> This is a pull request, doing nothing"
  elsif ENV['TRAVIS_BRANCH'] != "master"
    puts "=> This is not the master branch, doing nothing"
  elsif @bundle_digest == @old_digest
    puts "=> There were no changes, doing nothing"
  else
    archive_and_upload_bundle
  end
end

#installObject



16
17
18
19
20
# File 'lib/travis_bundle_cache/cache.rb', line 16

def install
  run_command %(cd ~ && wget -O "remote_#{@file_name}" "https://#{@bucket_name}.s3.amazonaws.com/#{@file_name}" && tar -xf "remote_#{@file_name}")
  run_command %(cd ~ && wget -O "remote_#{@file_name}.sha2" "https://#{@bucket_name}.s3.amazonaws.com/#{@file_name}.sha2")
  run_command %(bundle install --without #{ENV['BUNDLE_WITHOUT'] || "development production"} --path=~/.bundle)
end