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, verbose: false, quiet: false) ⇒ Object
- .save!(image_or_images, file_path, verbose: false, quiet: false) ⇒ Object
- .tag!(id:, tag:, verbose: false, quiet: false) ⇒ Object
- .tag?(name) ⇒ Boolean
- .tag_format ⇒ 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
104 105 106 |
# File 'lib/dapp/dimg/image/docker.rb', line 104 def cache @cache ||= (@cache = {}).tap { cache_reset } end |
.cache_reset(name = '') ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/dapp/dimg/image/docker.rb', line 108 def cache_reset(name = '') cache.delete(name) ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker_bin} 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(/^(\d+(\.\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[3] number = match[1].to_f unit = match[3].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 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_bin} inspect --format='{{json .Config.#{option.to_s.capitalize}}}' #{image_id}").stdout.strip output == 'null' ? [] : JSON.parse(output) end |
.image_name?(name) ⇒ 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_format ⇒ Object
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
100 101 102 |
# File 'lib/dapp/dimg/image/docker.rb', line 100 def load!(file_path, verbose: false, quiet: false) ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker_bin} load -i #{file_path}", verbose: verbose, quiet: quiet) end |
.save!(image_or_images, file_path, verbose: false, quiet: false) ⇒ Object
95 96 97 98 |
# File 'lib/dapp/dimg/image/docker.rb', line 95 def save!(image_or_images, file_path, verbose: false, quiet: false) images = Array(image_or_images).join(' ') ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker_bin} save -o #{file_path} #{images}", verbose: verbose, quiet: quiet) end |
.tag!(id:, tag:, verbose: false, quiet: false) ⇒ Object
90 91 92 93 |
# File 'lib/dapp/dimg/image/docker.rb', line 90 def tag!(id:, tag:, verbose: false, quiet: false) ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker_bin} tag #{id} #{tag}", verbose: verbose, quiet: quiet) cache_reset end |
.tag?(name) ⇒ Boolean
86 87 88 |
# File 'lib/dapp/dimg/image/docker.rb', line 86 def tag?(name) !(/^#{tag_format}$/ =~ name).nil? end |
.tag_format ⇒ Object
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_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!("#{dapp.host_docker_bin} pull #{name}", 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!("#{dapp.host_docker_bin} push #{name}", 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 |