Module: LambdaConvert::CLI
- Defined in:
- lib/lambda_convert/cli.rb
Overview
‘convert` command line tool implementation
Class Attribute Summary collapse
-
.logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
- .aws_credentials ⇒ Object
- .delete_files(keys) ⇒ Object
- .download_file(output_key, output_file) ⇒ Object
- .invoke_lambda(input_key, input_selecting, args, output_key) ⇒ Object
- .lambda_client ⇒ Object
- .lambda_convert ⇒ Object
- .lambda_function ⇒ Object
- .lambda_region ⇒ Object
- .local_convert ⇒ Object
- .main ⇒ Object
- .s3_bucket ⇒ Object
- .s3_client ⇒ Object
- .s3_key_prefix ⇒ Object
- .s3_region ⇒ Object
- .upload_file(input_file, input_key) ⇒ Object
Class Attribute Details
.logger ⇒ Object
Returns the value of attribute logger.
12 13 14 |
# File 'lib/lambda_convert/cli.rb', line 12 def logger @logger end |
Class Method Details
.aws_credentials ⇒ Object
14 15 16 17 18 19 |
# File 'lib/lambda_convert/cli.rb', line 14 def aws_credentials Aws::Credentials.new( ENV['CONVERT_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID'], ENV['CONVERT_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_ACCESS_KEY'] ) end |
.delete_files(keys) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lambda_convert/cli.rb', line 97 def self.delete_files(keys) logger.info("Delete files #{keys} from #{s3_bucket}") s3_client.delete_objects( bucket: s3_bucket, delete: { objects: keys.map { |key| { key: key } }, quiet: true } ) end |
.download_file(output_key, output_file) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/lambda_convert/cli.rb', line 85 def self.download_file(output_key, output_file) logger.info( "Downloading file from s3://#{s3_bucket}/#{output_key} to " \ "#{output_file}" ) s3_client.get_object( response_target: output_file, bucket: s3_bucket, key: output_key ) end |
.invoke_lambda(input_key, input_selecting, args, output_key) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lambda_convert/cli.rb', line 63 def self.invoke_lambda(input_key, input_selecting, args, output_key) source = '{source}' source += "[#{input_selecting}]" unless input_selecting.nil? instruction = { schema: 'envoy-convert-instruction', original: input_key, bucket: s3_bucket, write_options: {}, key: output_key, args: [source] + args + ['{dest}'] } logger.info("Invoking lambda with instruction #{instruction}") resp = lambda_client.invoke( function_name: lambda_function, invocation_type: 'RequestResponse', payload: JSON.dump(instruction) ) logger.info("Get response of invoke #{resp}") raise 'Failed to run convert on Lambda' if resp.status_code != 200 end |
.lambda_client ⇒ Object
48 49 50 51 52 53 |
# File 'lib/lambda_convert/cli.rb', line 48 def lambda_client @aws_lambda ||= Aws::Lambda::Client.new( region: lambda_region, credentials: aws_credentials ) end |
.lambda_convert ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lambda_convert/cli.rb', line 108 def self.lambda_convert input_file, input_selecting = LambdaConvert::Utils.parse_input_path( ARGV[0] ) # Notice: there is also special output file syntax for convert command, # but we are not supporting them now, as we probably won't use it output_file = ARGV[-1] input_key = "#{s3_key_prefix}#{SecureRandom.uuid}" output_key = "#{s3_key_prefix}#{SecureRandom.uuid}" upload_file(input_file, input_key) invoke_lambda(input_key, input_selecting, ARGV[1..-2], output_key) download_file(output_key, output_file) logger.info('Done') ensure if !input_key.nil? && !output_key.nil? delete_files([input_key, output_key]) end end |
.lambda_function ⇒ Object
25 26 27 |
# File 'lib/lambda_convert/cli.rb', line 25 def lambda_function ENV['CONVERT_LAMBDA_FUNCTION'] || 'image-convert-prod' end |
.lambda_region ⇒ Object
21 22 23 |
# File 'lib/lambda_convert/cli.rb', line 21 def lambda_region ENV['CONVERT_LAMBDA_REGION'] || ENV['AWS_REGION'] end |
.local_convert ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lambda_convert/cli.rb', line 129 def self.local_convert env = ENV.to_h # find the original convert bin path original_convert = LambdaConvert::Utils.original_convert abort('No local convert found') if original_convert.nil? # we also put a CONVERT_RECURSIVE_FLAG to avoid somehow calling ourself # again by mistake env['CONVERT_RECURSIVE_FLAG'] = '1' logger.info("Running local convert #{original_convert} with args #{ARGV}") system(env, *([original_convert] + ARGV)) abort('Failed to run local convert') unless $CHILD_STATUS.success? logger.info('Done') end |
.main ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/lambda_convert/cli.rb', line 143 def self.main abort('Recursive call') if ENV['CONVERT_RECURSIVE_FLAG'] == '1' abort('Invalid arguments') if ARGV.count < 2 lambda_convert rescue StandardError => e logger.warn("Failed to convert via lambda, error=#{e}") fallback_disabled = (ENV['CONVERT_DISABLE_FALLBACK'].to_i != 0) || false if fallback_disabled abort("Failed to convert via lambda, no fallback, error=#{e}") end logger.info('Fallback to local convert command') local_convert end |
.s3_bucket ⇒ Object
33 34 35 |
# File 'lib/lambda_convert/cli.rb', line 33 def s3_bucket ENV['CONVERT_S3_BUCKET'] end |
.s3_client ⇒ Object
41 42 43 44 45 46 |
# File 'lib/lambda_convert/cli.rb', line 41 def s3_client @s3_client ||= Aws::S3::Client.new( region: s3_region, credentials: aws_credentials ) end |
.s3_key_prefix ⇒ Object
37 38 39 |
# File 'lib/lambda_convert/cli.rb', line 37 def s3_key_prefix ENV['CONVERT_S3_KEY_PREFIX'] || '_convert_tmp/' end |
.s3_region ⇒ Object
29 30 31 |
# File 'lib/lambda_convert/cli.rb', line 29 def s3_region ENV['CONVERT_S3_REGION'] || ENV['AWS_REGION'] end |
.upload_file(input_file, input_key) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/lambda_convert/cli.rb', line 56 def self.upload_file(input_file, input_key) logger.info("Uploading file to s3://#{s3_bucket}/#{input_key}") File.open(input_file, 'rb') do |file| s3_client.put_object(bucket: s3_bucket, key: input_key, body: file) end end |