Class: Toaster::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/toaster/util/docker.rb

Class Method Summary collapse

Class Method Details

.get_container_namesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toaster/util/docker.rb', line 39

def self.get_container_names()
  result = []
  docker_containers = `docker ps | grep prototypes: | awk '{print $1}'`.strip.split("\n")
  all_cont_id_files = `find /lxc/ -maxdepth 2 -name "docker.container.id"`.strip.gsub(/\n/, " ")
  docker_containers.each do |docker_cont|
    cont_id_files = `grep -l -R "#{docker_cont}" #{all_cont_id_files}`.strip.split("\n")
    cont_id_files.each do |cont_id_file|
      cont_id_file = cont_id_file.gsub(/\/lxc\/([^\/]+)\/.*/, '\1')
      result << cont_id_file
    end
  end
  return result
end

.kill_container(lxc_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/toaster/util/docker.rb', line 53

def self.kill_container(lxc_name)
  container_id = `cat /lxc/#{lxc_name}/docker.container.id 2> /dev/null`
  container_id = container_id.strip
  puts "DEBUG: Killing docker container ID '#{container_id}'"
  if container_id != ""
    `docker kill #{container_id} 2> /dev/null`
    `docker rm #{container_id} 2> /dev/null`
  else
    puts "WARN: Could not find valid docker container ID in file /lxc/#{lxc_name}/docker.container.id"
  end
end

.save_container_changes(lxc_name, repository = "prototypes") ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/toaster/util/docker.rb', line 14

def self.save_container_changes(lxc_name, repository="prototypes")
  oldImgID=`cat /lxc/#{lxc_name}/docker.image.id 2> /dev/null`
  oldImgID = oldImgID.strip
  if oldImgID != ""
    # get existing container ID
    instID=`cat /lxc/#{lxc_name}/docker.container.id`
    instID = instID.strip
    # save container with the new changes
    puts "DEBUG: Committing container changes using command: docker commit #{instID} #{repository} #{lxc_name}"
    newImgID=`docker commit #{instID} #{repository} #{lxc_name}`
    newImgID = newImgID.strip
    images = `docker images | grep -v REPOSITORY | grep #{repository} | grep "#{lxc_name}"`
    images = images.strip.split("\n")
    puts "DEBUG: existing docker images for name '#{lxc_name}' (old image ID '#{oldImgID}'): #{images}"
    if images.size > 1
      # remove old docker image
      `docker rmi #{oldImgID}`
      # update new image id
      `echo #{newImgID} > /lxc/#{lxc_name}/docker.image.id`
    end
  else
    puts "WARN: Could not save container changes. Container image file empty: /lxc/#{lxc_name}/docker.image.id"
  end
end