Class: FPM::Fry::Command::Cook
- Inherits:
-
FPM::Fry::Command
- Object
- Clamp::Command
- FPM::Fry::Command
- FPM::Fry::Command::Cook
- Defined in:
- lib/fpm/fry/command/cook.rb
Defined Under Namespace
Classes: BuildFailed
Constant Summary collapse
- UPDATE_VALUES =
['auto','never','always']
Instance Attribute Summary collapse
Attributes inherited from FPM::Fry::Command
Instance Method Summary collapse
- #adjust_config_files(output) ⇒ Object
- #adjust_package_architecture(output) ⇒ Object
- #adjust_package_settings(output) ⇒ Object
- #attach_to_build_container_and_stream_logs(container) ⇒ Object
- #build! ⇒ Object
- #create_build_container ⇒ Object
- #execute ⇒ Object
- #flavour ⇒ Object
-
#initialize(invocation_path, ctx = {}) ⇒ Cook
constructor
A new instance of Cook.
- #input_package(container) ⇒ Object
- #lint_output_class! ⇒ Object
- #lint_recipe! ⇒ Object
- #lint_recipe_file! ⇒ Object
- #packages ⇒ Object
- #pull_base_image! ⇒ Object
- #start_build_container(container) ⇒ Object
- #update? ⇒ Boolean
- #wait_for_build_container_to_shut_down(container) ⇒ Object
- #write_output!(output) ⇒ Object
Methods inherited from FPM::Fry::Command
Constructor Details
#initialize(invocation_path, ctx = {}) ⇒ Cook
Returns a new instance of Cook.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fpm/fry/command/cook.rb', line 27 def initialize(invocation_path, ctx = {}) @tls = nil require 'digest' require 'fileutils' require 'fpm/fry/with_data' require 'fpm/fry/recipe' require 'fpm/fry/recipe/builder' require 'fpm/fry/detector' require 'fpm/fry/docker_file' require 'fpm/fry/stream_parser' require 'fpm/fry/block_enumerator' require 'fpm/fry/build_output_parser' require 'fpm/fry/inspector' require 'fpm/fry/plugin/config' super end |
Instance Attribute Details
#build_image ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/fpm/fry/command/cook.rb', line 117 def build_image @build_image ||= begin sum = Digest::SHA256.hexdigest( image_id + "\0" + cache.cachekey ) cachetag = "fpm-fry:#{sum[0..30]}" res = begin url = client.url("images/#{cachetag}/json") client.get( expects: [200,404], path: url ) rescue Excon::Error logger.error "could not fetch image json for #{image}, url: #{url}" raise end if res.status == 404 df = DockerFile::Source.new(builder.variables.merge(image: image_id),cache) begin url = client.url("build") query = { rm: 1, dockerfile: DockerFile::NAME, t: cachetag } query[:platform] = platform if platform client.post( headers: { 'Content-Type'=>'application/tar' }, query: query, expects: [200], path: url, request_block: BlockEnumerator.new(df.tar_io) ) rescue Excon::Error logger.error "could not build #{image}, url: #{url}" raise end else # Hack to trigger hints/warnings even when the cache is valid. DockerFile::Source.new(builder.variables.merge(image: image_id),cache).send(:file_map) end df = DockerFile::Build.new(cachetag, builder.variables.dup,builder.recipe, update: update?) parser = BuildOutputParser.new(out) begin url = client.url("build") query = { rm: 1, dockerfile: DockerFile::NAME} query[:platform] = platform if platform res = client.post( headers: { 'Content-Type'=>'application/tar' }, query: query, expects: [200], path: url, request_block: BlockEnumerator.new(df.tar_io), response_block: parser ) rescue Excon::Error logger.error "could not build #{image}, url: #{url}" raise end if parser.images.none? raise "Didn't find a build image in the stream. This usually means that the build script failed." end image = parser.images.last logger.debug("Detected build image", image: image) image end end |
#builder ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fpm/fry/command/cook.rb', line 61 def builder @builder ||= begin b = nil Inspector.for_image(client, image) do |inspector| variables = Detector.detect(inspector) variables[:architecture] = platform logger.debug("Loading recipe",variables: variables, recipe: recipe) b = Recipe::Builder.new(variables, logger: ui.logger, inspector: inspector) b.load_file( recipe ) end b end end |
#cache ⇒ Object
80 81 82 |
# File 'lib/fpm/fry/command/cook.rb', line 80 def cache @cache ||= builder.recipe.source.build_cache(tmpdir) end |
#image_id ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/fpm/fry/command/cook.rb', line 103 def image_id @image_id ||= begin url = client.url("images/#{image}/json") res = client.get(expects: [200], path: url) body = JSON.parse(res.body) body.fetch('id'){ body.fetch('Id') } rescue Excon::Error logger.error "could not fetch image json for #{image}, url: #{url}" raise end end |
#output_class ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fpm/fry/command/cook.rb', line 44 def output_class @output_class ||= begin logger.debug("Autodetecting package type",flavour: flavour) case(flavour) when 'debian' require 'fpm/package/deb' FPM::Package::Deb when 'redhat' require 'fpm/package/rpm' FPM::Package::RPM else raise "Cannot auto-detect package type." end end end |
Instance Method Details
#adjust_config_files(output) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/fpm/fry/command/cook.rb', line 388 def adjust_config_files( output ) # FPM flags all files in /etc as config files but only for debian :/. # Actually this behavior makes sense to me for all packages because it's # the thing I usually want. By setting this attribute at least the # misleading warning goes away. output.attributes[:deb_no_default_config_files?] = true output.attributes[:deb_auto_config_files?] = false return if output.attributes[:fry_config_explicitly_used] # Now that we have disabled this for debian we have to reenable if it for # all. etc = File.('etc', output.staging_path) if File.exist?( etc ) # Config plugin wasn't used. Add everything under /etc prefix_length = output.staging_path.size + 1 added = [] Find.find(etc) do | path | next unless File.file? path name = path[prefix_length..-1] if !output.config_files.include? name added << name output.config_files << name end end if added.any? logger.hint( "#{output.name} contains some config files in /etc. They were automatically added. You can customize this using the \"config\" plugin.", documentation: "https://github.com/xing/fpm-fry/wiki/Plugin-config", files: added) end end end |
#adjust_package_architecture(output) ⇒ Object
376 377 378 379 |
# File 'lib/fpm/fry/command/cook.rb', line 376 def adjust_package_architecture(output) # strip prefix and only use the architecture part output.architecture = platform.split("/").last if platform end |
#adjust_package_settings(output) ⇒ Object
381 382 383 384 385 386 |
# File 'lib/fpm/fry/command/cook.rb', line 381 def adjust_package_settings( output ) # FPM ignores the file permissions on rpm packages. output.attributes[:rpm_use_file_permissions?] = true output.attributes[:rpm_user] = 'root' output.attributes[:rpm_group] = 'root' end |
#attach_to_build_container_and_stream_logs(container) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/fpm/fry/command/cook.rb', line 263 def attach_to_build_container_and_stream_logs(container) url = client.url('containers',container,'attach?stderr=1&stdout=1&stream=1&logs=1') client.post( path: url, body: '', expects: [200], middlewares: [ StreamParser.new(out,err), Excon::Middleware::Expects, Excon::Middleware::Instrumentor, Excon::Middleware::Mock ] ) rescue Excon::Error logger.error "could not attach to container #{container}, url: #{url}" raise end |
#build! ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/fpm/fry/command/cook.rb', line 218 def build! container = create_build_container start_build_container(container) attach_to_build_container_and_stream_logs(container) wait_for_build_container_to_shut_down(container) yield container ensure unless keep? client.destroy(container) if container end end |
#create_build_container ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/fpm/fry/command/cook.rb', line 230 def create_build_container url = client.url('containers','create') args = { headers: { 'Content-Type' => 'application/json' }, path: url, expects: [201], body: JSON.generate({"Image" => build_image}) } args[:query] = { platform: platform } if platform res = client.post(args) JSON.parse(res.body)['Id'] rescue Excon::Error logger.error "could not create #{build_image}, url: #{url}" raise end |
#execute ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/fpm/fry/command/cook.rb', line 423 def execute pull_base_image! if pull? # force some eager loading lint_recipe_file! builder lint_output_class! lint_recipe! cache image_id build_image packages do | dir_map | build! do |container| input_package(container) do |input| input.split( container, dir_map ) end end end return 0 rescue Recipe::NotFound => e logger.error("Recipe not found", recipe: recipe, exception: e) return 1 rescue => e logger.error(e) return 1 end |
#flavour ⇒ Object
76 77 78 |
# File 'lib/fpm/fry/command/cook.rb', line 76 def flavour builder.variables[:flavour] end |
#input_package(container) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/fpm/fry/command/cook.rb', line 296 def input_package(container) input = FPM::Package::Docker.new( logger: logger, client: client, keep_modified_files: builder.keep_modified_files, verbose: verbose?, ) builder.recipe.apply_input(input) begin input.input(container) return yield(input) ensure input.cleanup_staging input.cleanup_build end end |
#lint_output_class! ⇒ Object
85 86 87 |
# File 'lib/fpm/fry/command/cook.rb', line 85 def lint_output_class! end |
#lint_recipe! ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/fpm/fry/command/cook.rb', line 93 def lint_recipe! problems = builder.recipe.lint if problems.any? problems.each do |p| logger.error(p) end raise end end |
#lint_recipe_file! ⇒ Object
89 90 91 |
# File 'lib/fpm/fry/command/cook.rb', line 89 def lint_recipe_file! File.exist?(recipe) || raise(Recipe::NotFound) end |
#packages ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/fpm/fry/command/cook.rb', line 339 def packages dir_map = [] out_map = {} builder.recipe.packages.each do | package | output = output_class.new output.instance_variable_set(:@logger,logger) package.files.each do | pattern | dir_map << [ pattern, output.staging_path ] end out_map[ output ] = package end dir_map = Hash[ dir_map.reverse ] yield dir_map out_map.each do |output, package| package.apply_output(output) adjust_package_architecture(output) adjust_package_settings(output) adjust_config_files(output) end out_map.each do |output, _| write_output!(output) end ensure out_map.each do |output, _| output.cleanup_staging output.cleanup_build end end |
#pull_base_image! ⇒ Object
211 212 213 214 215 216 |
# File 'lib/fpm/fry/command/cook.rb', line 211 def pull_base_image! client.pull(image, platform: platform) rescue Excon::Error logger.error "could not pull base image #{image}" raise end |
#start_build_container(container) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/fpm/fry/command/cook.rb', line 248 def start_build_container(container) url = client.url('containers',container,'start') client.post( headers: { 'Content-Type' => 'application/json' }, path: url, expects: [204], body: JSON.generate({}) ) rescue Excon::Error logger.error "could not start container #{container}, url: #{url}" raise end |
#update? ⇒ Boolean
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/fpm/fry/command/cook.rb', line 185 def update? if flavour == 'debian' case(update) when 'auto' Inspector.for_image(client, image) do |inspector| begin inspector.read('/var/lib/apt/lists') do |file| next if file.header.name == 'lists/' logger.hint("/var/lib/apt/lists is not empty, you could try to speed up builds with --update=never", documentation: 'https://github.com/xing/fpm-fry/wiki/The-update-parameter') break end rescue FPM::Fry::Client::FileNotFound logger.hint("/var/lib/apt/lists does not exists, so we will autoupdate") end end return true when 'always' return true when 'never' return false end else return false end end |
#wait_for_build_container_to_shut_down(container) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/fpm/fry/command/cook.rb', line 281 def wait_for_build_container_to_shut_down(container) res = client.post( path: client.url('containers',container,'wait'), expects: [200], body: '' ) json = JSON.parse(res.body) if json["StatusCode"] != 0 raise BuildFailed.new("Build script failed with non zero exit code", json) end rescue Excon::Error logger.error "could not wait successfully for container #{container}, url: #{url}" raise end |
#write_output!(output) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/fpm/fry/command/cook.rb', line 313 def write_output!(output) package_file = File.(output.to_s(nil)) FileUtils.mkdir_p(File.dirname(package_file)) tmp_package_file = package_file + '.tmp' begin FileUtils.rm_rf tmp_package_file rescue Errno::ENOENT end output.output(tmp_package_file) if output.config_files.any? logger.debug("Found config files for #{output.name}", files: output.config_files) else logger.debug("No config files for #{output.name}") end begin FileUtils.rm_rf package_file rescue Errno::ENOENT end File.rename tmp_package_file, package_file logger.info("Created package", :path => package_file) end |