Module: LambdaConvert::CLI

Extended by:
Forwardable
Defined in:
lib/lambda_convert/cli.rb

Overview

‘convert` command line tool implementation

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/lambda_convert/cli.rb', line 15

def logger
  @logger
end

Class Method Details

.aws_credentialsObject



17
18
19
20
21
# File 'lib/lambda_convert/cli.rb', line 17

def aws_credentials
  if config.access_key && config.secret_key
    Aws::Credentials.new(config.access_key, config.secret_key)
  end
end

.delete_files(keys) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/lambda_convert/cli.rb', line 79

def self.delete_files(keys)
  logger.info("Delete files #{keys} from #{config.s3_bucket}")
  s3_client.delete_objects(
    bucket: config.s3_bucket,
    delete: {
      objects: keys.map { |key| { key: key } },
      quiet: true
    }
  )
end

.download_file(output_key, output_file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lambda_convert/cli.rb', line 67

def self.download_file(output_key, output_file)
  logger.info(
    "Downloading file from s3://#{config.s3_bucket}/#{output_key} to " \
    "#{output_file}"
  )
  s3_client.get_object(
    response_target: output_file,
    bucket: config.s3_bucket,
    key: output_key
  )
end

.invoke_lambda(input_key, input_selecting, args, output_key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lambda_convert/cli.rb', line 45

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: config.s3_bucket,
    write_options: {},
    key: output_key,
    args: [source] + args + ['{dest}']
  }
  logger.info("Invoking lambda with instruction #{instruction}")

  resp = lambda_client.invoke(
    function_name: config.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_clientObject



30
31
32
33
34
35
# File 'lib/lambda_convert/cli.rb', line 30

def lambda_client
  @aws_lambda ||= Aws::Lambda::Client.new(
    region: config.lambda_region,
    credentials: aws_credentials
  )
end

.lambda_convertObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lambda_convert/cli.rb', line 90

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 = File.join config.s3_key_prefix.to_s, SecureRandom.uuid
  output_key = File.join config.s3_key_prefix.to_s, 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

.local_convertObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lambda_convert/cli.rb', line 111

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

.mainObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lambda_convert/cli.rb', line 125

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_clientObject



23
24
25
26
27
28
# File 'lib/lambda_convert/cli.rb', line 23

def s3_client
  @s3_client ||= Aws::S3::Client.new(
    region: config.s3_region,
    credentials: aws_credentials
  )
end

.upload_file(input_file, input_key) ⇒ Object



38
39
40
41
42
43
# File 'lib/lambda_convert/cli.rb', line 38

def self.upload_file(input_file, input_key)
  logger.info("Uploading file to s3://#{config.s3_bucket}/#{input_key}")
  File.open(input_file, 'rb') do |file|
    s3_client.put_object(bucket: config.s3_bucket, key: input_key, body: file)
  end
end