Class: Cnvrg::CLI
- Inherits:
-
Thor
- Object
- Thor
- Cnvrg::CLI
- Defined in:
- lib/cnvrg/cli.rb
Constant Summary collapse
- INSTALLATION_URLS =
{docker: "https://docs.docker.com/engine/installation/", jupyter: "http://jupyter.readthedocs.io/en/latest/install.html"}
Instance Method Summary collapse
- #clone(project_url) ⇒ Object
- #commit_notebook(notebook_image_name) ⇒ Object
- #download ⇒ Object
- #download_image(image_name) ⇒ Object
- #exec(*cmd) ⇒ Object
- #install_notebook_libraries ⇒ Object
- #link ⇒ Object
- #list_images ⇒ Object
- #login ⇒ Object
- #logout ⇒ Object
- #me ⇒ Object
-
#new(project_name) ⇒ Object
Projects.
- #pull_image(image_name) ⇒ Object
- #run_notebook ⇒ Object
- #set_api_url(url) ⇒ Object
- #set_default_owner ⇒ Object
- #status ⇒ Object
- #sync ⇒ Object
- #test ⇒ Object
- #upload(link = false, sync = false) ⇒ Object
- #upload_image(image_name) ⇒ Object
- #version ⇒ Object
Instance Method Details
#clone(project_url) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 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 |
# File 'lib/cnvrg/cli.rb', line 331 def clone(project_url) begin verify_logged_in(false) log_start(__method__,args,) url_parts = project_url.split("/") project_index = Cnvrg::Helpers.look_for_in_path(project_url, "projects") slug = url_parts[project_index+1] owner = url_parts[project_index-1] response = Cnvrg::API.request("users/#{owner}/projects/#{slug}/get_project", 'GET') Cnvrg::CLI.is_response_success(response) response = JSON.parse response["result"] project_name = response["title"] say "Cloning #{project_name}", Thor::Shell::Color::BLUE if Dir.exists? project_name or File.exists? project_name log_end(1,"conflict with dir/file #{project_name}") say "Error: Conflict with dir/file #{project_name}", Thor::Shell::Color::RED exit(1) end if Project.clone_dir(slug, owner, project_name) project_home = Dir.pwd+"/"+project_name @project = Project.new(project_home) @files = Cnvrg::Files.new(@project.owner, slug) response = @project.clone Cnvrg::CLI.is_response_success response idx = {commit: response["result"]["commit"], tree: response["result"]["tree"]} File.open(project_name + "/.cnvrg/idx.yml", "w+") { |f| f.write idx.to_yaml } successful_changes = [] say "Downloading files", Thor::Shell::Color::BLUE response["result"]["tree"].each do |f| relative_path = f[0].gsub(/^#{@project.local_path}/, "") if f[0].end_with? "/" # dir if @files.download_dir(f[0], relative_path, project_home) successful_changes << relative_path end else # blob if @files.download_file(f[0], relative_path, project_home) successful_changes << relative_path end end end say "Done.\nDownloaded total of #{successful_changes.size} files", Thor::Shell::Color::GREEN log_end(0) else log_end(1,"can't create directory") say "Error: Couldn't create directory: #{project_name}", Thor::Shell::Color::RED exit(1) end rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#commit_notebook(notebook_image_name) ⇒ Object
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/cnvrg/cli.rb', line 1033 def commit_notebook(notebook_image_name) verify_logged_in(false) log_start(__method__,args,) begin docker_path = verify_software_installed("docker") container_id = get_container_id owner = Cnvrg::CLI.get_owner() say "Commiting notebook changes to: #{notebook_image_name}", Thor::Shell::Color::BLUE commit_res = system("#{docker_path} commit #{container_id} #{notebook_image_name}") if commit_res checker = Helpers.checkmark() log_end(0) say "#{checker} Done.", Thor::Shell::Color::GREEN else log_End(1,"can't commit new notebook image") say "Couldn't commit new notebook image ", Thor::Shell::Color::RED end rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#download ⇒ Object
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 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 |
# File 'lib/cnvrg/cli.rb', line 601 def download begin verify_logged_in() log_start(__method__,args,) project_home = get_project_home @project = Project.new(project_home) @files = Cnvrg::Files.new(@project.owner, @project.slug) res = @project.compare_idx["result"] result = res["tree"] commit = res["commit"] if result["updated_on_server"].empty? and result["conflicts"] and result["deleted"].empty? say "Project is up to date", Thor::Shell::Color::GREEN log_end(0) return true end update_count = 0 update_total = result["updated_on_server"].size + result["conflicts"].size successful_changes = [] if update_total ==1 say "Downloading #{update_total} file", Thor::Shell::Color::BLUE else say "Downloading #{update_total} files", Thor::Shell::Color::BLUE end result["conflicts"].each do |f| relative_path = f.gsub(/^#{@project.local_path}/, "") if @files.download_file(f, relative_path, project_home, conflict=true) successful_changes << relative_path end end result["updated_on_server"].each do |f| relative_path = f.gsub(/^#{@project.local_path}/, "") if f.end_with? "/" # dir if @files.download_dir(f, relative_path, project_home) successful_changes << relative_path end else # blob if @files.download_file(f, relative_path, project_home) successful_changes << relative_path end end end if update_total == successful_changes.size # update idx with latest commit @project.update_idx_with_commit!(commit) say "Done. Downloaded:", Thor::Shell::Color::GREEN say successful_changes.join("\n"), Thor::Shell::Color::GREEN say "Total of #{successful_changes.size} / #{update_total} files.", Thor::Shell::Color::GREEN log_end(0) end rescue SignalException log_end(-1) say "\nAborting", Thor::Shell::Color::BLUE if successful_changes.nil? exit(1) end successful_changes.each do |f| abs_path = "#{@project.local_path}/#{f}" filename = File.basename abs_path say "revoking #{filename}" if result["conflicts"].include? f @files.revoke_download_file(abs_path,f,filename,true) elsif result["updated_on_server"].include? f if File.directory? abs_path @files.revoke_download_dir(abs_path,f,project_home) else @files.revoke_download_file(project_home,abs_path,filename) end end end exit(1) end end |
#download_image(image_name) ⇒ Object
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
# File 'lib/cnvrg/cli.rb', line 1126 def download_image(image_name) begin verify_logged_in(false) log_start(__method__,args,) owner = Cnvrg::CLI.get_owner() notebooks_res = Cnvrg::API.request("users/#{owner}/images/" + "find", 'POST', {image_name: image_name}) if Cnvrg::CLI.is_response_success(notebooks_res) images = notebooks_res["result"]["images"] if images.empty? say "Couldn't find any image with name: #{image_name}", Thor::Shell::Color::RED exit(1) elsif images.size == 1 image_id = images[0]["slug"] else printf "%-20s %-20s %-30s %-20s\n", "name", "version", "last updated", "created by" images.each_with_index do |u, i| time = Time.parse(u["updated_at"]) update_at = get_local_time(time) version = u["version"] || "v1" created_by = u["image_file_name"][/^[^\_]*/] printf "%-20s %-20s %-30s %-20s\n", u["name"], version, update_at, created_by end choice = ask("Which version to download for #{image_name}?") images.each do |u| if u["version"] == choice image_id = u["slug"] end end end end path = Dir.pwd+"/#{owner}_#{image_name}.tar.gz" @files = Cnvrg::Files.new(owner, "") say "Downloading image file", Thor::Shell::Color::BLUE begin res = @files.download_image(path, image_id, owner) if res checks = Helpers.checkmark() say "#{checks} Done", Thor::Shell::Color::GREEN log_end(0) return true else say "Couldn't download image #{image_name}", Thor::Shell::Color::RED log_end(1,"can't download image") return false end rescue Interrupt log_end(-1) say "The user has exited to process, aborting", Thor::Shell::Color::BLUE exit(1) end rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#exec(*cmd) ⇒ Object
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 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 889 890 891 892 893 894 895 896 897 898 899 900 901 |
# File 'lib/cnvrg/cli.rb', line 725 def exec(*cmd) # LogJob.perform_async(cmd,options) # log = [] cpu_average =0 memory_average = 0 verify_logged_in() log_start(__method__,args,) project_home = get_project_home @project = Project.new(project_home) sync_before = ["sync_before"] sync_after = ["sync_after"] print_log = ["log"] title = ["title"] email_notification = ["email_notification"] upload_output = ["upload_output"] time_to_upload = calc_output_time(upload_output) begin if sync_before # Sync before run say "Syncing project before running", Thor::Shell::Color::BLUE say 'Checking for new updates from remote version', Thor::Shell::Color::BLUE download() upload() say "Done Syncing", Thor::Shell::Color::BLUE end start_commit = @project.last_local_commit cmd = cmd.join("\s") say "Running: #{cmd}\n", Thor::Shell::Color::BLUE @exp = Experiment.new(@project.owner, @project.slug) platform = RUBY_PLATFORM machine_name = Socket.gethostname begin @exp.start(cmd, platform, machine_name, start_commit, title, email_notification) unless @exp.slug.nil? real = Time.now exp_success = true memory_total = [] cpu_total = [] start_loop = Time.now begin PTY.spawn(cmd) do |stdout, stdin, pid, stderr| begin stdout.each do |line| cur_time = Time.now monitor = %x{ps aux|awk '{print $2,$3,$4}'|grep #{pid} } monitor_by = monitor.split(" ") memory = monitor_by[2] cpu = monitor_by[1] memory_total << memory.to_f cpu_total << cpu.to_f real_time= Time.now-real cur_log = {time: cur_time, message: line, type: "stdout", real: real_time} if print_log puts cur_log end log << cur_log begin if time_to_upload !=0 if time_to_upload <= Time.now - start_loop #upload current log cpu_average = cpu_total.inject(0) { |sum, el| sum + el }.to_f / cpu_total.size memory_average = memory_total.inject(0) { |sum, el| sum + el }.to_f / memory_total.size @exp.upload_temp_log(log, cpu_average, memory_average) log = [] start_loop = Time.now end end rescue say "Failed to upload ongoing results, continuing with experiment", Thor::Shell::Color::YELLOW end end if stderr stderr.each do |err| log << {time: Time.now, message: err, type: "stderr"} end end rescue Errno::EIO => e break rescue Errno::ENOENT log_end(1, "command #{cmd} isn't valid") exp_success = false say "command \"#{cmd}\" couldn't be executed, verify command is valid", Thor::Shell::Color::RED rescue PTY::ChildExited log_end(1, "proccess exited") exp_success = false say "The process exited!", Thor::Shell::Color::RED rescue => e log_end(1,e.) end ::Process.wait pid cpu_average = cpu_total.inject(0) { |sum, el| sum + el }.to_f / cpu_total.size memory_average = memory_total.inject(0) { |sum, el| sum + el }.to_f / memory_total.size exit_status = $?.exitstatus if $?.exitstatus != 0 exp_success = false end if !exp_success if !Cnvrg::Helpers.internet_connection? wait_offline = agree "Seems like you're offline, wait until your'e back online?",limited_to: ['y', 'n'], default: 'n' if wait_offline say "Waiting until your'e online..", Thor::Shell::Color::BLUE while !Cnvrg::Helpers.internet_connection? end exit_status = 0 else say "Experiment has failed, your'e computer is offline", Thor::Shell::Color::RED log_end(1,"experiment has failed,computer is offline") exit(0) end else end_commit = @project.last_local_commit res = @exp.end(log, exit_status, end_commit, cpu_average, memory_average) say "Experiment has failed, look at the log for more details or run cnvrg exec --log", Thor::Shell::Color::RED log_end(1,"experiment has failed") exit(0) end end if sync_after say "Syncing project after running", Thor::Shell::Color::BLUE # Sync after run download() upload() say "Done Syncing", Thor::Shell::Color::BLUE end end_commit = @project.last_local_commit res = @exp.end(log, exit_status, end_commit, cpu_average, memory_average) check = Helpers.checkmark() say "#{check} Done. Experiment's result: #{Cnvrg::Helpers.remote_url}/#{@project.owner}/projects/#{@project.slug}/experiments/#{@exp.slug}", Thor::Shell::Color::GREEN log_end(0) end rescue =>e log_end(1,e.) say "Couldn't run #{cmd}, check your input parameters", Thor::Shell::Color::RED exit(1) end end end rescue SignalException exit_status = -1 log_end(-1) end_commit = @project.last_local_commit res = @exp.end(log, exit_status, end_commit, cpu_average, memory_average) say "\nAborting" exit(1) end end |
#install_notebook_libraries ⇒ Object
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/cnvrg/cli.rb', line 1011 def install_notebook_libraries begin verify_logged_in(false) log_start(__method__,args,) docker_path = verify_software_installed("docker") container_id = get_container_id say "Opening shell in notebook server\nYou can run pip install [library] to install more tools\ntype exit to finish", Thor::Shell::Color::BLUE system("#{docker_path} exec -it #{container_id} bash") commit_name = ["commit_name"] if !commit_name.empty? return commit_notebook(commit_name) end rescue SignalException log_End(-1) say "/nAborting" exit(1) end end |
#link ⇒ Object
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 324 325 326 327 |
# File 'lib/cnvrg/cli.rb', line 290 def link begin verify_logged_in(false) log_start(__method__,args,) sync = ["sync"] project_name =File.basename(Dir.getwd) say "Linking #{project_name}", Thor::Shell::Color::BLUE if File.directory?(Dir.getwd+"/.cnvrg") config = YAML.load_file("#{Dir.getwd}/.cnvrg/config.yml") say "Directory is already linked to #{config[:project_slug]}", Thor::Shell::Color::RED exit(0) end if Project.link(project_name) path = Dir.pwd @project = Project.new(path) @project.generate_idx() if sync upload(true) end url = @project.url say "#{project_name}'s location is: #{url}\n", Thor::Shell::Color::BLUE log_end(0) else log_end(1,"can't link project") say "Error linking project, please contact support.", Thor::Shell::Color::RED exit(0) end rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#list_images ⇒ Object
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/cnvrg/cli.rb', line 1192 def list_images begin verify_logged_in(false) log_start(__method__,args,) owner = Cnvrg::CLI.get_owner() res = Cnvrg::API.request("users/#{owner}/images/list", 'GET') if Cnvrg::CLI.is_response_success(res) printf "%-20s %-20s %-30s %-20s\n", "name", "version", "last updated", "created by" res["result"]["images"].each do |u| time = Time.parse(u["updated_at"]) update_at = get_local_time(time) version = u["version"] || "v1" created_by = u["image_file_name"][/^[^\_]*/] printf "%-20s %-20s %-30s %-20s\n", u["name"], version, update_at, created_by end end log_end(0) return res["result"]["images"] rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#login ⇒ 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cnvrg/cli.rb', line 117 def login begin cmd = HighLine.new say 'Authenticating with cnvrg', Thor::Shell::Color::YELLOW @auth = Cnvrg::Auth.new netrc = Netrc.read @email, token = netrc[Cnvrg::Helpers.netrc_domain] if @email and token say 'Seems you\'re already logged in', Thor::Shell::Color::BLUE exit(0) end @email = ask("Enter your email:") password = cmd.ask("Enter your password (hidden):") { |q| q.echo = "*" } result = @auth.sign_in(@email, password) if !result["token"].nil? netrc[Cnvrg::Helpers.netrc_domain] = @email, result["token"] netrc.save say "Authenticated successfully as #{@email}", Thor::Shell::Color::GREEN owners = result["owners"] choose_owner = result["username"] if owners.empty? else owners << choose_owner chosen = false while !chosen choose_owner = ask("Choose default owner:\n"+owners.join("\n")+"\n") owners_lower = owners.map{|o| o.downcase} if !owners_lower.include? choose_owner.downcase say "Could not find owner named #{choose_owner}", Thor::Shell::Color::RED else chosen = true end end end if set_owner(choose_owner, result["username"]) say "Setting default owner: #{choose_owner}", Thor::Shell::Color::GREEN else say "Setting default owenr has failed, logging out", Thor::Shell::Color::RED return logout() end else say "Failed to authenticate, wrong email/password", Thor::Shell::Color::RED exit(1) end rescue SignalException say "/nAborting" logout() exit(1) end end |
#logout ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/cnvrg/cli.rb', line 184 def logout begin netrc = Netrc.read netrc.delete(Cnvrg::Helpers.netrc_domain) netrc.save say "Logged out successfully.\n", Thor::Shell::Color::GREEN rescue SignalException say "/nAborting" exit(1) end end |
#me ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cnvrg/cli.rb', line 200 def me() begin verify_logged_in(false) log_start(__method__,args,) auth = Cnvrg::Auth.new if (email = auth.get_email) say "Logged in as: #{email}", Thor::Shell::Color::GREEN else say "You're not logged in.", Thor::Shell::Color::RED end log_end(0) rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#new(project_name) ⇒ Object
Projects
223 |
# File 'lib/cnvrg/cli.rb', line 223 desc 'new', 'Create a new cnvrg project' |
#pull_image(image_name) ⇒ Object
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'lib/cnvrg/cli.rb', line 1222 def pull_image(image_name) begin verify_logged_in(false) log_start(__method__,args,) if download_image(image_name) path = Dir.pwd+"/#{owner}_#{image_name}.tar.gz" loadRes = system("docker load < #{path}") if loadRes.include? "Loaded image" say loadRes, Thor::Shell::Color::GREEN log_end(0) else say loadRes, Thor::Shell::Color::RED log_end(1,loadRes) end end rescue SignalException say "\nAborting" log_end(-1) exit(1) end end |
#run_notebook ⇒ Object
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 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 |
# File 'lib/cnvrg/cli.rb', line 908 def run_notebook begin verify_logged_in(false) log_start(__method__,args,) cur_path = Dir.pwd notebook_dir = ["notebook_dir"] if notebook_dir.empty? notebook_dir = cur_path else notebook_dir = cur_path+ notebook_dir end say "Linking notebook directory to: #{notebook_dir}", Thor::Shell::Color::BLUE docker = ["docker"] try_again = true if docker docker_path = verify_software_installed("docker") image_name = ["image_name"] if image_name.empty? images_list = `#{docker_path} images` if images_list.empty? or images_list.nil? say "you don't have any images to run", Thor::Shell::Color::BLUE else say "Choose image name to run as a container\n", Thor::Shell::Color::BLUE say images_list image_name = ask("\nwhat is the image name?\n") end end while (try_again) do run_docker = `#{docker_path} run -d -p 8888:8888 -v #{notebook_dir}:/home/ds/notebooks -t -i #{image_name} 2>&1` if !run_docker.match(/[a-z0-9]{64}/).nil? and !run_docker.include? "Error" container_id = get_container_id() sleep(3) logs = `docker logs #{container_id}` url = URI.extract(logs).reject { |x| x if !x.include? "http" }.uniq![0] if !url.empty? check = Helpers.checkmark() say "#{check} Notebook server started successfully, view notebook in url: #{url}", Thor::Shell::Color::GREEN try_again= false else say "Couldn't start notebook server", Thor::Shell::Color::RED log_end(1, "can't start notebook server") exit(1) end elsif run_docker.include? "port is already allocated" say "Couldn't start notebook with port 8888, port is taken", Thor::Shell::Color::RED port_container = `#{docker_path} ps |grep 8888 |awk '{print $1}'`.strip! stop = agree "There is another running notebook server: #{port_container}, do you want to stop it?", limited_to: ['y', 'n'], default: 'y' if stop == "y" did_stop = system("#{docker_path} stop #{port_container}") if !did_stop say "Couldn't stop notebook server: #{port_container}", Thor::Shell::Color::RED log_end(1,"can't stop notebook server") exit(1) end else logs = `#{docker_path} logs #{port_container}` url = URI.extract(logs).reject { |x| x if !x.include? "http" }.uniq![0] say "Done, your notebook server is: #{url}", Thor::Shell::Color::BLUE log_end(0) exit(1) end else log_end(1, "can;t start notebook server") say "Couldn't start notebook server", Thor::Shell::Color::RED exit(1) end end else jup =verify_software_installed("jupyter-notebook") logs = `#{jup} --no-browser --ip=0.0.0.0 --notebook-dir=#{notebook_dir}` url = URI.extract(logs).reject { |x| x if !x.include? "http" }.uniq![0] if !url.empty? check = Helpers.checkmark() say "#{check} Notebook server started successfully, view notebook in url: #{url}", Thor::Shell::Color::GREEN log_end(0) else say "Couldn't start notebook server", Thor::Shell::Color::RED log_end(1,"can't start notebook server") exit(1) end end rescue SignalException log_end(-1) say "Aborting" exit(1) end end |
#set_api_url(url) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cnvrg/cli.rb', line 52 def set_api_url(url) home_dir = File.('~') if !url.end_with? "/api" url = url+"/api" end begin if !File.directory? home_dir+"/.cnvrg" FileUtils.mkdir_p([home_dir+"/.cnvrg", home_dir+"/.cnvrg/tmp"]) end if !File.exist?(home_dir+"/.cnvrg/config.yml") FileUtils.touch [home_dir+"/.cnvrg/config.yml"] end config = YAML.load_file(home_dir+"/.cnvrg/config.yml") say "Setting default api to be: #{url}", Thor::Shell::Color::BLUE if config.empty? config = {owner: "", username: "", version_last_check: get_start_day(), api: url} else config = {owner: config.to_h[:owner], username: config.to_h[:username], version_last_check: config.to_h[:version_last_check], api: url} end checks = Helpers.checkmark File.open(home_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } say "#{checks} Done", Thor::Shell::Color::GREEN rescue say "Couldn't set default api, contact [email protected]", Thor::Shell::Color::RED end end |
#set_default_owner ⇒ Object
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 |
# File 'lib/cnvrg/cli.rb', line 85 def set_default_owner path = File.('~')+"/.cnvrg/config.yml" if !File.exist?(path) say "Couldn't find ~/.cnvrg/config.yml file, please logout and login again", Thor::Shell::Color::RED exit(0) end config = YAML.load_file(path) username = config.to_h[:username] res = Cnvrg::API.request("/users/#{username}/get_possible_owners", 'GET') if Cnvrg::CLI.is_response_success(res) owner = username result = res["result"] if result["owners"].size > 1 owner = ask("Choose default owner:\n"+result["owners"].join("\n")+"\n") end if set_owner(owner, username) say "Setting default owner: #{owner}", Thor::Shell::Color::GREEN else say "Setting default owenr has failed, try to run cnvrg --config-default-owner", Thor::Shell::Color::RED end end end |
#status ⇒ Object
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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/cnvrg/cli.rb', line 392 def status begin verify_logged_in() log_start(__method__,args,) @project = Project.new(get_project_home) result = @project.compare_idx["result"] commit = result["commit"] result = result["tree"] say "Comparing local changes with remote version:", Thor::Shell::Color::BLUE if result["added"].empty? and result["updated_on_local"].empty? and result["updated_on_server"].empty? and result["deleted"].empty? and result["conflicts"].empty? say "Project is up to date", Thor::Shell::Color::GREEN log_end(0) return true end if result["added"].size > 0 say "Added files:\n", Thor::Shell::Color::BLUE result["added"].each do |a| say "\t\tA:\t#{a}", Thor::Shell::Color::GREEN end end if result["deleted"].size > 0 say "Deleted files:\n", Thor::Shell::Color::BLUE result["deleted"].each do |a| say "\t\tD:\t#{a}", Thor::Shell::Color::GREEN end end if result["updated_on_local"].size > 0 say "Local changes:\n", Thor::Shell::Color::BLUE result["updated_on_local"].each do |a| say "\t\tM:\t#{a}", Thor::Shell::Color::GREEN end end if result["updated_on_server"].size > 0 say "Remote changes:\n", Thor::Shell::Color::BLUE result["updated_on_server"].each do |a| say "\t\tM:\t#{a}", Thor::Shell::Color::GREEN end end if result["conflicts"].size > 0 say "Conflicted changes:\n", Thor::Shell::Color::BLUE result["conflicts"].each do |a| say "\t\tC:\t#{a}", Thor::Shell::Color::RED end end log_end(0) rescue SignalException log_end(-1) say "/nAborting" exit(1) end end |
#sync ⇒ Object
688 689 690 691 692 |
# File 'lib/cnvrg/cli.rb', line 688 def sync say 'Checking for new updates from remote version', Thor::Shell::Color::BLUE invoke :download invoke :upload end |
#test ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/cnvrg/cli.rb', line 33 def test verify_software_installed("docker") container = Docker::Container.create( 'Image' => 'cnvrgio/python2') # container = Docker::Container.create("1e4e23560813") container.start puts container.exec(['bash'], stdin: StringIO.new("python --version")) end |
#upload(link = false, sync = false) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/cnvrg/cli.rb', line 451 def upload(link=false, sync=false) begin verify_logged_in() log_start(__method__,args,) @project = Project.new(get_project_home) @files = Cnvrg::Files.new(@project.owner, @project.slug) ignore = [:ignore] || [] if !@project.update_ignore_list(ignore) say "Couldn't append new ignore files to .cnvrgignore", Thor::Shell::Color::YELLOW end result = @project.compare_idx commit = result["result"]["commit"] if !link if commit != @project.last_local_commit and !@project.last_local_commit.nil? and !result["result"]["tree"]["updated_on_server"].empty? log_end(0) say "Remote server has an updated version, please run `cnvrg download` first, or alternatively: `cnvrg sync`", Thor::Shell::Color::YELLOW exit(1) end say "Comparing local changes with remote version:", Thor::Shell::Color::BLUE end result = result["result"]["tree"] if result["added"].empty? and result["updated_on_local"].empty? and result["deleted"].empty? log_end(0) say "Project is up to date", Thor::Shell::Color::GREEN return true end update_count = 0 update_total = result["added"].size + result["updated_on_local"].size + result["deleted"].size successful_updates = [] successful_deletions = [] if update_total == 1 say "Updating #{update_total} file", Thor::Shell::Color::BLUE else say "Updating #{update_total} files", Thor::Shell::Color::BLUE end # Start commit commit_sha1 = @files.start_commit["result"]["commit_sha1"] # upload / update begin (result["added"] + result["updated_on_local"]).each do |f| puts f absolute_path = "#{@project.local_path}/#{f}" relative_path = f.gsub(/^#{@project.local_path + "/"}/, "") if File.directory?(absolute_path) resDir = @files.create_dir(absolute_path, relative_path, commit_sha1) if resDir update_count += 1 successful_updates<< relative_path end else res = @files.upload_file(absolute_path, relative_path, commit_sha1) if res update_count += 1 successful_updates<< relative_path else @files.rollback_commit(commit_sha1) log_end(1,"can't upload, Rolling Back all changes") say "Couldn't upload, Rolling Back all changes.", Thor::Shell::Color::RED exit(0) end end end # delete result["deleted"].each do |f| relative_path = f.gsub(/^#{@project.local_path + "/"}/, "") if relative_path.end_with?("/") if @files.delete_dir(f, relative_path, commit_sha1) update_count += 1 successful_updates<< relative_path end else if @files.delete_file(f, relative_path, commit_sha1) update_count += 1 successful_updates<< relative_path end end end log_end(0) rescue SignalException log_end(-1) @files.rollback_commit(commit_sha1) say "User aborted, Rolling Back all changes.", Thor::Shell::Color::RED exit(0) rescue => e log_end(1,e.) @files.rollback_commit(commit_sha1) say "Exception while trying to upload, Rolling back", Thor::Shell::Color::RED exit(0) end if update_count == update_total res = @files.end_commit(commit_sha1) if (Cnvrg::CLI.is_response_success(res, false)) # save idx begin @project.update_idx_with_files_commits!((successful_deletions+successful_updates), res["result"]["commit_time"]) @project.update_idx_with_commit!(commit_sha1) rescue => e log_end(1,e.) @files.rollback_commit(commit_sha1) say "Couldn't commit updates, Rolling Back all changes.", Thor::Shell::Color::RED exit(1) end say "Done", Thor::Shell::Color::BLUE if successful_updates.size >0 say "Updated:", Thor::Shell::Color::GREEN suc = successful_updates.map { |x| x=Helpers.checkmark() +" "+x } say suc.join("\n"), Thor::Shell::Color::GREEN end if successful_deletions.size >0 say "Deleted:", Thor::Shell::Color::GREEN del = successful_updates.map { |x| x=Helpers.checkmark() +" "+x } say del.join("\n"), Thor::Shell::Color::GREEN end say "Total of #{update_count} / #{update_total} files.", Thor::Shell::Color::GREEN log_end(0) else @files.rollback_commit(commit_sha1) log_end(1, "error. Rolling Back all changes") say "Error. Rolling Back all changes.", Thor::Shell::Color::RED end else log_end(1, "error. Rolling Back all changes") @files.rollback_commit(commit_sha1) end rescue SignalException log_end(-1) say "\nAborting",Thor::Shell::Color::BLUE say "\nRolling back all changes",Thor::Shell::Color::BLUE @files.rollback_commit(commit_sha1) exit(1) end end |
#upload_image(image_name) ⇒ Object
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 |
# File 'lib/cnvrg/cli.rb', line 1062 def upload_image(image_name) verify_logged_in(false) log_start(__method__,args,) docker_path = verify_software_installed("docker") owner = Cnvrg::CLI.get_owner() # verify image exist images = `#{docker_path} images |grep #{image_name}` if images.empty? say "Couldn't find any images named: #{image_name}", Thor::Shell::Color::RED exit(1) end path = File.('~')+"/.cnvrg/#{owner}_#{image_name}.tar" begin say "Creating image file to upload", Thor::Shell::Color::BLUE if !(File.exist? path or File.exist? path+"gz") saveRes = system("#{docker_path} save #{image_name}>#{path}") if !saveRes log_End(1, "can't create tar file from image") say "Couldn't create tar file from image", Thor::Shell::Color::RED exit(1) end gzipRes = system("gzip -f #{path}") if !gzipRes log_End(1, "can't create tar file from image") say "Couldn't create tar file from image", Thor::Shell::Color::RED exit(1) end end path = path+".gz" @files = Cnvrg::Files.new(owner, "") exit_status = $?.exitstatus if exit_status == 0 say "Uploading image file", Thor::Shell::Color::BLUE res = @files.upload_image(path, image_name, owner) if res File.delete(path) checks = Helpers.checkmark() say "#{checks} Done", Thor::Shell::Color::GREEN log_end(0) else say "Couldn't upload image", Thor::Shell::Color::RED log_end(1, "can't create upload imag") end else say "Couldn't create image file for: #{image_name}", Thor::Shell::Color::RED log_end(1, "can't create upload imag") exit(1) end rescue SignalException log_end(-1) say "Couldn't upload image file for: #{image_name}", Thor::Shell::Color::RED exit(1) end end |