Class: Cnvrg::Datafiles

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/datafiles.rb

Constant Summary collapse

LARGE_FILE =
1024*1024*5
MULTIPART_SPLIT =
10000000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, dataset_slug) ⇒ Datafiles

Returns a new instance of Datafiles.



13
14
15
16
17
# File 'lib/cnvrg/datafiles.rb', line 13

def initialize(owner, dataset_slug)
  @dataset_slug = dataset_slug
  @owner = owner
  @base_resource = "users/#{owner}/datasets/#{dataset_slug}/"
end

Instance Attribute Details

#base_resourceObject (readonly)

Returns the value of attribute base_resource.



11
12
13
# File 'lib/cnvrg/datafiles.rb', line 11

def base_resource
  @base_resource
end

Instance Method Details

#create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object



354
355
356
357
# File 'lib/cnvrg/datafiles.rb', line 354

def create_dir(absolute_path, relative_path, commit_sha1)
  response = Cnvrg::API.request(@base_resource + "create_dir", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
  return Cnvrg::CLI.is_response_success(response, false)
end

#delete_dir(absolute_path, relative_path, commit_sha1) ⇒ Object



349
350
351
352
# File 'lib/cnvrg/datafiles.rb', line 349

def delete_dir(absolute_path, relative_path, commit_sha1)
  response = Cnvrg::API.request(@base_resource + "delete_dir", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
  return Cnvrg::CLI.is_response_success(response, false)
end

#delete_file(absolute_path, relative_path, commit_sha1) ⇒ Object



344
345
346
347
# File 'lib/cnvrg/datafiles.rb', line 344

def delete_file(absolute_path, relative_path, commit_sha1)
  response = Cnvrg::API.request(@base_resource + "delete_file", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
  return Cnvrg::CLI.is_response_success(response, false)
end

#download_data_file(commit_sha1, dataset_home) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/cnvrg/datafiles.rb', line 397

def download_data_file(commit_sha1,dataset_home)
  begin
    res = Cnvrg::API.request(@base_resource + "download_data_file", 'POST', {commit_sha1:commit_sha1})
    Cnvrg::CLI.is_response_success(res, false)
    if res["result"]
      download_resp = res
      filename = download_resp["result"]["filename"]

      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
      URLcrypt::key = [body].pack('H*')
      s3 = Aws::S3::Client.new(
          :access_key_id => URLcrypt.decrypt(download_resp["result"]["sts_a"]),
          :secret_access_key => URLcrypt.decrypt(download_resp["result"]["sts_s"]),
          :session_token => URLcrypt.decrypt(download_resp["result"]["sts_st"]),
          :region => URLcrypt.decrypt(download_resp["result"]["region"]))

      File.open(dataset_home+"/"+filename, 'wb') do |file|
        resp = s3.get_object({ bucket:URLcrypt.decrypt(download_resp["result"]["bucket"]),
                               key:URLcrypt.decrypt(download_resp["result"]["key"])}, target: file)
      end
      return filename
    end

  rescue =>e
    return false

  end
end

#download_dir(dataset_home, absolute_path) ⇒ Object



457
458
459
# File 'lib/cnvrg/datafiles.rb', line 457

def download_dir(dataset_home, absolute_path)
  FileUtils.mkdir_p("#{dataset_home}/#{absolute_path}")
end

#download_file(absolute_path, relative_path, project_home, conflict = false) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/cnvrg/datafiles.rb', line 436

def download_file(absolute_path, relative_path, project_home, conflict=false)
  res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path})
  Cnvrg::CLI.is_response_success(res, false)
  if res["result"]
    res = res["result"]
    return false if res["link"].empty? or res["filename"].empty?
    filename = res["filename"]
    file_location = absolute_path.gsub(/#{filename}\/?$/, "")

    FileUtils.mkdir_p project_home + "/" + file_location
    filename += ".conflict" if conflict

    File.open("#{project_home}/#{file_location}/#{filename}", "wb") do |file|
      file.write open(res["link"]).read
    end
  else
    return false
  end
  return true
end

#download_file_s3(absolute_path, relative_path, project_home, conflict = false, commit_sha1 = nil) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/cnvrg/datafiles.rb', line 358

def download_file_s3(absolute_path, relative_path, project_home, conflict=false,commit_sha1=nil)
  begin
    res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path,commit_sha1:commit_sha1})
    Cnvrg::CLI.is_response_success(res, false)
    if res["result"]
      download_resp = res
      filename = download_resp["result"]["filename"]

      absolute_path += ".conflict" if conflict
      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
    URLcrypt::key = [body].pack('H*')
    s3 = Aws::S3::Client.new(
        :access_key_id => URLcrypt.decrypt(download_resp["result"]["sts_a"]),
        :secret_access_key => URLcrypt.decrypt(download_resp["result"]["sts_s"]),
        :session_token => URLcrypt.decrypt(download_resp["result"]["sts_st"]),
        :region => URLcrypt.decrypt(download_resp["result"]["region"]))
    File.open(project_home+"/"+absolute_path, 'wb') do |file|
      resp = s3.get_object({ bucket:URLcrypt.decrypt(download_resp["result"]["bucket"]),
                             key:URLcrypt.decrypt(download_resp["result"]["key"])}, target: file)
    end
      return true
    end

  rescue =>e
    return false

  end
end

#download_image(file_path_to_store, image_slug, owner) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cnvrg/datafiles.rb', line 202

def download_image(file_path_to_store, image_slug, owner)


  download_resp = Cnvrg::API.request("users/#{owner}/images/#{image_slug}/" + "download", 'GET')
  path = download_resp["result"]["path"]

  if Cnvrg::CLI.is_response_success(download_resp, false)
    begin
      open(file_path_to_store, 'wb') do |file|
        file << open(path).read
      end

      return true
    rescue => e
      return false
    end

    return true
  else
    return false
  end


end

#end_commit(commit_sha1) ⇒ Object



484
485
486
487
# File 'lib/cnvrg/datafiles.rb', line 484

def end_commit(commit_sha1)
  response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1})
  return response
end

#end_commit_tar(commit_sha1, cur_idx) ⇒ Object



488
489
490
491
# File 'lib/cnvrg/datafiles.rb', line 488

def end_commit_tar(commit_sha1,cur_idx)
  response = Cnvrg::API.request("#{base_resource}/commit/end_tar", 'POST', {commit_sha1: commit_sha1,idx: cur_idx})
  return response
end

#revoke_download_dir(absolute_path) ⇒ Object



460
461
462
# File 'lib/cnvrg/datafiles.rb', line 460

def revoke_download_dir(absolute_path)
  puts FileUtils.rmtree("#{absolute_path}")
end

#revoke_download_file(absolute_path, filename, conflict = false) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
# File 'lib/cnvrg/datafiles.rb', line 464

def revoke_download_file(absolute_path,filename,conflict=false)
  begin
  file_location = absolute_path.gsub(/#{filename}\/?$/, "")

  filename += ".conflict" if conflict
  FileUtils.remove("#{file_location}/#{filename}")
    return true
  rescue
   return false
  end
end

#rollback_commit(commit_sha1) ⇒ Object



493
494
495
496
# File 'lib/cnvrg/datafiles.rb', line 493

def rollback_commit(commit_sha1)
  response = Cnvrg::API.request("#{base_resource}/commit/rollback", 'POST', {commit_sha1: commit_sha1})
  Cnvrg::CLI.is_response_success(response, false)
end

#start_commit(new_branch) ⇒ Object



476
477
478
479
480
481
482
# File 'lib/cnvrg/datafiles.rb', line 476

def start_commit(new_branch)

  response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {dataset_slug: @dataset_slug,new_branch:false,
                                                                          username: @owner})
  Cnvrg::CLI.is_response_success(response)
  return response
end

#upload_cnvrg_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, message) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cnvrg/datafiles.rb', line 161

def upload_cnvrg_image(absolute_path, image_name, owner, is_public, is_base,dpkg,libraries,bash,message)
  file_name = File.basename absolute_path
  file_size = File.size(absolute_path).to_f
  if is_base

    content_type = "application/zip"
  else
    content_type = "application/gzip"
  end
  begin
    upload_resp = Cnvrg::API.request("users/#{owner}/images/" + "upload_cnvrg", 'POST_FILE', {relative_path: absolute_path,
                                                                                        file_name: file_name,
                                                                                        image_name: image_name,
                                                                                        file_size: file_size,
                                                                                        file_content_type: content_type,
                                                                                        is_public: is_public,
                                                                                        dpkg: dpkg,
                                                                                        libraries: libraries,
                                                                                        bash_history: bash,
                                                                                        commit_message:message,
                                                                                        is_base: is_base})
    # puts upload_resp
    if Cnvrg::CLI.is_response_success(upload_resp, false)
      path = upload_resp["result"]["path"]
      s3_res = upload_small_files_s3(path, absolute_path, content_type)
      if s3_res
        commit_resp = Cnvrg::API.request("users/#{owner}/images/#{upload_resp["result"]["id"]}/" + "commit", 'GET')
        if Cnvrg::CLI.is_response_success(commit_resp, false)
          return commit_resp["result"]["image"]
        else
          return false
        end

      end
    end
    return false
  rescue =>e
  end

end

#upload_exec_file(absolute_path, image_name, commit_id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cnvrg/datafiles.rb', line 82

def upload_exec_file(absolute_path,image_name,commit_id)
    file_name = File.basename absolute_path
    file_size = File.size(absolute_path).to_f
    content_type = "application/zip"
    begin
      upload_resp = Cnvrg::API.request("users/#{@owner}/images/" + "upload_config", 'POST_FILE', {relative_path: absolute_path,
                                                                                                  file_name: file_name,
                                                                                                  image_name:image_name,
                                                                                                  file_size: file_size,
                                                                                                  file_content_type: content_type,
                                                                                                  project_slug: @project_slug,
                                                                                                  commit_id:commit_id})
      # puts upload_resp
      if Cnvrg::CLI.is_response_success(upload_resp, false)
        if upload_resp["result"]["image"] == -1
          return -1
        end
        path = upload_resp["result"]["path"]
        s3_res = upload_small_files_s3(path, absolute_path, content_type)

      end
    if s3_res
      return upload_resp["result"]["id"]
    end
    return false
  rescue SignalException

    say "\nAborting"
    exit(1)
  end

end

#upload_file(absolute_path, relative_path, commit_sha1) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cnvrg/datafiles.rb', line 19

def upload_file(absolute_path, relative_path, commit_sha1)
  file_name = File.basename relative_path
  file_size = File.size(absolute_path).to_f
  mime_type = MimeMagic.by_path(absolute_path)
  content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
  sha1 = Digest::SHA1.file(absolute_path).hexdigest

  upload_resp = Cnvrg::API.request(@base_resource + "upload_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
                                                                                 commit_sha1: commit_sha1, file_name: file_name,
                                                                                 file_size: file_size, file_content_type: content_type,sha1:sha1})
  if Cnvrg::CLI.is_response_success(upload_resp, false)
    path = upload_resp["result"]["path"]
    if file_size.to_f>= Cnvrg::Files::LARGE_FILE.to_f
      s3_res = upload_large_files_s3(upload_resp, absolute_path)
    else
      s3_res = upload_small_files_s3(path, absolute_path, content_type)
    end
    if s3_res
      Cnvrg::API.request(@base_resource + "update_s3", 'POST', {path: path, commit_id: upload_resp["result"]["commit_id"],
                                                                blob_id: upload_resp["result"]["id"]})
      return true
    end
  end
  return false
end

#upload_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, message, commit_id) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cnvrg/datafiles.rb', line 117

def upload_image(absolute_path, image_name, owner, is_public, is_base,dpkg,libraries,bash,message,commit_id)
  file_name = File.basename absolute_path
  file_size = File.size(absolute_path).to_f
  if is_base

    content_type = "application/zip"
  else
    content_type = "application/gzip"
  end
  begin
  upload_resp = Cnvrg::API.request("users/#{owner}/images/" + "upload_cnvrg", 'POST_FILE', {relative_path: absolute_path,
                                                                                      file_name: file_name,
                                                                                      image_name: image_name,
                                                                                      file_size: file_size,
                                                                                      file_content_type: content_type,
                                                                                      is_public: is_public,
                                                                                      project_slug: @project_slug,
                                                                                      commit_id:commit_id ,
                                                                                      dpkg: dpkg,
                                                                                      py2: libraries,
                                                                                      py3: libraries,

                                                                                      bash_history: bash,
                                                                                      commit_message:message,
                                                                                      is_base: is_base})
  # puts upload_resp
  if Cnvrg::CLI.is_response_success(upload_resp, false)
    path = upload_resp["result"]["path"]
    s3_res = upload_small_files_s3(path, absolute_path, content_type)
    if s3_res
      commit_resp = Cnvrg::API.request("users/#{owner}/images/#{upload_resp["result"]["id"]}/" + "commit", 'GET')
      if Cnvrg::CLI.is_response_success(commit_resp, false)
        return commit_resp["result"]["image"]
      else
        return false
      end

    end
  end
  return false
  rescue =>e
  end

end

#upload_large_files_s3(upload_resp, file_path) ⇒ Object



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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/cnvrg/datafiles.rb', line 227

def   upload_large_files_s3(upload_resp, file_path)
  begin
    sts_path = upload_resp["result"]["path_sts"]
    s4cmd_path = upload_resp["result"]["path_s4cmd"]

    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

    URLcrypt::key = [body].pack('H*')

    python_version=`python --version  > /dev/null 2>&1` ;  is_python=$?.success?
    if is_python

      s4cmd=`pip freeze |grep s4cmd > /dev/null 2>&1` ;  s4cmd_suc=$?.success?
      if !s4cmd_suc
        `pip install s4cmd  > /dev/null 2>&1`
      end

    end

    if !is_python
      s3 = Aws::S3::Resource.new(
          :access_key_id => URLcrypt.decrypt(upload_resp["result"]["sts_a"]),
          :secret_access_key => URLcrypt.decrypt(upload_resp["result"]["sts_s"]),
          :session_token => URLcrypt.decrypt(upload_resp["result"]["sts_st"]),
          :region => URLcrypt.decrypt(upload_resp["result"]["region"]))
      resp = s3.bucket(URLcrypt.decrypt(upload_resp["result"]["bucket"])).
          object(upload_resp["result"]["path"]+"/"+File.basename(file_path)).
          upload_file(file_path,{:use_accelerate_endpoint=>true})

    else

      s4cmd_uri = URI.parse(s4cmd_path)
      s4cmd_http_object = Net::HTTP.new(s4cmd_uri.host, s4cmd_uri.port)
      s4cmd_http_object.use_ssl = true if s4cmd_uri.scheme == 'https'
      s4cmd_request = Net::HTTP::Get.new(s4cmd_path)

      s4cmd_body = ""
      s4cmd_http_object.start do |http|
        response = http.request s4cmd_request
        s4cmd_body = response.read_body
      end

      s4cmd_new_body = s4cmd_body.gsub(" self.client = self.boto3.client('s3',
                                  aws_access_key_id=aws_access_key_id,
                                  aws_secret_access_key=aws_secret_access_key)"," self.client = self.boto3.client('s3',
                                  aws_access_key_id='#{ URLcrypt.decrypt(upload_resp["result"]["sts_a"])}',
                                  aws_secret_access_key='#{URLcrypt.decrypt(upload_resp["result"]["sts_s"])}',
                                  aws_session_token='#{URLcrypt.decrypt(upload_resp["result"]["sts_st"])}')")

      tmp = Tempfile.new('s4cmd.py')
      tmp << s4cmd_new_body
      tmp.flush
      tmp.close

      is_success = false
      count = 0
      while !is_success and count <3
        resp = `python #{tmp.path} --num-threads=128 --max-singlepart-upload-size=#{MULTIPART_SPLIT} put -f #{file_path} s3://#{URLcrypt.decrypt(upload_resp["result"]["bucket"])}/#{upload_resp["result"]["path"]+"/"+File.basename(file_path)}  > /dev/null 2>&1`
        is_success =$?.success?
        count +=1

      end
      resp= is_success

    end

    return resp

  rescue =>e
    if File.exist? tmp
      FileUtils.rm_rf [tmp]
    end
    return false

  end
  return true

end

#upload_log_file(absolute_path, relative_path, log_date) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cnvrg/datafiles.rb', line 65

def upload_log_file(absolute_path, relative_path,log_date)
  file_name = File.basename relative_path
  file_size = File.size(absolute_path).to_f
  content_type = "text/x-log"
  upload_resp = Cnvrg::API.request("/users/#{@owner}/" + "upload_cli_log", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
                                                                                 file_name: file_name,log_date:log_date,
                                                                                 file_size: file_size, file_content_type: content_type})
  if Cnvrg::CLI.is_response_success(upload_resp, false)
      path = upload_resp["result"]["path"]
      s3_res = upload_small_files_s3(path, absolute_path,  "text/plain")
  end
  if s3_res
    return true
  end
  return false

end

#upload_small_files_s3(url_path, file_path, content_type) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/cnvrg/datafiles.rb', line 316

def upload_small_files_s3(url_path, file_path, content_type)
  url = URI.parse(url_path)
  file = File.open(file_path, "rb")
  body = file.read
  begin
    Net::HTTP.start(url.host) do |http|
      http.send_request("PUT", url.request_uri, body, {
          "content-type" => content_type,
      })
    end
    return true
  rescue Interrupt
    return false
  rescue
    return false
  end
end

#upload_tar_file(absolute_path, relative_path, commit_sha1) ⇒ Object



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

def upload_tar_file(absolute_path, relative_path, commit_sha1)
  file_name = File.basename relative_path
  file_size = File.size(absolute_path).to_f
  mime_type = MimeMagic.by_path(absolute_path)
  content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
  sha1 = Digest::SHA1.file(absolute_path).hexdigest

  upload_resp = Cnvrg::API.request(@base_resource + "upload_tar_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
                                                                                 commit_sha1: commit_sha1, file_name: file_name,
                                                                                 file_size: file_size, file_content_type: content_type,sha1:sha1})
  if Cnvrg::CLI.is_response_success(upload_resp, false)
    path = upload_resp["result"]["path"]
    s3_res = upload_large_files_s3(upload_resp, absolute_path)
    if s3_res
      Cnvrg::API.request(@base_resource + "update_s3", 'POST', {path: path, commit_id: upload_resp["result"]["commit_id"],
                                                                blob_id: upload_resp["result"]["id"]})
      return true
    end
  end
  return false
end

#upload_url(file_path) ⇒ Object



334
335
336
337
338
339
340
341
342
# File 'lib/cnvrg/datafiles.rb', line 334

def upload_url(file_path)
  response = Cnvrg::API.request(@base_resource + "upload_url", 'POST', {file_s3_path: file_path})
  if Cnvrg::CLI.is_response_success(response, false)
    return response
  else
    return nil
  end

end