prepare_index 3.4 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env ruby

require 'digest'

class ReleaseIndexer
6
  attr_reader :name, :sources_url, :version, :revision, :workdir
7 8 9
  attr_reader :index_file, :checksum_file
  attr_reader :files, :created_at

10
  def initialize(name, sources_url, version, revision, workdir)
11 12
    @name = name
    @sources_url = sources_url
13 14
    @version = version
    @revision = revision
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
    @workdir = workdir

    @index_file = "#{workdir}/index.html"
    @checksum_file = "#{workdir}/release.sha256"

    @created_at = Time.now.strftime("%Y-%m-%dT%H:%M:%S%:z")
  end

  def generate!
    prepare_files
    generate_checksums
    generate_index
  end

  private

  def prepare_files
    @files = []
    search_files.each do |file|
      add_file(file)
    end
  end

  def search_files
    files_cmd = `find #{workdir}/ -type f | sort`
    files_cmd.split("\n").select{ |f| f != index_file && f != checksum_file }
  end

  def add_file(file)
    @files << {
        name: file.gsub("#{workdir}/", ''),
        checksum: Digest::SHA256.file(file).hexdigest,
        size: File.stat(file).size.to_f
    }
  end

  def generate_checksums
    File.open(checksum_file, 'w') do |f|
      f.puts files.map{ |file| "#{file[:checksum]}\t#{file[:name]}" }.join("\n")
    end
    add_file(checksum_file)
  end

  def generate_index
    title = "#{name} :: Release for #{version}"

    File.open(index_file, 'w') do |f|
      f.puts <<EOS
<html>
    <head>
        <meta charset="utf-8/">
        <title>#{title}</title>
        <style type="text/css">
            body {font-family: monospace; font-size: 14px; margin: 40px; padding: 0;}
            h1 {border-bottom: 1px solid #aaa; padding: 10px;}
            p {font-size: 0.85em; margin: 5px 10px;}
            p span {display: inline-block; font-weight: bold; width: 100px;}
            p a {color: #000; font-weight: bold; text-decoration: none;}
            p a:hover {text-decoration: underline;}
            ul {background: #eee; border: 1px solid #aaa; border-radius: 3px; box-shadow: 0 0 5px #aaa inset; list-style-type: none; margin: 10px 0; padding: 10px;}
            li {margin: 5px 0; padding: 5px; font-size: 12px;}
            li:hover {background: #dedede;}
            .file_name {display: inline-block; font-weight: bold; width: calc(100% - 600px);}
            .file_name a {color: #000; display: inline-block; text-decoration: none; width: calc(100% - 10px);}
            .file_checksum {display: inline-block; width: 500px;}
            .file_size {display: inline-block; width: 90px;}
        </style>
    </head>
    <body>
        <h1>#{title}</h1>
        <p><span>Sources:</span> <a href="#{sources_url}" target="_blank">#{sources_url}</a></p>
86
        <p><span>Revision:</span> #{revision}</p>
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        <p><span>Created at:</span> #{created_at}</p>
        <ul>
EOS

      files.each do |file|
        line = "<li>"
        line += "<span class=\"file_name\"><a href=\"./#{file[:name]}\">#{file[:name]}</a></span>"
        line += "<span class=\"file_checksum\">#{file[:checksum]}</span>"
        line += "<span class=\"file_size\">%.2f MiB</span>" % (file[:size] / 1048576)
        line += '</li>'
        f.puts line
      end

      f.puts <<EOS
        </ul>
    </body>
</html>
EOS
    end
  end
end

109 110 111 112 113
ReleaseIndexer.new('GitLab Runner',
                   "#{ENV['CI_PROJECT_URL']}/tree/#{ENV['CI_COMMIT_REF_NAME']}",
                   ENV['VERSION'],
                   ENV['CI_COMMIT_SHA'],
                   'out').generate!