class OpsBuild::BoxIndexer

Public Class Methods

new(dir:, out:, name:, desc:, root_url:, checksum_type: :sha1) click to toggle source
# File lib/ops_build/box_indexer.rb, line 3
def initialize(dir:, out:, name:, desc:, root_url:, checksum_type: :sha1)
  @dir           = File.expand_path(dir)
  @out           = out
  @name          = name
  @desc          = desc
  @root_url      = root_url
  @checksum_type = checksum_type

  check_dir!
  check_checksum_type!
end

Public Instance Methods

index() click to toggle source
# File lib/ops_build/box_indexer.rb, line 15
def index
  OpsBuild.logger.debug("Indexing directory '#{@dir}'")
  out = {
      name:        @name,
      description: @desc,
      versions:    []
  }

  Dir.glob("#{@dir}/*.box").each do |path|
    filename = File.basename(path)
    m = /^#{@name}[\_\-](?<version>[0-9]+((\.[0-9A-Z]+){1,}([\-\.][0-9]+))?)\.box$/.match(filename)
    next if m.nil?

    OpsBuild.logger.debug("Found box '#{filename}'")

    out[:versions] << box_info(File.expand_path(path), m[:version])
  end

  out
end
index!() click to toggle source
# File lib/ops_build/box_indexer.rb, line 36
def index!
  write(index)
end

Private Instance Methods

box_info(path, version) click to toggle source
# File lib/ops_build/box_indexer.rb, line 53
def box_info(path, version)
  out = {
      version:   version,
      providers: [{
                      name:          'virtualbox',
                      url:           File.join(@root_url, File.basename(path)),
                      checksum_type: @checksum_type.to_s,
                      checksum:      checksum(path)
                  }]
  }

  metadata_path = "#{path}.metadata"
  out.merge!(JSON.parse(File.read(metadata_path))) if File.exists?(metadata_path)

  out
end
check_checksum_type!() click to toggle source
# File lib/ops_build/box_indexer.rb, line 45
def check_checksum_type!
  raise "Unknown checksum type '#{@checksum_type}'!" unless %w(sha1 sha2 md5 rmd160).include?(@checksum_type.to_s.downcase)
end
check_dir!() click to toggle source
# File lib/ops_build/box_indexer.rb, line 41
def check_dir!
  raise "Folder '#{@dir}' does not exist!" unless Dir.exists?(@dir)
end
checksum(path) click to toggle source
# File lib/ops_build/box_indexer.rb, line 70
def checksum(path)
  Digest.const_get(@checksum_type.upcase).file(path).hexdigest
end
write(hash) click to toggle source
# File lib/ops_build/box_indexer.rb, line 49
def write(hash)
  File.open(@out, 'w+') { |f| f.write(JSON.pretty_generate(hash)) }
end