class Kitchen::Transport::Dokken::Connection

@author Sean OMeara <sean@sean.io>

def docker_connection

def docker_connection
  @docker_connection ||= ::Docker::Connection.new(options[:docker_host_url], options[:docker_host_options])
end

def execute(command)

def execute(command)
  return if command.nil?
  with_retries { @runner = ::Docker::Container.get(instance_name, {}, docker_connection) }
  with_retries do
    o = @runner.exec(Shellwords.shellwords(command), wait: options[:timeout], 'e' => { 'TERM' => 'xterm' }) { |_stream, chunk| print chunk.to_s }
    @exit_code = o[2]
  end
  raise Transport::DockerExecFailed.new("Docker Exec (#{@exit_code}) for command: [#{command}]", @exit_code) if @exit_code != 0
end

def image_prefix

def image_prefix
  options[:image_prefix]
end

def instance_name

def instance_name
  options[:instance_name]
end

def login_command

def login_command
  @runner = options[:instance_name].to_s
  cols = `tput cols`
  lines = `tput lines`
  args = ['exec', '-e', "COLUMNS=#{cols}", '-e', "LINES=#{lines}", '-it', @runner, '/bin/bash', '-login', '-i']
  LoginCommand.new('docker', args)
end

def upload(locals, remote)

def upload(locals, remote)
  if options[:host_ip_override]
    # Allow connecting to any ip/hostname to support sibling containers
    ssh_ip = options[:host_ip_override]
    ssh_port = options[:data_container][:NetworkSettings][:Ports][:"22/tcp"][0][:HostPort]
  elsif options[:docker_host_url] =~ /unix:/
    if options[:data_container][:NetworkSettings][:Ports][:"22/tcp"][0][:HostIp] == '0.0.0.0'
      ssh_ip = options[:data_container][:NetworkSettings][:IPAddress]
      ssh_port = '22'
    else
      # we should read the proper mapped ip, since this allows us to upload the files
      ssh_ip = options[:data_container][:NetworkSettings][:Ports][:"22/tcp"][0][:HostIp]
      ssh_port = options[:data_container][:NetworkSettings][:Ports][:"22/tcp"][0][:HostPort]
    end
  elsif options[:docker_host_url] =~ /tcp:/
    name = options[:data_container][:Name]
    # DOCKER_HOST
    docker_host_url_ip = options[:docker_host_url].split('tcp://')[1].split(':')[0]
    # mapped IP of data container
    candidate_ip = ::Docker::Container.all.select do |x|
      x.info['Names'][0].eql?(name)
    end.first.info['NetworkSettings']['Networks']['dokken']['IPAddress']
    # mapped port
    candidate_ssh_port = options[:data_container][:NetworkSettings][:Ports][:"22/tcp"][0][:HostPort]
    debug "candidate_ip - #{candidate_ip}"
    debug "candidate_ssh_port - #{candidate_ssh_port}"
    if port_open?(candidate_ip, candidate_ssh_port)
      debug "candidate_ip - #{candidate_ip}/#{candidate_ssh_port} open"
      ssh_ip = candidate_ip
      ssh_port = candidate_ssh_port
    elsif port_open?(candidate_ip, '22')
      ssh_ip = candidate_ip
      ssh_port = '22'
      debug "candidate_ip - #{candidate_ip}/22 open"
    else
      ssh_ip = docker_host_url_ip
      ssh_port = candidate_ssh_port
    end
  else
    raise Kitchen::UserError, 'docker_host_url must be tcp:// or unix://'
  end
  debug "ssh_ip : #{ssh_ip}"
  debug "ssh_port : #{ssh_port}"
  tmpdir = Dir.tmpdir + '/dokken/'
  FileUtils.mkdir_p tmpdir.to_s, mode: 0o777
  tmpdir += Process.uid.to_s
  FileUtils.mkdir_p tmpdir.to_s
  File.write("#{tmpdir}/id_rsa", insecure_ssh_private_key)
  FileUtils.chmod(0o600, "#{tmpdir}/id_rsa")
  begin
    rsync_cmd = '/usr/bin/rsync -a -e'
    rsync_cmd << ' \''
    rsync_cmd << 'ssh -2'
    rsync_cmd << " -i #{tmpdir}/id_rsa"
    rsync_cmd << ' -o CheckHostIP=no'
    rsync_cmd << ' -o Compression=no'
    rsync_cmd << ' -o PasswordAuthentication=no'
    rsync_cmd << ' -o StrictHostKeyChecking=no'
    rsync_cmd << ' -o UserKnownHostsFile=/dev/null'
    rsync_cmd << ' -o LogLevel=ERROR'
    rsync_cmd << " -p #{ssh_port}"
    rsync_cmd << '\''
    rsync_cmd << " #{locals.join(' ')} root@#{ssh_ip}:#{remote}"
    debug "rsync_cmd :#{rsync_cmd}:"
    `#{rsync_cmd}`
  rescue Errno::ENOENT
    debug 'Rsync is not installed. Falling back to SCP.'
    locals.each do |local|
      Net::SCP.upload!(ssh_ip,
                       'root',
                       local,
                       remote,
                       recursive: true,
                       ssh: { port: ssh_port, keys: ["#{tmpdir}/id_rsa"] })
    end
  end
end

def with_retries

def with_retries
  tries = 20
  begin
    yield
    # Only catch errors that can be fixed with retries.
  rescue ::Docker::Error::ServerError, # 404
         ::Docker::Error::UnexpectedResponseError, # 400
         ::Docker::Error::TimeoutError,
         ::Docker::Error::IOError => e
    tries -= 1
    retry if tries > 0
    raise e
  end
end

def work_image

def work_image
  return "#{image_prefix}/#{instance_name}" unless image_prefix.nil?
  instance_name
end