summaryrefslogtreecommitdiff
path: root/lib/gitlab/storage_check/gitlab_caller.rb
blob: 44952b688449a8a13393460c05b90272efe25e16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'excon'

module Gitlab
  module StorageCheck
    class GitlabCaller
      def initialize(options)
        @options = options
      end

      def call!
        Gitlab::StorageCheck::Response.new(get_response)
      rescue Errno::ECONNREFUSED, Excon::Error
        # Server not ready, treated as invalid response.
        Gitlab::StorageCheck::Response.new(nil)
      end

      def get_response
        scheme, *other_parts = URI.split(@options.target)
        socket_path = if  scheme == 'unix'
                        other_parts.compact.join
                      end

        connection = Excon.new(@options.target, socket: socket_path)
        connection.post(path: Gitlab::StorageCheck::ENDPOINT,
                        headers: headers)
      end

      def headers
        @headers ||= begin
                       headers = {}
                       headers['Content-Type'] = headers['Accept'] = 'application/json'
                       headers['TOKEN'] = @options.token if @options.token

                       headers
                     end
      end
    end
  end
end