Class: DockerBuilder::Builder::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_builder/builder/packer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_settings) ⇒ Packer

Returns a new instance of Packer.



21
22
23
24
# File 'lib/docker_builder/builder/packer.rb', line 21

def initialize(_settings)
  self.server = _settings

end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/docker_builder/builder/packer.rb', line 5

def server
  @server
end

Instance Method Details

#buildObject



26
27
28
29
30
31
32
# File 'lib/docker_builder/builder/packer.rb', line 26

def build
  # config json
  save_packer_config

  DockerBuilder::Command.cmd %Q(packer build #{filename_config} )

end

#build_packer_configObject



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/docker_builder/builder/packer.rb', line 50

def build_packer_config
  res = {}

  res['variables'] = {}

  res['builders'] = []

  bi = settings.properties['build']['base_image']
  base_image_name = "#{bi['name']}:#{bi['tag']}"
  builder1 = {
      pull: false,
      type: "docker",
      image: base_image_name,
      commit: true
  }

  # changes
  entrypoint = settings.properties['build']['entrypoint']
  if entrypoint
    builder1['changes'] ||= []
    builder1['changes'] << "ENTRYPOINT #{entrypoint}"
  end

  res['builders'] << builder1

  #
  recipe_name = settings['build']['packer']['recipe_name'] || 'build'
  cookbook_paths = settings['build']['packer']['cookbook_paths'] || []
  cookbook_paths << settings.dir_cookbooks

  res["provisioners"] = [
      {
          type: "chef-solo",
          prevent_sudo: true,
          cookbook_paths: cookbook_paths,
          json: settings.properties['attributes'],
          run_list: ["recipe[#{settings.name}::#{recipe_name}]"]
    },
  ]

  # tag image
  res["post-processors"] = [
      {
        repository: "#{settings.image_name}",
        type: "docker-tag"
      }
    ]

  res
end

#filename_configObject



46
47
48
# File 'lib/docker_builder/builder/packer.rb', line 46

def filename_config
  File.join(Config.root_path, 'temp', "packer-#{settings.name}.json")
end

#save_packer_configObject

helpers



35
36
37
38
39
40
41
42
43
44
# File 'lib/docker_builder/builder/packer.rb', line 35

def save_packer_config
  require 'json'
  filename = filename_config
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename,"w+") do |f|
    f.write(build_packer_config.to_json)
  end

  true
end

#settingsObject



16
17
18
# File 'lib/docker_builder/builder/packer.rb', line 16

def settings
  server
end