Class: Dapp::Dimg::Image::Stage

Inherits:
Object
  • Object
show all
Includes:
Argument
Defined in:
lib/dapp/dimg/image/stage.rb

Direct Known Subclasses

Dimg, Scratch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Argument

#add_change_cmd, #add_change_entrypoint, #add_change_env, #add_change_expose, #add_change_label, #add_change_onbuild, #add_change_user, #add_change_volume, #add_change_workdir, #add_command, #add_env, #add_service_change_label, #add_service_command, #add_volume, #add_volumes_from, #prepare_instructions

Constructor Details

#initialize(name:, dapp:, built_id: nil, from: nil) ⇒ Stage

Returns a new instance of Stage.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dapp/dimg/image/stage.rb', line 57

def initialize(name:, dapp:, built_id: nil, from: nil)
  @built_id = built_id

  @bash_commands          = []
  @service_bash_commands  = []
  @options                = {}
  @change_options         = {}
  @service_change_options = {}

  @from = from
  @name = name
  @dapp = dapp
end

Instance Attribute Details

#dappObject (readonly)

Returns the value of attribute dapp.



9
10
11
# File 'lib/dapp/dimg/image/stage.rb', line 9

def dapp
  @dapp
end

#fromObject (readonly)

Returns the value of attribute from.



7
8
9
# File 'lib/dapp/dimg/image/stage.rb', line 7

def from
  @from
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/dapp/dimg/image/stage.rb', line 8

def name
  @name
end

Class Method Details

.image_by_name(name:, **kwargs) ⇒ Object



12
13
14
# File 'lib/dapp/dimg/image/stage.rb', line 12

def image_by_name(name:, **kwargs)
  images[name] ||= new(name: name, **kwargs)
end

.image_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dapp/dimg/image/stage.rb', line 32

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

.image_name_formatObject



24
25
26
# File 'lib/dapp/dimg/image/stage.rb', line 24

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

.image_reset(name) ⇒ Object



16
17
18
# File 'lib/dapp/dimg/image/stage.rb', line 16

def image_reset(name)
  images.delete(name)
end

.imagesObject



20
21
22
# File 'lib/dapp/dimg/image/stage.rb', line 20

def images
  @images ||= {}
end

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



44
45
46
# File 'lib/dapp/dimg/image/stage.rb', line 44

def load!(dapp, file_path, verbose: false, quiet: false)
  ruby2go_command(dapp, command: :load, options: { file_path: file_path })
end

.ruby2go_command(dapp, command:, **options) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/dapp/dimg/image/stage.rb', line 48

def ruby2go_command(dapp, command:, **options)
  (options[:options] ||= {}).merge!(host_docker_config_dir: dapp.class.host_docker_config_dir)
  dapp.ruby2go_image({ command: command }.merge(options)).tap do |res|
    raise Error::Build, code: :ruby2go_image_command_failed_unexpected_error, data: { command: command, message: res["error"] } unless res["error"].nil?
    break res['data']
  end
end

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



40
41
42
# File 'lib/dapp/dimg/image/stage.rb', line 40

def save!(dapp, image_or_images, file_path, verbose: false, quiet: false)
  ruby2go_command(dapp, command: :save, options: { images: Array(image_or_images), file_path: file_path })
end

.tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dapp/dimg/image/stage.rb', line 36

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

.tag_formatObject



28
29
30
# File 'lib/dapp/dimg/image/stage.rb', line 28

def tag_format
  '(?![-.])[a-zA-Z0-9_.-]{1,127}'
end

Instance Method Details

#build!Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/dapp/dimg/image/stage.rb', line 128

def build!
  res = self.dapp.ruby2go_image(**ruby2go_image_build_options)
  if res["error"].nil?
    set_ruby2go_state_hash(JSON.load(res['data']['image']))
  elsif res["error"].start_with? "container run failed"
    raise Error::Build, code: :ruby2go_image_command_failed, data: { command: "build" }
  else
    raise Error::Build, code: :ruby2go_image_command_failed_unexpected_error, data: { command: "build", message: res["error"] }
  end
end

#built?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/dapp/dimg/image/stage.rb', line 75

def built?
  !built_id.nil?
end

#built_idObject



79
80
81
# File 'lib/dapp/dimg/image/stage.rb', line 79

def built_id
  @built_id ||= id
end

#built_image_inspectObject



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

def built_image_inspect
  @built_image_inspect || image_inspect
end

#built_image_inspect!Object



99
100
101
# File 'lib/dapp/dimg/image/stage.rb', line 99

def built_image_inspect!
  built_image_inspect
end

#created_atObject



91
92
93
# File 'lib/dapp/dimg/image/stage.rb', line 91

def created_at
  built_image_inspect!["Created"]
end

#export!(name) ⇒ Object



157
158
159
# File 'lib/dapp/dimg/image/stage.rb', line 157

def export!(name)
  ruby2go_command(:export, options: { name: name })
end

#get_ruby2go_state_hashObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dapp/dimg/image/stage.rb', line 203

def get_ruby2go_state_hash
  [
    :name,
    :from,
    :built_id,
    :built_image_inspect,
    :image_inspect,
    :bash_commands,
    :service_bash_commands,
    :options,
    :change_options,
    :service_change_options,
  ].map do |name|
    if name == :from
      [name, from.get_ruby2go_state_hash] unless from.nil?
    elsif name == :built_image_inspect && built_image_inspect.empty?
    elsif name == :image_inspect && image_inspect.empty?
    else
      [name, send(name)]
    end
  end
    .compact
    .to_h
end

#idObject



83
84
85
# File 'lib/dapp/dimg/image/stage.rb', line 83

def id
  image_inspect["Id"]
end

#image_inspectObject



111
112
113
114
# File 'lib/dapp/dimg/image/stage.rb', line 111

def image_inspect
  ruby2go_command(:inspect, extended_image_option: false) if @image_inspect.nil?
  @image_inspect
end

#import!(name) ⇒ Object



165
166
167
# File 'lib/dapp/dimg/image/stage.rb', line 165

def import!(name)
  ruby2go_command(:import, options: { name: name })
end

#introspect!Object



153
154
155
# File 'lib/dapp/dimg/image/stage.rb', line 153

def introspect!
  ruby2go_command(:introspect)
end

#labelsObject



87
88
89
# File 'lib/dapp/dimg/image/stage.rb', line 87

def labels
  built_image_inspect!.fetch('Config', {}).fetch('Labels', {})
end

#pull!Object



116
117
118
119
120
# File 'lib/dapp/dimg/image/stage.rb', line 116

def pull!
  dapp.log_secondary_process(dapp.t(code: 'process.image_pull', data: { name: name })) do
    ruby2go_command(:pull)
  end
end

#push!Object



122
123
124
125
126
# File 'lib/dapp/dimg/image/stage.rb', line 122

def push!
  dapp.log_secondary_process(dapp.t(code: 'process.image_push', data: { name: name })) do
    ruby2go_command(:push)
  end
end

#reset_image_inspectObject



107
108
109
# File 'lib/dapp/dimg/image/stage.rb', line 107

def reset_image_inspect
  @image_inspect = nil
end

#ruby2go_command(command, extended_image_option: true, options: {}) ⇒ Object



177
178
179
180
181
182
# File 'lib/dapp/dimg/image/stage.rb', line 177

def ruby2go_command(command, extended_image_option: true, options: {})
  command_options = ruby2go_command_options(command, extended_image_option: extended_image_option).in_depth_merge(options: options)
  self.class.ruby2go_command(dapp, **command_options).tap do |data|
    set_ruby2go_state_hash(JSON.load(data['image']))
  end
end

#ruby2go_command_options(command, extended_image_option: true) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dapp/dimg/image/stage.rb', line 184

def ruby2go_command_options(command, extended_image_option: true)
  image = begin
    if extended_image_option
      ruby2go_image_option
    else
      JSON.dump({name: name})
    end
  end

  {
    image: image,
    command: command,
  }
end

#ruby2go_image_build_optionsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dapp/dimg/image/stage.rb', line 139

def ruby2go_image_build_options
  {
    image: ruby2go_image_option,
    command: :build,
    options: {
      introspection: {
        before: dapp.introspect_before_error?,
        after: dapp.introspect_error?
      },
      host_docker_config_dir: dapp.class.host_docker_config_dir,
    }
  }
end

#ruby2go_image_optionObject



199
200
201
# File 'lib/dapp/dimg/image/stage.rb', line 199

def ruby2go_image_option
  JSON.dump(get_ruby2go_state_hash)
end

#save_in_cache!Object



169
170
171
# File 'lib/dapp/dimg/image/stage.rb', line 169

def save_in_cache!
  ruby2go_command(:save_in_cache)
end

#set_ruby2go_state_hash(state_hash) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/dapp/dimg/image/stage.rb', line 228

def set_ruby2go_state_hash(state_hash)
  state_hash.each do |name, value|
    variable = "@#{name}".to_sym

    case name
    when "from"
      from.set_ruby2go_state_hash(value) unless from.nil? || value.nil?
    when "built_id"
      if value.to_s.empty?
        @built_id = nil
      else
        @built_id = value
      end
    when "image_inspect"
      instance_variable_set(variable, (value || {}))
    when "options", "change_options", "service_change_options"
      instance_variable_set(variable, (value || {}).reject { |_, v| v.nil? || v.empty? }.symbolize_keys)
    when "bash_commands", "service_bash_commands"
      instance_variable_set(variable, value || [])
    else
      instance_variable_set(variable, value)
    end
  end
end

#sizeObject



95
96
97
# File 'lib/dapp/dimg/image/stage.rb', line 95

def size
  Float(built_image_inspect!["Size"])
end

#tag!(name) ⇒ Object



161
162
163
# File 'lib/dapp/dimg/image/stage.rb', line 161

def tag!(name)
  ruby2go_command(:tag, options: { name: name })
end

#tagged?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/dapp/dimg/image/stage.rb', line 71

def tagged?
  not image_inspect.empty?
end

#untag!Object



173
174
175
# File 'lib/dapp/dimg/image/stage.rb', line 173

def untag!
  ruby2go_command(:untag)
end