Class: Pfm::Command::Build

Inherits:
Base
  • Object
show all
Defined in:
lib/iapi-idlc-sdk-pfm/command/build.rb

Instance Method Summary collapse

Methods inherited from Base

#build_base_dir, #build_dir, #build_exists?, #build_setup, #deploy_setup, #deploy_setupv2, #inf_base_dir, #needs_help?, #needs_version?, #run_with_default_options, #templates_dir, #verbose?

Methods included from Helpers

debug, err, msg, system_command

Constructor Details

#initializeBuild

Returns a new instance of Build.

[View source]

39
40
41
42
43
# File 'lib/iapi-idlc-sdk-pfm/command/build.rb', line 39

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

Instance Method Details

#buildObject

[View source]

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
100
# File 'lib/iapi-idlc-sdk-pfm/command/build.rb', line 65

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

#params_valid?Boolean

Returns:

  • (Boolean)
[View source]

116
117
118
# File 'lib/iapi-idlc-sdk-pfm/command/build.rb', line 116

def params_valid?
  @params_valid
end

#read_and_validate_paramsObject

[View source]

102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/iapi-idlc-sdk-pfm/command/build.rb', line 102

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

#run(params) ⇒ Object

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/iapi-idlc-sdk-pfm/command/build.rb', line 45

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