Class: Dapp::Dimg

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

Overview

Dimg

Direct Known Subclasses

Artifact

Defined Under Namespace

Modules: GitArtifact, Path, Stages, SystemShellout, Tags

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Sha256

#hashsum, #paths_content_hashsum, #sha256

Methods included from SystemShellout

#system_shellout, #system_shellout!

Methods included from Stages

#all_images, #signature, #stage_cache_format, #stage_dapp_label

Methods included from Path

#build_path, #container_dapp_path, #container_tmp_path, #cookbook_path, #home_path, #tmp_path

Methods included from GitArtifact

#git_artifacts, #local_git_artifacts, #remote_git_artifacts

Constructor Details

#initialize(config:, project:, should_be_built: false, ignore_git_fetch: false) ⇒ Dimg

Returns a new instance of Dimg.

Raises:



17
18
19
20
21
22
23
24
25
# File 'lib/dapp/dimg.rb', line 17

def initialize(config:, project:, should_be_built: false, ignore_git_fetch: false)
  @config = config
  @project = project

  @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

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#ignore_git_fetchObject (readonly)

Returns the value of attribute ignore_git_fetch.



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

def ignore_git_fetch
  @ignore_git_fetch
end

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

#should_be_builtObject (readonly)

Returns the value of attribute should_be_built.



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

def should_be_built
  @should_be_built
end

Instance Method Details

#artifact?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/dapp/dimg.rb', line 139

def artifact?
  false
end

#artifactsObject



135
136
137
# File 'lib/dapp/dimg.rb', line 135

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

#build!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dapp/dimg.rb', line 27

def build!
  with_introspection do
    project.lock("#{project.name}.images", readonly: true) do
      last_stage.build_lock! do
        begin
          last_stage.build!
        ensure
          last_stage.save_in_cache! if last_stage.image.built? || dev_mode?
        end
      end
    end
  end
ensure
  cleanup_tmp
end

#build_cache_versionObject



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

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

#builderObject



131
132
133
# File 'lib/dapp/dimg.rb', line 131

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

#cleanup_tmpObject



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

def cleanup_tmp
  FileUtils.rm_rf(tmp_path)
  artifacts.each(&:cleanup_tmp)
end

#dev_mode?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/dapp/dimg.rb', line 147

def dev_mode?
  config._dev_mode || project.dev_mode?
end

#export!(repo, format:) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/dapp/dimg.rb', line 56

def export!(repo, format:)
  project.lock("#{project.name}.images", readonly: true) do
    tags.each do |tag|
      image_name = format % { repo: repo, dimg_name: config._name, tag: tag }
      export_base!(last_stage.image, image_name)
    end
  end
end

#export_base!(image, image_name) ⇒ Object



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

def export_base!(image, image_name)
  if project.dry_run?
    project.log_state(image_name, state: project.t(code: 'state.push'), styles: { status: :success })
  else
    project.lock("image.#{hashsum image_name}") do
      Dapp::Image::Stage.cache_reset(image_name)
      project.log_process(image_name, process: project.t(code: 'status.process.pushing')) do
        project.with_log_indent do
          image.export!(image_name)
        end
      end
    end
  end
end

#export_stages!(repo, format:) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/dapp/dimg.rb', line 65

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

#import_base!(image, image_name) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dapp/dimg.rb', line 103

def import_base!(image, image_name)
  if project.dry_run?
    project.log_state(image_name, state: project.t(code: 'state.pull'), styles: { status: :success })
  else
    project.lock("image.#{hashsum image_name}") do
      project.log_process(image_name,
                          process: project.t(code: 'status.process.pulling'),
                          status: { failed: project.t(code: 'status.failed.not_pulled') },
                          style: { failed: :secondary }) do
        image.import!(image_name)
      end
    end
  end
end

#import_stages!(repo, format:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dapp/dimg.rb', line 89

def import_stages!(repo, format:)
  project.lock("#{project.name}.images", readonly: true) do
    import_images.each do |image|
      begin
        image_name = format % { repo: repo, signature: image.name.split(':').last }
        import_base!(image, image_name)
      rescue Error::Shellout
        next
      end
      break unless project.pull_all_stages?
    end
  end
end

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



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

def introspect_image!(image:, options:)
  cmd = "docker run -ti --rm --entrypoint #{project.bash_bin} #{options} #{image}"
  system(cmd)
end

#run(docker_options, command) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/dapp/dimg.rb', line 118

def run(docker_options, command)
  cmd = "docker run #{[docker_options, last_stage.image.name, command].flatten.compact.join(' ')}"
  if project.dry_run?
    project.log(cmd)
  else
    system(cmd) || raise(Error::Dimg, code: :dimg_not_run)
  end
end

#scratch?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/dapp/dimg.rb', line 143

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

#stage_image_name(stage_name) ⇒ Object



127
128
129
# File 'lib/dapp/dimg.rb', line 127

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

#tag!(tag) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dapp/dimg.rb', line 43

def tag!(tag)
  project.lock("#{project.name}.images", readonly: true) do
    dimg_name = config._name
    if project.dry_run?
      project.log_state(dimg_name, state: project.t(code: 'state.tag'), styles: { status: :success })
    else
      project.log_process(dimg_name, process: project.t(code: 'status.process.tagging')) do
        last_stage.image.tag!(tag)
      end
    end
  end
end