Class: Fidoci::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/fidoci/env.rb

Overview

Environment configuration of D encapsulates container image building and running commands in it

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, name, env_config) ⇒ Env

Returns a new instance of Env.



9
10
11
12
13
# File 'lib/fidoci/env.rb', line 9

def initialize(image, name, env_config)
    @name = name.to_s
    @image = image
    @env_config = env_config
end

Instance Attribute Details

#env_configObject

Returns the value of attribute env_config.



7
8
9
# File 'lib/fidoci/env.rb', line 7

def env_config
  @env_config
end

Instance Method Details

#build_imageObject



23
24
25
26
27
28
29
30
# File 'lib/fidoci/env.rb', line 23

def build_image
    image = Docker::Image.get(image_name) rescue nil
    unless image
        image = do_build_image
    end

    image
end

#clean!Object

clean images and containers associated with this Env



148
149
150
151
# File 'lib/fidoci/env.rb', line 148

def clean!
    clean_containers! rescue nil
    clean_image! rescue nil
end

#clean_container(cname) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/fidoci/env.rb', line 122

def clean_container(cname)
    begin
        debug "Cleaning container #{cname}..."
        container = Docker::Container.get(cname)
        container.remove(force: true, v: true)
    rescue
        debug "Cleaning failed"
        nil
    end
end

#clean_containers!Object



137
138
139
140
141
142
143
144
145
# File 'lib/fidoci/env.rb', line 137

def clean_containers!
    containers = [container_name]
    containers += links.map{|key, config| link_container_name(key) }

    containers.each{|cname|
        clean_container(cname)
    }
    true
end

#clean_image!Object



112
113
114
115
116
117
118
119
120
# File 'lib/fidoci/env.rb', line 112

def clean_image!
    begin
        debug "Cleaning image #{image_name}"
        image = Docker::Image.get(image_name)
        image.remove(force: true)
    rescue
        true
    end
end

#cmd(*args) ⇒ Object

run command in docker, building the image and starting services first if needed



174
175
176
177
178
179
180
181
182
# File 'lib/fidoci/env.rb', line 174

def cmd(*args)
    if build_image && link_containers
        debug "Running `#{args.join(' ')}`..."
        docker_run_or_exec(*args)
    else
        debug "Build failed"
        return false
    end
end

#commandsObject



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/fidoci/env.rb', line 364

def commands
    return false unless env_config['commands']

    success = env_config['commands'].all? { |command|
        info "Running `#{command}`..."

        unless command.is_a? Array
            command = command.split(/\s+/)
        end

        state = cmd(*command)
        info "Exited with state #{state}"

        state == 0
    }

    success
end

#config_params(params, config) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/fidoci/env.rb', line 311

def config_params(params, config)
    params['HostConfig'] ||= {}

    # env
    if config['environment']
        config['environment'].each {|key,val|
            params['Env'] ||= []
            params['Env'] << "#{key}=#{val}"
        }
    end

    if config['environment_pass']
        config['environment_pass'].each{|key|
            params['Env'] ||= []
            params['Env'] << "#{key}=#{ENV[key]}"

            puts "Warning: Passing environment variable #{key} is not locally set" unless ENV[key]
        }
    end

    # volumes
    if config['volumes']
        config['volumes'].each {|v|
            params['HostConfig']['Binds'] ||= []

            host_path, container_path = v.split(':')
            host_path = File.expand_path(host_path)

            params['HostConfig']['Binds'] << "#{host_path}:#{container_path}"
        }
    end

    # ports
    if config['ports']
        config['ports'].each {|p|
            parts = p.split(':')
            container_port = parts.last
            host_port = parts[-2]
            host_ip = parts[-3] || ""

            params['HostConfig']['PortBindings'] ||= {}
            params['HostConfig']['PortBindings']["#{container_port}/tcp"] = [
                {
                    "HostIp" => host_ip,
                    "HostPort" => host_port
                }
            ]
        }
    end

    params
end

#container_nameObject



108
109
110
# File 'lib/fidoci/env.rb', line 108

def container_name
    @image.gsub(/[^a-zA-Z0-9_]/, '_') + "_" + @name.gsub(/[^a-zA-Z0-9_]/, '_')
end

#debug(msg) ⇒ Object



15
16
17
# File 'lib/fidoci/env.rb', line 15

def debug(msg)
    puts msg if ENV['DEBUG']
end

#do_build_imageObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fidoci/env.rb', line 32

def do_build_image
    info "Building image #{image_name}..."
    params = {}

    params['forcerm'] = true
    params['t'] = image_name
    if env_config['dockerfile']
        params['dockerfile'] = env_config['dockerfile']
    end

    last_intermediate_image = nil
    image = Docker::Image.build_from_dir('.', params) do |chunk|
        json = JSON.parse(chunk)
        if (json['stream'] =~ /\ --->\ ([a-f0-9]{12})/) == 0
            last_intermediate_image = $1
        end
        $stdout << json['stream']
    end
    last_intermediate_image = nil

    image
ensure
    begin
        if last_intermediate_image
            img = Docker::Image.get(last_intermediate_image)
            img.json
            puts "Removing intermediate image #{last_intermediate_image}"
            img.remove('force' => true)
        end
    rescue
        nil
    end
end

#docker_exec(id, *args) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/fidoci/env.rb', line 239

def docker_exec(id, *args)
    params = {}
    params["Container"] = id
    params["AttachStdin"] = true
    params["AttachStdout"] = true
    params["AttachStderr"] = true
    params["Tty"] = true
    params["Cmd"] = args

    docker_exec = Docker::Exec.create(params)
    result = nil

    $stdin.raw do
        result = docker_exec.start!(tty: true, stdin: $stdin) do |msg|
            $stdout << msg
        end
    end

    debug "Exited with status #{result[2]}"

    result[2]
end

#docker_run(*args) ⇒ Object

calls docker run command with all needed parameters attaches stdin and stdout



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/fidoci/env.rb', line 264

def docker_run(*args)
    params = {}
    params['AttachStdin'] = true
    params['OpenStdin'] = true
    params['Tty'] = true
    params['name'] = container_name

    # links
    link_containers.each{|name, container_name|
        params['HostConfig'] ||= {}
        params['HostConfig']['Links'] ||= []
        params['HostConfig']['Links'] << "#{container_name}:#{name}"
    }

    config_params(params, env_config)
    params['Image'] = image_name
    params['Cmd'] = args

    #puts params

    container = Docker::Container.create(params)

    receiver = ->(msg) {
        $stdout << msg
        $stdout.flush
    }

    if $stdin && $stdin.tty?

        $stdin.raw do
            container.start!
            lines, cols = IO.console.winsize rescue [60,80]
            container.connection.post("/containers/#{container.id}/resize", h: lines, w: cols)
            container.attach(tty: true, stdin: $stdin, &receiver)
        end
    else
        container.start!.attach(tty: true, &receiver)
    end

    status = container.json['State']['ExitCode']
    debug "Exited with status #{status}"

    status
ensure
    clean_container(container_name)
end

#docker_run_or_exec(*args) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/fidoci/env.rb', line 229

def docker_run_or_exec(*args)
    container = Docker::Container.get(container_name) rescue nil

    if container
        docker_exec(container.id, *args)
    else
        docker_run(*args)
    end
end

#image_nameObject



66
67
68
# File 'lib/fidoci/env.rb', line 66

def image_name
    "%s:%s" % [@image, @name]
end

#info(msg) ⇒ Object



19
20
21
# File 'lib/fidoci/env.rb', line 19

def info(msg)
    puts msg
end


184
185
186
# File 'lib/fidoci/env.rb', line 184

def link_container_name(key)
    links[key]['shared'] || "#{container_name}_#{key}"
end

starts link containers and return dict name:container_name



189
190
191
192
193
# File 'lib/fidoci/env.rb', line 189

def link_containers
    links.map {|key, link_config|
        [key, start_link(link_container_name(key), link_config)]
    }
end


133
134
135
# File 'lib/fidoci/env.rb', line 133

def links
    env_config['links'] || []
end

#push(tag) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fidoci/env.rb', line 77

def push(tag)
    repo_tag = "#{@image}:#{tag}"
    image = Docker::Image.get(repo_tag)

    if ENV['DOCKER_AUTH_FILE']
      # load file
      auth_file = JSON.parse(File.read ENV['DOCKER_AUTH_FILE'])
      image_parts = @image.split('/')
	      registry = image_parts.size == 2 ? "https://#{image_parts[0]}" : "https://index.docker.io/v1/"
      creds = auth_file['auths'][registry]
      if creds['auth']
        creds['username'], creds['password'] = Base64.decode64(creds['auth']).split(':', 2)
        creds.delete 'auth'
      end
    else
      creds = {
        'username' => ENV['DOCKER_REGISTRY_USERNAME'],
        'password' => ENV['DOCKER_REGISTRY_PASSWORD'],
        'email' => ENV['DOCKER_REGISTRY_EMAIL']
      }
    end
   
    info "Pushing #{repo_tag}..."
    image.push(creds, 'repo_tag' => repo_tag) do |msg|
        json = JSON.parse(msg)
        $stdout.puts json['status']
        $stdout.puts json['progress'] if json['progress']
        $stdout.puts "Error: #{json['error']}" if json['error']
    end
end


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/fidoci/env.rb', line 195

def start_link(link_container_name, link_config)
    container = Docker::Container.get(link_container_name) rescue nil

    unless container
        params = {}
        params['name'] = link_container_name
        params['Image'] = link_config['image']

        config_params(params, link_config)

        debug "Creating container #{link_container_name}..."

        unless Docker::Image.exist?(link_config['image'])
            # obtain image
            link_image_name, link_image_tag = link_config['image'].split(':')
            link_image_tag = 'latest' unless link_image_tag

            full_image_name = "#{link_image_name}:#{link_image_tag}"
            Docker::Image.create(fromImage: full_image_name)
        end

        container = Docker::Container.create(params)
    end

    unless container.json['State']['Running']
        debug "Starting container #{link_container_name}..."
        container.start!
    end

    debug "Using container #{link_container_name}..."

    link_container_name
end

#stop!Object

stop services and clean main container



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fidoci/env.rb', line 154

def stop!
    links.map{|key, config|
        cname = link_container_name(key)
 		if links[key]['shared']
            debug "Keeping running #{cname}..."
            next
		end

        begin
            debug "Stopping container #{cname}..."
            container = Docker::Container.get(cname)
            container.stop!
        rescue
            nil
        end
    }
end

#tag_image(tag) ⇒ Object



70
71
72
73
74
75
# File 'lib/fidoci/env.rb', line 70

def tag_image(tag)
    info "Tagging images as #{@image}:#{tag}..."

    image = Docker::Image.get(image_name)
    image.tag('repo' => @image, 'tag' => tag, 'force' => true)
end