Class: Pfm::Command::Package

Inherits:
Base
  • Object
show all
Defined in:
lib/iapi-idlc-sdk-pfm/command/package.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

#initializePackage

Returns a new instance of Package.



17
18
19
20
21
# File 'lib/iapi-idlc-sdk-pfm/command/package.rb', line 17

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

Instance Method Details

#packageObject

Raises:



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
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/package.rb', line 42

def package
  raise InvalidRepository, 'This doesn\'t look like a valid infrastructure repository' unless File.directory? "#{inf_base_dir}/tf"

  # tag git repo with version number.
  begin
    system("git tag -am 'v#{REPO_VERSION}' v#{REPO_VERSION} &>/dev/null") || raise
    msg("Tagged v#{REPO_VERSION}".colorize(:green))
  rescue
    msg("Tag v#{REPO_VERSION} has already been created... skipping.".colorize(:green))
  end

  begin
    system('git push --follow-tags &>/dev/null') || raise
    msg('Pushed git tags.'.colorize(:green))
  rescue
    err('Failed to push tags.'.colorize(:red))
  end

  workspace = Idlc::Workspace.new

  workspace.flatten("#{inf_base_dir}/tf", 'tf')
  workspace.add('lib/')
  workspace.add('.pfm/config') if File.exist? '.pfm/config'
  workspace.add('backend.tf') if File.exist? 'backend.tf'
  workspace.add('infraspec.yml') if File.exist? 'infraspec.yml'

  package_name = "#{@config[:application_name]}.#{REPO_VERSION}.infra.zip"
  dest_zip = "./.pfm/#{package_name}"
  FileUtils.rm_rf(dest_zip) if File.exist? dest_zip
  Idlc::Workspace.zip_folder(workspace.tmp_dir, dest_zip)
  msg("packaged to #{dest_zip}".colorize(:green))

  # upload to s3
  s3 = Aws::S3::Resource.new(region: SETTINGS['AWS_REGION'])
  bucket_name = Idlc::SERVICES[SETTINGS['AWS_REGION']]['build']['publish_bucket']

  obj = s3.bucket(bucket_name).object(package_name)
  obj.upload_file(dest_zip)
  msg('Pushed package to S3.'.colorize(:green))

  # register with Orchestrate Build
  raise InvalidRepository, 'Missing configuration.schema.json file in root.' unless File.exist? 'configuration.schema.json'
  client = Idlc::AWSRestClient.new()

  request = {
    service: 'build',
    method: 'PUT',
    path: '/builds',
    body: {
      application_name: @config[:application_name],
      revision: REPO_VERSION,
      artifact_path: "s3://#{bucket_name}/#{package_name}",
      configuration_schema: JSON.parse(File.read('configuration.schema.json'))
    }
  }

  response = client.fetch(request.to_json)
  msg("Registered #{@config[:application_name]} #{REPO_VERSION} with Orchestrate.".colorize(:green))
end

#params_valid?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/iapi-idlc-sdk-pfm/command/package.rb', line 113

def params_valid?
  @params_valid
end

#read_and_validate_paramsObject



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

def read_and_validate_params
  arguments = parse_options(@params)

  case arguments.size
  when 0
    @params_valid = true
  else
    @params_valid = false
  end
end

#run(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/iapi-idlc-sdk-pfm/command/package.rb', line 23

def run(params)
  @params = params
  read_and_validate_params

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