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
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) ⇒ 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.
10 11 12 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 |
# File 'lib/cnvrg/Images.rb', line 10 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.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def commit_id @commit_id end |
#image_name ⇒ Object (readonly)
Returns the value of attribute image_name.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def image_name @image_name end |
#image_slug ⇒ Object (readonly)
Returns the value of attribute image_slug.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def image_slug @image_slug end |
#image_tag ⇒ Object (readonly)
Returns the value of attribute image_tag.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def image_tag @image_tag end |
#is_docker ⇒ Object (readonly)
Returns the value of attribute is_docker.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def is_docker @is_docker end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def owner @owner end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def port @port end |
#project_slug ⇒ Object (readonly)
Returns the value of attribute project_slug.
7 8 9 |
# File 'lib/cnvrg/Images.rb', line 7 def project_slug @project_slug end |
Class Method Details
.image_exist(owner, image_name) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cnvrg/Images.rb', line 70 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
61 62 63 64 65 66 67 68 |
# File 'lib/cnvrg/Images.rb', line 61 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
172 173 174 175 176 177 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/cnvrg/Images.rb', line 172 def create_container(port=7654, is_remote=false) begin image_settings = { 'Image' => "#{@image_name}:#{@image_tag}", 'User' => 'ds', 'Cmd' => '/home/ds/run_ipython.sh', 'ExposedPorts' => { '8888/tcp' => {}, }, 'HostConfig' => { 'Binds' => ["#{@working_dir}:/home/ds/notebooks"], 'PortBindings' => { '8888/tcp' => [ {'HostPort' => "#{port}", 'HostIp' => 'localhost'} ], }, }, } # if !is_remote # image_settings['HostConfig'].merge!({ 'Binds' => ["#{@working_dir}:/home/ds/notebooks"]}) # end 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) command = ["/bin/bash", "-lc", "mkdir /home/ds/.cnvrg"] container.exec(command, tty: true) command = ["/bin/bash", "-lc", "mkdir /home/ds/.cnvrg/tmp"] 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
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 |
# File 'lib/cnvrg/Images.rb', line 97 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
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/cnvrg/Images.rb', line 298 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
277 278 279 280 281 282 283 284 285 |
# File 'lib/cnvrg/Images.rb', line 277 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
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 |
# File 'lib/cnvrg/Images.rb', line 138 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
288 289 290 291 292 293 294 295 296 |
# File 'lib/cnvrg/Images.rb', line 288 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
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/cnvrg/Images.rb', line 260 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
355 356 357 358 359 360 361 362 363 |
# File 'lib/cnvrg/Images.rb', line 355 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
53 54 55 56 57 58 59 |
# File 'lib/cnvrg/Images.rb', line 53 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
343 344 345 346 347 348 |
# File 'lib/cnvrg/Images.rb', line 343 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
320 321 322 323 324 325 326 327 328 |
# File 'lib/cnvrg/Images.rb', line 320 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) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/cnvrg/Images.rb', line 252 def remote_notebook(notebook_path, instance_type, kernel) 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}) return response end |
#remove_note_slug ⇒ Object
329 330 331 332 333 334 |
# File 'lib/cnvrg/Images.rb', line 329 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
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/cnvrg/Images.rb', line 238 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
315 316 317 318 319 |
# File 'lib/cnvrg/Images.rb', line 315 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cnvrg/Images.rb', line 82 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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cnvrg/Images.rb', line 123 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
350 351 352 353 |
# File 'lib/cnvrg/Images.rb', line 350 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
337 338 339 340 341 |
# File 'lib/cnvrg/Images.rb', line 337 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 |