github.com/secure-build/gitlab-runner@v12.5.0+incompatible/ci/prepare_index (about)

     1  #!/usr/bin/env ruby
     2  
     3  require 'digest'
     4  
     5  class ReleaseIndexer
     6    attr_reader :name, :sources_url, :version, :revision, :workdir
     7    attr_reader :index_file, :checksum_file
     8    attr_reader :files, :created_at
     9  
    10    def initialize(name, sources_url, version, revision, workdir)
    11      @name = name
    12      @sources_url = sources_url
    13      @version = version
    14      @revision = revision
    15      @workdir = workdir
    16  
    17      @index_file = "#{workdir}/index.html"
    18      @checksum_file = "#{workdir}/release.sha256"
    19  
    20      @created_at = Time.now.strftime("%Y-%m-%dT%H:%M:%S%:z")
    21    end
    22  
    23    def generate!
    24      prepare_files
    25      generate_checksums
    26      generate_index
    27    end
    28  
    29    private
    30  
    31    def prepare_files
    32      @files = []
    33      search_files.each do |file|
    34        add_file(file)
    35      end
    36    end
    37  
    38    def search_files
    39      files_cmd = `find #{workdir}/ -type f | sort`
    40      files_cmd.split("\n").select{ |f| f != index_file && f != checksum_file }
    41    end
    42  
    43    def add_file(file)
    44      @files << {
    45          name: file.gsub("#{workdir}/", ''),
    46          checksum: Digest::SHA256.file(file).hexdigest,
    47          size: File.stat(file).size.to_f
    48      }
    49    end
    50  
    51    def generate_checksums
    52      File.open(checksum_file, 'w') do |f|
    53        f.puts files.map{ |file| "#{file[:checksum]}\t#{file[:name]}" }.join("\n")
    54      end
    55      add_file(checksum_file)
    56    end
    57  
    58    def generate_index
    59      title = "#{name} :: Release for #{version}"
    60  
    61      File.open(index_file, 'w') do |f|
    62        f.puts <<EOS
    63  <html>
    64      <head>
    65          <meta charset="utf-8/">
    66          <title>#{title}</title>
    67          <style type="text/css">
    68              body {font-family: monospace; font-size: 14px; margin: 40px; padding: 0;}
    69              h1 {border-bottom: 1px solid #aaa; padding: 10px;}
    70              p {font-size: 0.85em; margin: 5px 10px;}
    71              p span {display: inline-block; font-weight: bold; width: 100px;}
    72              p a {color: #000; font-weight: bold; text-decoration: none;}
    73              p a:hover {text-decoration: underline;}
    74              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;}
    75              li {margin: 5px 0; padding: 5px; font-size: 12px;}
    76              li:hover {background: #dedede;}
    77              .file_name {display: inline-block; font-weight: bold; width: calc(100% - 600px);}
    78              .file_name a {color: #000; display: inline-block; text-decoration: none; width: calc(100% - 10px);}
    79              .file_checksum {display: inline-block; width: 500px;}
    80              .file_size {display: inline-block; width: 90px;}
    81          </style>
    82      </head>
    83      <body>
    84          <h1>#{title}</h1>
    85          <p><span>Sources:</span> <a href="#{sources_url}" target="_blank">#{sources_url}</a></p>
    86          <p><span>Revision:</span> #{revision}</p>
    87          <p><span>Created at:</span> #{created_at}</p>
    88          <ul>
    89  EOS
    90  
    91        files.each do |file|
    92          line = "<li>"
    93          line += "<span class=\"file_name\"><a href=\"./#{file[:name]}\">#{file[:name]}</a></span>"
    94          line += "<span class=\"file_checksum\">#{file[:checksum]}</span>"
    95          line += "<span class=\"file_size\">%.2f MiB</span>" % (file[:size] / 1048576)
    96          line += '</li>'
    97          f.puts line
    98        end
    99  
   100        f.puts <<EOS
   101          </ul>
   102      </body>
   103  </html>
   104  EOS
   105      end
   106    end
   107  end
   108  
   109  ReleaseIndexer.new('GitLab Runner',
   110                     "#{ENV['CI_PROJECT_URL']}/tree/#{ENV['CI_COMMIT_REF_NAME']}",
   111                     ENV['VERSION'],
   112                     ENV['CI_COMMIT_SHA'],
   113                     'out').generate!