Class: Dapp::Dimg::Dimg

Inherits:
Object
  • Object
show all
Includes:
GitArtifact, Path, Stages, Helper::Sha256, Helper::Trivia
Defined in:
lib/dapp/dimg/dimg.rb,
lib/dapp/dimg/dimg/path.rb,
lib/dapp/dimg/dimg/stages.rb,
lib/dapp/dimg/dimg/git_artifact.rb

Direct Known Subclasses

Artifact

Defined Under Namespace

Modules: GitArtifact, Path, Stages

Instance Attribute Summary collapse

Attributes included from Stages

#last_stage

Instance Method Summary collapse

Methods included from Helper::Trivia

#check_path?, #check_subpath?, #class_to_lowercase, class_to_lowercase, #delete_file, #ignore_path?, #ignore_path_base, #kwargs, #make_path, #path_checker, #search_file_upward

Methods included from Helper::Sha256

#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_dir_exists?, #tmp_path

Methods included from GitArtifact

#generate_git_artifacts, #generate_git_embedded_artifacts, #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.

Raises:



16
17
18
19
20
21
22
23
24
25
26
# 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

  @dapp._terminate_dimg_on_terminate(self)

  raise Error::Dimg, code: :dimg_not_built if should_be_built?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/dapp/dimg/dimg.rb', line 11

def config
  @config
end

#dappObject (readonly)

Returns the value of attribute dapp.



14
15
16
# File 'lib/dapp/dimg/dimg.rb', line 14

def dapp
  @dapp
end

#ignore_git_fetchObject (readonly)

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_builtObject (readonly)

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



47
48
49
50
51
# File 'lib/dapp/dimg/dimg.rb', line 47

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

Returns:

  • (Boolean)


162
163
164
# File 'lib/dapp/dimg/dimg.rb', line 162

def artifact?
  false
end

#artifactsObject



158
159
160
# File 'lib/dapp/dimg/dimg.rb', line 158

def artifacts
  @artifacts ||= artifacts_stages.map { |stage| stage.artifacts.map { |artifact| artifact[:dimg] } }.flatten
end

#build!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dapp/dimg/dimg.rb', line 32

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
end

#build_cache_versionObject



182
183
184
# File 'lib/dapp/dimg/dimg.rb', line 182

def build_cache_version
  [::Dapp::BUILD_CACHE_VERSION, dev_mode? ? 1 : 0]
end

#build_export_image!(image_name, scheme_name:) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/dapp/dimg/dimg.rb', line 92

def build_export_image!(image_name, scheme_name:)
  Image::Dimg.image_by_name(name: image_name, from: last_stage.image, dapp: dapp).tap do |export_image|
    export_image.add_service_change_label(:'dapp-tag-scheme' => scheme_name)
    export_image.add_service_change_label(:'dapp-dimg' => true)
    export_image.build!
  end
end

#builderObject



154
155
156
# File 'lib/dapp/dimg/dimg.rb', line 154

def builder
  @builder ||= Builder.const_get(config._builder.capitalize).new(self)
end

#cleanup_tmpObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dapp/dimg/dimg.rb', line 191

def cleanup_tmp
  return unless tmp_dir_exists?

  # В tmp-директории могли остаться файлы, владельцами которых мы не являемся.
  # Такие файлы могут попасть туда при экспорте файлов артефакта.
  # Чтобы от них избавиться — запускаем docker-контейнер под root-пользователем
  # и удаляем примонтированную 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
end

#dev_mode?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/dapp/dimg/dimg.rb', line 170

def dev_mode?
  dapp.dev_mode?
end

#dimg_export_base!(repo, export_format:, push: false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dapp/dimg/dimg.rb', line 72

def dimg_export_base!(repo, export_format:, push: false)
  dapp.lock("#{dapp.name}.images", readonly: true) do
    dapp.tags_by_scheme.each do |tag_scheme_name, tags|
      dapp.log_step_with_indent(tag_scheme_name) do
        tags.each do |tag|
          image_name = format(export_format, repo: repo, dimg_name: config._name, tag: tag)
          export_base!(image_name, push: push) do
            export_image = build_export_image!(image_name, scheme_name: tag_scheme_name)
            if push
              export_image.export!
            else
              export_image.tag!
            end
          end
        end
      end unless tags.empty?
    end
  end
end

#export!(repo, format:) ⇒ Object



57
58
59
# File 'lib/dapp/dimg/dimg.rb', line 57

def export!(repo, format:)
  dimg_export_base!(repo, export_format: format, push: true)
end

#export_base!(image_name, push: true) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/dapp/dimg/dimg.rb', line 100

def export_base!(image_name, push: true)
  if dapp.dry_run?
    dapp.log_state(image_name, state: dapp.t(code: push ? 'state.push' : 'state.export'), 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: push ? 'status.process.pushing' : 'status.process.exporting')) { yield }
    end
  end
end

#export_stages!(repo, format:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/dapp/dimg/dimg.rb', line 61

def export_stages!(repo, format:)
  dapp.lock("#{dapp.name}.images", readonly: true) do
    export_images.each do |stage_image|
      image_name = format(format, repo: repo, signature: stage_image.name.split(':').last)
      export_base!(image_name, push: true) do
        stage_image.export!(image_name)
      end
    end
  end
end

#force_save_cache?Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
# File 'lib/dapp/dimg/dimg.rb', line 174

def force_save_cache?
  if ENV.key? "DAPP_FORCE_SAVE_CACHE"
    %w(yes 1 true).include? ENV["DAPP_FORCE_SAVE_CACHE"].to_s
  else
    !!dapp.options[:force_save_cache]
  end
end

#import_base!(image, image_name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dapp/dimg/dimg.rb', line 126

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dapp/dimg/dimg.rb', line 111

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.options[:pull_all_stages]
    end
  end
end

#introspect_image!(image:, options:) ⇒ Object



186
187
188
189
# File 'lib/dapp/dimg/dimg.rb', line 186

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



141
142
143
144
145
146
147
148
# File 'lib/dapp/dimg/dimg.rb', line 141

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

Returns:

  • (Boolean)


166
167
168
# File 'lib/dapp/dimg/dimg.rb', line 166

def scratch?
  config._docker._from.nil?
end

#stage_image_name(stage_name) ⇒ Object



150
151
152
# File 'lib/dapp/dimg/dimg.rb', line 150

def stage_image_name(stage_name)
  stages.find { |stage| stage.name == stage_name }.image.name
end

#stage_should_be_introspected?(name) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/dapp/dimg/dimg.rb', line 207

def stage_should_be_introspected?(name)
  dapp.options[:introspect_stage] == name
end

#tag!(repo, format:) ⇒ Object



53
54
55
# File 'lib/dapp/dimg/dimg.rb', line 53

def tag!(repo, format:)
  dimg_export_base!(repo, export_format: format)
end

#terminateObject



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

def terminate
  cleanup_tmp
end