Class: Dapp::Dimg

Inherits:
Object
  • Object
show all
Includes:
GitArtifact, Path, Stages, 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

Overview

Dimg

Direct Known Subclasses

Artifact

Defined Under Namespace

Modules: GitArtifact, Path, Stages, Tags

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Sha256

#hashsum, #paths_content_hashsum, #sha256

Methods included from Stages

#images, #signature, #stage_cache_format, #stage_dapp_label

Methods included from Path

#build_path, #chef_path, #container_dapp_path, #container_tmp_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:



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

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

#config ⇒ Object (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#ignore_git_fetch ⇒ Object (readonly)

Returns the value of attribute ignore_git_fetch.



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

def ignore_git_fetch
  @ignore_git_fetch
end

#project ⇒ Object (readonly)

Returns the value of attribute project.



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

def project
  @project
end

#should_be_built ⇒ Object (readonly)

Returns the value of attribute should_be_built.



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

def should_be_built
  @should_be_built
end

Instance Method Details

#artifact? ⇒ Boolean



121
122
123
# File 'lib/dapp/dimg.rb', line 121

def artifact?
  false
end

#build! ⇒ Object



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

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
  FileUtils.rm_rf(tmp_path)
end

#build_cache_version ⇒ Object



133
134
135
# File 'lib/dapp/dimg.rb', line 133

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

#builder ⇒ Object



117
118
119
# File 'lib/dapp/dimg.rb', line 117

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

#dev_mode? ⇒ Boolean



129
130
131
# File 'lib/dapp/dimg.rb', line 129

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

#export!(repo, format:) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/dapp/dimg.rb', line 42

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



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

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



51
52
53
54
55
56
57
58
# File 'lib/dapp/dimg.rb', line 51

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



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

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



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

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

#run(docker_options, command) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/dapp/dimg.rb', line 104

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



125
126
127
# File 'lib/dapp/dimg.rb', line 125

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

#stage_image_name(stage_name) ⇒ Object



113
114
115
# File 'lib/dapp/dimg.rb', line 113

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