Class: CfnDslPipeline::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/params.rb,
lib/run-cfndsl.rb,
lib/run-syntax.rb,
lib/run-cfn_nag.rb,
lib/cfndsl-pipeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_dir, options) ⇒ Pipeline

Returns a new instance of Pipeline.



11
12
13
14
# File 'lib/run-syntax.rb', line 11

def initialize
  self.cfn_client = Aws::CloudFormation::Client.new(region: self.aws_region)
  self.s3_client = Aws::S3::Client.new(region: self.aws_region)
end

Instance Attribute Details

#base_nameObject

Returns the value of attribute base_name.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def base_name
  @base_name
end

#cfn_clientObject

Returns the value of attribute cfn_client.



9
10
11
# File 'lib/run-syntax.rb', line 9

def cfn_client
  @cfn_client
end

#input_filenameObject

Returns the value of attribute input_filename.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def input_filename
  @input_filename
end

#optionsObject

Returns the value of attribute options.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def options
  @options
end

#output_dirObject

Returns the value of attribute output_dir.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def output_dir
  @output_dir
end

#output_fileObject

Returns the value of attribute output_file.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def output_file
  @output_file
end

#output_filenameObject

Returns the value of attribute output_filename.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def output_filename
  @output_filename
end

#s3_clientObject

Returns the value of attribute s3_client.



9
10
11
# File 'lib/run-syntax.rb', line 9

def s3_client
  @s3_client
end

#syntax_reportObject

Returns the value of attribute syntax_report.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def syntax_report
  @syntax_report
end

#templateObject

Returns the value of attribute template.



41
42
43
# File 'lib/cfndsl-pipeline.rb', line 41

def template
  @template
end

Instance Method Details

#build(input_filename, cfndsl_extras) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/cfndsl-pipeline.rb', line 54

def build(input_filename, cfndsl_extras)
  abort "Input file #{input_filename} doesn't exist!" if !File.file?(input_filename)
  self.input_filename = "#{input_filename}"
  self.base_name = File.basename(input_filename, '.*')
  self.output_filename = File.expand_path("#{self.output_dir}/#{self.base_name}.yaml")
  exec_cfndsl cfndsl_extras
  exec_syntax_validation if self.options.validate_syntax
  exec_dump_params if self.options.dump_deploy_params
  exec_cfn_nag if self.options.validate_cfn_nag
end

#exec_cfn_nagObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/run-cfn_nag.rb', line 7

def exec_cfn_nag
  puts "Auditing template with cfn-nag..."
  
  CfnNagLogging.configure_logging({:debug => self.options.debug_audit})
  cfn_nag = CfnNag.new(config: self.options.cfn_nag)
  result = cfn_nag.audit(cloudformation_string: self.template)
  if self.options.save_audit_report
    audit_report = Capture.capture do
      SimpleStdoutResults.new.render([{
        filename: output_filename,
        file_results: result
      }])
    end
    audit_filename = "#{self.output_dir}/#{self.base_name}.audit"
    File.open(File.expand_path(audit_filename), 'w').puts audit_report['stdout']
    puts "Saved audit report to #{audit_filename}"
    if result[:failure_count]>0
      puts "Audit failed. #{result[:failure_count]} error(s) found     ( ಠ ʖ̯ ಠ)  ".red
    elsif result[:violations].count>0
      puts "Audit passed with #{result[:warning_count]} warnings.     (._.)  ".yellow
    else
      puts "Audit passed!        ヽ( ゚ヮ゚)/      ヽ(´ー`)ノ".green
    end        
  else
    ColoredStdoutResults.new.render([{
      filename: "cfn-nag results:",
      file_results: result
    }]) 
  end
end

#exec_cfndsl(cfndsl_extras) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/run-cfndsl.rb', line 7

def exec_cfndsl(cfndsl_extras)
  print "Generating CloudFormation template...\n"
  model = CfnDsl.eval_file_with_extras("#{@input_filename}", cfndsl_extras)
  @template = JSON.parse(model.to_json).to_yaml
  File.open(@output_filename, 'w') do |file|
    file.puts @template
  end
  @output_file = File.open(@output_filename)
  puts "  #{@output_file.size} bytes written to #{@output_filename}"
end

#exec_dump_paramsObject



6
7
8
9
10
11
12
13
# File 'lib/params.rb', line 6

def exec_dump_params
  param_filename = "#{self.output_dir}/#{self.base_name}.params"
  puts "Deploy parameters written to #{param_filename}"
  param_file = File.open(File.expand_path(param_filename), 'w')
  self.syntax_report['parameters'].each do | param |
    param_file.puts "#{param['parameter_key']}=#{Shellwords.escape(param['default_value'])}"
  end
end

#exec_syntax_validationObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/run-syntax.rb', line 16

def exec_syntax_validation
  print "Validating template syntax...\n"
  if self.options.estimate_cost || (self.output_file.size > 51200)
    puts "Filesize is greater than 51200, or cost estimation required. Validating via S3 bucket "
    uuid = UUID.new
    object_name = "#{uuid.generate}"

    if self.options.validation_bucket
      bucket_name = self.options.validation_bucket
      puts "Using existing S3 bucket #{bucket_name}..."
      bucket = self.s3_client.bucket(self.options.validation_bucket)
    else
      bucket_name = "arch-code-#{uuid.generate}"
      puts "Creating temporary S3 bucket #{bucket_name}..."
      bucket = self.s3_client.bucket(bucket_name)
      bucket.create 
    end
    upload_template(bucket, object_name)

    self.syntax_report = s3_validate_syntax(bucket, object_name)

    if self.options.estimate_cost
      estimate_cost(bucket_name, object_name)
    end

    if !self.options.validation_bucket
      puts "Deleting temporary S3 bucket..."
      bucket.delete! 
    end

  else 
    self.syntax_report = local_validate_syntax
  end

  save_syntax_report if self.options.save_syntax_report

end