2017-02-05 13:04:23 -05:00
|
|
|
require 'gitaly'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
2017-03-13 10:04:46 -04:00
|
|
|
SERVER_VERSION_FILE = 'GITALY_SERVER_VERSION'.freeze
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
MUTEX = Mutex.new
|
|
|
|
private_constant :MUTEX
|
2017-03-30 10:26:30 -04:00
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
def self.stub(name, storage)
|
|
|
|
MUTEX.synchronize do
|
|
|
|
@stubs ||= {}
|
|
|
|
@stubs[storage] ||= {}
|
|
|
|
@stubs[storage][name] ||= begin
|
|
|
|
klass = Gitaly.const_get(name.to_s.camelcase.to_sym).const_get(:Stub)
|
|
|
|
addr = address(storage)
|
|
|
|
addr = addr.sub(%r{^tcp://}, '') if URI(addr).scheme == 'tcp'
|
|
|
|
klass.new(addr, :this_channel_is_insecure)
|
2017-03-30 10:26:30 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-22 13:23:40 -04:00
|
|
|
end
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
def self.clear_stubs!
|
|
|
|
MUTEX.synchronize do
|
|
|
|
@stubs = nil
|
|
|
|
end
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
def self.address(storage)
|
|
|
|
params = Gitlab.config.repositories.storages[storage]
|
|
|
|
raise "storage not found: #{storage.inspect}" if params.nil?
|
2017-03-30 10:26:30 -04:00
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
address = params['gitaly_address']
|
|
|
|
unless address.present?
|
|
|
|
raise "storage #{storage.inspect} is missing a gitaly_address"
|
|
|
|
end
|
2017-03-24 13:22:42 -04:00
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
unless URI(address).scheme.in?(%w(tcp unix))
|
|
|
|
raise "Unsupported Gitaly address: #{address.inspect} does not use URL scheme 'tcp' or 'unix'"
|
2017-03-30 10:26:30 -04:00
|
|
|
end
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
address
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.enabled?
|
2017-03-22 13:23:40 -04:00
|
|
|
Gitlab.config.gitaly.enabled
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
2017-02-24 10:53:44 -05:00
|
|
|
|
|
|
|
def self.feature_enabled?(feature)
|
|
|
|
enabled? && ENV["GITALY_#{feature.upcase}"] == '1'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.migrate(feature)
|
|
|
|
is_enabled = feature_enabled?(feature)
|
|
|
|
metric_name = feature.to_s
|
|
|
|
metric_name += "_gitaly" if is_enabled
|
|
|
|
|
|
|
|
Gitlab::Metrics.measure(metric_name) do
|
|
|
|
yield is_enabled
|
|
|
|
end
|
|
|
|
end
|
2017-03-13 10:04:46 -04:00
|
|
|
|
|
|
|
def self.expected_server_version
|
|
|
|
path = Rails.root.join(SERVER_VERSION_FILE)
|
|
|
|
path.read.chomp
|
|
|
|
end
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
end
|