http_connection_check.rb 2.67 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7
module SystemCheck
  module Geo
    class HttpConnectionCheck < SystemCheck::BaseCheck
      set_name 'GitLab Geo HTTP(S) connectivity'

8 9
      NOT_SECONDARY_NODE = 'not a secondary node'.freeze
      GEO_NOT_ENABLED = 'Geo is not enabled'.freeze
10

11 12 13
      def skip?
        unless Gitlab::Geo.enabled?
          self.skip_reason = GEO_NOT_ENABLED
14

15
          return true
16 17
        end

18 19 20 21
        unless Gitlab::Geo.secondary?
          self.skip_reason = NOT_SECONDARY_NODE

          return true
22
        end
23 24 25 26 27 28 29 30

        false
      end

      def multi_check
        $stdout.puts
        $stdout.print '* Can connect to the primary node ... '
        check_gitlab_geo_node(Gitlab::Geo.primary_node)
31 32
      end

Gabriel Mazetto's avatar
Gabriel Mazetto committed
33 34
      private

35
      def check_gitlab_geo_node(node)
Gabriel Mazetto's avatar
Gabriel Mazetto committed
36 37
        response = Net::HTTP.start(node.uri.host, node.uri.port, use_ssl: (node.uri.scheme == 'https')) do |http|
          http.request(Net::HTTP::Get.new(node.uri))
38 39
        end

Gabriel Mazetto's avatar
Gabriel Mazetto committed
40 41 42 43 44 45 46
        if response.code_type == Net::HTTPFound
          $stdout.puts 'yes'.color(:green)
        else
          $stdout.puts 'no'.color(:red)
        end
      rescue Errno::ECONNREFUSED => e
        display_exception(e)
47

Gabriel Mazetto's avatar
Gabriel Mazetto committed
48 49 50 51 52 53 54
        try_fixing_it(
          'Check if the machine is online and GitLab is running',
          'Check your firewall rules and make sure this machine can reach the target machine',
          "Make sure port and protocol are correct: '#{node.url}', or change it in Admin > Geo Nodes"
        )
      rescue SocketError => e
        display_exception(e)
55

Gabriel Mazetto's avatar
Gabriel Mazetto committed
56
        if e.cause && e.cause.message.starts_with?('getaddrinfo')
57
          try_fixing_it(
Gabriel Mazetto's avatar
Gabriel Mazetto committed
58 59 60
            'Check if your machine can connect to a DNS server',
            "Check if your machine can resolve DNS for: '#{node.uri.host}'",
            'If machine host is incorrect, change it in Admin > Geo Nodes'
61
          )
Gabriel Mazetto's avatar
Gabriel Mazetto committed
62 63 64
        end
      rescue OpenSSL::SSL::SSLError => e
        display_exception(e)
65

Gabriel Mazetto's avatar
Gabriel Mazetto committed
66 67 68 69
        try_fixing_it(
          'If you have a self-signed CA or certificate you need to whitelist it in Omnibus'
        )
        for_more_information(see_custom_certificate_doc)
70

Gabriel Mazetto's avatar
Gabriel Mazetto committed
71 72 73 74 75 76
        try_fixing_it(
          'If you have a valid certificate make sure you have the full certificate chain in the pem file'
        )
      rescue StandardError => e
        display_exception(e)
      end
77

Gabriel Mazetto's avatar
Gabriel Mazetto committed
78 79 80 81
      def display_exception(exception)
        $stdout.puts 'no'.color(:red)
        $stdout.puts '  Reason:'.color(:blue)
        $stdout.puts "  #{exception.message}"
82
      end
83 84 85 86

      def see_custom_certificate_doc
        'https://docs.gitlab.com/omnibus/common_installation_problems/README.html#using-self-signed-certificate-or-custom-certificate-authorities'
      end
87 88 89
    end
  end
end