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
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
|