Class: Minke::Docker::DockerRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/minke/docker/docker_runner.rb

Instance Method Summary collapse

Instance Method Details

#build_image(dockerfile_dir, name) ⇒ Object

build_image creates a new image from the given Dockerfile and name



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/minke/docker/docker_runner.rb', line 87

def build_image dockerfile_dir, name
  ::Docker.options = {:read_timeout => 6200}
  begin
    ::Docker::Image.build_from_dir(dockerfile_dir, {:t => name}) do |v|
      data = /{"stream.*:"(.*)".*/.match(v)
      data = data[1].encode(Encoding.find('UTF-8'), {invalid: :replace, undef: :replace, replace: ''}) unless data == nil || data.length < 1
      $stdout.puts data unless data == nil
    end
  rescue => e
    message = /.*{"message":"(.*?)"}/.match(e.to_s)
    puts "Error: #{message[1]}" unless message == nil || message.length < 1
  end
end

#create_and_run_container(image, volumes, environment, working_directory, cmd) ⇒ Object

create_and_run_container starts a conatainer of the given image name and executes a command

Returns:

  • Docker::Container

  • sucess (true if command succeded without error)



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/minke/docker/docker_runner.rb', line 48

def create_and_run_container image, volumes, environment, working_directory, cmd
	# update the timeout for the Excon Http Client
	# set the chunk size to enable streaming of log files
  #puts working_directory
  puts volumes
  #puts environment

  ::Docker.options = {:chunk_size => 1, :read_timeout => 3600}
  container = ::Docker::Container.create(
		'Image' => image,
		'Cmd' => cmd,
		"Binds" => volumes,
		"Env" => environment,
		'WorkingDir' => working_directory)

  success = true

  thread = Thread.new do
    container.attach(:stream => true, :stdin => nil, :stdout => true, :stderr => true, :logs => false, :tty => false) do
       |stream, chunk|
        stream.to_s == 'stdout' ? color = :green : color  = :red
        puts "#{chunk.strip}".colorize(color)

        if stream.to_s == "stderr"
          success = false
        else
          success = true
        end
    end
  end

  container.start
  thread.join

	return container, success
end

#delete_container(container) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/minke/docker/docker_runner.rb', line 101

def delete_container container
  if container != nil
    begin
      container.delete()
    rescue => e
      puts "Error: Unable to delete container"
    end
  end
end

#find_image(image_name) ⇒ Object

find_image finds a docker image in the local registry Returns

Docker::Image



26
27
28
29
30
31
32
33
# File 'lib/minke/docker/docker_runner.rb', line 26

def find_image image_name
	found = nil
	::Docker::Image.all.each do | image |
		found = image if image.info["RepoTags"].include? image_name
	end

	return found
end

#get_docker_ip_addressObject

returns the ip address that docker is running on



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/minke/docker/docker_runner.rb', line 6

def get_docker_ip_address
  if ENV['DOCKER_IP'] == nil
    if ENV['DOCKER_HOST']
  		# dockerhost set
  		host = ENV['DOCKER_HOST'].dup
  		host.gsub!(/tcp:\/\//, '')
  		host.gsub!(/:\d+/,'')

  		return host
    else
      return '127.0.0.1'
  	end
  end
end

#login_registry(url, user, password, email) ⇒ Object



111
112
113
114
# File 'lib/minke/docker/docker_runner.rb', line 111

def  url, user, password, email
  system("docker login -u #{user} -p #{password} -e #{email} #{url}")
  $?.exitstatus
end

#pull_image(image_name) ⇒ Object

pull_image pulls a new copy of the given image from the registry



37
38
39
40
# File 'lib/minke/docker/docker_runner.rb', line 37

def pull_image image_name
	puts "Pulling Image: #{image_name}"
	puts `docker pull #{image_name}`
end

#push_image(image_name) ⇒ Object



121
122
123
124
# File 'lib/minke/docker/docker_runner.rb', line 121

def push_image image_name
	system("docker push #{image_name}:latest")
  $?.exitstatus ==  0
end

#tag_image(image_name, tag) ⇒ Object



116
117
118
119
# File 'lib/minke/docker/docker_runner.rb', line 116

def tag_image image_name, tag
  image =  self.find_image "#{image_name}:latest"
	image.tag('repo' => tag, 'force' => true) unless image.info["RepoTags"].include? "#{tag}:latest"
end