Class: Dapp::Dimg::Builder::Chef

Inherits:
Base
  • Object
show all
Defined in:
lib/dapp/dimg/builder/chef.rb

Defined Under Namespace

Classes: Berksfile, Cookbook, CookbookMetadata

Constant Summary collapse

DEFAULT_CHEFDK_IMAGE =

TODO: config, DSL, DEFAULT_CHEFDK_IMAGE

'dappdeps/chefdk:1.2.22-1'.freeze

Instance Attribute Summary

Attributes inherited from Base

#dimg

Instance Method Summary collapse

Methods inherited from Base

#before_install, #before_install?, #before_install_checksum, #before_setup, #before_setup?, #before_setup_checksum, #build_artifact, #build_artifact?, #build_artifact_checksum, #initialize, #install, #install?, #install_checksum, #setup, #setup?, #setup_checksum

Constructor Details

This class inherits a constructor from Dapp::Dimg::Builder::Base

Instance Method Details

#before_build_checkObject



38
39
40
41
42
43
44
45
# File 'lib/dapp/dimg/builder/chef.rb', line 38

def before_build_check
  %i(before_install install before_setup setup build_artifact).tap do |stages|
    (builder_cookbook.enabled_recipes -
     stages.map {|stage| builder_cookbook.stage_enabled_recipes(stage)}.flatten.uniq).each do |recipe|
      dimg.dapp.log_warning(desc: {code: :recipe_does_not_used, data: {recipe: recipe}})
    end
  end
end

#before_dimg_should_be_built_checkObject



28
29
30
31
32
33
34
35
36
# File 'lib/dapp/dimg/builder/chef.rb', line 28

def before_dimg_should_be_built_check
  super

  %i(before_install install before_setup setup).each do |stage|
    unless stage_empty?(stage) || stage_cookbooks_checksum_path(stage).exist?
      raise Error::Chef, code: :stage_checksum_not_calculated, data: {stage: stage}
    end
  end
end

#builder_cookbookObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dapp/dimg/builder/chef.rb', line 71

def builder_cookbook
  @builder_cookbook ||= begin
    unless dimg.dapp.builder_cookbook_path.exist?
      raise Error::Chef,
            code: :builder_cookbook_not_found,
            data: {path: dimg.dapp.builder_cookbook_path.to_s}
    end

    cookbooks = Marshal.load Marshal.dump(dimg.config._chef._cookbook)

    cookbooks.each do |_name, desc|
      # Получение относительного пути из директории .dapp_chef до указанной зависимости.
      # В Dappfile указываются пути относительно самого Dappfile либо абсолютные пути.
      # В объекте конфига должны лежать абсолютные пути по ключу :path.
      if desc[:path]
        relative_from_cookbook_path = Pathname.new(desc[:path]).relative_path_from(dimg.dapp.builder_cookbook_path).to_s
        desc[:path] = relative_from_cookbook_path
      end
    end

    # Добавление самого cookbook'а builder'а.
    cookbooks[dimg.dapp.name] = {
      name: dimg.dapp.name,
      path: '.'
    }

    berksfile = Berksfile.from_conf(cookbook_path: dimg.dapp.builder_cookbook_path.to_s, cookbooks: cookbooks)
     = CookbookMetadata.from_conf(name: dimg.dapp.name, version: '1.0.0', cookbooks: cookbooks)

    Cookbook.new(self,
                 path: dimg.dapp.builder_cookbook_path,
                 berksfile: berksfile,
                 metadata: ,
                 enabled_recipes: dimg.config._chef._recipe,
                 enabled_modules: dimg.config._chef._dimod)
  end
end

#chefdk_containerObject



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

def chefdk_container
  @chefdk_container ||= begin
    if dimg.dapp.shellout("#{dimg.dapp.host_docker} inspect #{chefdk_container_name}").exitstatus.nonzero?
      dimg.dapp.log_secondary_process(dimg.dapp.t(code: 'process.chefdk_container_creating'), short: true) do
        dimg.dapp.shellout!(
          ["#{dimg.dapp.host_docker} create",
           "--name #{chefdk_container_name}",
           "--volume /.dapp/deps/chefdk #{chefdk_image}"].join(' ')
        )
      end
    end

    chefdk_container_name
  end
end

#chefdk_container_nameObject

FIXME: hashsum(image) or dockersafe()



51
52
53
# File 'lib/dapp/dimg/builder/chef.rb', line 51

def chefdk_container_name # FIXME: hashsum(image) or dockersafe()
  chefdk_image.tr('/', '_').tr(':', '_')
end

#chefdk_imageObject



47
48
49
# File 'lib/dapp/dimg/builder/chef.rb', line 47

def chefdk_image
  DEFAULT_CHEFDK_IMAGE # TODO: config, DSL, DEFAULT_CHEFDK_IMAGE
end

#container_stage_build_path(_stage) ⇒ Object



209
210
211
# File 'lib/dapp/dimg/builder/chef.rb', line 209

def container_stage_build_path(_stage)
  Pathname.new('/.dapp/chef/build')
end

#container_stage_config_path(stage) ⇒ Object



188
189
190
191
# File 'lib/dapp/dimg/builder/chef.rb', line 188

def container_stage_config_path(stage)
  install_chef_solo_stage_config(stage)
  container_stage_build_path(stage).join('config.rb')
end

#container_stage_json_attributes_path(stage) ⇒ Object



200
201
202
203
# File 'lib/dapp/dimg/builder/chef.rb', line 200

def container_stage_json_attributes_path(stage)
  install_json_attributes(stage)
  container_stage_build_path(stage).join('attributes.json')
end

#install_chef_solo_stage_config(stage) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/dapp/dimg/builder/chef.rb', line 178

def install_chef_solo_stage_config(stage)
  @install_chef_solo_stage_config ||= {}
  @install_chef_solo_stage_config[stage] ||= true.tap do
    stage_build_path(stage).join('config.rb').write [
      "file_cache_path \"/.dapp/chef/cache\"\n",
      "cookbook_path \"#{container_stage_build_path(stage).join('cookbooks')}\"\n"
    ].join
  end
end

#install_json_attributes(stage) ⇒ Object



193
194
195
196
197
198
# File 'lib/dapp/dimg/builder/chef.rb', line 193

def install_json_attributes(stage)
  @install_json_attributes ||= {}
  @install_json_attributes[stage] ||= true.tap do
    stage_build_path(stage).join('attributes.json').write "#{stage_attributes_raw(stage)}\n"
  end
end

#stage_attributes(stage) ⇒ Object



109
110
111
# File 'lib/dapp/dimg/builder/chef.rb', line 109

def stage_attributes(stage)
  dimg.config._chef.send("__#{stage}_attributes")
end

#stage_attributes_raw(stage) ⇒ Object



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

def stage_attributes_raw(stage)
  JSON.dump stage_attributes(stage)
end

#stage_build_path(stage) ⇒ Object



205
206
207
# File 'lib/dapp/dimg/builder/chef.rb', line 205

def stage_build_path(stage)
  dimg.tmp_path(dimg.config._name).join(stage.to_s)
end

#stage_cookbooks_checksum(stage) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dapp/dimg/builder/chef.rb', line 121

def stage_cookbooks_checksum(stage)
  if stage_cookbooks_checksum_path(stage).exist?
    stage_cookbooks_checksum_path(stage).read.strip
  else
    checksum = builder_cookbook.stage_checksum(stage)

    stage_cookbooks_checksum_path(stage).tap do |path|
      path.parent.mkpath
      path.write "#{checksum}\n"
    end

    checksum
  end
end

#stage_cookbooks_checksum_path(stage) ⇒ Object



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

def stage_cookbooks_checksum_path(stage)
  dimg.build_path.join("#{builder_cookbook.checksum}.#{dimg.config._name}.#{stage}.checksum")
end

#stage_cookbooks_runlist(stage) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dapp/dimg/builder/chef.rb', line 137

def stage_cookbooks_runlist(stage)
  @stage_cookbooks_runlist ||= {}
  @stage_cookbooks_runlist[stage] ||= begin
    res = []

    format_entry = proc do |cookbook, entrypoint|
      entrypoint = 'void' if entrypoint.nil?
      "#{cookbook}::#{entrypoint}"
    end

    builder_cookbook.enabled_modules.map do |cookbook|
      if builder_cookbook.stage_enabled_modules(stage).include? cookbook
        [cookbook, stage]
      else
        [cookbook, nil]
      end
    end.tap {|entries| res.concat entries}

    builder_cookbook.stage_enabled_recipes(stage)
                    .map {|recipe| [builder_cookbook.name, recipe]}
                    .tap do |entries|
      if entries.any?
        res.concat entries
      else
        res << [builder_cookbook.name, nil]
      end
    end

    if res.all? {|_, entrypoint| entrypoint.nil?}
      []
    else
      res.map(&format_entry)
    end
  end
end

#stage_empty?(stage) ⇒ Boolean

rubocop:enable Metrics/PerceivedComplexity

Returns:

  • (Boolean)


174
175
176
# File 'lib/dapp/dimg/builder/chef.rb', line 174

def stage_empty?(stage)
  stage_cookbooks_runlist(stage).empty?
end