Class: DockerImageCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/support/docker_image_creator.rb

Direct Known Subclasses

DockerRailsAppCreator

Constant Summary collapse

REMOVE_INTERMEDIATE_FLAG =
"--rm=true"
DAEMON_OPTIONS =
" -d -t -P"
INTERACT_OPTIONS =
" -a stdin -a stdout -i -t -P"
DOCKER_DIR =
Dir.pwd + "/lib"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ DockerImageCreator

Returns a new instance of DockerImageCreator.



15
16
17
18
19
20
21
22
# File 'lib/support/docker_image_creator.rb', line 15

def initialize(definition)
  raise "Definition is nil" if definition == nil
  @definition = definition
  @home = FileUtils.pwd
  @version = nil
  @docker_host = nil
  @docker_host_secure = nil
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



13
14
15
# File 'lib/support/docker_image_creator.rb', line 13

def definition
  @definition
end

#docker_hostObject

Returns the value of attribute docker_host.



12
13
14
# File 'lib/support/docker_image_creator.rb', line 12

def docker_host
  @docker_host
end

#docker_host_secureObject

Returns the value of attribute docker_host_secure.



12
13
14
# File 'lib/support/docker_image_creator.rb', line 12

def docker_host_secure
  @docker_host_secure
end

#image_name_forcedObject

Returns the value of attribute image_name_forced.



12
13
14
# File 'lib/support/docker_image_creator.rb', line 12

def image_name_forced
  @image_name_forced
end

#use_date_as_versionObject

Returns the value of attribute use_date_as_version.



5
6
7
# File 'lib/support/docker_image_creator.rb', line 5

def use_date_as_version
  @use_date_as_version
end

#version_forcedObject

Returns the value of attribute version_forced.



12
13
14
# File 'lib/support/docker_image_creator.rb', line 12

def version_forced
  @version_forced
end

Instance Method Details

#add_it(host, host_ssl, run_instance_count, registry) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/support/docker_image_creator.rb', line 176

def add_it(host, host_ssl, run_instance_count, registry)
  @docker_host = host
  @docker_host_secure = host_ssl
  number_added = 0
  while (number_added < run_instance_count)
    run_def = @definition[:run]
    run_name = run_def[:name]
    extension = 1
    run_with_extension(:next, registry, false, true)
    number_added += 1
  end
end

#after_build(definition) ⇒ Object



225
226
227
# File 'lib/support/docker_image_creator.rb', line 225

def after_build(definition)
  delete_image_support_from_stage()
end

#api_image(registry, docker_connection) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/support/docker_image_creator.rb', line 116

def api_image(registry, docker_connection)
  full_name = "#{registry}/#{image_name}"
  full_name += ":#{version()}" if (version() != nil)
  image = Docker::Image.get(full_name, {}, docker_connection)
  puts "Pushing Image: #{full_name} ..."
  return image
end

#before_build(definition) ⇒ Object



220
221
222
223
# File 'lib/support/docker_image_creator.rb', line 220

def before_build(definition)
  delete_image_support_from_stage()
  copy_image_support_to_stage()
end

#build_itObject



44
45
46
47
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
84
85
86
# File 'lib/support/docker_image_creator.rb', line 44

def build_it()
  docker_connection = docker_api.connection
  build_def = @definition[:build]
  if (build_def == nil)
    puts "Warning: build not defined for #{definition[:code]} - ignoring request"
  else
    begin
      before_build(@definition)
      remove_intermediate = REMOVE_INTERMEDIATE_FLAG
  
      volume_path = build_def[:volume_path]
      build_command = nil
    
      image_and_version = image_name()
      image_and_version += ":#{version()}" if (version() != nil)

      opts = build_def[:api_options] || {}
      opts[:t] = "#{image_and_version}"
      opts[:rm] = true

      Excon.defaults[:write_timeout] = 1000
      Excon.defaults[:read_timeout] = 1000
      puts "Building image.  Docker dir: #{docker_dir} ..."
      
      image = Docker::Image.build_from_dir(docker_dir, opts, docker_connection) do |v|
        begin
          if (log = JSON.parse(v)) && log.has_key?("stream")
            $stdout.puts log["stream"]
          end
        rescue Exception => ex
          puts "#{v}"
        end
      end
    rescue Exception => ex
      puts"Exception: #{ex.message}"
      puts ex.backtrace
      debugger
      raise "Exception building image.  #{ex.message}\nCheck connection to: #{docker_connection}"
    ensure
      after_build(@definition)
    end
  end
end

#copy_image_support_to_stageObject



237
238
239
240
241
242
# File 'lib/support/docker_image_creator.rb', line 237

def copy_image_support_to_stage
  raise "Definition nil" if @definition == nil
  raise "Build Definition nil" if @definition[:build] == nil
  image_support_dir = "#{FileUtils.pwd}/lib/support/image_support/"
  FileUtils.cp_r(image_support_dir, docker_dir())
end

#delete_image_support_from_stageObject



229
230
231
# File 'lib/support/docker_image_creator.rb', line 229

def delete_image_support_from_stage()
  FileUtils.remove_dir("#{docker_dir}image_support", true)
end

#do_push(registry) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/support/docker_image_creator.rb', line 124

def do_push(registry)
  if (registry)
    Excon.defaults[:write_timeout] = 1000
    Excon.defaults[:read_timeout] = 1000
  
    docker_connection = docker_api.connection

    image = api_image(registry, docker_connection)

    credentials = nil
    result = image.push(credentials, {tag: version, repo: registry}) 
    puts "Done pushing Image: #{registry}/#{image_name}"
    the_registry = DockerRegistry.new(registry)
    
    if (the_registry.has_image_with_version?("#{image_name}", version) == false) 
      raise "After push, image doesn't appear in registry.  Image: #{image_name}:#{version}.  Push result: #{result}"
    end
  else
    puts "No registry specified for pushing"
  end
end

#do_tag(registry) ⇒ Object

def do_tag(image_name, version, registry)



103
104
105
106
107
108
# File 'lib/support/docker_image_creator.rb', line 103

def do_tag(registry)
  docker_connection = docker_api.connection
  image = Docker::Image.get("#{image_name}:#{version}", {}, docker_connection)
  puts "Tagging image: #{image_name}"
  image.tag('repo' => "#{registry}/#{image_name()}", 'image' => 'unicorn', 'tag' => version(), force: true)
end

#docker_apiObject



36
37
38
39
40
41
42
# File 'lib/support/docker_image_creator.rb', line 36

def docker_api
  @docker_host = @docker_host || ENV['DOCKER_HOST']
  @docker_host_secure = @docker_host_secure || (ENV['DOCKER_TLS_VERIFY'].to_s == '1')
  raise "Need to define DOCKER_HOST env variable" if (!@docker_host)
  raise "Need to define DOCKER_TLS_VERIFY env variable" if (@docker_host_secure == nil)
  docker_api = DockerApi.new(@docker_host, @docker_host_secure)
end

#docker_dirObject



233
234
235
# File 'lib/support/docker_image_creator.rb', line 233

def docker_dir
  return @definition[:build][:docker_directory] + "/"
end

#image_nameObject



28
29
30
31
32
33
34
# File 'lib/support/docker_image_creator.rb', line 28

def image_name
  return (@image_name_forced) if (@image_name_forced)
  build_def = @definition[:build]
  name = @definition[:run][:image_name] if (@definition[:run])
  name = build_def[:name] if (build_def)
  return name
end

#kill_all(host, host_ssl) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/support/docker_image_creator.rb', line 189

def kill_all(host, host_ssl)
  docker_runner = DockerRunner.new(self, nil)
  docker_runner.registry = nil
  docker_runner.use_extension = true
  docker_runner.interact =  false
  docker_runner.remote_address = host
  docker_runner.secure_docker_api = host_ssl

  run_def = @definition[:run]
  run_name = run_def[:name]
  docker_runner.kill_all(run_name)
end

#openlogic_homeObject



215
216
217
218
# File 'lib/support/docker_image_creator.rb', line 215

def openlogic_home()
  @openlogic_home = @openlogic_home[0..(@openlogic_home.length - 2)] if (@openlogic_home.end_with? "/")
  return @openlogic_home
end

#push_it(registry) ⇒ Object



110
111
112
113
114
# File 'lib/support/docker_image_creator.rb', line 110

def push_it(registry)
  raise "DOCKER_REGISTRY environment variable not defined for push operation" if registry == nil
  build_def = @definition[:build]
  do_push(registry)
end

#run_it(host, host_ssl, interact, run_instance_count, registry = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/support/docker_image_creator.rb', line 146

def run_it(host, host_ssl, interact, run_instance_count, registry = nil)
  @docker_host = host
  @docker_host_secure = host_ssl
  puts "Warning:  registry not defined, assuming run is local" if registry == nil
  run_def = @definition[:run]
  build_def = @definition[:build]
  if (run_def == nil)
    puts "No run definition for: #{@definition[:code]}" 
  else
    (1..run_instance_count).each do |instance_index|
      use_extension = (run_instance_count > 1)
      run_with_extension(:next, registry, interact, use_extension)
    end
  end
end

#run_with_extension(instance_index, registry, interact, use_extension) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/support/docker_image_creator.rb', line 162

def run_with_extension(instance_index, registry, interact, use_extension)
  @docker_api = DockerApi.new(@docker_host)
  docker_runner = DockerRunner.new(self, nil)
  docker_runner.registry = registry
  docker_runner.instance_index = instance_index
  docker_runner.use_extension = use_extension
  docker_runner.interact =  interact
  docker_runner.image_name = image_name
  docker_runner.code = @definition[:code]
  docker_runner.remote_address = @docker_host
  docker_runner.secure_docker_api = @docker_host_secure
  docker_runner.run_with_extension
end

#subtract(host, host_ssl, count) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/support/docker_image_creator.rb', line 202

def subtract(host, host_ssl, count)
  docker_runner = DockerRunner.new(self, nil)
  docker_runner.registry = nil
  docker_runner.use_extension = true
  docker_runner.interact =  false
  docker_runner.remote_address = host
  docker_runner.secure_docker_api = host_ssl

  run_def = @definition[:run]
  run_name = run_def[:name]
  docker_runner.kill(run_name, count)
end

#tag_it(registry) ⇒ Object



97
98
99
100
# File 'lib/support/docker_image_creator.rb', line 97

def tag_it(registry)
  raise "DOCKER_REGISTRY environment variable not defined for push operation" if registry == nil
  do_tag(registry)
end

#use_date_as_version?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/support/docker_image_creator.rb', line 24

def use_date_as_version?
  false
end

#versionObject



88
89
90
91
92
93
94
95
# File 'lib/support/docker_image_creator.rb', line 88

def version
  return (@version_forced) if (@version_forced)
  build_def = @definition[:build]
  raise "No build definition for: #{@definition[:code]}" if (!build_def)
  version = 'latest'
  version = build_def[:version] if (build_def[:version] != nil)
  return version
end