Class: Dapp::Dimg::Image::Docker

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

Direct Known Subclasses

Stage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Docker.



13
14
15
16
17
# File 'lib/dapp/dimg/image/docker.rb', line 13

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

Instance Attribute Details

#dappObject (readonly)

Returns the value of attribute dapp.



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

def dapp
  @dapp
end

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.cacheObject



98
99
100
# File 'lib/dapp/dimg/image/docker.rb', line 98

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

.cache_reset(name = '') ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dapp/dimg/image/docker.rb', line 102

def cache_reset(name = '')
  cache.delete(name)
  ::Dapp::Dapp.shellout!("docker images --format='{{.Repository}}:{{.Tag}};{{.ID}};{{.CreatedAt}};{{.Size}}' --no-trunc #{name}")
              .stdout
              .lines
              .each do |l|
    name, id, created_at, size_field = l.split(';')
    size = begin
      number, unit = size_field.split
      coef = case unit.to_s.downcase
             when 'b'  then return number.to_f
             when 'kb' then 1
             when 'mb' then 2
             when 'gb' then 3
             when 'tb' then 4
             end
      number.to_f * (1000**coef)
    end
    cache[name] = { id: id, created_at: created_at, size: size }
  end
end

.image_by_name(name:, **kwargs) ⇒ Object



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

def self.image_by_name(name:, **kwargs)
  (@images ||= {})[name] ||= new(name: name, **kwargs)
end

.image_config_option(image_id:, option:) ⇒ Object



58
59
60
61
# File 'lib/dapp/dimg/image/docker.rb', line 58

def self.image_config_option(image_id:, option:)
  output = ::Dapp::Dapp.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)


80
81
82
# File 'lib/dapp/dimg/image/docker.rb', line 80

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

.image_name_formatObject



74
75
76
77
78
# File 'lib/dapp/dimg/image/docker.rb', line 74

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

.load!(file_path) ⇒ Object



94
95
96
# File 'lib/dapp/dimg/image/docker.rb', line 94

def load!(file_path)
  ::Dapp::Dapp.shellout!("docker load -i #{file_path}", log_verbose: true)
end

.save!(image_or_images, file_path) ⇒ Object



89
90
91
92
# File 'lib/dapp/dimg/image/docker.rb', line 89

def save!(image_or_images, file_path)
  images = Array(image_or_images).join(' ')
  ::Dapp::Dapp.shellout!("docker save -o #{file_path} #{images}", log_verbose: true)
end

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



84
85
86
87
# File 'lib/dapp/dimg/image/docker.rb', line 84

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

Instance Method Details

#cache_resetObject



63
64
65
# File 'lib/dapp/dimg/image/docker.rb', line 63

def cache_reset
  self.class.cache_reset(name)
end

#created_atObject

Raises:



48
49
50
51
# File 'lib/dapp/dimg/image/docker.rb', line 48

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

#idObject



19
20
21
# File 'lib/dapp/dimg/image/docker.rb', line 19

def id
  cache[:id]
end

#pull!Object



36
37
38
39
40
41
42
# File 'lib/dapp/dimg/image/docker.rb', line 36

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

#push!Object

Raises:



29
30
31
32
33
34
# File 'lib/dapp/dimg/image/docker.rb', line 29

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

#sizeObject

Raises:



53
54
55
56
# File 'lib/dapp/dimg/image/docker.rb', line 53

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

#tagged?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dapp/dimg/image/docker.rb', line 44

def tagged?
  !!id
end

#untag!Object

Raises:



23
24
25
26
27
# File 'lib/dapp/dimg/image/docker.rb', line 23

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