Class: Boxes::Builder

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/boxes/builder.rb

Overview

Class which drives the build process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, args) ⇒ Builder

Initialise a new build.

Parameters:

  • env (Boxes::Environment)

    environment to operate in.

  • args (Hash)
  • template (String)

    the name of the template.

  • scripts (Array)

    scripts to include in the build.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/boxes/builder.rb', line 14

def initialize(env, args) # rubocop:disable Metrics/MethodLength
  @name = args[:name] || fail(MissingArgumentError,
                              'The name must be specified.')
  @provider = args[:provider] || fail(MissingArgumentError,
                                      'The provider must be specified.')
  template = args[:template] || fail(MissingArgumentError,
                                     'The template must be specified.')
  scripts = args.fetch(:scripts, [])

  @template = Template.new(env, template)
  @scripts = scripts.collect do |c|
    env.available_scripts.include?(c) ? c : fail(ScriptNotFoundError)
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/boxes/builder.rb', line 6

def name
  @name
end

#providerObject

Returns the value of attribute provider.



6
7
8
# File 'lib/boxes/builder.rb', line 6

def provider
  @provider
end

#scriptsObject

Returns the value of attribute scripts.



6
7
8
# File 'lib/boxes/builder.rb', line 6

def scripts
  @scripts
end

#templateObject

Returns the value of attribute template.



6
7
8
# File 'lib/boxes/builder.rb', line 6

def template
  @template
end

Instance Method Details

#cleanObject

Clean any temporary files used during building.



74
75
76
# File 'lib/boxes/builder.rb', line 74

def clean
  FileUtils.rm("#{build_name}.json")
end

#runObject

Run the build.



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
# File 'lib/boxes/builder.rb', line 30

def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  original_directory = FileUtils.pwd
  box_name = ''

  # render the template
  rendered_template = template.render(name: name,
                                      provider: provider,
                                      scripts: scripts)

  # write the template to a file
  File.open(Boxes.config.working_dir + "#{build_name}.json", 'w') do |f|
    f.puts rendered_template
  end

  # set the environment vars
  Boxes.config.environment_vars.each do |e|
    e.each do |k, v|
      ENV[k] = v.to_s
    end
  end

  # execute the packer command
  FileUtils.chdir(Boxes.config.working_dir)
  cmd = "packer build #{build_name}.json"
  status = Subprocess.run(cmd) do |stdout, stderr, _thread|
    puts stdout unless stdout.nil?
    puts stderr unless stderr.nil?

    # catch the name of the artifact
    if stdout =~ /\.box/
      box_name = stdout.gsub(/[a-zA-Z0-9:\-_]*?\.box/).first
    end
  end

  if status.exitstatus == 0
    FileUtils.mv(Boxes.config.working_dir + box_name,
                 "#{original_directory}/#{name}.box")
  else
    fail BuildRunError,
         'The build didn\'t complete successfully. Check the logs.'
  end
end