BigW Consortium Gitlab

tag.rb 1.74 KB
Newer Older
1
module ContainerRegistry
2 3 4
  class Tag
    attr_reader :repository, :name

5
    delegate :registry, :client, to: :repository
6
    delegate :revision, :short_revision, to: :config_blob, allow_nil: true
7

8 9 10 11 12 13 14 15
    def initialize(repository, name)
      @repository, @name = repository, name
    end

    def valid?
      manifest.present?
    end

16 17 18 19 20 21 22 23
    def v1?
      manifest && manifest['schemaVersion'] == 1
    end

    def v2?
      manifest && manifest['schemaVersion'] == 2
    end

24 25
    def manifest
      return @manifest if defined?(@manifest)
Kamil Trzcinski committed
26

27 28 29
      @manifest = client.repository_manifest(repository.name, name)
    end

30 31 32 33
    def path
      "#{repository.path}:#{name}"
    end

34 35
    def [](key)
      return unless manifest
Kamil Trzcinski committed
36

37 38 39 40 41
      manifest[key]
    end

    def digest
      return @digest if defined?(@digest)
Kamil Trzcinski committed
42

43 44 45
      @digest = client.repository_tag_digest(repository.name, name)
    end

46 47
    def config_blob
      return @config_blob if defined?(@config_blob)
48
      return unless manifest && manifest['config']
Kamil Trzcinski committed
49

50
      @config_blob = repository.blob(manifest['config'])
51 52 53 54
    end

    def config
      return unless config_blob
Kamil Trzcinski committed
55

56
      @config ||= ContainerRegistry::Config.new(self, config_blob) if config_blob.data
57 58 59 60
    end

    def created_at
      return unless config
Kamil Trzcinski committed
61

62 63 64 65 66 67
      @created_at ||= DateTime.rfc3339(config['created'])
    end

    def layers
      return @layers if defined?(@layers)
      return unless manifest
Kamil Trzcinski committed
68

69 70 71
      layers = manifest['layers'] || manifest['fsLayers']

      @layers = layers.map do |layer|
72
        repository.blob(layer)
73 74 75 76 77
      end
    end

    def total_size
      return unless layers
Kamil Trzcinski committed
78

79
      layers.map(&:size).sum if v2?
80 81 82 83
    end

    def delete
      return unless digest
Kamil Trzcinski committed
84

85 86 87 88
      client.delete_repository_tag(repository.name, digest)
    end
  end
end