Class: Vanagon::Engine::Docker

Inherits:
Base
  • Object
show all
Defined in:
lib/vanagon/engine/docker.rb

Instance Attribute Summary

Attributes inherited from Base

#remote_workdir, #target

Instance Method Summary collapse

Methods inherited from Base

#dispatch, #get_remote_workdir, #retrieve_built_artifact, #setup, #ship_workdir, #startup, #validate_platform

Constructor Details

#initialize(platform, target = nil, **opts) ⇒ Docker

Both the docker_image and the docker command itself are required for the docker engine to work



8
9
10
11
12
13
# File 'lib/vanagon/engine/docker.rb', line 8

def initialize(platform, target = nil, **opts)
  super

  @docker_cmd = Vanagon::Utilities.find_program_on_path('docker')
  @required_attributes << "docker_image"
end

Instance Method Details

#build_host_nameObject

Return the docker image name to build on



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vanagon/engine/docker.rb', line 21

def build_host_name
  if @build_host_name.nil?
    validate_platform
    # Docker requires container names to match: [a-zA-Z0-9][a-zA-Z0-9_.-]
    # So, transform slashes and colons commonly used as separators in
    # image names.
    @build_host_name = @platform.docker_image.gsub(%r{[/:]}, '_')
  end

  @build_host_name
end

#nameObject

Get the engine name



16
17
18
# File 'lib/vanagon/engine/docker.rb', line 16

def name
  'docker'
end

#select_targetObject

This method is used to obtain a vm to build upon using a docker container.

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vanagon/engine/docker.rb', line 36

def select_target # rubocop:disable Metrics/AbcSize
  extra_args = @platform.docker_run_args.nil? ? [] : @platform.docker_run_args

  Vanagon::Utilities.ex("#{@docker_cmd} run -d --name #{build_host_name}-builder -p #{@platform.ssh_port}:22 #{extra_args.join(' ')} #{@platform.docker_image}")
  @target = 'localhost'

  # Wait for ssh to come up in the container. Retry 5 times with a 1
  # second sleep between errors to account for network resets while SSHD
  # is starting. Allow a maximum of 5 seconds for SSHD to start.
  Vanagon::Utilities.retry_with_timeout(5, 5) do
    begin
      Vanagon::Utilities.remote_ssh_command("#{@target_user}@#{@target}", 'exit', @platform.ssh_port)
    rescue StandardError => e
      sleep(1) # Give SSHD some time to start.
      raise e
    end
  end
rescue StandardError => e
  raise Vanagon::Error.wrap(e, "Something went wrong getting a target vm to build on using docker. Ssh was not up in the container after 5 seconds.")
end

#teardownObject

This method is used to tell the vmpooler to delete the instance of the vm that was being used so the pool can be replenished.



59
60
61
62
63
64
# File 'lib/vanagon/engine/docker.rb', line 59

def teardown
  Vanagon::Utilities.ex("#{@docker_cmd} stop #{build_host_name}-builder")
  Vanagon::Utilities.ex("#{@docker_cmd} rm #{build_host_name}-builder")
rescue Vanagon::Error => e
  warn "There was a problem tearing down the docker container #{build_host_name}-builder (#{e.message})."
end