Class: DockerDistribution::Normalize

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_distribution/normalize.rb

Constant Summary collapse

LEGACY_DEFAULT_DOMAIN =
"index.docker.io"
DEFAULT_DOMAIN =
"docker.io"
OFFICIAL_REPO_NAME =
"library"
DEFAULT_TAG =
"latest"

Class Method Summary collapse

Class Method Details

.familiarize_name(repo) ⇒ Object

familiarizeName returns a shortened version of the name familiar to to the Docker UI. Familiar names have the default domain “docker.io” and “library/” repository prefix removed. For example, “docker.io/library/redis” will have the familiar name “redis” and “docker.io/dmcgowan/myapp” will be “dmcgowan/myapp”. Returns a familiarized named only reference.



80
81
82
83
84
85
86
87
88
89
# File 'lib/docker_distribution/normalize.rb', line 80

def familiarize_name(repo)
  familiar_repo = Repository.new(repo.domain, repo.path)

  if familiar_repo.domain == DEFAULT_DOMAIN
    familiar_repo.domain = nil
    parts = familiar_repo.path.split("/")
    familiar_repo.path = parts[1] if parts.length == 2 && parts[0] == OFFICIAL_REPO_NAME
  end
  familiar_repo
end

.parse_any_reference(ref) ⇒ Object

ParseAnyReference parses a reference string as a possible identifier, full digest, or familiar name.



101
102
103
104
105
106
107
108
# File 'lib/docker_distribution/normalize.rb', line 101

def parse_any_reference(ref)
  return DigestReference.new("sha256:#{ref}") if Regexp.anchored_identifier_regexp.match?(ref)

  digest = Digest.parse!(ref)
  DigestReference.new(digest.digest)
rescue DigestError
  parse_normalized_named(ref)
end

.parse_any_reference_with_set(ref, digest_set) ⇒ Object

ParseAnyReferenceWithSet parses a reference string as a possible short identifier to be matched in a digest set, a full digest, or familiar name.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/docker_distribution/normalize.rb', line 112

def parse_any_reference_with_set(ref, digest_set)
  if Regexp.anchored_short_identifier_regexp.match?(ref)
    dgst = digest_set.lookup!(ref)
    return DigestReference.new(dgst) if dgst
  else
    dgst = Digest.parse!(ref)
    DigestReference.new(dgst.digest)
  end
rescue DigestError
  parse_normalized_named(ref)
end

.parse_docker_ref(ref) ⇒ Object

ParseDockerRef normalizes the image reference following the docker convention. This is added mainly for backward compatibility. The reference returned can only be either tagged or digested. For reference contains both tag and digest, the function returns digested reference, e.g. docker.io/library/busybox:latest@ sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa will be returned as docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa.



36
37
38
39
40
41
42
43
44
45
# File 'lib/docker_distribution/normalize.rb', line 36

def parse_docker_ref(ref)
  named = parse_normalized_named(ref)
  if Helpers.tagged?(named) && Helpers.canonical?(named)
    new_named = Reference.with_name(named.name)
    new_canonical = Reference.with_digest(new_named, named.digest)
    return new_canonical
  end

  tag_name_only(named)
end

.parse_normalized_named(str) ⇒ Object

ParseNormalizedNamed parses a string into a named reference transforming a familiar name from Docker UI to a fully qualified reference. If the value may be an identifier use ParseAnyReference.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/docker_distribution/normalize.rb', line 15

def parse_normalized_named(str)
  raise ParseNormalizedNamedError if Regexp.anchored_identifier_regexp.match?(str)

  domain, remainder = split_docker_domain(str)

  tag_sep = remainder.index(":") || -1
  remote_name = tag_sep > -1 ? Helpers.to(remainder, tag_sep) : remainder
  raise ParseNormalizedNamedError if remote_name != remote_name.downcase

  ref = DockerDistribution::Reference.parse("#{domain}/#{remainder}")
  raise ParseNormalizedNamedError if Helpers.empty?(ref)

  ref
end

.split_docker_domain(name) ⇒ Object

splitDockerDomain splits a repository name to domain and remote name string. If no valid domain is found, the default domain is used. Repository name needs to be already validated before. rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/docker_distribution/normalize.rb', line 51

def split_docker_domain(name)
  i = name.index("/") || -1

  start_part = Helpers.to(name, i)
  end_part = Helpers.from(name, i + 1)

  if (i == -1 || [".", ":"].none? do |sep|
        start_part.include?(sep)
      end) && start_part != "localhost" && start_part.downcase == start_part
    domain = DEFAULT_DOMAIN
    remainder = name
  else
    domain = start_part
    remainder = end_part
  end

  domain = DEFAULT_DOMAIN if domain == LEGACY_DEFAULT_DOMAIN
  remainder = [OFFICIAL_REPO_NAME, remainder].join("/") if domain == DEFAULT_DOMAIN && !remainder.include?("/")

  [domain, remainder]
end

.tag_name_only(ref) ⇒ Object

TagNameOnly adds the default tag “latest” to a reference if it only has a repo name.



93
94
95
96
97
# File 'lib/docker_distribution/normalize.rb', line 93

def tag_name_only(ref)
  return Reference.with_tag(ref, DEFAULT_TAG) if Helpers.name_only?(ref)

  ref
end