Class: Cnvrg::Images
- Inherits:
-
Object
- Object
- Cnvrg::Images
- Defined in:
- lib/cnvrg/Images.rb
Instance Attribute Summary collapse
-
#commit_id ⇒ Object
readonly
Returns the value of attribute commit_id.
-
#image_name ⇒ Object
readonly
Returns the value of attribute image_name.
-
#image_slug ⇒ Object
readonly
Returns the value of attribute image_slug.
-
#image_tag ⇒ Object
readonly
Returns the value of attribute image_tag.
-
#is_docker ⇒ Object
readonly
Returns the value of attribute is_docker.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#project_slug ⇒ Object
readonly
Returns the value of attribute project_slug.
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
- .ssh_to_machine(resp) ⇒ Object
Instance Method Summary collapse
- #container_port ⇒ Object
- #create_container(port = 7654, is_remote = false) ⇒ Object
- #create_custom_image(new_image_name, working_dir, stored_commands) ⇒ Object
- #find_image(update = true) ⇒ Object
- #get_bash_history ⇒ Object
- #get_container(stop = false) ⇒ Object
- #get_image_state ⇒ Object
- #get_installed_packages(repo) ⇒ Object
- #handle_image_activity ⇒ Object
-
#initialize(working_dir, image_name = "") ⇒ 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_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
Constructor Details
#initialize(working_dir, image_name = "") ⇒ Images
Returns a new instance of Images.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cnvrg/Images.rb', line 13 def initialize(working_dir, image_name="") begin config = YAML.load_file(working_dir+"/.cnvrg/config.yml") idx = YAML.load_file(working_dir + "/.cnvrg/idx.yml") @working_dir = working_dir @commit_id =idx[:commit] @project_title = config[:project_name] @project_slug = config[:project_slug] @owner = config[:owner] @is_docker = config[:docker] if image_name.empty? @image_name = config[:image_base] @image_tag = config[:image_tag] @image_slug = find_image() else @image_name = image_name if !@image_name.nil? and !@image_name.empty? if image_name.include? ":" @image_name = image_name[0, image_name.index(":")] @image_tag = image_name[image_name.index(":")+1, image_name.size] else @image_name = image_name @image_tag = "lastest" end end @image_slug = find_image(false) update_image_activity(@commit_id, nil) config = {project_name: config[:project_name], project_slug: config[:project_slug], owner: config[:owner], docker: true, image_base: @image_name, image_tag: @image_tag, image_slug: image_slug} File.open(working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } end rescue => e end end |
Instance Attribute Details
#commit_id ⇒ Object (readonly)
Returns the value of attribute commit_id.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def commit_id @commit_id end |
#image_name ⇒ Object (readonly)
Returns the value of attribute image_name.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def image_name @image_name end |
#image_slug ⇒ Object (readonly)
Returns the value of attribute image_slug.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def image_slug @image_slug end |
#image_tag ⇒ Object (readonly)
Returns the value of attribute image_tag.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def image_tag @image_tag end |
#is_docker ⇒ Object (readonly)
Returns the value of attribute is_docker.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def is_docker @is_docker end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def owner @owner end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def port @port end |
#project_slug ⇒ Object (readonly)
Returns the value of attribute project_slug.
10 11 12 |
# File 'lib/cnvrg/Images.rb', line 10 def project_slug @project_slug end |
Class Method Details
.commit_custom_image(owner, slug, logs) ⇒ Object
137 138 139 140 |
# File 'lib/cnvrg/Images.rb', line 137 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
99 100 101 102 103 104 105 |
# File 'lib/cnvrg/Images.rb', line 99 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cnvrg/Images.rb', line 106 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
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cnvrg/Images.rb', line 73 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 |
.revoke_custom_new_image(owner, slug) ⇒ Object
133 134 135 136 |
# File 'lib/cnvrg/Images.rb', line 133 def self.revoke_custom_new_image(owner,slug) response = Cnvrg::API.request("users/#{owner}/images/#{slug}/revoke_image", 'GET') return response end |
.ssh_to_machine(resp) ⇒ 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/cnvrg/Images.rb', line 141 def self.ssh_to_machine(resp) sts_path = resp["result"]["sts_path"] uri = URI.parse(sts_path) http_object = Net::HTTP.new(uri.host, uri.port) http_object.use_ssl = true if uri.scheme == 'https' request = Net::HTTP::Get.new(sts_path) body = "" http_object.start do |http| response = http.request request body = response.read_body end URLcrypt::key = [body].pack('H*') ip = URLcrypt.decrypt(resp["result"]["machine_i"]) user = URLcrypt.decrypt(resp["result"]["machine_u"]) key = URLcrypt.decrypt(resp["result"]["machine_k"]) tempssh = Tempfile.new "sshkey" tempssh.write open(key).read tempssh.rewind key_path = tempssh.path count = 0 while count < 5 begin ssh = Net::SSH.start(ip, user=user, :keys => key_path, :timeout => 10) if !ssh.nil? return ssh else count+=1 sleep(2) end rescue count+=1 sleep(2) end end if tempssh tempssh.close tempssh.unlink end return false end |
Instance Method Details
#container_port ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/cnvrg/Images.rb', line 64 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_container(port = 7654, is_remote = false) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/cnvrg/Images.rb', line 270 def create_container(port=7654, is_remote=false) begin image_settings = { 'Image' => "#{@image_name}:latest", 'User' => 'ds', 'Cmd' => '/usr/local/cnvrg/run_ipython.sh', 'WorkingDir' => '/home/ds/notebooks', 'ExposedPorts' => { '8888/tcp' => {}, }, 'HostConfig' => { 'Binds' => ["#{@working_dir}:/home/ds/notebooks"], 'PortBindings' => { '8888/tcp' => [ {'HostPort' => "#{port}", 'HostIp' => 'localhost'} ], }, }, } container = Docker::Container.create(image_settings) container.start() netrc = File.open(File.('~')+"/.netrc", "rb") netrc_content = netrc.read container.store_file("/home/ds/.netrc", netrc_content) command = ["/bin/bash", "-lc", "sudo chmod 600 /home/ds/.netrc"] p = container.exec(command, tty: true) command = ["/bin/bash", "-lc", "sudo chown -R ds /home/ds/.netrc"] p = container.exec(command, tty: true) config = File.open(File.('~')+"/.cnvrg/config.yml", "rb") config_content = config.read container.store_file("/home/ds/.cnvrg/config.yml", config_content) command = ["/bin/bash", "-lc", "sudo chown -R ds /home/ds/.cnvrg"] container.exec(command, tty: true) # Libraries instlled save_installed_libraries(container) config = {project_name: @project_name, project_slug: @project_slug, owner: @owner, docker: true, image_base: @image_name, image_tag: @image_tag, container: container.id, port: port, image_slug: @image_slug} File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } return container rescue => e if e..include? "is not running" return create_container(port-1) end return false rescue SignalException say "\nAborting", Thor::Shell::Color::RED exit(1) end end |
#create_custom_image(new_image_name, working_dir, stored_commands) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cnvrg/Images.rb', line 195 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
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/cnvrg/Images.rb', line 392 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
371 372 373 374 375 376 377 378 379 |
# File 'lib/cnvrg/Images.rb', line 371 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_container(stop = false) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/cnvrg/Images.rb', line 236 def get_container(stop=false) begin container_id=is_container_exist() if !container_id return create_container() else container = Docker::Container.get(container_id) status = container.json["State"]["Status"] if status == "running" return container else if stop return false end res = container.start() if res.info["State"]["Status"].eql? "exited" and res.info["State"]["Error"].include? "port is already allocated" return create_container() end return container end end rescue => e if e..include? "No such container" return create_container() else return false end end end |
#get_image_state ⇒ Object
382 383 384 385 386 387 388 389 390 |
# File 'lib/cnvrg/Images.rb', line 382 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
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/cnvrg/Images.rb', line 354 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
449 450 451 452 453 454 455 456 457 |
# File 'lib/cnvrg/Images.rb', line 449 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
56 57 58 59 60 61 62 |
# File 'lib/cnvrg/Images.rb', line 56 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
437 438 439 440 441 442 |
# File 'lib/cnvrg/Images.rb', line 437 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
414 415 416 417 418 419 420 421 422 |
# File 'lib/cnvrg/Images.rb', line 414 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
344 345 346 347 348 349 350 351 352 |
# File 'lib/cnvrg/Images.rb', line 344 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
423 424 425 426 427 428 |
# File 'lib/cnvrg/Images.rb', line 423 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_installed_libraries(container) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/cnvrg/Images.rb', line 330 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
409 410 411 412 413 |
# File 'lib/cnvrg/Images.rb', line 409 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cnvrg/Images.rb', line 85 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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/cnvrg/Images.rb', line 221 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
444 445 446 447 |
# File 'lib/cnvrg/Images.rb', line 444 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
431 432 433 434 435 |
# File 'lib/cnvrg/Images.rb', line 431 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 |