Class: CFnDK::TemplatePackager

Inherits:
Object
  • Object
show all
Defined in:
lib/cfndk/template_packager.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_file, region, package, global_config, s3_client, sts_client) ⇒ TemplatePackager

Returns a new instance of TemplatePackager.



7
8
9
10
11
12
13
14
15
16
# File 'lib/cfndk/template_packager.rb', line 7

def initialize(template_file, region, package, global_config, s3_client, sts_client)
  @template_file = template_file
  @region = region
  @package = package
  @global_config = global_config
  @s3_client = s3_client
  @sts_client = sts_client
  @template_body = nil
  @is_uploaded = false
end

Instance Method Details

#large_template?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cfndk/template_packager.rb', line 18

def large_template?
  template_body.size > 51200
end

#package_templteObject



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cfndk/template_packager.rb', line 43

def package_templte
  if !@template_body
    if !@package
      @template_body = File.open(@template_file, 'r').read
      return @template_body
    end
    orgTemplate = File.open(@template_file, 'r').read
    CFnDK.logger.debug('Original Template:' + orgTemplate)
    if is_json?(orgTemplate)
      data = JSON.parse(orgTemplate)
    else
      data = YAML.load(orgTemplate.gsub(/!/, '____CFNDK!____'))
    end
    
    if data['Resources']
      data['Resources'].each do |k, v|
        next unless v.key?('Type')
        t = v['Type'] 
        properties = v['Properties'] || {}
        case t
        when 'AWS::CloudFormation::Stack' then
          if properties['TemplateURL'] =~ /^\s*./
            tp = TemplatePackager.new(File.dirname(@template_file) + '/' + properties['TemplateURL'].sub(/^\s*.\//, ''), @region, @package, @global_config, @s3_client, @sts_client)
            v['Properties']['TemplateURL'] = tp.upload_template_file
          end
        when 'AWS::Lambda::Function' then
          if properties['Code'].kind_of?(String)
            result = upload_zip_file(File.dirname(@template_file) + '/' + properties['Code'].sub(/^\s*.\//, ''))
            v['Properties']['Code'] = {
              'S3Bucket' => result['bucket'],
              'S3Key' => result['key']  
            }
          end
        when 'AWS::Serverless::Function' then
          if properties['CodeUri'].kind_of?(String)
            result = upload_zip_file(File.dirname(@template_file) + '/' + properties['CodeUri'].sub(/^\s*.\//, ''))
            v['Properties']['CodeUri'] = {
              'Bucket' => result['bucket'],
              'Key' => result['key']  
            }
          end
        when 'AWS::Serverless::Api' then
          if properties['DefinitionUri'].kind_of?(String)
            result = upload_file(File.dirname(@template_file) + '/' + properties['DefinitionUri'].sub(/^\s*.\//, ''))
            v['Properties']['DefinitionUri'] = {
              'Bucket' => result['bucket'],
              'Key' => result['key']  
            }
          end
        when 'AWS::ApiGateway::RestApi' then
          if properties['BodyS3Location'].kind_of?(String)
            result = upload_file(File.dirname(@template_file) + '/' + properties['BodyS3Location'].sub(/^\s*.\//, ''))
            v['Properties']['BodyS3Location'] = {
              'Bucket' => result['bucket'],
              'Key' => result['key']  
            }
          end
        end
        ## TODO support resources
        # * AWS::AppSync::GraphQLSchema DefinitionS3Location
        # * AWS::AppSync::Resolver RequestMappingTemplateS3Location
        # * AWS::AppSync::Resolver ResponseMappingTemplateS3Location
        # * AWS::ElasticBeanstalk::ApplicationVersion SourceBundle
        # * AWS::Glue::Job Command ScriptLocation
        # * AWS::Include Location
      end
    end

    if is_json?(orgTemplate)
      @template_body = JSON.dump(data)
    else
      @template_body = YAML.dump_stream(data).gsub(/____CFNDK!____/, '!')
    end
    CFnDK.logger.info('Template Packager diff: ' + @template_file) 
    CFnDK.logger.info(CFnDK.diff(orgTemplate, @template_body).to_s)
    CFnDK.logger.debug('Package Template size: ' + @template_body.size.to_s)
    CFnDK.logger.debug('Package Template:' + @template_body)
  end
  @template_body
end

#template_bodyObject



22
23
24
# File 'lib/cfndk/template_packager.rb', line 22

def template_body
  package_templte
end

#upload_template_fileObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cfndk/template_packager.rb', line 26

def upload_template_file
  key = [@global_config.s3_template_hash, @template_file].compact.join('/')
  url = "https://s3.amazonaws.com/#{bucket_name}/#{key}"

  unless @is_uploaded
    create_bucket
    @s3_client.put_object(
      body: template_body,
      bucket: bucket_name,
      key: key
    )
    @is_uploaded = true
    CFnDK.logger.info('Put S3 object: ' + url + ' Size: ' + template_body.size.to_s)
  end
  url
end