Class: Cnvrg::Files
- Inherits:
-
Object
- Object
- Cnvrg::Files
- Defined in:
- lib/cnvrg/files.rb
Constant Summary collapse
- ParallelThreads =
Cnvrg::Helpers.parallel_threads
- VALID_FILE_NAME =
/[\x00\\\*\?\"<>\|]/
- LARGE_FILE =
1024*1024*5
- MULTIPART_SPLIT =
10000000
Instance Attribute Summary collapse
-
#base_resource ⇒ Object
readonly
Returns the value of attribute base_resource.
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_sha1(files_list) ⇒ Object
- #create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
- #create_progressbar(length = 10, title = 'Progress') ⇒ Object
- #delete(file) ⇒ Object
- #delete_commit_files_local(deleted) ⇒ Object
- #delete_conflict(file) ⇒ Object
- #delete_dir(relative_path, commit_sha1) ⇒ Object
- #delete_file(relative_path, commit_sha1) ⇒ Object
- #delete_files_from_server(files, commit_sha1, suppress_exceptions: false) ⇒ Object
- #delete_files_from_server_old(files, commit_sha1) ⇒ Object
- #delete_files_local(deleted, conflicted: [], progress: nil) ⇒ Object
- #download_and_read(path) ⇒ Object
- #download_cnvrg_image(image_name, secret) ⇒ Object
- #download_commit(sha1) ⇒ Object
- #download_dir(absolute_path, relative_path, project_home) ⇒ Object
- #download_file(file_path: '', key: '', iv: '', bucket: '', path: '', client: nil) ⇒ Object
- #download_file_s3(relative_path, commit_sha1 = nil, postfix: '') ⇒ Object
- #download_files(files, commit, postfix: '', progress: nil, threads: 15) ⇒ Object
- #download_files_old(files, commit, postfix: '', progress: nil) ⇒ Object
- #download_image(file_path_to_store, image_slug, owner) ⇒ Object
- #download_multpile_files_s3(files, project_home, postfix: '', progress: nil, threads: 15) ⇒ Object
- #end_commit(commit_sha1, force: false, message: "") ⇒ Object
- #get_upload_options(number_of_items: 0, progress: false) ⇒ Object
- #handle_compare_idx(compared, resolver: {}) ⇒ Object
-
#initialize(owner, project_slug, project_home: '', project: nil, progressbar: nil, cli: nil, options: {}) ⇒ Files
constructor
A new instance of Files.
- #parse_file(file) ⇒ Object
- #refresh_storage_token ⇒ Object
- #resolve_bucket(response) ⇒ Object
- #revoke_clone(project_home) ⇒ Object
- #revoke_download(conflicted_changes, downloaded_changes) ⇒ Object
- #revoke_download_dir(absolute_path, relative_path, project_home) ⇒ Object
- #revoke_download_file(project_home, absolute_path, filename, conflict = false) ⇒ Object
- #rollback_commit(commit_sha1) ⇒ Object
- #show_file_s3(relative_path, commit_sha1 = nil) ⇒ Object
- #start_commit(new_branch, force: false, exp_start_commit: nil, job_slug: nil, job_type: nil, start_commit: nil, message: nil, debug_mode: false) ⇒ Object
- #upload_cnvrg_image(absolute_path, image_name, secret) ⇒ Object
- #upload_exec_file(absolute_path, image_name, commit_id) ⇒ Object
- #upload_file(absolute_path, relative_path, commit_sha1) ⇒ Object
- #upload_files_old(files_list, commit_sha1, progress: nil) ⇒ Object
- #upload_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, message, commit_id) ⇒ Object
- #upload_large_files_s3(upload_resp, file_path) ⇒ Object
- #upload_log_file(absolute_path, relative_path, log_date) ⇒ Object
- #upload_multiple_files(files_list, commit_sha1, progress: nil, suppress_exceptions: false, chunk_size: 100) ⇒ Object
- #upload_old(absolute_path, relative_path, commit_sha1) ⇒ Object
- #upload_single_file(file) ⇒ Object
- #upload_small_files_s3(url_path, file_path, content_type) ⇒ Object
- #upload_url(file_path) ⇒ Object
Constructor Details
#initialize(owner, project_slug, project_home: '', project: nil, progressbar: nil, cli: nil, options: {}) ⇒ Files
Returns a new instance of Files.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cnvrg/files.rb', line 16 def initialize(owner, project_slug, project_home: '', project: nil, progressbar: nil, cli: nil, options: {}) @project_slug = project_slug @owner = owner @base_resource = "users/#{owner}/projects/#{project_slug}/" @project_home = project_home.presence || Cnvrg::CLI.get_project_home @project = project @client = nil if @project.present? @client = @project.get_storage_client end @progressbar = @custom_progess = false @cli = cli @options = @token_issue_time = Time.current end |
Instance Attribute Details
#base_resource ⇒ Object (readonly)
Returns the value of attribute base_resource.
14 15 16 |
# File 'lib/cnvrg/files.rb', line 14 def base_resource @base_resource end |
Class Method Details
.valid_file_name?(fullpath) ⇒ Boolean
41 42 43 |
# File 'lib/cnvrg/files.rb', line 41 def self.valid_file_name?(fullpath) VALID_FILE_NAME.match(fullpath).blank? end |
Instance Method Details
#calculate_sha1(files_list) ⇒ Object
622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/cnvrg/files.rb', line 622 def calculate_sha1(files_list) files_list = files_list.map{|file| "#{@project_home}/#{file}"} files_list = files_list.select{|file| !file.ends_with? '/'} #TODO: parallel files_list.map do |file| next [file, nil] unless File.exists? file next [file, nil] if File.directory? file sha1 = OpenSSL::Digest::SHA1.file(file).hexdigest [file.gsub("#{@project_home}/", ""), sha1] end.to_h end |
#create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
616 617 618 619 |
# File 'lib/cnvrg/files.rb', line 616 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 |
#create_progressbar(length = 10, title = 'Progress') ⇒ Object
715 716 717 718 719 720 721 722 723 724 |
# File 'lib/cnvrg/files.rb', line 715 def (length = 10, title = 'Progress') @progressbar = ProgressBar.create(:title => title, :progress_mark => '=', :format => "%b>>%i| %p%% %t", :starting_at => 0, :total => length, :autofinish => true) @custom_progess = true @progressbar end |
#delete(file) ⇒ Object
968 969 970 971 972 |
# File 'lib/cnvrg/files.rb', line 968 def delete(file) file = "#{@project_home}/#{file}" unless File.exists? file return unless File.exists? file FileUtils.rm_rf(file) end |
#delete_commit_files_local(deleted) ⇒ Object
932 933 934 935 936 937 938 939 |
# File 'lib/cnvrg/files.rb', line 932 def delete_commit_files_local(deleted) begin FileUtils.rm_rf(deleted) unless (deleted.nil? or deleted.empty?) return true rescue => e return false end end |
#delete_conflict(file) ⇒ Object
974 975 976 977 978 |
# File 'lib/cnvrg/files.rb', line 974 def delete_conflict(file) file = "#{@project_home}/#{file}" unless File.exists? file return unless File.exists? file File.rename(file, "#{file}.deleted") end |
#delete_dir(relative_path, commit_sha1) ⇒ Object
611 612 613 614 |
# File 'lib/cnvrg/files.rb', line 611 def delete_dir(relative_path, commit_sha1) response = Cnvrg::API.request(@base_resource + "delete_dir", 'DELETE', {relative_path: relative_path, commit_sha1: commit_sha1}) return Cnvrg::CLI.is_response_success(response, false) end |
#delete_file(relative_path, commit_sha1) ⇒ Object
606 607 608 609 |
# File 'lib/cnvrg/files.rb', line 606 def delete_file(relative_path, commit_sha1) response = Cnvrg::API.request(@base_resource + "delete_file", 'DELETE', {relative_path: relative_path, commit_sha1: commit_sha1}) return Cnvrg::CLI.is_response_success(response, false) end |
#delete_files_from_server(files, commit_sha1, suppress_exceptions: false) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/cnvrg/files.rb', line 168 def delete_files_from_server(files, commit_sha1, suppress_exceptions: false) #files are absolute path files here. ^^ if Cnvrg::Helpers.server_version < 1 return self.delete_files_from_server_old(files, commit_sha1) end #convert files to relative path files = files.map{|file| file.gsub(/^#{@project_home + "/"}/, "")} return if files.blank? resp = Cnvrg::API.request(@base_resource + "delete_files", 'DELETE', {files: files, commit: commit_sha1}) unless Cnvrg::CLI.is_response_success(resp, false) raise SignalException.new("Cant delete the following files from the server.") unless suppress_exceptions Cnvrg::Logger.("Cant delete the following files from the server: ") Cnvrg::Logger.(files.to_s) end rescue => e Cnvrg::Logger.("An exception raised in delete_files_from_server: ") Cnvrg::Logger.log_error(e) raise e unless suppress_exceptions end |
#delete_files_from_server_old(files, commit_sha1) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cnvrg/files.rb', line 155 def delete_files_from_server_old(files, commit_sha1) #files are absolute path here. files.each do |file| if file.ends_with? '/' #dir self.delete_dir(file, commit_sha1) else #file self.delete_file(file, commit_sha1) end end end |
#delete_files_local(deleted, conflicted: [], progress: nil) ⇒ Object
747 748 749 750 751 |
# File 'lib/cnvrg/files.rb', line 747 def delete_files_local(deleted, conflicted: [], progress: nil) deleted -= conflicted deleted.each{|file| self.delete(file); progress.progress += 1 if progress.present?} conflicted.each{|file| self.delete_conflict(file); progress.progress += 1 if progress.present?} end |
#download_and_read(path) ⇒ Object
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/cnvrg/files.rb', line 473 def download_and_read(path) body = nil retries = 0 success= false while !success and retries < 20 begin if !Helpers.is_verify_ssl body = open(path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read else body = open(path).read end success = true rescue => e retries +=1 sleep(1) end end body end |
#download_cnvrg_image(image_name, secret) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 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 |
# File 'lib/cnvrg/files.rb', line 379 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 Cnvrg::Logger.log_error(e) return false end |
#download_commit(sha1) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cnvrg/files.rb', line 46 def download_commit(sha1) response = @project.clone(false, sha1) log_error("Cant download commit #{sha1}") unless Cnvrg::CLI.is_response_success response, false commit_sha1 = response["result"]["commit"] files = response["result"]["tree"].keys log_progress("Downloading #{files.size} Files") idx = {commit: commit_sha1, tree: response["result"]["tree"]} @progressbar ||= (files.size, "Download Progress") download_files(files, commit_sha1, progress: @progressbar) @progressbar.finish if @custom_progess Project.verify_cnvrgignore_exist(@project_slug, false) @project.set_idx(idx) log("Done") log("Downloaded #{files.size} files") end |
#download_dir(absolute_path, relative_path, project_home) ⇒ Object
890 891 892 |
# File 'lib/cnvrg/files.rb', line 890 def download_dir(absolute_path, relative_path, project_home) FileUtils.mkdir_p("#{project_home}/#{absolute_path}") end |
#download_file(file_path: '', key: '', iv: '', bucket: '', path: '', client: nil) ⇒ Object
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
# File 'lib/cnvrg/files.rb', line 815 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(relative_path, commit_sha1 = nil, postfix: '') ⇒ Object
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/cnvrg/files.rb', line 635 def download_file_s3(relative_path, commit_sha1=nil, postfix: '') begin res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {relative_path: relative_path, commit_sha1: commit_sha1,new_version:true}) 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"] retries = 0 success= false while !success and retries < 20 begin if !Helpers.is_verify_ssl body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read else body = open(sts_path).read end success = true rescue => e retries +=1 sleep(5) end end if !success puts "error in sts" return false 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"]) file_key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"]) is_s3 = download_resp["result"]["is_s3"] if is_s3 or is_s3.nil? 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) else endpoint = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["endpoint"]) client = Aws::S3::Client.new( :access_key_id =>access, :secret_access_key => secret, :region => region, :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false, :http_open_timeout => 60, :retry_limit => 20) end absolute_path = "#{@project_home}/#{relative_path}#{postfix}" File.open(absolute_path, 'w+') do |file| resp = client.get_object({bucket:bucket, key:file_key}, target: file) end return true end rescue => e puts "error in aws" puts e. return false end end |
#download_files(files, commit, postfix: '', progress: nil, threads: 15) ⇒ Object
726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/cnvrg/files.rb', line 726 def download_files(files, commit, postfix: '', progress: nil, threads: 15) return if files.blank? if Cnvrg::Helpers.server_version < 1 Cnvrg::Logger.log_info("Download files from older server.") return self.download_files_old(files, commit, progress: progress, postfix: postfix) end res = Cnvrg::API.request(@base_resource + "download_files", 'POST', {files: files, commit: commit}) unless Cnvrg::CLI.is_response_success(res, false) raise SignalException.new("Cant download files from the server.") end self.download_multpile_files_s3(res['result'], @project_home, postfix: postfix, progress: progress, threads: threads) end |
#download_files_old(files, commit, postfix: '', progress: nil) ⇒ Object
740 741 742 743 744 745 |
# File 'lib/cnvrg/files.rb', line 740 def download_files_old(files, commit, postfix: '', progress: nil) files.each do |file| self.download_file_s3(file, commit, postfix: postfix) progress.progress += 1 if progress.present? end end |
#download_image(file_path_to_store, image_slug, owner) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/cnvrg/files.rb', line 355 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 |
#download_multpile_files_s3(files, project_home, postfix: '', progress: nil, threads: 15) ⇒ Object
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/cnvrg/files.rb', line 753 def download_multpile_files_s3(files, project_home, postfix: '', progress: nil, threads: 15) begin props = {} client = props[:client] iv = props[:iv] key = props[:key] bucket = props[:bucket] download_succ_count = 0 = { in_threads: threads, isolation: true } token_mutex = Mutex.new Parallel.map(files["keys"], ) do |f| token_mutex.synchronize { refresh_storage_token } file_path = f["name"] if file_path.end_with? "/" # dir if download_dir(file_path, file_path, project_home) download_succ_count += 1 else return Cnvrg::Result.new(false,"Could not create directory: #{file_path}") raise Parallel::Kill end else file_path += postfix # blob begin if not File.exists?(project_home+"/"+File.dirname(file_path)) FileUtils.makedirs(project_home+"/"+File.dirname(file_path)) end local_path = project_home+"/"+file_path storage_path = f["path"] @client.download(storage_path, local_path) progress.progress += 1 if progress.present? download_succ_count += 1 rescue => e return Cnvrg::Result.new(false,"Could not create file: #{file_path}", e., e.backtrace) raise Parallel::Kill end end end if download_succ_count == files["keys"].size return Cnvrg::Result.new(true,"Done.\nDownloaded #{download_succ_count} files") end rescue => e return Cnvrg::Result.new(false,"Could not download some files", e., e.backtrace) end end |
#end_commit(commit_sha1, force: false, message: "") ⇒ Object
958 959 960 961 |
# File 'lib/cnvrg/files.rb', line 958 def end_commit(commit_sha1,force:false,message:"") response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1,force:force,message:}) return response end |
#get_upload_options(number_of_items: 0, progress: false) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cnvrg/files.rb', line 62 def (number_of_items: 0, progress: false) = { in_processes: Cnvrg::CLI::ParallelProcesses, in_thread: Cnvrg::CLI::ParallelThreads, isolation: true } if progress [:progress] = { :title => "Upload Progress", :progress_mark => '=', :format => "%b>>%i| %p%% %t", :starting_at => 0, :total => number_of_items, :autofinish => true } end end |
#handle_compare_idx(compared, resolver: {}) ⇒ Object
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 |
# File 'lib/cnvrg/files.rb', line 980 def handle_compare_idx(compared, resolver: {}) begin all_files = compared.values.flatten.uniq props = {} files = resolver['keys'].map{|f| [f['name'], f]}.to_h client = props[:client] iv = props[:iv] key = props[:key] bucket = props[:bucket] = { :progress => { :title => "Jump Progress", :progress_mark => '=', :format => "%b>>%i| %p%% %t", :starting_at => 0, :total => all_files.size, :autofinish => true }, in_processes: Cnvrg::CLI::ParallelProcesses, in_thread: Cnvrg::CLI::ParallelThreads } Parallel.map(all_files, ) do |file| Cnvrg::CLI.("Trying #{file}") if compared['conflicts'].include? file self.download_file(file_path: "#{file}.conflict", key: key, iv: iv, bucket: bucket, path: files[file]['path'], client: client) next end if compared['updated_on_server'].include? file self.download_file(file_path: file, key: key, iv: iv, bucket: bucket, path: files[file]['path'], client: client) next end if compared['deleted'].include? file self.delete(file) next end Cnvrg::CLI.("Failed #{file}") end rescue => e Cnvrg::Logger.log_error(e) raise SignalException.new("Cant upload files") end end |
#parse_file(file) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/cnvrg/files.rb', line 194 def parse_file(file) abs_path = "#{@project_home}/#{file}" return {relative_path: file, absolute_path: abs_path} if file.ends_with? '/' file_name = File.basename(file) file_size = File.size abs_path mime_type = MimeMagic.by_path(abs_path) content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain" sha1 = OpenSSL::Digest::SHA1.file(abs_path).hexdigest {relative_path: file, absolute_path: abs_path, file_name: file_name, file_size: file_size, content_type: content_type, sha1: sha1} rescue => e return false end |
#refresh_storage_token ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/cnvrg/files.rb', line 33 def refresh_storage_token current_time = Time.current if current_time - @token_issue_time > 3.hours @client = @project.get_storage_client @token_issue_time = Time.current end end |
#resolve_bucket(response) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/cnvrg/files.rb', line 428 def resolve_bucket(response) begin sts_path = response["path_sts"] sts_body = self.download_and_read(sts_path) split = sts_body.split("\n") key = split[0] iv = split[1] access = Cnvrg::Helpers.decrypt(key, iv, response["sts_a"]) secret = Cnvrg::Helpers.decrypt(key, iv, response["sts_s"]) session = Cnvrg::Helpers.decrypt(key, iv, response["sts_st"]) region = Cnvrg::Helpers.decrypt(key, iv, response["region"]) bucket = Cnvrg::Helpers.decrypt(key, iv, response["bucket"]) Cnvrg::Logger.log_info("Resolving bucket #{bucket}, region: #{region}") is_s3 = response["is_s3"] if is_s3 or is_s3.nil? client = Aws::S3::Client.new( :access_key_id => access, :secret_access_key => secret, :session_token => session, :region => region, :use_accelerate_endpoint => true, :http_open_timeout => 60, :retry_limit => 20) else endpoint = Cnvrg::Helpers.decrypt(key, iv, response["endpoint"]) client = Aws::S3::Client.new( :access_key_id => access, :secret_access_key => secret, :region => region, :endpoint => endpoint, :force_path_style => true, :ssl_verify_peer => false, :use_accelerate_endpoint => false, :server_side_encryption => 'AES256', :http_open_timeout => 60, :retry_limit => 20) end s3 = Aws::S3::Resource.new(client: client) s3.bucket(bucket) rescue => e Cnvrg::Logger.log_error(e) Cnvrg::Logger.log_method(bind: binding) end end |
#revoke_clone(project_home) ⇒ Object
924 925 926 927 928 929 930 |
# File 'lib/cnvrg/files.rb', line 924 def revoke_clone(project_home) begin FileUtils.rm_rf(project_home) rescue end end |
#revoke_download(conflicted_changes, downloaded_changes) ⇒ Object
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 |
# File 'lib/cnvrg/files.rb', line 909 def revoke_download(conflicted_changes,downloaded_changes) begin if !conflicted_changes.nil? and !conflicted_changes.empty? conflicted_changes.each do |c| # FileUtils.rm_rf(c+".conflict") end end # FileUtils.rm_rf(downloaded_changes) unless (downloaded_changes.nil? or downloaded_changes.empty?) rescue => e return false end return true end |
#revoke_download_dir(absolute_path, relative_path, project_home) ⇒ Object
894 895 896 |
# File 'lib/cnvrg/files.rb', line 894 def revoke_download_dir(absolute_path, relative_path, project_home) puts FileUtils.rmtree("#{absolute_path}") end |
#revoke_download_file(project_home, absolute_path, filename, conflict = false) ⇒ Object
898 899 900 901 902 903 904 905 906 907 908 |
# File 'lib/cnvrg/files.rb', line 898 def revoke_download_file(project_home, 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
1022 1023 1024 1025 |
# File 'lib/cnvrg/files.rb', line 1022 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 |
#show_file_s3(relative_path, commit_sha1 = nil) ⇒ Object
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 |
# File 'lib/cnvrg/files.rb', line 836 def show_file_s3(relative_path, commit_sha1=nil) begin res = Cnvrg::API.request(@base_resource + "download_file", 'POST', { absolute_path: '', relative_path: relative_path, commit_sha1: commit_sha1, new_version:true }) 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 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 ) resp = client.get_object({bucket:bucket, key:key}) return resp.body.string end rescue => e puts e return false end end |
#start_commit(new_branch, force: false, exp_start_commit: nil, job_slug: nil, job_type: nil, start_commit: nil, message: nil, debug_mode: false) ⇒ Object
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 |
# File 'lib/cnvrg/files.rb', line 941 def start_commit(new_branch,force:false, exp_start_commit:nil, job_slug: nil, job_type: nil, start_commit: nil, message: nil, debug_mode: false) response = Cnvrg::API.request( "#{base_resource}/commit/start", 'POST', { project_slug: @project_slug, username: @owner, new_branch: new_branch, force:force, exp_start_commit:exp_start_commit, start_commit: start_commit, job_slug: job_slug, job_type: job_type, message: , debug_mode: debug_mode } ) Cnvrg::CLI.is_response_success(response,false) return response end |
#upload_cnvrg_image(absolute_path, image_name, secret) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/cnvrg/files.rb', line 325 def upload_cnvrg_image(absolute_path, image_name,secret) file_name = File.basename absolute_path file_size = File.size(absolute_path).to_f content_type = MimeMagic.by_path(absolute_path) begin upload_resp = Cnvrg::API.request("images/#{image_name}/upload", 'POST_FILE', {relative_path: absolute_path, file_name: file_name, file_size: file_size, file_content_type: content_type, secret:secret }) # puts upload_resp 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 return true else return false end end rescue => e return false end return false end |
#upload_exec_file(absolute_path, image_name, commit_id) ⇒ Object
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 |
# File 'lib/cnvrg/files.rb', line 247 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
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/cnvrg/files.rb', line 216 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 = OpenSSL::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, new_version:true,only_large:true}) if Cnvrg::CLI.is_response_success(upload_resp, false) s3_res = upload_large_files_s3(upload_resp, absolute_path) return s3_res end return false end |
#upload_files_old(files_list, commit_sha1, progress: nil) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/cnvrg/files.rb', line 81 def upload_files_old(files_list, commit_sha1, progress: nil) # Parallel.map(files_list) do |file| files_list.each do |file| Cnvrg::Helpers.try_until_success{self.upload_old("#{@project_home}/#{file}", file, commit_sha1)} progress.progress += 1 end end |
#upload_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, message, commit_id) ⇒ Object
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 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/cnvrg/files.rb', line 281 def upload_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, , 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: , is_base: is_base}) # puts upload_resp if Cnvrg::CLI.is_response_success(upload_resp, false) s3_res = upload_large_files_s3(upload_resp, absolute_path) 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
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/cnvrg/files.rb', line 493 def upload_large_files_s3(upload_resp, file_path) begin return true if upload_resp['result']['already_exists'].present? sts_path = upload_resp["result"]["path_sts"] retries = 0 success= false while !success and retries < 20 begin if !Helpers.is_verify_ssl body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read else body = open(sts_path).read end success = true rescue => e retries +=1 sleep(5) end end if !success return false end split = body.split("\n") key = split[0] iv = split[1] access = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["sts_a"]) secret = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["sts_s"]) session = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["sts_st"]) region = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["region"]) bucket = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["bucket"]) is_s3 = upload_resp["result"]["is_s3"] server_side_encryption =upload_resp["result"]["server_side_encryption"] use_accelerate_endpoint = false if is_s3 or is_s3.nil? use_accelerate_endpoint =true 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) else endpoint = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["endpoint"]) client = Aws::S3::Client.new( :access_key_id =>access, :secret_access_key => secret, :region => region, :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false, :http_open_timeout => 60, :retry_limit => 20) end if !server_side_encryption = {:use_accelerate_endpoint => use_accelerate_endpoint} else = {:use_accelerate_endpoint => use_accelerate_endpoint, :server_side_encryption => server_side_encryption} end s3 = Aws::S3::Resource.new(client: client) resp = s3.bucket(bucket). object(upload_resp["result"]["path"]+"/"+File.basename(file_path)). upload_file(file_path, ) return resp rescue => e puts e return false rescue SignalException return false end return true end |
#upload_log_file(absolute_path, relative_path, log_date) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/cnvrg/files.rb', line 235 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}) path, client = upload_resp["path"], upload_resp["client"] @client = Cnvrg::Downloader::Client.factory(client) @client.upload(path, absolute_path) end |
#upload_multiple_files(files_list, commit_sha1, progress: nil, suppress_exceptions: false, chunk_size: 100) ⇒ Object
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 114 115 116 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 |
# File 'lib/cnvrg/files.rb', line 89 def upload_multiple_files(files_list, commit_sha1, progress: nil, suppress_exceptions: false, chunk_size: 100) #open files on the server. Cnvrg::Logger.log_info("Uploading project files") return if files_list.blank? if Cnvrg::Helpers.server_version < 1 Cnvrg::Logger.log_info("Upload files to older server..") return self.upload_files_old(files_list, commit_sha1, progress: progress) end blob_ids = [] buffered_errors = {} files_list.each_slice(chunk_size).each do |chunk_of_files| Cnvrg::Logger.log_info("Upload chunk") parsed_chunk_of_files = chunk_of_files.map{|x| [x, self.parse_file(x)] if self.parse_file(x)}.compact.to_h resp = Cnvrg::API.request(@base_resource + "upload_files", 'POST', { files: parsed_chunk_of_files, commit: commit_sha1 }) unless Cnvrg::CLI.is_response_success(resp, false) raise SignalException.new("Cant upload files to the server.") end # resolve bucket res = resp['result'] files = res['files'] #upload files blob_id_chunk = Parallel.map(files.keys, in_threads: ParallelThreads) do |file| begin Cnvrg::Helpers.try_until_success{self.upload_single_file(files[file].merge(parsed_chunk_of_files[file]))} rescue => e Cnvrg::CLI.("Failed to upload #{file}: #{e.}", 'red') unless suppress_exceptions Cnvrg::Logger.log_error(e) Cnvrg::Logger.log_method(bind: binding) buffered_errors[file] = "Failed to upload #{file}: #{e.}" if suppress_exceptions raise e unless suppress_exceptions end progress.progress += 1 if progress.present? unless buffered_errors.key?(file) files[file]["bv_id"] else nil end end blob_ids.concat blob_id_chunk end # remove nil files (failed files) from blob_ids blob_ids.compact! #save files on the server. resp = Cnvrg::API.request(@base_resource + "upload_files_save", 'POST', {blob_ids: blob_ids, commit: commit_sha1}) unless Cnvrg::CLI.is_response_success(resp, false) raise SignalException.new("Cant save uploaded files to the server.") end return buffered_errors end |
#upload_old(absolute_path, relative_path, commit_sha1) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/cnvrg/files.rb', line 208 def upload_old(absolute_path, relative_path, commit_sha1) if relative_path.ends_with? '/' self.create_dir(absolute_path, relative_path, commit_sha1) else self.upload_file(absolute_path, relative_path, commit_sha1) end end |
#upload_single_file(file) ⇒ Object
188 189 190 191 192 |
# File 'lib/cnvrg/files.rb', line 188 def upload_single_file(file) path = file['path'] absolute_path = file[:absolute_path] @client.upload(path, absolute_path) end |
#upload_small_files_s3(url_path, file_path, content_type) ⇒ Object
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/cnvrg/files.rb', line 574 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| if !Helpers.is_verify_ssl http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.send_request("PUT", url.request_uri, body, { "content-type" => content_type, }) end return true rescue Interrupt return false rescue => e puts e return false end end |
#upload_url(file_path) ⇒ Object
596 597 598 599 600 601 602 603 604 |
# File 'lib/cnvrg/files.rb', line 596 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 |