Sha256: 4dea9f3602adea6a7e7bb13d0dc534c5f8a1697f3a0b4e608148f8f30e5167cc

Contents?: true

Size: 1.67 KB

Versions: 53

Compression:

Stored size: 1.67 KB

Contents

require 'tempfile'

module Gitlab
  module QA
    module Component
      class InternetTunnel < Base
        DOCKER_IMAGE = 'gitlab/ssh-tunnel'.freeze
        DOCKER_IMAGE_TAG = '1.0.0'.freeze

        attr_writer :gitlab_hostname

        def initialize
          super

          key_dir = ENV['CI_PROJECT_DIR'] || Dir.tmpdir
          @ssh_key = Tempfile.new('tunnel-ssh-private-key', key_dir)
          @ssh_key.write(ENV.fetch('TUNNEL_SSH_PRIVATE_KEY'))
          @ssh_key.close

          File.chmod(0o600, @ssh_key.path)

          @volumes['/root/.ssh/id_rsa'] = @ssh_key.path
        end

        def instance
          raise 'Please provide a block!' unless block_given?

          super
        end

        def url
          "https://#{subdomain}.#{tunnel_server_hostname}"
        end

        private

        def name
          @name ||= "ssh-tunnel-#{SecureRandom.hex(4)}"
        end

        def tunnel_server_hostname
          ENV.fetch("TUNNEL_SERVER_HOSTNAME")
        end

        def subdomain
          @subdomain ||= rand(30_000..49_000)
        end

        def start
          raise "Must set gitlab_hostname" unless @gitlab_hostname

          @docker.run(DOCKER_IMAGE, DOCKER_IMAGE_TAG, "-o StrictHostKeyChecking=no -N -R #{subdomain}:#{@gitlab_hostname}:80 #{ENV.fetch('TUNNEL_SSH_USER')}@#{tunnel_server_hostname}") do |command|
            command << '-d '
            command << "--name #{name}"
            command << "--net #{network}"

            @volumes.to_h.each do |to, from|
              command.volume(from, to, 'Z')
            end
          end
        end

        def teardown
          super

          @ssh_key.unlink
        end
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
gitlab-qa-6.1.2 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-6.1.1 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-6.1.0 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-6.0.0 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.17.1 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.17.0 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.16.0 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.15.0 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.14.1 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.14.0 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.13.7 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.13.5 lib/gitlab/qa/component/internet_tunnel.rb
gitlab-qa-5.13.4 lib/gitlab/qa/component/internet_tunnel.rb