Class: Dapp::Dimg::Dimg
Defined Under Namespace
Modules: GitArtifact, Path, Stages
Instance Attribute Summary collapse
Instance Method Summary
collapse
class_to_lowercase, #class_to_lowercase, #delete_file, #kwargs, #make_path, #search_file_upward
#hashsum, #paths_content_hashsum, #sha256
Methods included from Stages
#all_images, #all_stages, #signature, #stage_by_name, #stage_cache_format, #stage_dapp_label, #tagged_images
Methods included from Path
#build_path, #container_dapp_path, #container_tmp_path, #home_path, #tmp_path
#git_artifacts, #local_git_artifacts, #remote_git_artifacts
Constructor Details
#initialize(config:, dapp:, should_be_built: false, ignore_git_fetch: false) ⇒ Dimg
Returns a new instance of Dimg.
16
17
18
19
20
21
22
23
24
|
# File 'lib/dapp/dimg/dimg.rb', line 16
def initialize(config:, dapp:, should_be_built: false, ignore_git_fetch: false)
@config = config
@dapp = dapp
@ignore_git_fetch = ignore_git_fetch
@should_be_built = should_be_built
raise Error::Dimg, code: :dimg_not_built if should_be_built?
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
11
12
13
|
# File 'lib/dapp/dimg/dimg.rb', line 11
def config
@config
end
|
#dapp ⇒ Object
Returns the value of attribute dapp.
14
15
16
|
# File 'lib/dapp/dimg/dimg.rb', line 14
def dapp
@dapp
end
|
#ignore_git_fetch ⇒ Object
Returns the value of attribute ignore_git_fetch.
12
13
14
|
# File 'lib/dapp/dimg/dimg.rb', line 12
def ignore_git_fetch
@ignore_git_fetch
end
|
#should_be_built ⇒ Object
Returns the value of attribute should_be_built.
13
14
15
|
# File 'lib/dapp/dimg/dimg.rb', line 13
def should_be_built
@should_be_built
end
|
Instance Method Details
#after_stages_build! ⇒ Object
43
44
45
46
47
|
# File 'lib/dapp/dimg/dimg.rb', line 43
def after_stages_build!
return unless last_stage.image.built? || dev_mode? || force_save_cache?
last_stage.save_in_cache!
artifacts.each { |artifact| artifact.last_stage.save_in_cache! }
end
|
#artifact? ⇒ Boolean
150
151
152
|
# File 'lib/dapp/dimg/dimg.rb', line 150
def artifact?
false
end
|
#artifacts ⇒ Object
146
147
148
|
# File 'lib/dapp/dimg/dimg.rb', line 146
def artifacts
@artifacts ||= artifacts_stages.map { |stage| stage.artifacts.map { |artifact| artifact[:dimg] } }.flatten
end
|
#build! ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/dapp/dimg/dimg.rb', line 26
def build!
with_introspection do
dapp.lock("#{dapp.name}.images", readonly: true) do
last_stage.build_lock! do
begin
builder.before_build_check
last_stage.build!
ensure
after_stages_build!
end
end
end
end
ensure
cleanup_tmp
end
|
#build_cache_version ⇒ Object
166
167
168
|
# File 'lib/dapp/dimg/dimg.rb', line 166
def build_cache_version
[::Dapp::BUILD_CACHE_VERSION, dev_mode? ? 1 : 0]
end
|
#builder ⇒ Object
142
143
144
|
# File 'lib/dapp/dimg/dimg.rb', line 142
def builder
@builder ||= Builder.const_get(config._builder.capitalize).new(self)
end
|
#cleanup_tmp ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/dapp/dimg/dimg.rb', line 175
def cleanup_tmp
cmd = "".tap do |cmd|
cmd << "#{dapp.host_docker} run --rm"
cmd << " --volume #{dapp.tmp_base_dir}:#{dapp.tmp_base_dir}"
cmd << " alpine:3.6"
cmd << " rm -rf #{tmp_path}"
end
dapp.shellout! cmd
artifacts.each(&:cleanup_tmp)
end
|
#dev_mode? ⇒ Boolean
158
159
160
|
# File 'lib/dapp/dimg/dimg.rb', line 158
def dev_mode?
dapp.dev_mode?
end
|
#export!(repo, format:) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/dapp/dimg/dimg.rb', line 62
def export!(repo, format:)
dapp.lock("#{dapp.name}.images", readonly: true) do
dapp.tags_by_scheme.each do |tag_scheme_name, tags|
tags.each do |tag|
image_name = format(format, repo: repo, dimg_name: config._name, tag: tag)
export_base!(image_name) do
last_stage.image.export_dimg!(image_name, sheme_name: tag_scheme_name)
end
end
end
end
end
|
#export_base!(image_name) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/dapp/dimg/dimg.rb', line 86
def export_base!(image_name)
if dapp.dry_run?
dapp.log_state(image_name, state: dapp.t(code: 'state.push'), styles: { status: :success })
else
dapp.lock("image.#{hashsum image_name}") do
::Dapp::Dimg::Image::Docker.reset_image_inspect(image_name)
dapp.log_process(image_name, process: dapp.t(code: 'status.process.pushing')) do
yield
end
end
end
end
|
#export_stages!(repo, format:) ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/dapp/dimg/dimg.rb', line 75
def export_stages!(repo, format:)
dapp.lock("#{dapp.name}.images", readonly: true) do
export_images.each do |image|
image_name = format(format, repo: repo, signature: image.name.split(':').last)
export_base!(image_name) do
image.export!(image_name)
end
end
end
end
|
#force_save_cache? ⇒ Boolean
162
163
164
|
# File 'lib/dapp/dimg/dimg.rb', line 162
def force_save_cache?
!!dapp.options[:force_save_cache]
end
|
#import_base!(image, image_name) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/dapp/dimg/dimg.rb', line 115
def import_base!(image, image_name)
if dapp.dry_run?
dapp.log_state(image_name, state: dapp.t(code: 'state.pull'), styles: { status: :success })
else
dapp.lock("image.#{hashsum image_name}") do
dapp.log_process(image_name, process: dapp.t(code: 'status.process.pulling'),
status: { failed: dapp.t(code: 'status.failed.not_pulled') },
style: { failed: :secondary }) do
image.import!(image_name)
end
end
end
end
|
#import_stages!(repo, format:) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/dapp/dimg/dimg.rb', line 100
def import_stages!(repo, format:)
dapp.lock("#{dapp.name}.images", readonly: true) do
import_images.each do |image|
begin
image_name = format(format, repo: repo, signature: image.name.split(':').last)
import_base!(image, image_name)
rescue ::Dapp::Error::Shellout => e
dapp.log_info ::Dapp::Helper::NetStatus.message(e)
next
end
break unless dapp.pull_all_stages?
end
end
end
|
#introspect_image!(image:, options:) ⇒ Object
170
171
172
173
|
# File 'lib/dapp/dimg/dimg.rb', line 170
def introspect_image!(image:, options:)
cmd = "#{dapp.host_docker} run -ti --rm --entrypoint #{dapp.bash_bin} #{options} #{image}"
system(cmd)
end
|
#run(docker_options, command) ⇒ Object
129
130
131
132
133
134
135
136
|
# File 'lib/dapp/dimg/dimg.rb', line 129
def run(docker_options, command)
cmd = "#{dapp.host_docker} run #{[docker_options, last_stage.image.built_id, command].flatten.compact.join(' ')}"
if dapp.dry_run?
dapp.log(cmd)
else
system(cmd) || raise(Error::Dimg, code: :dimg_not_run)
end
end
|
#scratch? ⇒ Boolean
154
155
156
|
# File 'lib/dapp/dimg/dimg.rb', line 154
def scratch?
config._docker._from.nil?
end
|
#stage_image_name(stage_name) ⇒ Object
138
139
140
|
# File 'lib/dapp/dimg/dimg.rb', line 138
def stage_image_name(stage_name)
stages.find { |stage| stage.name == stage_name }.image.name
end
|
#stage_should_be_introspected?(name) ⇒ Boolean
191
192
193
|
# File 'lib/dapp/dimg/dimg.rb', line 191
def stage_should_be_introspected?(name)
dapp.options[:introspect_stage] == name
end
|
#tag!(tag) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/dapp/dimg/dimg.rb', line 49
def tag!(tag)
dapp.lock("#{dapp.name}.images", readonly: true) do
dimg_name = config._name
if dapp.dry_run?
dapp.log_state(dimg_name, state: dapp.t(code: 'state.tag'), styles: { status: :success })
else
dapp.log_process(dimg_name, process: dapp.t(code: 'status.process.tagging')) do
last_stage.image.tag!(tag)
end
end
end
end
|