Class: GoBuilder::GoDocker

Inherits:
Object
  • Object
show all
Defined in:
lib/go_builder/go_docker.rb

Class Method Summary collapse

Class Method Details

.create_and_start_container(args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/go_builder/go_docker.rb', line 52

def self.create_and_start_container args
  # update the timeout for the Excon Http Client
  # set the chunk size to enable streaming of log files
  Docker.options = {:chunk_size => 1, :read_timeout => 3600}

  container = Docker::Container.create(
    'Image' => args['build_args']['image'],
    'Cmd' => ['/bin/bash'],
    'Tty' => true,
    "Binds" => ["#{ENV['GOPATH']}/src:/go/src"],
    "Env" => args['build_args']['env'],
    'WorkingDir' => args['build_args']['working_directory'])
  container.start

  return container
end

.find_image(image_name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/go_builder/go_docker.rb', line 16

def self.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

.find_running_containerObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/go_builder/go_docker.rb', line 39

def self.find_running_container
  containers = Docker::Container.all(:all => true)
  found = nil

  containers.each do | container |
    if container.info["Image"] == "golang" && container.info["Status"].start_with?("Up")
      return container
    end
  end

  return nil
end

.get_container(args) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/go_builder/go_docker.rb', line 30

def self.get_container args
  container = self.find_running_container
  if container != nil
    return container
  else
    return self.create_and_start_container(args)
  end
end

.get_docker_ip_addressObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/go_builder/go_docker.rb', line 3

def self.get_docker_ip_address
  if !ENV['DOCKER_HOST']
    return "127.0.0.1"
  else
    # dockerhost set
    host = ENV['DOCKER_HOST'].dup
    host.gsub!(/tcp:\/\//, '')
    host.gsub!(/:\d+/,'')

    return host
  end
end

.pull_image(image_name) ⇒ Object



25
26
27
28
# File 'lib/go_builder/go_docker.rb', line 25

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

.tag_and_push(args) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/go_builder/go_docker.rb', line 69

def self.tag_and_push args
  image =  find_image "#{args[:go][:application_name]}:latest"
  image.tag('repo' => "#{args[:docker_registry][:namespace]}/#{args[:go][:application_name]}", 'force' => true) unless image.info["RepoTags"].include? "#{args[:docker_registry][:namespace]}/#{args[:go][:application_name]}:latest"

  sh "docker login -u #{args[:docker_registry][:user]} -p #{args[:docker_registry][:password]} -e #{args[:docker_registry][:email]} #{args[:docker_registry][:url]}"
  sh "docker push #{args[:docker_registry][:namespace]}/#{args[:go][:application_name]}:latest"
end