lambda_convert 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25c44e1fb6fa07cb8e1aaf5654100b73721aa0c6
4
- data.tar.gz: ec7ba833db940fa8dea581cdcd37265e1cea6142
3
+ metadata.gz: ec904ee282cb8ab57051dc9c2add258f690bfb8a
4
+ data.tar.gz: 0b795afa9b435d58c2f36b8c7b0c86ed68044f6d
5
5
  SHA512:
6
- metadata.gz: 12347aac344bf05dd0e0ca8add187610d50d5adf81b962f0136a04c989c5a669db5e78f0ecd075ff5087f32c261c764783714c20304297407c6bfff7a1353aaf
7
- data.tar.gz: fa4d46a0d15d393f455e20fcd45a2cd68c9ff4585052bb59727550cce5741be24c9be1f02f2f0cadd8abcbc8bbef8387adf46eb6904c59da8074d4a015e0eb5a
6
+ metadata.gz: fe5a60653a5ad932860fbbb59fd88ecbddfe9ddf7ace5d3ec985a645dc0ae8262c3baa8d75c7492a7ba33fcd6437bffc22ed9fcfae3ab76d45330c7eecf2b5e9
7
+ data.tar.gz: 9f2f3bc73241b227a786e44ea30e8584593d62198a745badb343d6d6a465cc68fc916de448e9d532fdca92afa5963e4f8f04f791708e7bbd48baa775b98fcfff
data/bin/convert CHANGED
@@ -1,6 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
  $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
3
 
4
- require 'lambda_convert/cli'
4
+ require 'logger'
5
+ require 'lambda_convert'
5
6
 
6
- main
7
+ debug_log = ENV['CONVERT_DEBUG_LOG']
8
+ if debug_log.nil?
9
+ LambdaConvert::CLI.logger = Logger.new(STDERR)
10
+ else
11
+ file = File.open(debug_log, File::WRONLY | File::APPEND | File::CREAT)
12
+ LambdaConvert::CLI.logger = Logger.new(file)
13
+ end
14
+ LambdaConvert::CLI.main
@@ -0,0 +1,2 @@
1
+ require 'lambda_convert/cli'
2
+ require 'lambda_convert/utils'
@@ -1,137 +1,127 @@
1
- require 'logger'
2
1
  require 'rubygems'
2
+ require 'json'
3
3
  require 'English'
4
4
 
5
5
  require 'aws-sdk'
6
6
 
7
- # find command path array matching given `cmd` name in $PATH
8
- def find_cmd(cmd)
9
- (ENV['PATH'].split(File::PATH_SEPARATOR).map do |path|
10
- cmd_path = File.join(path, cmd)
11
- cmd_path if File.executable?(cmd_path) && !File.directory?(cmd_path)
12
- end).compact
13
- end
14
-
15
- def parse_input_path(path)
16
- # convert command input path could be attached with selecting syntax, let's
17
- # parse it and return them in an array of [filename, selecting syntax]
18
- # ref: https://www.imagemagick.org/script/command-line-processing.php
19
- match = /([^\[\]]+)(\[(.+)\])?/.match(path)
20
- [match[1], match[3]]
21
- end
7
+ module LambdaConvert
8
+ # `convert` command line tool implementation
9
+ module CLI
10
+ class <<self
11
+ attr_accessor :logger
12
+ end
22
13
 
23
- def lambda_convert
24
- logger = Logger.new(STDERR)
14
+ def self.lambda_convert
15
+ s3_region = ENV['CONVERT_S3_REGION'] || ENV['AWS_REGION']
16
+ lambda_region = ENV['CONVERT_LAMBDA_REGION'] || ENV['AWS_REGION']
17
+ aws_credentials = Aws::Credentials.new(
18
+ ENV['CONVERT_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID'],
19
+ ENV['CONVERT_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_ACCESS_KEY']
20
+ )
21
+ s3_bucket = ENV['CONVERT_S3_BUCKET']
22
+ s3_key_prefix = ENV['CONVERT_S3_KEY_PREFIX'] || '_convert_tmp/'
23
+ lambda_function = ENV['CONVERT_LAMBDA_FUNCTION'] || 'image-convert-prod'
25
24
 
26
- s3_region = ENV['CONVERT_S3_REGION'] || ENV['AWS_REGION']
27
- lambda_region = ENV['CONVERT_LAMBDA_REGION'] || ENV['AWS_REGION']
28
- aws_credentials = Aws::Credentials.new(
29
- ENV['CONVERT_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID'],
30
- ENV['CONVERT_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_ACCESS_KEY']
31
- )
32
- s3_bucket = ENV['CONVERT_S3_BUCKET']
33
- s3_key_prefix = ENV['CONVERT_S3_KEY_PREFIX'] || '_convert_tmp/'
34
- lambda_function = ENV['CONVERT_LAMBDA_FUNCTION'] || 'image-convert-prod'
25
+ s3 = Aws::S3::Client.new(
26
+ region: s3_region,
27
+ credentials: aws_credentials
28
+ )
29
+ aws_lambda = Aws::Lambda::Client.new(
30
+ region: lambda_region,
31
+ credentials: aws_credentials
32
+ )
35
33
 
36
- s3 = Aws::S3::Client.new(
37
- region: s3_region,
38
- credentials: aws_credentials
39
- )
40
- aws_lambda = Aws::Lambda::Client.new(
41
- region: lambda_region,
42
- credentials: aws_credentials
43
- )
34
+ input_file, input_selecting = LambdaConvert::Utils.parse_input_path(
35
+ ARGV[0]
36
+ )
37
+ # Notice: there is also special output file syntax for convert command,
38
+ # but we are not supporting them now, as we probably won't use it
39
+ output_file = ARGV[-1]
44
40
 
45
- input_file, input_selecting = parse_input_path(ARGV[0])
46
- # Notice: there is also special output file syntax for convert command, but
47
- # we are not supporting them now, as we probably won't use it
48
- output_file = ARGV[-1]
41
+ input_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
42
+ output_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
49
43
 
50
- input_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
51
- output_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
52
-
53
- logger.info("Uploading file to s3://#{s3_bucket}/#{input_key}")
54
- File.open(input_file, 'rb') do |file|
55
- s3.put_object(bucket: s3_bucket, key: input_key, body: file)
56
- end
57
- source = '{source}'
58
- source += "[#{input_selecting}]" unless input_selecting.nil?
59
- instruction = {
60
- schema: 'envoy-convert-instruction',
61
- original: input_key,
62
- bucket: s3_bucket,
63
- write_options: {
64
- acl: 'private'
65
- },
66
- key: output_key,
67
- args: [source] + ARGV[1..-2] + ['{dest}']
68
- }
69
- logger.info("Invoking lambda with instruction #{instruction}")
44
+ logger.info("Uploading file to s3://#{s3_bucket}/#{input_key}")
45
+ File.open(input_file, 'rb') do |file|
46
+ s3.put_object(bucket: s3_bucket, key: input_key, body: file)
47
+ end
48
+ source = '{source}'
49
+ source += "[#{input_selecting}]" unless input_selecting.nil?
50
+ instruction = {
51
+ schema: 'envoy-convert-instruction',
52
+ original: input_key,
53
+ bucket: s3_bucket,
54
+ write_options: {
55
+ acl: 'private'
56
+ },
57
+ key: output_key,
58
+ args: [source] + ARGV[1..-2] + ['{dest}']
59
+ }
60
+ logger.info("Invoking lambda with instruction #{instruction}")
70
61
 
71
- resp = aws_lambda.invoke(
72
- function_name: lambda_function,
73
- invocation_type: 'RequestResponse',
74
- payload: JSON.dump(instruction)
75
- )
76
- logger.info("Get response of invoke #{resp}")
77
- raise 'Failed to run convert on Lambda' if resp.status_code != 200
62
+ resp = aws_lambda.invoke(
63
+ function_name: lambda_function,
64
+ invocation_type: 'RequestResponse',
65
+ payload: JSON.dump(instruction)
66
+ )
67
+ logger.info("Get response of invoke #{resp}")
68
+ raise 'Failed to run convert on Lambda' if resp.status_code != 200
78
69
 
79
- logger.info(
80
- "Downloading file from s3://#{s3_bucket}/#{output_key} to #{output_file}"
81
- )
82
- s3.get_object(
83
- response_target: output_file,
84
- bucket: s3_bucket,
85
- key: output_key
86
- )
87
- logger.info('Done')
88
- ensure
89
- logger.info("Delete files #{input_key} and #{output_key} from #{s3_bucket}")
90
- s3.delete_objects(
91
- bucket: s3_bucket,
92
- delete: {
93
- objects: [
94
- {
95
- key: input_key
96
- },
97
- {
98
- key: output_key
70
+ logger.info(
71
+ "Downloading file from s3://#{s3_bucket}/#{output_key} to " \
72
+ "#{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(
82
+ "Delete files #{input_key} and #{output_key} from #{s3_bucket}"
83
+ )
84
+ s3.delete_objects(
85
+ bucket: s3_bucket,
86
+ delete: {
87
+ objects: [
88
+ {
89
+ key: input_key
90
+ },
91
+ {
92
+ key: output_key
93
+ }
94
+ ],
95
+ quiet: true
99
96
  }
100
- ],
101
- quiet: true
102
- }
103
- )
104
- end
97
+ )
98
+ end
105
99
 
106
- def local_convert
107
- logger = Logger.new(STDERR)
100
+ def self.local_convert
101
+ env = ENV.to_h
102
+ # find the original convert bin path
103
+ original_convert = LambdaConvert::Utils.original_convert
104
+ # we also put a CONVERT_RECURSIVE_FLAG to avoid somehow calling ourself
105
+ # again by mistake
106
+ env['CONVERT_RECURSIVE_FLAG'] = '1'
107
+ logger.info("Running local convert #{original_convert} with args #{ARGV}")
108
+ system(env, *([original_convert] + ARGV))
109
+ abort('Failed to run local convert') unless $CHILD_STATUS.success?
110
+ logger.info('Done')
111
+ end
108
112
 
109
- env = ENV.to_h
110
- # find the original convert bin path
111
- original_convert = find_cmd('convert').find do |path|
112
- # TODO: maybe we need a more robust way to determine whether is given
113
- # convert path from us or someone else
114
- File.dirname(path) != Gem.bindir && !path.include?('.rbenv/shims')
115
- end
116
- # we also put a CONVERT_RECURSIVE_FLAG to avoid somehow calling ourself again
117
- # by mistake
118
- env['CONVERT_RECURSIVE_FLAG'] = '1'
119
- logger.info("Running local convert #{original_convert} with args #{ARGV}")
120
- system(env, *([original_convert] + ARGV))
121
- abort('Failed to run local convert') unless $CHILD_STATUS.success?
122
- logger.info('Done')
123
- end
113
+ def self.main
114
+ abort('Recursive call') if ENV['CONVERT_RECURSIVE_FLAG'] == '1'
115
+ lambda_convert
116
+ rescue StandardError => e
124
117
 
125
- def main
126
- abort('Recursive call') if ENV['CONVERT_RECURSIVE_FLAG'] == '1'
127
- lambda_convert
128
- rescue StandardError => e
129
- logger = Logger.new(STDERR)
130
- logger.warn("Failed to convert via lambda, error=#{e}")
131
- fallback_disabled = (ENV['CONVERT_DISABLE_FALLBACK'].to_i != 0) || false
132
- if fallback_disabled
133
- abort("Failed to convert via lambda, no fallback, error=#{e}")
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
134
126
  end
135
- logger.info('Fallback to local convert command')
136
- local_convert
137
127
  end
@@ -0,0 +1,31 @@
1
+ module LambdaConvert
2
+ # Utils functions
3
+ module Utils
4
+ # find command path array matching given `cmd` name in $PATH
5
+ def self.find_cmd(cmd)
6
+ (ENV['PATH'].split(File::PATH_SEPARATOR).map do |path|
7
+ cmd_path = File.join(path, cmd)
8
+ cmd_path if File.executable?(cmd_path) && !File.directory?(cmd_path)
9
+ end).compact
10
+ end
11
+
12
+ def self.original_convert
13
+ find_cmd('convert').find do |path|
14
+ # TODO: maybe we need a more robust way to determine whether is given
15
+ # convert path from us or someone else
16
+ File.dirname(path) != Gem.bindir && !path.include?('.rbenv/shims')
17
+ end
18
+ end
19
+
20
+ def self.parse_input_path(path)
21
+ # convert command input path could be attached with selecting syntax,
22
+ # let's parse it and return them in an array of
23
+ #
24
+ # [filename, selecting syntax]
25
+ #
26
+ # ref: https://www.imagemagick.org/script/command-line-processing.php
27
+ match = /([^\[\]]+)(\[(.+)\])?/.match(path)
28
+ [match[1], match[3]]
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lambda_convert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fang-Pen Lin
@@ -32,7 +32,9 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - "./lib/.DS_Store"
35
+ - "./lib/lambda_convert.rb"
35
36
  - "./lib/lambda_convert/cli.rb"
37
+ - "./lib/lambda_convert/utils.rb"
36
38
  - LICENSE
37
39
  - README.md
38
40
  - bin/convert