Class: FPM::Cookery::DockerPackager

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fpm/cookery/docker_packager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe, config) ⇒ DockerPackager

Returns a new instance of DockerPackager.



18
19
20
21
# File 'lib/fpm/cookery/docker_packager.rb', line 18

def initialize(recipe, config)
  @recipe = recipe
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/fpm/cookery/docker_packager.rb', line 16

def config
  @config
end

#packagerObject (readonly)

Returns the value of attribute packager.



16
17
18
# File 'lib/fpm/cookery/docker_packager.rb', line 16

def packager
  @packager
end

#recipeObject (readonly)

Returns the value of attribute recipe.



16
17
18
# File 'lib/fpm/cookery/docker_packager.rb', line 16

def recipe
  @recipe
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fpm/cookery/docker_packager.rb', line 23

def run
  recipe_dir = File.dirname(recipe.filename)

  # The cli settings should have precendence
  image_name = config.docker_image || recipe.docker_image
  cache_paths = get_cache_paths
  docker_bin = config.docker_bin.nil? || config.docker_bin.empty? ? 'docker' : config.docker_bin
  dockerfile = get_dockerfile(recipe_dir)

  if File.exist?(dockerfile)
    image_name = "local/fpm-cookery/#{File.basename(recipe_dir)}:latest"
    Log.info "Building custom Docker image #{image_name} from #{dockerfile}"
    build_cmd = [
      config.docker_bin, 'build',
      '-f', dockerfile,
      '-t', image_name,
      '--force-rm',
      '.'
    ].compact.flatten.join(' ')
    sh build_cmd
  else
    Log.warn "File #{dockerfile} does not exist - not building a custom Docker image"
  end

  if image_name.nil? || image_name.empty?
    image_name = "fpmcookery/#{FPM::Cookery::Facts.platform}-#{FPM::Cookery::Facts.osrelease}:#{FPM::Cookery::VERSION}"
  end

  Log.info "Building #{recipe.name}-#{recipe.version} inside a Docker container using image #{image_name}"
  Log.info "Mounting #{recipe_dir} as /recipe"

  cmd = [
    config.docker_bin, "run", "-ti",
    "--name", "fpm-cookery-build-#{File.basename(recipe_dir)}",
    config.docker_keep_container ? nil : "--rm",
    "-e", "FPMC_UID=#{Process.uid}",
    "-e", "FPMC_GID=#{Process.gid}",
    config.debug ? ["-e", "FPMC_DEBUG=true"] : nil,
    build_cache_mounts(cache_paths),
    "-v", "#{recipe_dir}:/recipe",
    "-w", "/recipe",
    image_name,
    "fpm-cook", "package",
    config.debug ? '-D' : nil,
    File.basename(recipe.filename)
  ].compact.flatten.join(' ')


  Log.debug "Running: #{cmd}"
  begin
    sh cmd
  rescue => e
    Log.debug e
  end
end