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.



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

#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

.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_inspectsObject



103
104
105
# File 'lib/dapp/dimg/image/docker.rb', line 103

def image_inspects
  @image_inspects ||= {}
end

.image_name?(name) ⇒ Boolean

Returns:

  • (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_formatObject



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

.imagesObject



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

Returns:

  • (Boolean)


123
124
125
# File 'lib/dapp/dimg/image/docker.rb', line 123

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

.tag_formatObject



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

Raises:



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_atObject

Raises:



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

#idObject



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

Raises:



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

#sizeObject

Raises:



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

Returns:

  • (Boolean)


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

def tagged?
  not self.class.image_inspect(self.name).empty?
end

#untag!Object

Raises:



33
34
35
36
37
# File 'lib/dapp/dimg/image/docker.rb', line 33

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