2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
require 'toml-rb'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module SetupHelper
|
|
|
|
class << self
|
|
|
|
# We cannot create config.toml files for all possible Gitaly configuations.
|
|
|
|
# For instance, if Gitaly is running on another machine then it makes no
|
|
|
|
# sense to write a config.toml file on the current machine. This method will
|
|
|
|
# only generate a configuration for the most common and simplest case: when
|
|
|
|
# we have exactly one Gitaly process and we are sure it is running locally
|
|
|
|
# because it uses a Unix socket.
|
|
|
|
# For development and testing purposes, an extra storage is added to gitaly,
|
|
|
|
# which is not known to Rails, but must be explicitly stubbed.
|
2018-11-20 20:47:30 +05:30
|
|
|
def gitaly_configuration_toml(gitaly_dir, storage_paths, gitaly_ruby: true)
|
2018-03-17 18:26:18 +05:30
|
|
|
storages = []
|
|
|
|
address = nil
|
|
|
|
|
|
|
|
Gitlab.config.repositories.storages.each do |key, val|
|
|
|
|
if address
|
|
|
|
if address != val['gitaly_address']
|
|
|
|
raise ArgumentError, "Your gitlab.yml contains more than one gitaly_address."
|
|
|
|
end
|
|
|
|
elsif URI(val['gitaly_address']).scheme != 'unix'
|
|
|
|
raise ArgumentError, "Automatic config.toml generation only supports 'unix:' addresses."
|
|
|
|
else
|
|
|
|
address = val['gitaly_address']
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
storages << { name: key, path: storage_paths[key] }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
if Rails.env.test?
|
2018-12-13 13:39:08 +05:30
|
|
|
storage_path = Rails.root.join('tmp', 'tests', 'second_storage').to_s
|
|
|
|
|
|
|
|
FileUtils.mkdir(storage_path) unless File.exist?(storage_path)
|
|
|
|
storages << { name: 'test_second_storage', path: storage_path }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
config = { socket_path: address.sub(/\Aunix:/, ''), storage: storages }
|
|
|
|
config[:auth] = { token: 'secret' } if Rails.env.test?
|
|
|
|
config[:'gitaly-ruby'] = { dir: File.join(gitaly_dir, 'ruby') } if gitaly_ruby
|
|
|
|
config[:'gitlab-shell'] = { dir: Gitlab.config.gitlab_shell.path }
|
|
|
|
config[:bin_dir] = Gitlab.config.gitaly.client_path
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
TomlRB.dump(config)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable Rails/Output
|
2018-11-20 20:47:30 +05:30
|
|
|
def create_gitaly_configuration(dir, storage_paths, force: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
config_path = File.join(dir, 'config.toml')
|
|
|
|
FileUtils.rm_f(config_path) if force
|
|
|
|
|
|
|
|
File.open(config_path, File::WRONLY | File::CREAT | File::EXCL) do |f|
|
2018-11-20 20:47:30 +05:30
|
|
|
f.puts gitaly_configuration_toml(dir, storage_paths)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
rescue Errno::EEXIST
|
|
|
|
puts "Skipping config.toml generation:"
|
|
|
|
puts "A configuration file already exists."
|
|
|
|
rescue ArgumentError => e
|
|
|
|
puts "Skipping config.toml generation:"
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
# rubocop:enable Rails/Output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|