Class: Dapp::Dimg::Image::Docker
- Inherits:
-
Object
- Object
- Dapp::Dimg::Image::Docker
- Defined in:
- lib/dapp/dimg/image/docker.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dapp ⇒ Object
readonly
Returns the value of attribute dapp.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .cache ⇒ Object
- .cache_reset(name = '') ⇒ Object
- .image_by_name(name:, **kwargs) ⇒ Object
- .image_config_option(image_id:, option:) ⇒ Object
- .image_name?(name) ⇒ Boolean
- .image_name_format ⇒ Object
- .load!(file_path) ⇒ Object
- .save!(image_or_images, file_path) ⇒ Object
- .tag!(id:, tag:) ⇒ Object
Instance Method Summary collapse
- #cache_reset ⇒ Object
- #created_at ⇒ Object
- #id ⇒ Object
-
#initialize(name:, dapp:, from: nil) ⇒ Docker
constructor
A new instance of Docker.
- #pull! ⇒ Object
- #push! ⇒ Object
- #size ⇒ Object
- #tagged? ⇒ Boolean
- #untag! ⇒ Object
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
#dapp ⇒ Object (readonly)
Returns the value of attribute dapp.
7 8 9 |
# File 'lib/dapp/dimg/image/docker.rb', line 7 def dapp @dapp end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
5 6 7 |
# File 'lib/dapp/dimg/image/docker.rb', line 5 def from @from end |
#name ⇒ Object (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
.cache ⇒ Object
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
80 81 82 |
# File 'lib/dapp/dimg/image/docker.rb', line 80 def image_name?(name) !(/^#{image_name_format}$/ =~ name).nil? end |
.image_name_format ⇒ Object
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 |
Instance Method Details
#cache_reset ⇒ Object
63 64 65 |
# File 'lib/dapp/dimg/image/docker.rb', line 63 def cache_reset self.class.cache_reset(name) end |
#created_at ⇒ Object
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 |
#id ⇒ Object
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
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 |
#size ⇒ Object
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
44 45 46 |
# File 'lib/dapp/dimg/image/docker.rb', line 44 def tagged? !!id end |