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-03-30 10:26:30 -04:00
|
|
|
# This function is not thread-safe because it updates Hashes in instance variables.
|
|
|
|
def self.configure_channels
|
|
|
|
@addresses = {}
|
|
|
|
@channels = {}
|
|
|
|
Gitlab.config.repositories.storages.each do |name, params|
|
|
|
|
address = params['gitaly_address']
|
|
|
|
unless address.present?
|
|
|
|
raise "storage #{name.inspect} is missing a gitaly_address"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless URI(address).scheme.in?(%w(tcp unix))
|
|
|
|
raise "Unsupported Gitaly address: #{address.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@addresses[name] = address
|
|
|
|
@channels[name] = new_channel(address)
|
|
|
|
end
|
2017-03-22 13:23:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.new_channel(address)
|
2017-04-03 09:27:14 -04:00
|
|
|
address = address.sub(%r{^tcp://}, '') if URI(address).scheme == 'tcp'
|
|
|
|
# NOTE: When Gitaly runs on a Unix socket, permissions are
|
2017-03-22 13:23:40 -04:00
|
|
|
# handled using the file system and no additional authentication is
|
|
|
|
# required (therefore the :this_channel_is_insecure flag)
|
2017-04-03 09:27:14 -04:00
|
|
|
# TODO: Add authentication support when Gitaly is running on a TCP socket.
|
2017-03-22 13:23:40 -04:00
|
|
|
GRPC::Core::Channel.new(address, {}, :this_channel_is_insecure)
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
|
2017-03-24 13:22:42 -04:00
|
|
|
def self.get_channel(storage)
|
2017-03-30 10:26:30 -04:00
|
|
|
if !Rails.env.production? && @channels.nil?
|
|
|
|
# In development mode the Rails auto-loader may reset the instance
|
|
|
|
# variables of this class. What we do here is not thread-safe. In normal
|
|
|
|
# circumstances (including production) these instance variables have
|
|
|
|
# been initialized from config/initializers.
|
|
|
|
configure_channels
|
|
|
|
end
|
|
|
|
|
2017-03-24 13:22:42 -04:00
|
|
|
@channels[storage]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_address(storage)
|
2017-03-30 10:26:30 -04:00
|
|
|
if !Rails.env.production? && @addresses.nil?
|
|
|
|
# In development mode the Rails auto-loader may reset the instance
|
|
|
|
# variables of this class. What we do here is not thread-safe. In normal
|
|
|
|
# circumstances (including development) these instance variables have
|
|
|
|
# been initialized from config/initializers.
|
|
|
|
configure_channels
|
|
|
|
end
|
|
|
|
|
2017-03-24 13:22:42 -04:00
|
|
|
@addresses[storage]
|
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
|