class Armada::Deploy::Rolling

Attributes

environment[R]
options[R]
project[R]

Public Class Methods

new(project, environment, options) click to toggle source
# File lib/armada/deploy/rolling.rb, line 6
def initialize(project, environment, options)
  @project     = project
  @environment = environment
  @options     = Armada::Configuration.load!(project, environment, options)
  @declared_hostname = @options[:hostname]
end

Public Instance Methods

run() click to toggle source
# File lib/armada/deploy/rolling.rb, line 13
def run
  Armada.ui.info "Deploying the following image [#{@options[:image]}:#{@options[:tag]}]"
  begin
    @options[:hosts].each_in_parallel do |host|
      if host.is_a? String
        docker_host_connect_string = host
      else
        docker_host_connect_string = host[:docker_host]
      end
      docker_host = Armada::Host.create(docker_host_connect_string, options)
      docker_host.get_image(@options[:image], @options[:tag], @options).pull
    end

    @options[:hosts].each do |host|
      if host.is_a? String
        docker_host_connect_string = host
        health_check_host = host.split(':')[0]
      else
        docker_host_connect_string = host[:docker_host]
        health_check_host = host[:health_check_host]
      end

      unless @declared_hostname
        @options[:hostname] = health_check_host
      end

      docker_host = Armada::Host.create(docker_host_connect_string, @options)
      image = docker_host.get_image @options[:image], @options[:tag], @options
      container = Armada::Container.new(image, docker_host, @options)
      container.stop
      container.start

      if @options[:health_check] && @options[:health_check_port]
        ports = container.ports

        # if --net=host, then there's no port mapping to be done.
        if container.container.json["HostConfig"]["NetworkMode"] != "host"
          if ports.empty?
            raise "No ports exposed for this container. Please expose a port for the health check or use the --no-health-check option!"
          end

          begin
            health_check_port = ports["#{@options[:health_check_port]}/tcp"][0]["HostPort"]
          rescue Exception => e
            raise "Could not find the host port for [#{health_check_port}]. Make sure you put the container port as the :health_check_port."
          end
        else
          health_check_port = @options[:health_check_port]
        end

        health_check = Armada::Connection::HealthCheck.new(
          health_check_host,
          health_check_port,
          @options[:health_check_endpoint],
          @options[:health_check_delay],
          @options[:health_check_retries],
          @options[:ssh_gateway],
          @options[:ssh_gateway_user]
        )

        raise "Health check failed! - #{health_check_host}" unless health_check.run
      end
    end
  rescue Exception => e
    Armada.ui.error "#{e.message} \n\n #{e.backtrace.join("\n")}"
    exit(1)
  end
end