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
- .image_by_name(name:, **kwargs) ⇒ Object
- .image_config(image_id) ⇒ Object
- .image_config_option(image_id:, option:) ⇒ Object
- .image_inspect(image_id) ⇒ Object
- .image_inspects ⇒ Object
- .image_name?(name) ⇒ Boolean
- .image_name_format ⇒ Object
- .image_reset(name) ⇒ Object
- .images ⇒ Object
- .load!(file_path, verbose: false, quiet: false) ⇒ Object
- .reset_image_inspect(image_id) ⇒ 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
- #config_option(option) ⇒ 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.
23 24 25 26 27 |
# File 'lib/dapp/dimg/image/docker.rb', line 23 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
.image_by_name(name:, **kwargs) ⇒ Object
10 11 12 |
# File 'lib/dapp/dimg/image/docker.rb', line 10 def image_by_name(name:, **kwargs) images[name] ||= new(name: name, **kwargs) end |
.image_config(image_id) ⇒ Object
91 92 93 |
# File 'lib/dapp/dimg/image/docker.rb', line 91 def image_config(image_id) image_inspect(image_id)["Config"] || {} end |
.image_config_option(image_id:, option:) ⇒ Object
95 96 97 |
# File 'lib/dapp/dimg/image/docker.rb', line 95 def image_config_option(image_id:, option:) image_config(image_id)[option] end |
.image_inspect(image_id) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dapp/dimg/image/docker.rb', line 75 def image_inspect(image_id) image_inspects[image_id] ||= begin cmd = ::Dapp::Dapp.shellout("#{::Dapp::Dapp.host_docker} inspect --type=image #{image_id}") if cmd.exitstatus != 0 if cmd.stderr.start_with? "Error: No such image:" {} else ::Dapp::Dapp.shellout_cmd_should_succeed! cmd end else Array(JSON.parse(cmd.stdout.strip)).first || {} end end end |
.image_inspects ⇒ Object
103 104 105 |
# File 'lib/dapp/dimg/image/docker.rb', line 103 def image_inspects @image_inspects ||= {} end |
.image_name?(name) ⇒ Boolean
119 120 121 |
# File 'lib/dapp/dimg/image/docker.rb', line 119 def image_name?(name) !(/^#{image_name_format}$/ =~ name).nil? end |
.image_name_format ⇒ Object
111 112 113 |
# File 'lib/dapp/dimg/image/docker.rb', line 111 def image_name_format "#{DockerRegistry.repo_name_format}(:(?<tag>#{tag_format}))?" end |
.image_reset(name) ⇒ Object
14 15 16 |
# File 'lib/dapp/dimg/image/docker.rb', line 14 def image_reset(name) images.delete(name) end |
.images ⇒ Object
18 19 20 |
# File 'lib/dapp/dimg/image/docker.rb', line 18 def images @images ||= {} end |
.load!(file_path, verbose: false, quiet: false) ⇒ Object
137 138 139 |
# File 'lib/dapp/dimg/image/docker.rb', line 137 def load!(file_path, verbose: false, quiet: false) ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker} load -i #{file_path}", verbose: verbose, quiet: quiet) end |
.reset_image_inspect(image_id) ⇒ Object
99 100 101 |
# File 'lib/dapp/dimg/image/docker.rb', line 99 def reset_image_inspect(image_id) image_inspects.delete(image_id) end |
.save!(image_or_images, file_path, verbose: false, quiet: false) ⇒ Object
132 133 134 135 |
# File 'lib/dapp/dimg/image/docker.rb', line 132 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
127 128 129 130 |
# File 'lib/dapp/dimg/image/docker.rb', line 127 def tag!(id:, tag:, verbose: false, quiet: false) ::Dapp::Dapp.shellout!("#{::Dapp::Dapp.host_docker} tag #{id} #{tag}", verbose: verbose, quiet: quiet) image_inspects[tag] = image_inspect(id) end |
.tag?(name) ⇒ Boolean
123 124 125 |
# File 'lib/dapp/dimg/image/docker.rb', line 123 def tag?(name) !(/^#{tag_format}$/ =~ name).nil? end |
.tag_format ⇒ Object
115 116 117 |
# File 'lib/dapp/dimg/image/docker.rb', line 115 def tag_format '(?![-.])[a-zA-Z0-9_.-]{1,127}' end |
Instance Method Details
#config_option(option) ⇒ Object
69 70 71 72 |
# File 'lib/dapp/dimg/image/docker.rb', line 69 def config_option(option) raise Error::Build, code: :image_not_exist, data: { name: name } if built_id.nil? self.class.image_config_option(image_id: built_id, option: option) end |
#created_at ⇒ Object
59 60 61 62 |
# File 'lib/dapp/dimg/image/docker.rb', line 59 def created_at raise Error::Build, code: :image_not_exist, data: { name: name } unless tagged? self.class.image_inspect(self.name)["Created"] end |
#id ⇒ Object
29 30 31 |
# File 'lib/dapp/dimg/image/docker.rb', line 29 def id self.class.image_inspect(self.name)["Id"] end |
#pull! ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/dapp/dimg/image/docker.rb', line 46 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 self.class.reset_image_inspect(self.name) end |
#push! ⇒ Object
39 40 41 42 43 44 |
# File 'lib/dapp/dimg/image/docker.rb', line 39 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 |
#size ⇒ Object
64 65 66 67 |
# File 'lib/dapp/dimg/image/docker.rb', line 64 def size raise Error::Build, code: :image_not_exist, data: { name: name } unless tagged? Float(self.class.image_inspect(self.name)["Size"]) end |
#tagged? ⇒ Boolean
55 56 57 |
# File 'lib/dapp/dimg/image/docker.rb', line 55 def tagged? not self.class.image_inspect(self.name).empty? end |