lambda_convert 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/convert +10 -2
- data/lib/lambda_convert.rb +2 -0
- data/lib/lambda_convert/cli.rb +109 -119
- data/lib/lambda_convert/utils.rb +31 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec904ee282cb8ab57051dc9c2add258f690bfb8a
|
4
|
+
data.tar.gz: 0b795afa9b435d58c2f36b8c7b0c86ed68044f6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 '
|
4
|
+
require 'logger'
|
5
|
+
require 'lambda_convert'
|
5
6
|
|
6
|
-
|
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
|
data/lib/lambda_convert/cli.rb
CHANGED
@@ -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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
46
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
102
|
-
}
|
103
|
-
)
|
104
|
-
end
|
97
|
+
)
|
98
|
+
end
|
105
99
|
|
106
|
-
def local_convert
|
107
|
-
|
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
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
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
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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.
|
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
|