Class: Cnvrg::Images
- Inherits:
-
Object
- Object
- Cnvrg::Images
- Defined in:
- lib/cnvrg/Images.rb
Class Method Summary collapse
- .commit_custom_image(owner, slug, logs) ⇒ Object
- .create_new_custom_image(type, owner, image_name, is_public, is_base, image_extend, python3, tar_path) ⇒ Object
- .create_new_custom_image_with_docker(type, owner, image_name, is_public, is_base, image_extend, python3, tar_path, files) ⇒ Object
- .image_exist(owner, image_name) ⇒ Object
- .revoke_custom_new_image(owner, slug) ⇒ Object
Instance Method Summary collapse
- #container_port ⇒ Object
- #create_custom_image(new_image_name, working_dir, stored_commands) ⇒ Object
- #find_image(update = true) ⇒ Object
- #get_bash_history ⇒ Object
- #get_image_state ⇒ Object
- #get_installed_packages(repo) ⇒ Object
- #handle_image_activity ⇒ Object
-
#initialize ⇒ Images
constructor
A new instance of Images.
- #is_container_exist ⇒ Object
- #new_machine(instance_type) ⇒ Object
- #note_slug ⇒ Object
- #remote_notebook(notebook_path, instance_type, kernel, data, data_commit) ⇒ Object
- #remove_note_slug ⇒ Object
- #save_docker_image(image_id) ⇒ Object
- #save_installed_libraries(container) ⇒ Object
- #set_note_url(note_slug) ⇒ Object
- #store_image_build_commands(working_dir, cmd) ⇒ Object
- #update_image(image_name, container, image_slug) ⇒ Object
- #update_image_activity(commit, exp_slug) ⇒ Object
- #update_slug(slug) ⇒ Object
- #upload_docker_image(image_path, image_name, workdir, user, description, is_gpu) ⇒ Object
Constructor Details
#initialize ⇒ Images
Returns a new instance of Images.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cnvrg/Images.rb', line 11 def initialize() begin home_dir = File.('~') config = YAML.load_file(home_dir+"/.cnvrg/config.yml") @owner = config.to_h[:owner] @username = config.to_h[:username] rescue => e @owner = "" @username = "" Cnvrg::Logger.log_info("cnvrg is not configured") end end |
Class Method Details
.commit_custom_image(owner, slug, logs) ⇒ Object
172 173 174 175 |
# File 'lib/cnvrg/Images.rb', line 172 def self.commit_custom_image(owner,slug,logs) response = Cnvrg::API.request("users/#{owner}/images/#{slug}/commit_custom_image", 'POST', {image_logs:logs}) return response end |
.create_new_custom_image(type, owner, image_name, is_public, is_base, image_extend, python3, tar_path) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/cnvrg/Images.rb', line 134 def self.create_new_custom_image(type,owner,image_name,is_public,is_base,image_extend,python3,tar_path) response = Cnvrg::API.request("users/#{owner}/images/custom", 'POST', {instance_type:type,image_name:image_name,is_public:is_public, is_base:is_base,image_extend:image_extend, python3:python3}) return response end |
.create_new_custom_image_with_docker(type, owner, image_name, is_public, is_base, image_extend, python3, tar_path, files) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cnvrg/Images.rb', line 141 def self.create_new_custom_image_with_docker(type,owner,image_name,is_public,is_base,image_extend,python3,tar_path,files) file_name = File.basename tar_path file_size = File.size(tar_path).to_f mime_type = MimeMagic.by_path(tar_path) content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain" upload_resp = Cnvrg::API.request("/users/#{owner}/images/upload_docker", 'POST_FILE', {absolute_path: tar_path, relative_path: tar_path, file_name: file_name, file_size: file_size, file_content_type: content_type, image_name:image_name,is_public:is_public, is_base:is_base,image_extend:image_extend, python3:python3 }) if Cnvrg::CLI.is_response_success(upload_resp, false) path = upload_resp["result"]["path"] s3_res = files.upload_small_files_s3(path, tar_path, content_type) if s3_res image_slug = upload_resp["result"]["id"] response = Cnvrg::API.request("users/#{owner}/images/#{image_slug}/build", 'POST', {instance_type:type,image_extend:image_extend}) end end end |
.image_exist(owner, image_name) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cnvrg/Images.rb', line 108 def self.image_exist(owner, image_name) image_res = Cnvrg::API.request("users/#{owner}/images/" + "find", 'POST', {image_name: image_name}) if Cnvrg::CLI.is_response_success(image_res) image= image_res["result"]["image"] return image else return false end end |
Instance Method Details
#container_port ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/cnvrg/Images.rb', line 99 def container_port() config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") if config[:container].nil? return false else return config[:port] end end |
#create_custom_image(new_image_name, working_dir, stored_commands) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/cnvrg/Images.rb', line 178 def create_custom_image(new_image_name,working_dir,stored_commands) python2_arr = get_installed_packages("python2") py2 = python2_arr.join(",") unless python2_arr.nil? or python2_arr.empty? python3_arr = get_installed_packages("python3") py3 = python3_arr.join(",") unless python3_arr.nil? or python3_arr.empty? system_arr = get_installed_packages("system") sys = system_arr.join(",") unless system_arr.nil? or system_arr.empty? response = Cnvrg::API.request("users/#{@owner}/projects/#{@project_slug}/images/push", 'POST', {image_slug: @image_slug, py2: py2,py3:py3, dpkg: sys, new_image_name: new_image_name, run_commands:stored_commands}) if Cnvrg::CLI.is_response_success(response) and !response["result"]["slug"].nil? container = get_container() name = response["result"]["name"] container = get_container() container.commit({repo:name,tag:"latest"}) update_image(name+":latest", container, response["result"]["slug"]) File.truncate(working_dir+"/.cnvrg/custom_image.txt", 0) end return true end |
#find_image(update = true) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/cnvrg/Images.rb', line 281 def find_image(update=true) config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") image_slug = config[:image_slug] if image_slug.nil? or image_slug.empty? image_res = Cnvrg::API.request("users/#{@owner}/images/" + "find", 'POST', {image_name: @image_name}) if Cnvrg::CLI.is_response_success(image_res) image_slug = image_res["result"]["image"]["slug"] update_slug(image_slug) unless !update return image_slug end else return image_slug end end |
#get_bash_history ⇒ Object
260 261 262 263 264 265 266 267 268 |
# File 'lib/cnvrg/Images.rb', line 260 def get_bash_history container = get_container() command = ["/bin/bash", "-lc", "cat /home/ds/.bash_history"] history = container.exec(command, tty: true)[0][0] if history.include? "No such file" history = "" end return history end |
#get_image_state ⇒ Object
271 272 273 274 275 276 277 278 279 |
# File 'lib/cnvrg/Images.rb', line 271 def get_image_state python_arr = self.get_installed_packages("python") py = python_arr.join(",") unless python_arr.nil? or python_arr.empty? system_arr = self.get_installed_packages("system") sys = system_arr.join(",") unless system_arr.nil? or system_arr.empty? # bash_history = self.get_bash_history diff = [py, sys] end |
#get_installed_packages(repo) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/cnvrg/Images.rb', line 243 def get_installed_packages(repo) container = get_container() case repo when "python2" command = ['/bin/bash', '-lc', '/opt/ds/bin/pip freeze'] when "python3" command = ['/bin/bash', '-lc', '/opt/ds3/bin/pip3 freeze'] when "system" command = ["/bin/bash", "-lc", "dpkg -l | grep '^ii' | awk '{print $2\"==\"$3}'"] end libs = container.exec(command, tty: true)[0] libs_arr = libs.join("").split("\r\n") return libs_arr end |
#handle_image_activity ⇒ Object
338 339 340 341 342 343 344 345 346 |
# File 'lib/cnvrg/Images.rb', line 338 def handle_image_activity home_dir = File.('~') zip_dir = "#{home_dir}/.cnvrg/tmp/config.zip" compress = `zip -j #{zip_dir} #{home_dir}/.netrc #{home_dir}/.cnvrg/config.yml` @files = Cnvrg::Files.new(@owner, @project_slug) res_id = @files.upload_exec_file(zip_dir, @image_name, @commit_id) FileUtils.remove zip_dir return res_id end |
#is_container_exist ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/cnvrg/Images.rb', line 91 def is_container_exist() config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") if config[:container].nil? return false end return config[:container] end |
#new_machine(instance_type) ⇒ Object
326 327 328 329 330 331 |
# File 'lib/cnvrg/Images.rb', line 326 def new_machine(instance_type) title = "#{instance_type} auto created by cli" response = Cnvrg::API.request("users/#{@owner}/machines/new", 'POST', {machine_name: title, instance_type: instance_type}) return Cnvrg::CLI.is_response_success(response) end |
#note_slug ⇒ Object
303 304 305 306 307 308 309 310 311 |
# File 'lib/cnvrg/Images.rb', line 303 def note_slug config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") note_slug = config[:notebook_slug] if note_slug.nil? or note_slug.empty? return false else return note_slug end end |
#remote_notebook(notebook_path, instance_type, kernel, data, data_commit) ⇒ Object
233 234 235 236 237 238 239 240 241 |
# File 'lib/cnvrg/Images.rb', line 233 def remote_notebook(notebook_path, instance_type, kernel,data,data_commit) response = Cnvrg::API.request("users/#{@owner}/images/#{@image_slug}/remote_notebook", 'POST', {dir: notebook_path, project_slug: @project_slug, instance_type: instance_type, kernel: kernel, dataset_slug:data, dataset_commit: data_commit}) return response end |
#remove_note_slug ⇒ Object
312 313 314 315 316 317 |
# File 'lib/cnvrg/Images.rb', line 312 def remove_note_slug config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") config[:notebook_slug] = "" File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } end |
#save_docker_image(image_id) ⇒ Object
85 86 87 88 89 |
# File 'lib/cnvrg/Images.rb', line 85 def save_docker_image(image_id) image_res = Cnvrg::API.request("users/#{@owner}/images/#{image_id}/save" , 'POST', {}) Cnvrg::CLI.is_response_success(image_res, true) return image_res end |
#save_installed_libraries(container) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/cnvrg/Images.rb', line 219 def save_installed_libraries(container) begin command = ['/bin/bash', '-lc', '/opt/ds/bin/pip freeze'] pip = container.exec(command, tty: true)[0] command = ["/bin/bash", "-lc", "dpkg -l | grep '^ii' | awk '{print $2\"==\"$3}'"] dpkg = container.exec(command, tty: true)[0] File.open(@working_dir+"/.cnvrg/pip_base.txt", "w+") { |f| f.write pip } File.open(@working_dir+"/.cnvrg/dpkg_base.txt", "w+") { |f| f.write dpkg } rescue => e end end |
#set_note_url(note_slug) ⇒ Object
298 299 300 301 302 |
# File 'lib/cnvrg/Images.rb', line 298 def set_note_url(note_slug) config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") config[:notebook_slug] = note_slug File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } end |
#store_image_build_commands(working_dir, cmd) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cnvrg/Images.rb', line 120 def store_image_build_commands(working_dir, cmd) begin custom_image_file = working_dir+"/.cnvrg/custom_image.txt" if !File.exist? custom_image_file FileUtils.touch [custom_image_file] end File.open(custom_image_file, 'a' ) do |f| f.puts cmd end rescue end end |
#update_image(image_name, container, image_slug) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/cnvrg/Images.rb', line 204 def update_image(image_name, container, image_slug) config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") @image_name = image_name if !@image_name.nil? and !@image_name.empty? @image_name = image_name[0, image_name.index(":")] @image_tag = image_name[image_name.index(":")+1, image_name.size] end config = {project_name: config[:project_name], project_slug: config[:project_slug], owner: config[:owner], docker: true, image_base: @image_name, image_tag: @image_tag, container: container.id, image_slug: image_slug} File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } end |
#update_image_activity(commit, exp_slug) ⇒ Object
333 334 335 336 |
# File 'lib/cnvrg/Images.rb', line 333 def update_image_activity(commit, exp_slug) response = Cnvrg::API.request("users/#{@owner}/images/#{@image_slug}/update_activity", 'POST', {commit: commit, project_slug: @project_slug, experiment: exp_slug}) return Cnvrg::CLI.is_response_success(response) end |
#update_slug(slug) ⇒ Object
320 321 322 323 324 |
# File 'lib/cnvrg/Images.rb', line 320 def update_slug(slug) config = YAML.load_file(@working_dir+"/.cnvrg/config.yml") config[:image_slug] = slug File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } end |
#upload_docker_image(image_path, image_name, workdir, user, description, is_gpu) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 80 81 82 83 |
# File 'lib/cnvrg/Images.rb', line 25 def upload_docker_image(image_path, image_name,workdir, user, description, is_gpu ) begin if image_name.present? and image_name.include? ":" image_split = image_name.split(":") image_name = image_split[0] image_tag = image_split[1] if image_tag.blank? image_tag = "latest" end if image_name.blank? Cnvrg::Logger.log_info("image_name: #{image_name} is not valid") return false end end if !File.exist? image_path Cnvrg::Logger.log_info("image_path: #{image_path} was not found") return false end is_dockerfile = (!image_path.end_with? ".tar") ? true : false file_name = File.basename image_path file_size = File.size(image_path).to_f mime_type = MimeMagic.by_path(image_path) content_type = mime_type.present? ? mime_type.type : "application/x-tar" image_res = Cnvrg::API.request("users/#{@owner}/images/upload" , 'POST_FILE', {image_name: image_name, image_tag: image_tag, workdir: workdir, user: user, description:description, is_gpu:is_gpu, file_name: file_name,relative_path:image_path, file_size: file_size, file_content_type: content_type, is_dockerfile: is_dockerfile }) Cnvrg::CLI.is_response_success(image_res, true) path = image_res["result"]["path"] image_id = image_res["result"]["image_id"] props = Cnvrg::Helpers.get_s3_props(image_res["result"]) if props.is_a? Cnvrg::Result return false end client = props[:client] = props[:upload_options] bucket = Aws::S3::Resource.new(client: client).bucket(props[:bucket]) resp = bucket.object(path). upload_file(image_path, ) unless resp raise Exception.new("Cant upload #{image_path}") return false end return save_docker_image(image_id) rescue => e Cnvrg::Logger.log_error(e) return false end end |