Sha256: 37c0a3d8618dec63ce759e37a347ece1b9bc048f4a2e470f1a4162f60e7f0ae9

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 KB

Contents

require 'httpclient'
require 'rexml/document'

require 'bosh-bootstrap/public_stemcell'

module Bosh::Bootstrap
  class PublicStemcells
    PUBLIC_STEMCELLS_BASE_URL = 'https://bosh-jenkins-artifacts.s3.amazonaws.com'

    def has_stemcell?(stemcell_filename)
      all.any? { |stemcell| stemcell.name == stemcell_filename }
    end

    def find(stemcell_filename)
      all.detect { |stemcell| stemcell.name == stemcell_filename }
    end

    def all
      response = self.class.http_client.get(PUBLIC_STEMCELLS_BASE_URL, {'prefix' => 'bosh-stemcell'})
      doc = REXML::Document.new(response.body)
      stemcells_tags = parse_document(doc)
      stemcells = parse_stemcells(stemcells_tags)

      while is_truncated(doc)
        response = self.class.http_client.get(PUBLIC_STEMCELLS_BASE_URL, {
          'prefix' => 'bosh-stemcell',
          'marker' => stemcells_tags.last.get_text('Key').value
        })

        doc = REXML::Document.new(response.body)
        stemcells_tags = parse_document(doc)
        stemcells += parse_stemcells(stemcells_tags)
      end

      stemcells
    end

    def recent
      stemcell_varietes = all.reject(&:legacy?).group_by(&:variety).values
      stemcell_varietes.map { |stemcells| stemcells.sort_by(&:version).last }
    end

    private

    def parse_document(doc)
      REXML::XPath.match(doc, "/ListBucketResult/Contents[Key[text()[not(contains(.,'latest'))]]]")
    end

    def parse_stemcells(stemcell_tags)
      stemcell_tags.map do |stemcell_tag|
        stemcell_key = stemcell_tag.get_text('Key').value
        stemcell_size = Integer(stemcell_tag.get_text('Size').value)
        PublicStemcell.new(stemcell_key, stemcell_size)
      end
    end

    def is_truncated(doc)
      REXML::XPath.match(doc, "/ListBucketResult/IsTruncated").first.get_text == 'true'
    end

    def self.http_client
      @http_client ||= HTTPClient.new
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bosh-bootstrap-0.18.1 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.18.0 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.17.1 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.17.0 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.16.2 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.16.1 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.16.0 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.15.0 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.14.5 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.14.4 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.14.3 lib/bosh-bootstrap/public_stemcells.rb
bosh-bootstrap-0.14.2 lib/bosh-bootstrap/public_stemcells.rb