class Pfm::Command::Build

def build

def build
  # Zip the cookbooks for transfer
  Idlc::Workspace.zip_folder('./chef', "#{@workspace.tmp_dir}/cookbooks.zip")
  # Start the HTTP server to facilitate file transfers. This is to replace
  # WINRM for file transfers in Packer due to slowness. This function returns
  # the process id of the server instance.
  pid = Idlc::Build::Httpd.start(@workspace.tmp_dir)
  # #start will return twice when forking the HTTP server off, once for the
  # parent and once for the child. When the child return, the pid is nil. We want
  # to skip that run.
  unless pid.nil?
    # Pass some ENV vars for Packer
    @build_config.add_build_var_v2('aws_region', SETTINGS['AWS_REGION'])
    @build_config.add_build_var_v2('app_release', @config[:app_release])
    @build_config.add_build_var_v2('build_uuid', SecureRandom.uuid.to_s)
    @build_config.add_build_var_v2('build_number', @config[:build_number])
    @build_config.add_build_var_v2('httpd_server', Idlc::Build::Httpd.private_ip.to_s)
    @build_config.add_build_var_v2('httpd_port', ENV['HTTPD_PORT'])
    @build_config.add_build_var_v2('cookbooks_zip', "#{@workspace.tmp_dir}/cookbooks.zip")
    begin
      Packer::Binary.build("#{@build_config.dump_build_vars} #{@config[:build_template]}")
      Idlc::Build::Httpd.stop(pid)
      Dir.chdir(build_base_dir)
    rescue
      Idlc::Build::Httpd.stop(pid)
      Dir.chdir(build_base_dir)
      raise BuildFailure, 'The build finished with errors'
    end
  end
  Dir.chdir(build_base_dir)
end

def initialize

def initialize
  super
  @params_valid = true
  @errors = []
end

def params_valid?

def params_valid?
  @params_valid
end

def read_and_validate_params

def read_and_validate_params
  arguments = parse_options(@params)
  case arguments.size
  when 1
    @params_valid = build_exists?
  when 2
  else
    @params_valid = false
  end
end

def run(params)

def run(params)
  @params = params
  read_and_validate_params
  if params_valid?
    build_setup
    build
    # @workspace.cleanup causing bundler issues
    0
  else
    @errors.each { |error| err("Error: #{error}") }
    parse_options(params)
    msg(opt_parser)
    1
  end
rescue BuildFailure => e
  err("ERROR: #{e.message}\n")
  1
end