lambda_convert 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 825e8ca26b989d26a26b063d735e7da3db1fd181
4
+ data.tar.gz: 26be24f32c9170cd23931ebfd2790e822f4bd750
5
+ SHA512:
6
+ metadata.gz: 4e9aa2767b1080468a3ca599e97090004a3e22133dce311534f85187b0baad546feb5d2a5afff57e24091753d31a0a96c1b173dbfb6ea834cf4e233633730c92
7
+ data.tar.gz: fa48bfa41584f0ba13ddc0d6494bc69984c25cb61bdd4756a621cdfdb64df6f232b2bba0a98a852ab1e5dcd4545688bd836c6e202d1f91c4bb803d8f7c779b12
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Envoy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # lambda-convert
2
+ AWS Lambda powered drop-in replacement for ImageMagick convert command line tool
data/bin/convert ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'lambda_convert/cli'
5
+
6
+ main
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,125 @@
1
+ require 'logger'
2
+ require 'rubygems'
3
+ require 'English'
4
+
5
+ require 'aws-sdk'
6
+
7
+ def parse_input_path(path)
8
+ # convert command input path could be attached with selecting syntax, let's
9
+ # parse it and return them in an array of [filename, selecting syntax]
10
+ # ref: https://www.imagemagick.org/script/command-line-processing.php
11
+ match = /([^\[\]]+)(\[(.+)\])?/.match(path)
12
+ [match[1], match[3]]
13
+ end
14
+
15
+ def lambda_convert
16
+ logger = Logger.new(STDERR)
17
+
18
+ s3_region = ENV['CONVERT_S3_REGION'] || ENV['AWS_REGION']
19
+ lambda_region = ENV['CONVERT_LAMBDA_REGION'] || ENV['AWS_REGION']
20
+ aws_credentials = Aws::Credentials.new(
21
+ ENV['CONVERT_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID'],
22
+ ENV['CONVERT_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_ACCESS_KEY']
23
+ )
24
+ s3_bucket = ENV['CONVERT_S3_BUCKET']
25
+ s3_key_prefix = ENV['CONVERT_S3_KEY_PREFIX'] || '_convert_tmp/'
26
+ lambda_function = ENV['CONVERT_LAMBDA_FUNCTION'] || 'image-convert-prod'
27
+
28
+ s3 = Aws::S3::Client.new(
29
+ region: s3_region,
30
+ credentials: aws_credentials
31
+ )
32
+ aws_lambda = Aws::Lambda::Client.new(
33
+ region: lambda_region,
34
+ credentials: aws_credentials
35
+ )
36
+
37
+ input_file, input_selecting = parse_input_path(ARGV[0])
38
+ # Notice: there is also special output file syntax for convert command, but
39
+ # we are not supporting them now, as we probably won't use it
40
+ output_file = ARGV[-1]
41
+
42
+ input_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
43
+ output_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
44
+
45
+ logger.info("Uploading file to s3://#{s3_bucket}/#{input_key}")
46
+ File.open(input_file, 'rb') do |file|
47
+ s3.put_object(bucket: s3_bucket, key: input_key, body: file)
48
+ end
49
+ source = '{source}'
50
+ source += "[#{input_selecting}]" unless input_selecting.nil?
51
+ instruction = {
52
+ schema: 'envoy-convert-instruction',
53
+ original: input_key,
54
+ bucket: s3_bucket,
55
+ write_options: {
56
+ acl: 'private'
57
+ },
58
+ key: output_key,
59
+ args: [source] + ARGV[1..-2] + ['{dest}']
60
+ }
61
+ logger.info("Invoking lambda with instruction #{instruction}")
62
+
63
+ resp = aws_lambda.invoke(
64
+ function_name: lambda_function,
65
+ invocation_type: 'RequestResponse',
66
+ payload: JSON.dump(instruction)
67
+ )
68
+ logger.info("Get response of invoke #{resp}")
69
+ raise 'Failed to run convert on Lambda' if resp.status_code != 200
70
+
71
+ logger.info(
72
+ "Downloading file from s3://#{s3_bucket}/#{output_key} to #{output_file}"
73
+ )
74
+ s3.get_object(
75
+ response_target: output_file,
76
+ bucket: s3_bucket,
77
+ key: output_key
78
+ )
79
+ logger.info('Done')
80
+ ensure
81
+ logger.info("Delete files #{input_key} and #{output_key} from #{s3_bucket}")
82
+ s3.delete_objects(
83
+ bucket: s3_bucket,
84
+ delete: {
85
+ objects: [
86
+ {
87
+ key: input_key
88
+ },
89
+ {
90
+ key: output_key
91
+ }
92
+ ],
93
+ quiet: true
94
+ }
95
+ )
96
+ end
97
+
98
+ def local_convert
99
+ logger = Logger.new(STDERR)
100
+ env = ENV.to_h
101
+ # remove Gem bindir from the path, so that we won't invoke ourself
102
+ path = ENV['PATH'].split(File::PATH_SEPARATOR) - [Gem.bindir]
103
+ env['PATH'] = path.join(File::PATH_SEPARATOR)
104
+ # we also put a CONVERT_RECURSIVE_FLAG to avoid somehow calling ourself again
105
+ # by mistake
106
+ env['CONVERT_RECURSIVE_FLAG'] = '1'
107
+ logger.info("Running local convert with args #{ARGV}")
108
+ system(env, *(['convert'] + ARGV))
109
+ abort('Failed to run local convert') unless $CHILD_STATUS.success?
110
+ logger.info('Done')
111
+ end
112
+
113
+ def main
114
+ abort('Recursive call') if ENV['CONVERT_RECURSIVE_FLAG'] == '1'
115
+ lambda_convert
116
+ rescue StandardError => e
117
+ logger = Logger.new(STDERR)
118
+ logger.warn("Failed to convert via lambda, error=#{e}")
119
+ fallback_disabled = (ENV['CONVERT_DISABLE_FALLBACK'].to_i != 0) || false
120
+ if fallback_disabled
121
+ abort("Failed to convert via lambda, no fallback, error=#{e}")
122
+ end
123
+ logger.info('Fallback to local convert command')
124
+ local_convert
125
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lambda_convert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fang-Pen Lin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ description:
28
+ email: fang@envoy.com
29
+ executables:
30
+ - convert
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - "./lib/.DS_Store"
35
+ - "./lib/lambda_convert/cli.rb"
36
+ - LICENSE
37
+ - README.md
38
+ - bin/convert
39
+ homepage:
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.5.1
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: AWS Lambda powered drop-in replacement for ImageMagick convert command line
63
+ tool
64
+ test_files: []
65
+ has_rdoc: