Class: Tomo::Testing::DockerImage

Inherits:
Object
  • Object
show all
Defined in:
lib/tomo/testing/docker_image.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.running_imagesObject (readonly)

Returns the value of attribute running_images.



23
24
25
# File 'lib/tomo/testing/docker_image.rb', line 23

def running_images
  @running_images
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/tomo/testing/docker_image.rb', line 27

def host
  @host
end

Instance Method Details

#build_and_runObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tomo/testing/docker_image.rb', line 29

def build_and_run
  raise "Already running!" if frozen?

  set_up_build_dir
  pull_base_image_if_needed
  set_up_private_key
  @image_id = build_image
  @container_id = start_container
  @host = Host.parse("deployer@localhost", port: find_ssh_port)
  DockerImage.running_images << self
  freeze
end

#puma_portObject



48
49
50
# File 'lib/tomo/testing/docker_image.rb', line 48

def puma_port
  Local.capture("docker port #{container_id} 3000")[/:(\d+)/, 1].to_i
end

#ssh_settingsObject

Connecting to SSH servers on local docker containers often triggers known_hosts errors due to each container potentially having a different host key. Work around this by using an empty blank temp file for storing known_hosts.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tomo/testing/docker_image.rb', line 56

def ssh_settings
  hosts_file = File.join(Dir.tmpdir, "tomo_#{SecureRandom.hex(8)}_hosts")
  {
    ssh_extra_opts: [
      "-o",
      "UserKnownHostsFile=#{hosts_file}",
      "-o",
      "IdentityFile=#{private_key_path}"
    ],
    ssh_strict_host_key_checking: false
  }
end

#stopObject



42
43
44
45
46
# File 'lib/tomo/testing/docker_image.rb', line 42

def stop
  DockerImage.running_images.delete(self)
  Local.capture("docker stop #{container_id}", raise_on_error: false)
  nil
end