Method: Cnvrg::Files#download_cnvrg_image

Defined in:
lib/cnvrg/files.rb

#download_cnvrg_image(image_name, secret) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/cnvrg/files.rb', line 205

def download_cnvrg_image(image_name, secret)
  res =Cnvrg::API.request("images/#{image_name}/" + "download", 'POST', {secret:secret},true)
  Cnvrg::CLI.is_response_success(res, true)
  if res["result"]
    download_resp = res
    sts_path = download_resp["result"]["path_sts"]
    uri = URI.parse(sts_path)
    http_object = Net::HTTP.new(uri.host, uri.port)
    http_object.use_ssl = true if uri.scheme == 'https'
    request = Net::HTTP::Get.new(sts_path)
    body = ""
    http_object.start do |http|
      response = http.request request
      body = response.read_body
    end
    split = body.split("\n")
    key = split[0]
    iv = split[1]

    access =  Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])

    secret =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_s"])

    session =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_st"])
    region =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])

    bucket =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
    key =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])

    client = Aws::S3::Client.new(
        :access_key_id =>access,
        :secret_access_key => secret,
        :session_token => session,
        :region => region,
        :http_open_timeout => 60, :retry_limit => 20
    )

    File.open("/tmp/#{image_name}.tar", 'w+') do |file|
      resp = client.get_object({bucket:bucket,
                                key:key}, target: file)
    end
    return true
  end

rescue => e
  puts e
  return false


end