Class: DockerRunner

Inherits:
Object
  • Object
show all
Includes:
LaunchSupport
Defined in:
lib/support/docker_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LaunchSupport

#is_port_open?, #wait_for_port, #wait_for_short_lived_image_to_complete

Constructor Details

#initialize(config) ⇒ DockerRunner

Returns a new instance of DockerRunner.



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
# File 'lib/support/docker_runner.rb', line 12

def initialize(config)
  @config = config
  @run_config = config['run']
  @build_config = config['build']
  @run_untagged = false
  
  @image_name = @run_config['image_name']
  if (!@image_name) 
    if (@build_config)
      @image_name = @build_config['name']
      raise "Build config does not have 'name'" if (@image_name == nil)
    else
      raise "Can't find an image name.  Either specify 'name' in build config or 'image_name' in run config."
    end
  end
  
  @registry = @config['registry'] 
  
  @instance_index = 0
  @use_extension = false
  @interact = false
  @remote_address = nil
  @api_container = nil
  @version = nil
  @secure_docker_api = true
  @build_args = nil
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/support/docker_runner.rb', line 7

def code
  @code
end

#deployment_idObject

Returns the value of attribute deployment_id.



8
9
10
# File 'lib/support/docker_runner.rb', line 8

def deployment_id
  @deployment_id
end

#docker_hostObject

Returns the value of attribute docker_host.



9
10
11
# File 'lib/support/docker_runner.rb', line 9

def docker_host
  @docker_host
end

#docker_host_secureObject

Returns the value of attribute docker_host_secure.



9
10
11
# File 'lib/support/docker_runner.rb', line 9

def docker_host_secure
  @docker_host_secure
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/support/docker_runner.rb', line 8

def host
  @host
end

#image_nameObject

Returns the value of attribute image_name.



7
8
9
# File 'lib/support/docker_runner.rb', line 7

def image_name
  @image_name
end

#instance_indexObject

Returns the value of attribute instance_index.



7
8
9
# File 'lib/support/docker_runner.rb', line 7

def instance_index
  @instance_index
end

#interactObject

Returns the value of attribute interact.



7
8
9
# File 'lib/support/docker_runner.rb', line 7

def interact
  @interact
end

#registryObject

Returns the value of attribute registry.



7
8
9
# File 'lib/support/docker_runner.rb', line 7

def registry
  @registry
end

#remote_addressObject

Returns the value of attribute remote_address.



8
9
10
# File 'lib/support/docker_runner.rb', line 8

def remote_address
  @remote_address
end

#run_untaggedObject

Returns the value of attribute run_untagged.



9
10
11
# File 'lib/support/docker_runner.rb', line 9

def run_untagged
  @run_untagged
end

#secure_docker_apiObject

Returns the value of attribute secure_docker_api.



8
9
10
# File 'lib/support/docker_runner.rb', line 8

def secure_docker_api
  @secure_docker_api
end

#use_extensionObject

Returns the value of attribute use_extension.



7
8
9
# File 'lib/support/docker_runner.rb', line 7

def use_extension
  @use_extension
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/support/docker_runner.rb', line 8

def user
  @user
end

#versionObject

Returns the value of attribute version.



8
9
10
# File 'lib/support/docker_runner.rb', line 8

def version
  @version
end

Instance Method Details

#find_machine_apisObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/support/docker_runner.rb', line 40

def find_machine_apis
  @run_on_apis = []
  @all_docker_apis_in_cluster = []
  if (@deploy_config != nil) && (@deploy_config[:hosts] != nil)
    @deploy_config[:hosts].each do |host_key|
      host = Hosts::HOSTS[host_key]
      address = "tcp://#{host[:address]}:#{host[:docker_port]}"
      docker_api = DockerApi.new(address, host[:ssl])
      @all_docker_apis_in_cluster << docker_api
      @run_on_apis << docker_api if (address == @remote_address) || ( @deploy_config[:targets][run_name] == :all)
    end
  else
    # Config not given, we need to create api from remote address and ssl setting.  This is the only api for this class.
    @docker_host = @docker_host || ENV['DOCKER_HOST']
    @docker_host_secure = @docker_host_secure || (ENV['DOCKER_TLS_VERIFY'].to_s == '1')
    docker_api = DockerApi.new(@docker_host, @docker_host_secure)
    
    @all_docker_apis_in_cluster << docker_api
    @run_on_apis << docker_api
  end
end

#image_versionObject



66
67
68
69
70
71
# File 'lib/support/docker_runner.rb', line 66

def image_version
  return @version if (@version != nil)
  the_version = 'latest'
  the_version = @build_config['version'] if (@build_config) && (@build_config['version'] != nil)
  return the_version
end

#kill_all(name_prefix) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/support/docker_runner.rb', line 73

def kill_all(name_prefix)
  find_machine_apis
  @all_docker_apis_in_cluster.each do |api|
    #options = {filters: {status: [:running, :exited, :paused, :restarting]}}
    options = {}
    containers = Docker::Container.all(options.to_json, api.connection)
    containers.each do |container|
      if (container.json["Name"].start_with?("/#{name_prefix}"))
        pp "Killing and removing: #{container.json["Name"]}"
        container.kill() if (container.json["State"]["Running"])
        container.remove() if (container != nil)
      else
        pp " * Container didn't match: #{container.json["Name"]}"
      end
    end
  end
end

#runObject



92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/support/docker_runner.rb', line 92

def run
  find_machine_apis
  raise "No run on apis" if (@run_on_apis.count == 0)

  code = @code
  instance_index = 1
  if (@instance_index == :next)
    options = {filters: {status: [:running]}}
    @active_container_names = []
    containers = Docker::Container.all(options.to_json, @run_on_apis.first.connection)
    containers.each do |container|
      container_json = container.json
      container_name = container_json["Name"]
      @active_container_names << container_name
    end
    
    instance_index = 1
    container_name_taken = true
    while (true)
      instance_ext = "_#{instance_index}"
      instance_name = "/#{run_name}#{instance_ext}"
      if (!@active_container_names.include? instance_name)
        @instance_index = instance_index
        break
      else
        instance_index += 1
      end
    end
  end
  
  instance_ext = ""
  instance_ext = "_#{@instance_index}" if (@use_extension)
  instance_name = "#{run_name}#{instance_ext}"

  @all_docker_apis_in_cluster.each do |docker_api|
    kill_previous_containers_in_cluster(docker_api, instance_name)
  end
  image_name = image_name_with_registry()

  @run_on_apis.each do |docker_api|
    if (!@run_untagged)
      pull_image_to_host(docker_api, registry)
    end
    run_container(docker_api, instance_name)
  end
end

#run_nameObject



62
63
64
# File 'lib/support/docker_runner.rb', line 62

def run_name
  return @run_config['name']
end