Class: Jarl::Docker::Container

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

Overview

Container

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Container

Returns a new instance of Container.



268
269
270
# File 'lib/jarl/docker.rb', line 268

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



266
267
268
# File 'lib/jarl/docker.rb', line 266

def id
  @id
end

Class Method Details

.clean_containers(*names) ⇒ Object



341
342
343
344
345
346
347
348
349
350
# File 'lib/jarl/docker.rb', line 341

def self.clean_containers(*names)
  names.each do |name|
    begin
      sh "docker rm #{name} >/dev/null 2>&1"
    rescue RuntimeError => e
      # omit reports on missing container
      raise unless e.to_s =~ /Command failed/
    end
  end
end

Instance Method Details

#clean!Object



332
333
334
# File 'lib/jarl/docker.rb', line 332

def clean!
  self.class.clean_containers(name)
end

#container_inspectObject



276
277
278
279
280
# File 'lib/jarl/docker.rb', line 276

def container_inspect
  return @container_inspect if @container_inspect
  # puts "Loading inspect for: #{ps}"
  @container_inspect ||= JSON.parse(`docker inspect #{id}`).first
end

#imageObject



291
292
293
# File 'lib/jarl/docker.rb', line 291

def image
  container_inspect['Config']['Image']
end

#image_idObject



295
296
297
# File 'lib/jarl/docker.rb', line 295

def image_id
  container_inspect['Image']
end

#ipObject



299
300
301
# File 'lib/jarl/docker.rb', line 299

def ip
  container_inspect['NetworkSettings']['IPAddress']
end

#long_idObject



282
283
284
# File 'lib/jarl/docker.rb', line 282

def long_id
  container_inspect['Id']
end

#nameObject



286
287
288
289
# File 'lib/jarl/docker.rb', line 286

def name
  ps['NAMES']
  # container_inspect['Name'].gsub('/', '')
end

#open_ssh_session!(params = {}, command = nil) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/jarl/docker.rb', line 314

def open_ssh_session!(params = {}, command = nil)
  ssh_flags = ['-oStrictHostKeyChecking=no']
  if params['ssh_identity']
    ssh_flags << "-i #{params['ssh_identity']}" if params['ssh_identity'].is_a?(String)
    if params['ssh_identity'].is_a?(Array)
      ssh_flags << params['ssh_identity'].map { |i| "-i #{i}" }
    end
  end
  ssh_user = params['ssh_user'] ? "#{params['ssh_user']}@" : ''
  ssh_cmd = command ? "-C \"#{command.join(' ')}\"" : ''
  sh "ssh #{ssh_flags.join(' ')} #{ssh_user}#{ip} #{ssh_cmd}"
end

#portsObject



303
304
305
306
307
308
# File 'lib/jarl/docker.rb', line 303

def ports
  port_maps = container_inspect['NetworkSettings']['Ports']
  port_maps.keys.map do |port|
    { from: port, to: port_maps[port] }
  end
end

#psObject



272
273
274
# File 'lib/jarl/docker.rb', line 272

def ps
  Docker.ps[id] or fail "Failed to find ps info for #{id}"
end

#stop!Object



327
328
329
330
# File 'lib/jarl/docker.rb', line 327

def stop!
  sh "docker stop #{name}"
  clean!
end

#uptimeObject



336
337
338
339
# File 'lib/jarl/docker.rb', line 336

def uptime
  container_inspect['State'] && container_inspect['State']['StartedAt'] &&
  Time.now.utc - DateTime.parse(container_inspect['State']['StartedAt']).to_time.utc
end

#volumesObject



310
311
312
# File 'lib/jarl/docker.rb', line 310

def volumes
  container_inspect['Volumes']
end