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



109
110
111
# File 'lib/dapp/dimg/image/docker.rb', line 109

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

.cache_reset(name = '') ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dapp/dimg/image/docker.rb', line 113

def cache_reset(name = '')
  # FIXME: rework images cache, then delete time measure
  #t = Time.now
  cache.delete(name)
  ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker} images --format='{{.Repository}}:{{.Tag}};{{.ID}};{{.CreatedAt}};{{.Size}}' --no-trunc #{name}")
              .stdout
              .lines
              .each do |l|
    name, id, created_at, size_field = l.split(';').map(&:strip)
    name = name.reverse.chomp('docker.io/'.reverse).reverse
    size = begin
      match = size_field.match(/^(\S*\d)\ ?(b|kb|mb|gb|tb)$/i)
      raise Error::Build, code: :unsupported_docker_image_size_format, data: {value: size_field} unless match and match[1] and match[2]

      number = match[1].to_f
      unit = match[2].downcase

      coef = case unit
             when 'b'  then 0
             when 'kb' then 1
             when 'mb' then 2
             when 'gb' then 3
             when 'tb' then 4
             end

      number * (1000**coef)
    end
    cache[name] = { id: id, created_at: created_at, size: size }
  end
  #p [:cache_reset, name, :took, Time.now - t]
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!("#{::Dapp::Dapp.host_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)


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

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

.image_name_formatObject



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

def image_name_format
  "#{DockerRegistry.repo_name_format}(:(?<tag>#{tag_format}))?"
end

.load!(file_path, verbose: false, quiet: false) ⇒ Object



105
106
107
# File 'lib/dapp/dimg/image/docker.rb', line 105

def load!(file_path, verbose: false, quiet: false)
  ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker} load -i #{file_path}", verbose: verbose, quiet: quiet)
end

.save!(image_or_images, file_path, verbose: false, quiet: false) ⇒ Object



100
101
102
103
# File 'lib/dapp/dimg/image/docker.rb', line 100

def save!(image_or_images, file_path, verbose: false, quiet: false)
  images = Array(image_or_images).join(' ')
  ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker} save -o #{file_path} #{images}", verbose: verbose, quiet: quiet)
end

.tag!(id:, tag:, verbose: false, quiet: false) ⇒ Object



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

def tag!(id:, tag:, verbose: false, quiet: false)
  ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker} tag #{id} #{tag}", verbose: verbose, quiet: quiet)

  if cache_entry = cache.values.find {|v| v[:id] == id}
    cache[tag] = cache_entry
  else
    cache_reset
  end
end

.tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.tag_formatObject



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

def tag_format
  '(?![-.])[a-zA-Z0-9_.-]{1,127}'
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!("#{dapp.host_docker} pull #{name}", 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!("#{dapp.host_docker} push #{name}", 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!("#{dapp.host_docker} rmi #{name}")
  self.class.cache.delete(name)
end