File: redis_builder.rb

package info (click to toggle)
ruby-redis-client 0.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: ansic: 7,517; ruby: 5,801; makefile: 246; sh: 123
file content (90 lines) | stat: -rwxr-xr-x 1,986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
89
90
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'digest/sha1'
require 'English'
require 'fileutils'
require 'net/http'

class RedisBuilder
  TARBALL_CACHE_EXPIRATION = 60 * 60 * 30

  def initialize(redis_branch, tmp_dir)
    @redis_branch = redis_branch
    @tmp_dir = tmp_dir
    @build_dir = Servers::CACHE_DIR.join("redis-#{redis_branch}").to_s
  end

  def bin_path
    File.join(@build_dir, "src/redis-server")
  end

  def install
    download_tarball_if_needed
    if old_checkum != checksum
      build
      update_checksum
    end
  end

  private

  def download_tarball_if_needed
    return if File.exist?(tarball_path) && File.mtime(tarball_path) > Time.now - TARBALL_CACHE_EXPIRATION

    FileUtils.mkdir_p(@tmp_dir)
    download(tarball_url, tarball_path)
  end

  def download(url, path)
    response = Net::HTTP.get_response(URI(url))
    case Integer(response.code)
    when 300..399
      download(response['Location'], path)
    when 200
      File.binwrite(tarball_path, response.body)
    else
      raise "Unexpected HTTP response #{response.code} #{url}"
    end
  end

  def tarball_path
    File.join(@tmp_dir, "redis-#{@redis_branch}.tar.gz")
  end

  def tarball_url
    "https://github.com/redis/redis/archive/#{@redis_branch}.tar.gz"
  end

  def build
    FileUtils.rm_rf(@build_dir)
    FileUtils.mkdir_p(@build_dir)
    command!('tar', 'xf', tarball_path, '-C', File.expand_path('../', @build_dir))
    Dir.chdir(@build_dir) do
      command!('make', 'BUILD_TLS=yes')
    end
  end

  def update_checksum
    File.write(checksum_path, checksum)
  end

  def old_checkum
    File.read(checksum_path)
  rescue Errno::ENOENT
    nil
  end

  def checksum_path
    File.join(@build_dir, 'build.checksum')
  end

  def checksum
    @checksum ||= Digest::SHA1.file(tarball_path).hexdigest
  end

  def command!(*args)
    puts "$ #{args.join(' ')}"
    raise "Command failed with status #{$CHILD_STATUS.exitstatus}" unless system(*args)
  end
end