Class: CloudFormationTool::CloudFormation::LambdaCode
- Inherits:
-
Object
- Object
- CloudFormationTool::CloudFormation::LambdaCode
show all
- Includes:
- Storable
- Defined in:
- lib/cloud_formation_tool/cloud_formation/lambda_code.rb
Constant Summary
VERSION
Instance Method Summary
collapse
Methods included from Storable
#make_filename, #upload
#aws_config, #awsas, #awscf, #awscreds, #awsec2, #awss3, #cf_bucket_name, #find_profile, #profile, #region, #s3_bucket_name
Constructor Details
#initialize(url: nil, path: nil) ⇒ LambdaCode
Returns a new instance of LambdaCode.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 10
def initialize(url: nil, path: nil)
log "Downloading Lambda code from #{url}#{path}"
case url
when nil
@s3_url = if File.directory?(path)
URI(upload(make_filename(path.split('/').last), fetch_from_folder(path), mime_type: 'application/zip', gzip: false))
else
URI(upload(make_filename(path.split('/').last), File.open(path, "rb").read, gzip: false))
end
else
res = fetch_from_url(url)
@s3_url = URI(upload(make_filename(url.split('.').last), res.body, mime_type: res['content-type'], gzip: false))
end
log "uploaded Lambda function to #{@s3_url}"
end
|
Instance Method Details
#fetch_from_folder(path_str) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 26
def fetch_from_folder(path_str)
begin
temp_file = Tempfile.new("#{path_str.split('/').last}.zip")
Zip::ZipOutputStream.open(temp_file) { |zos| }
Zip::ZipFile.open(temp_file.path, Zip::ZipFile::CREATE) do |zipfile|
Dir[File.join(path_str, '*')].each do |file|
zipfile.add(file.sub("#{path_str}/", ''), file)
end
end
zip_data = File.read(temp_file.path)
ensure
temp_file.close
temp_file.unlink
end
zip_data
end
|
#fetch_from_url(uri_str, limit = 10) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 43
def fetch_from_url(uri_str, limit = 10)
raise ArgumentError, 'too many HTTP redirects' if limit == 0
response = Net::HTTP.get_response(URI(uri_str))
case response
when Net::HTTPSuccess then
response
when Net::HTTPRedirection then
location = response['location']
log "redirected to #{location}"
fetch_from_url(location, limit - 1)
else
raise CloudFormationTool::Errors::AppError, "Error downloading #{url}: #{response.value}"
end
end
|
58
59
60
61
62
63
|
# File 'lib/cloud_formation_tool/cloud_formation/lambda_code.rb', line 58
def to_cloudformation
{
'S3Bucket' => @s3_url.hostname.split('.').first,
'S3Key' => @s3_url.path[1..-1]
}
end
|