cnvrg 0.3.6 → 0.3.8

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.
@@ -22,35 +22,69 @@ module Cnvrg
22
22
  cli.init_data(public)
23
23
  end
24
24
  desc "data upload", "upload data folder"
25
- method_option :ignore, :type => :array, :aliases => ["-i", "--i"], :desc => "ignore following files"
26
25
  method_option :verbose, :type => :boolean, :aliases => ["-v"], :default => false
27
- method_option :sync, :type => :boolean, :aliases => ["-s"], :default => false
28
- method_option :no_compression, :type => :boolean, :aliases => ["-nc", "--no_compression"], :default => false
26
+ method_option :new_branch, :type => :boolean, :aliases => ["-nb"], :desc => "create new branch of commits"
27
+ method_option :force, :type => :boolean, :aliases => ["-f","--force"], :default => false
28
+ method_option :sync, :type => :boolean, :aliases => ["-s","--sync"], :default => false
29
29
 
30
30
  def upload
31
31
  cli = Cnvrg::CLI.new()
32
- ignore = options["ignore"]
33
32
  verbose = options["verbose"]
34
33
  sync = options["sync"]
35
- no_compression = options["no_compression"]
36
- cli.upload_data_tar(ignore, verbose,sync,no_compression)
34
+ force = options["force"]
35
+ new_branch = options["new_branch"]
36
+ cli.upload_data_new(new_branch, verbose,sync,force)
37
+ end
38
+ desc 'sync', 'sync_data_new'
39
+ method_option :new_branch, :type => :boolean, :aliases => ["-nb"], :desc => "create new branch of commits"
40
+ method_option :force, :type => :boolean, :aliases => ["-f","--force"], :default => false
41
+ method_option :verbose, :type => :boolean, :aliases => ["-v"], :default => false
42
+ method_option :commit, :type => :string, :aliases => ["-c"], :desc => "download specified commit", :default => nil
43
+ method_option :all_files, :type => :boolean, :aliases => ["--all"], :desc => "download specified commit", :default => true
44
+
45
+ def sync_data_new()
46
+ cli = Cnvrg::CLI.new()
47
+ force = options["force"]
48
+ new_branch = options["new_branch"]
49
+ verbose = options["verbose"]
50
+ commit = options["commit"]
51
+ all_files = options["all_files"]
52
+
53
+ cli.sync_data_new(new_branch, force, verbose,commit,all_files)
37
54
  end
38
55
  desc 'data download', 'pull data'
56
+ method_option :new_branch, :type => :boolean, :aliases => ["-nb"], :desc => "create new branch of commits", :default => false
39
57
  method_option :verbose, :type => :boolean, :aliases => ["-v"], :default => false
40
58
  method_option :sync, :type => :boolean, :aliases => ["-s"], :default => false
59
+ method_option :commit, :type => :string, :aliases => ["-c"], :desc => "download specified commit", :default => nil
60
+ method_option :all_files, :type => :boolean, :aliases => ["--all"], :desc => "download specified commit", :default => false
41
61
 
42
62
  def download()
43
63
  cli = Cnvrg::CLI.new()
44
64
  verbose = options["verbose"]
45
65
  sync = options["sync"]
66
+ new_branch = options["new_branch"]
67
+ commit = options["commit"]
68
+ all_files = options["all_files"]
46
69
 
47
- cli.download_data(verbose,sync,Dir.pwd)
70
+ cli.download_data_new(verbose,sync,new_branch, commit,all_files)
48
71
 
49
72
  end
50
73
  desc 'data clone', 'clone datset'
74
+ method_option :only_tree, :type => :boolean, :aliases => ["-t", "--tree"], :default => false
75
+ method_option :commit, :type => :string, :aliases => ["-c", "--commit"], :default => nil
51
76
  def clone(dataset_url)
52
77
  cli = Cnvrg::CLI.new()
53
- cli.clone_data(dataset_url)
78
+ only_tree =options[:only_tree]
79
+ commit =options[:commit]
80
+
81
+ cli.clone_data(dataset_url, only_tree=only_tree,commit=commit)
82
+
83
+ end
84
+ desc 'data delete', 'delete datset'
85
+ def delete(dataset_slug)
86
+ cli = Cnvrg::CLI.new()
87
+ cli.delete_data(dataset_slug)
54
88
 
55
89
  end
56
90
  desc 'data list', 'list of datasets'
@@ -1,6 +1,8 @@
1
1
  require 'mimemagic'
2
2
  require 'aws-sdk'
3
3
  require 'URLcrypt'
4
+ require "down"
5
+
4
6
  module Cnvrg
5
7
  class Datafiles
6
8
 
@@ -16,7 +18,6 @@ module Cnvrg
16
18
  end
17
19
 
18
20
  def upload_file(absolute_path, relative_path, commit_sha1)
19
-
20
21
  file_name = File.basename relative_path
21
22
  file_size = File.size(absolute_path).to_f
22
23
  mime_type = MimeMagic.by_path(absolute_path)
@@ -26,22 +27,11 @@ module Cnvrg
26
27
  commit_sha1: commit_sha1, file_name: file_name,
27
28
  file_size: file_size, file_content_type: content_type, sha1: sha1})
28
29
  if Cnvrg::CLI.is_response_success(upload_resp, false)
29
- path = upload_resp["result"]["path"]
30
- if file_size.to_f>= Cnvrg::Files::LARGE_FILE.to_f
31
- s3_res = upload_large_files_s3(upload_resp, absolute_path)
32
- else
33
- s3_res = upload_small_files_s3(path, absolute_path, content_type)
34
- end
35
- if s3_res
36
- Cnvrg::API.request(@base_resource + "update_s3", 'POST', {path: path, commit_id: upload_resp["result"]["commit_id"],
37
- blob_id: upload_resp["result"]["id"]})
38
- return true
39
- end
40
- end
41
- return false
42
-
43
- end
30
+ s3_res = upload_large_files_s3(upload_resp, absolute_path)
44
31
 
32
+ return s3_res
33
+ end
34
+ end
45
35
  def upload_tar_file(absolute_path, relative_path, commit_sha1)
46
36
  begin
47
37
  file_name = File.basename relative_path
@@ -82,9 +72,9 @@ module Cnvrg
82
72
  puts e.message
83
73
  return false
84
74
  end
85
-
86
75
  end
87
76
 
77
+
88
78
  def upload_log_file(absolute_path, relative_path, log_date)
89
79
  file_name = File.basename relative_path
90
80
  file_size = File.size(absolute_path).to_f
@@ -251,82 +241,59 @@ module Cnvrg
251
241
  def upload_large_files_s3(upload_resp, file_path)
252
242
  begin
253
243
  sts_path = upload_resp["result"]["path_sts"]
254
- # s4cmd_path = upload_resp["result"]["path_s4cmd"]
255
-
256
- uri = URI.parse(sts_path)
257
- http_object = Net::HTTP.new(uri.host, uri.port)
258
- http_object.use_ssl = true if uri.scheme == 'https'
259
- request = Net::HTTP::Get.new(sts_path)
260
244
 
261
- body = ""
262
- http_object.start do |http|
263
- response = http.request request
264
- body = response.read_body
245
+ if !Helpers.is_verify_ssl
246
+ body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
247
+ else
248
+ body = open(sts_path).read
265
249
  end
266
250
  home_dir = File.expand_path('~')
267
- log_file = "#{home_dir}/.cnvrg/tmp/upload_#{File.basename(file_path)}.log"
268
-
269
251
 
270
252
  split = body.split("\n")
271
253
  key = split[0]
272
254
  iv = split[1]
255
+ is_s3 = upload_resp["result"]["is_s3"]
256
+ access = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["sts_a"])
273
257
 
274
- access = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["new_a"])
275
-
276
- secret = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["new_s"])
258
+ secret = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["sts_s"])
277
259
 
260
+ session = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["sts_st"])
278
261
  region = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["region"])
279
262
 
280
263
  bucket = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["bucket"])
281
264
 
265
+ if is_s3 or is_s3.nil?
266
+
267
+ use_accelerate_endpoint =true
268
+ client = Aws::S3::Client.new(
269
+ :access_key_id =>access,
270
+ :secret_access_key => secret,
271
+ :session_token => session,
272
+ :region => region,
273
+ :http_open_timeout => 60, :retry_limit => 20)
274
+ else
275
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["endpoint"])
276
+ use_accelerate_endpoint = false
277
+ client = Aws::S3::Client.new(
278
+ :access_key_id =>access,
279
+ :secret_access_key => secret,
280
+ :region => region,
281
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
282
+ :http_open_timeout => 60, :retry_limit => 20)
283
+ end
284
+
285
+
286
+
287
+
288
+
289
+
282
290
 
283
- client = Aws::S3::Client.new(:access_key_id => access,
284
- :secret_access_key =>secret,
285
- :region => region,
286
- :logger => Logger.new(log_file),
287
- :http_open_timeout => 60, :retry_limit => 20,
288
- :http_wire_trace => true)
289
291
  s3 = Aws::S3::Resource.new(client: client)
290
292
 
291
293
  resp = s3.bucket(bucket).
292
294
  object(upload_resp["result"]["path"]+"/"+File.basename(file_path)).
293
- upload_file(file_path, {:use_accelerate_endpoint => true,:server_side_encryption => 'AES256'})
294
- # s4cmd_path = upload_resp["result"]["path_s4cmd"]
295
- #
296
- # s4cmd_uri = URI.parse(s4cmd_path)
297
- # s4cmd_http_object = Net::HTTP.new(s4cmd_uri.host, s4cmd_uri.port)
298
- # s4cmd_http_object.use_ssl = true if s4cmd_uri.scheme == 'https'
299
- # s4cmd_request = Net::HTTP::Get.new(s4cmd_path)
300
- #
301
- # s4cmd_body = ""
302
- # s4cmd_http_object.start do |http|
303
- # response = http.request s4cmd_request
304
- # s4cmd_body = response.read_body
305
- # end
306
- # s4cmd_new_body = s4cmd_body.gsub(" self.client = self.boto3.client('s3',
307
- # aws_access_key_id=aws_access_key_id,
308
- # aws_secret_access_key=aws_secret_access_key)", " self.client = self.boto3.client('s3',
309
- # aws_access_key_id='#{ URLcrypt.decrypt(upload_resp["result"]["new_a"])}',
310
- # aws_secret_access_key='#{URLcrypt.decrypt(upload_resp["result"]["new_s"])}')")
311
- #
312
- #
313
- # tmp = Tempfile.new('s4cmd.py')
314
- # tmp << s4cmd_new_body
315
- # tmp.flush
316
- # tmp.close
317
- # #
318
- # is_success = false
319
- # count = 0
320
- # while !is_success and count <3
321
- # resp = `python #{tmp.path} --num-threads=8 --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 `
322
- # is_success =$?.success?
323
- # count +=1
324
- #
325
- # end
326
- # resp= is_success
327
- if resp == true
328
- FileUtils.rm_rf(log_file)
329
- end
295
+ upload_file(file_path, {:use_accelerate_endpoint => use_accelerate_endpoint,:server_side_encryption => 'AES256'})
296
+
330
297
 
331
298
  return resp
332
299
 
@@ -380,60 +347,82 @@ module Cnvrg
380
347
 
381
348
  def create_dir(absolute_path, relative_path, commit_sha1)
382
349
  response = Cnvrg::API.request(@base_resource + "create_dir", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
383
- puts response
384
350
  return Cnvrg::CLI.is_response_success(response, false)
385
351
  end
386
352
 
387
- def download_file_s3(absolute_path, relative_path, project_home, conflict=false, commit_sha1=nil)
353
+
354
+ def download_file_s3(absolute_path, relative_path, project_home, conflict=false, commit_sha1=nil, as_link=false)
388
355
  begin
389
- res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
356
+ res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1 ,new_version:true, as_link:as_link})
390
357
  Cnvrg::CLI.is_response_success(res, false)
391
- if res["result"]
392
- download_resp = res
393
- filename = download_resp["result"]["filename"]
394
-
395
- absolute_path += ".conflict" if conflict
396
- sts_path = download_resp["result"]["path_sts"]
397
- uri = URI.parse(sts_path)
398
- http_object = Net::HTTP.new(uri.host, uri.port)
399
- http_object.use_ssl = true if uri.scheme == 'https'
400
- request = Net::HTTP::Get.new(sts_path)
401
-
402
- body = ""
403
- http_object.start do |http|
404
- response = http.request request
405
- body = response.read_body
406
- end
407
- split = body.split("\n")
408
- key = split[0]
409
- iv = split[1]
410
-
411
- access = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])
358
+ if res["result"]
359
+ file_url = res["result"]["file_url"]
412
360
 
413
- secret = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_s"])
361
+ if as_link
362
+ return res["result"]
363
+ end
364
+ # begin
365
+ # if !Helpers.is_verify_ssl
366
+ # tempfile = Down.download(file_url,open_timeout: 60,ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE )
367
+ #
368
+ # else
369
+ # tempfile = Down.download(file_url,open_timeout: 60)
370
+ #
371
+ # end
372
+ #
373
+ # FileUtils.move(tempfile.path, project_home+"/"+ absolute_path)
374
+ # return true
375
+ # rescue
376
+ #
377
+ # end
378
+ download_resp = res
379
+ filename = download_resp["result"]["filename"]
380
+
381
+ absolute_path += ".conflict" if conflict
382
+ sts_path = download_resp["result"]["path_sts"]
383
+ if !Helpers.is_verify_ssl
384
+ body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
385
+ else
386
+ body = open(sts_path).read
387
+ end
388
+ split = body.split("\n")
389
+ key = split[0]
390
+ iv = split[1]
414
391
 
415
- session = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_st"])
416
- region = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])
392
+ access = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])
417
393
 
418
- bucket = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
419
- key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])
394
+ secret = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_s"])
420
395
 
396
+ session = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_st"])
397
+ region = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])
421
398
 
399
+ bucket = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
400
+ file_key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])
422
401
 
423
- client = Aws::S3::Client.new(
424
- :access_key_id =>access,
425
- :secret_access_key => secret,
426
- :session_token => session,
427
- :region => region,
428
- :http_open_timeout => 60, :retry_limit => 20
429
- )
402
+ is_s3 = download_resp["result"]["is_s3"]
403
+ if is_s3 or is_s3.nil?
404
+ client = Aws::S3::Client.new(
405
+ :access_key_id =>access,
406
+ :secret_access_key => secret,
407
+ :session_token => session,
408
+ :region => region,
409
+ :http_open_timeout => 60, :retry_limit => 20)
410
+ else
411
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["endpoint_url"])
412
+ client = Aws::S3::Client.new(
413
+ :access_key_id =>access,
414
+ :secret_access_key => secret,
415
+ :region => region,
416
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
417
+ :http_open_timeout => 60, :retry_limit => 20)
418
+ end
430
419
 
431
- File.open(project_home+"/"+absolute_path, 'w+') do |file|
432
- resp = client.get_object({bucket:bucket,
433
- key:key}, target: file)
420
+ File.open(project_home+"/"+absolute_path, 'w+') do |file|
421
+ resp = client.get_object({bucket:bucket,
422
+ key:file_key}, target: file)
423
+ end
424
+ return true
434
425
  end
435
- return true
436
- end
437
426
 
438
427
  rescue => e
439
428
  return false
@@ -450,15 +439,10 @@ module Cnvrg
450
439
  filename = download_resp["result"]["filename"]
451
440
 
452
441
  sts_path = download_resp["result"]["path_sts"]
453
- uri = URI.parse(sts_path)
454
- http_object = Net::HTTP.new(uri.host, uri.port)
455
- http_object.use_ssl = true if uri.scheme == 'https'
456
- request = Net::HTTP::Get.new(sts_path)
457
-
458
- body = ""
459
- http_object.start do |http|
460
- response = http.request request
461
- body = response.read_body
442
+ if !Helpers.is_verify_ssl
443
+ body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
444
+ else
445
+ body = open(sts_path).read
462
446
  end
463
447
  split = body.split("\n")
464
448
  key = split[0]
@@ -473,21 +457,30 @@ module Cnvrg
473
457
  region = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])
474
458
 
475
459
  bucket = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
476
- key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])
477
-
478
-
479
- client = Aws::S3::Client.new(
480
- :access_key_id =>access,
481
- :secret_access_key => secret,
482
- :session_token => session,
483
- :region => region,
484
- :http_open_timeout => 60, :retry_limit => 20
485
- )
460
+ file_key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])
461
+
462
+ is_s3 = download_resp["result"]["is_s3"]
463
+ if is_s3 or is_s3.nil?
464
+ client = Aws::S3::Client.new(
465
+ :access_key_id =>access,
466
+ :secret_access_key => secret,
467
+ :session_token => session,
468
+ :region => region,
469
+ :http_open_timeout => 60, :retry_limit => 20)
470
+ else
471
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["endpoint_url"])
472
+ client = Aws::S3::Client.new(
473
+ :access_key_id =>access,
474
+ :secret_access_key => secret,
475
+ :region => region,
476
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
477
+ :http_open_timeout => 60, :retry_limit => 20)
478
+ end
486
479
 
487
480
 
488
481
  File.open(dataset_home+"/"+filename, 'w+') do |file|
489
482
  resp = client.get_object({bucket: bucket,
490
- key: key }, target: file)
483
+ key: file_key }, target: file)
491
484
  end
492
485
  return filename
493
486
  end
@@ -518,6 +511,17 @@ module Cnvrg
518
511
  end
519
512
  return true
520
513
  end
514
+ def delete_commit_files_local(deleted)
515
+ begin
516
+ FileUtils.rm_rf(deleted) unless (deleted.nil? or deleted.empty?)
517
+ return true
518
+ rescue => e
519
+ return false
520
+ end
521
+
522
+ return true
523
+
524
+ end
521
525
 
522
526
  def download_dir(dataset_home, absolute_path)
523
527
  FileUtils.mkdir_p("#{dataset_home}/#{absolute_path}")
@@ -551,18 +555,37 @@ module Cnvrg
551
555
  return true
552
556
 
553
557
  end
554
-
555
- def start_commit(new_branch)
556
-
557
- response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {dataset_slug: @dataset_slug, new_branch: false,
558
- username: @owner})
559
- Cnvrg::CLI.is_response_success(response, false)
558
+ def delete_commit(commit_sha1)
559
+ response = Cnvrg::API.request("#{base_resource}/commit/#{commit_sha1}", 'DELETE')
560
+ Cnvrg::CLI.is_response_success(response, true)
560
561
  return response
561
562
  end
562
-
563
- def end_commit(commit_sha1)
564
- response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1})
563
+ def get_commit(commit_sha1)
564
+ response = Cnvrg::API.request("#{base_resource}/commit/#{commit_sha1}", 'GET')
565
+ Cnvrg::CLI.is_response_success(response, true)
566
+ return response
567
+ end
568
+ def start_commit(new_branch,force=false,delete_commit=nil)
569
+ begin
570
+ response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {dataset_slug: @dataset_slug, new_branch: new_branch,force:force,
571
+ username: @owner,delete_commit:delete_commit})
572
+ Cnvrg::CLI.is_response_success(response, true)
565
573
  return response
574
+ rescue => e
575
+ return false
576
+ end
577
+
578
+ end
579
+
580
+ def end_commit(commit_sha1,force)
581
+ begin
582
+ response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1,force:force})
583
+ Cnvrg::CLI.is_response_success(response, true)
584
+ return response
585
+ rescue => e
586
+ return false
587
+ end
588
+
566
589
  end
567
590
 
568
591
  def end_commit_tar(commit_sha1, cur_idx)