Class: Dapp::Image::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/image/docker.rb

Overview

Docker

Direct Known Subclasses

Stage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, project:, from: nil) ⇒ Docker

Returns a new instance of Docker.



10
11
12
13
14
# File 'lib/dapp/image/docker.rb', line 10

def initialize(name:, project:, from: nil)
  @from = from
  @name = name
  @project = project
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/dapp/image/docker.rb', line 6

def from
  @from
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/dapp/image/docker.rb', line 7

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



8
9
10
# File 'lib/dapp/image/docker.rb', line 8

def project
  @project
end

Class Method Details

.cacheObject



86
87
88
# File 'lib/dapp/image/docker.rb', line 86

def cache
  @cache ||= (@cache = {}).tap { cache_reset }
end

.cache_reset(name = '') ⇒ Object



90
91
92
93
94
95
96
# File 'lib/dapp/image/docker.rb', line 90

def cache_reset(name = '')
  cache.delete(name)
  Project.shellout!("docker images --format='{{.Repository}}:{{.Tag}};{{.ID}};{{.CreatedAt}};{{.Size}}' --no-trunc #{name}").stdout.lines.each do |l|
    name, id, created_at, size = l.split(';')
    cache[name] = { id: id, created_at: created_at, size: size }
  end
end

.image_config_option(image_id:, option:) ⇒ Object



55
56
57
58
# File 'lib/dapp/image/docker.rb', line 55

def self.image_config_option(image_id:, option:)
  output = Project.shellout!("docker inspect --format='{{json .Config.#{option.to_s.capitalize}}}' #{image_id}").stdout.strip
  output == 'null' ? [] : JSON.parse(output)
end

.image_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/dapp/image/docker.rb', line 77

def image_name?(name)
  !(/^#{image_name_format}$/ =~ name).nil?
end

.image_name_formatObject



71
72
73
74
75
# File 'lib/dapp/image/docker.rb', line 71

def image_name_format
  separator = '[_.]|__|[-]*'
  tag = "[[:alnum:]][[[:alnum:]]#{separator}]{0,127}"
  "#{DockerRegistry.repo_name_format}(:(?<tag>#{tag}))?"
end

.tag!(id:, tag:) ⇒ Object



81
82
83
84
# File 'lib/dapp/image/docker.rb', line 81

def tag!(id:, tag:)
  Project.shellout!("docker tag #{id} #{tag}")
  cache_reset
end

Instance Method Details

#cache_resetObject



60
61
62
# File 'lib/dapp/image/docker.rb', line 60

def cache_reset
  self.class.cache_reset(name)
end

#created_atObject

Raises:



45
46
47
48
# File 'lib/dapp/image/docker.rb', line 45

def created_at
  raise Error::Build, code: :image_not_exist, data: { name: name } unless tagged?
  cache[:created_at]
end

#idObject



16
17
18
# File 'lib/dapp/image/docker.rb', line 16

def id
  cache[:id]
end

#pull!Object



33
34
35
36
37
38
39
# File 'lib/dapp/image/docker.rb', line 33

def pull!
  return if tagged?
  project.log_secondary_process(project.t(code: 'process.image_pull', data: { name: name })) do
    project.shellout!("docker pull #{name}", log_verbose: true)
  end
  cache_reset
end

#push!Object

Raises:



26
27
28
29
30
31
# File 'lib/dapp/image/docker.rb', line 26

def push!
  raise Error::Build, code: :image_not_exist, data: { name: name } unless tagged?
  project.log_secondary_process(project.t(code: 'process.image_push', data: { name: name })) do
    project.shellout!("docker push #{name}", log_verbose: true)
  end
end

#sizeObject

Raises:



50
51
52
53
# File 'lib/dapp/image/docker.rb', line 50

def size
  raise Error::Build, code: :image_not_exist, data: { name: name } unless tagged?
  cache[:size]
end

#tagged?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dapp/image/docker.rb', line 41

def tagged?
  !!id
end

#untag!Object

Raises:



20
21
22
23
24
# File 'lib/dapp/image/docker.rb', line 20

def untag!
  raise Error::Build, code: :image_already_untagged, data: { name: name } unless tagged?
  project.shellout!("docker rmi #{name}")
  cache_reset
end