2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-09 01:08:09 -04:00
|
|
|
require 'toml-rb'
|
|
|
|
|
2018-01-05 06:31:12 -05:00
|
|
|
module Gitlab
|
|
|
|
module SetupHelper
|
2020-12-16 22:10:36 -05:00
|
|
|
def create_configuration(dir, storage_paths, force: false, options: {})
|
2020-03-25 14:08:10 -04:00
|
|
|
generate_configuration(
|
2020-12-16 22:10:36 -05:00
|
|
|
configuration_toml(dir, storage_paths, options),
|
|
|
|
get_config_path(dir, options),
|
2020-03-25 14:08:10 -04:00
|
|
|
force: force
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable Rails/Output
|
|
|
|
def generate_configuration(toml_data, config_path, force: false)
|
|
|
|
FileUtils.rm_f(config_path) if force
|
|
|
|
|
|
|
|
File.open(config_path, File::WRONLY | File::CREAT | File::EXCL) do |f|
|
|
|
|
f.puts toml_data
|
|
|
|
end
|
|
|
|
rescue Errno::EEXIST
|
|
|
|
puts 'Skipping config.toml generation:'
|
2021-08-09 08:10:09 -04:00
|
|
|
puts "A configuration file for #{config_path} already exists."
|
2020-03-25 14:08:10 -04:00
|
|
|
rescue ArgumentError => e
|
|
|
|
puts 'Skipping config.toml generation:'
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
# rubocop:enable Rails/Output
|
|
|
|
|
2020-08-24 14:10:19 -04:00
|
|
|
module Workhorse
|
|
|
|
extend Gitlab::SetupHelper
|
|
|
|
class << self
|
2020-12-16 22:10:36 -05:00
|
|
|
def configuration_toml(dir, _, _)
|
2021-07-22 17:09:40 -04:00
|
|
|
config = { redis: { URL: redis_url, DB: redis_db } }
|
2020-08-24 14:10:19 -04:00
|
|
|
|
|
|
|
TomlRB.dump(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_url
|
|
|
|
Gitlab::Redis::SharedState.url
|
|
|
|
end
|
|
|
|
|
2021-07-22 17:09:40 -04:00
|
|
|
def redis_db
|
|
|
|
Gitlab::Redis::SharedState.params.fetch(:db, 0)
|
|
|
|
end
|
|
|
|
|
2020-12-16 22:10:36 -05:00
|
|
|
def get_config_path(dir, _)
|
|
|
|
File.join(dir, 'config_path')
|
2020-08-24 14:10:19 -04:00
|
|
|
end
|
2020-12-11 10:10:04 -05:00
|
|
|
|
|
|
|
def compile_into(dir)
|
|
|
|
command = %W[#{make} -C #{Rails.root.join('workhorse')} install PREFIX=#{File.absolute_path(dir)}]
|
|
|
|
|
|
|
|
make_out, make_status = Gitlab::Popen.popen(command)
|
|
|
|
unless make_status == 0
|
|
|
|
warn make_out
|
|
|
|
raise 'workhorse make failed'
|
|
|
|
end
|
|
|
|
|
|
|
|
# 'make install' puts the binaries in #{dir}/bin but the init script expects them in dir
|
|
|
|
FileUtils.mv(Dir["#{dir}/bin/*"], dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def make
|
|
|
|
_, which_status = Gitlab::Popen.popen(%w[which gmake])
|
|
|
|
which_status == 0 ? 'gmake' : 'make'
|
|
|
|
end
|
2020-08-24 14:10:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
module Gitaly
|
|
|
|
extend Gitlab::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.
|
2020-12-16 22:10:36 -05:00
|
|
|
def configuration_toml(gitaly_dir, storage_paths, options, gitaly_ruby: true)
|
2020-03-25 14:08:10 -04:00
|
|
|
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']
|
2018-01-05 06:31:12 -05:00
|
|
|
end
|
2020-03-25 14:08:10 -04:00
|
|
|
|
|
|
|
storages << { name: key, path: storage_paths[key] }
|
2018-01-05 06:31:12 -05:00
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
config = { socket_path: address.sub(/\Aunix:/, '') }
|
2018-01-05 06:31:12 -05:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
if Rails.env.test?
|
2020-12-16 22:10:36 -05:00
|
|
|
socket_filename = options[:gitaly_socket] || "gitaly.socket"
|
2021-03-16 14:11:53 -04:00
|
|
|
prometheus_listen_addr = options[:prometheus_listen_addr]
|
2020-12-16 22:10:36 -05:00
|
|
|
|
|
|
|
config = {
|
|
|
|
# Override the set gitaly_address since Praefect is in the loop
|
|
|
|
socket_path: File.join(gitaly_dir, socket_filename),
|
|
|
|
auth: { token: 'secret' },
|
|
|
|
# Compared to production, tests run in constrained environments. This
|
|
|
|
# number is meant to grow with the number of concurrent rails requests /
|
|
|
|
# sidekiq jobs, and concurrency will be low anyway in test.
|
2021-04-18 02:09:22 -04:00
|
|
|
git: {
|
|
|
|
catfile_cache_size: 5,
|
2021-10-22 08:09:12 -04:00
|
|
|
bin_path: File.expand_path(File.join(gitaly_dir, '_build', 'deps', 'git', 'install', 'bin', 'git'))
|
|
|
|
},
|
2021-03-16 14:11:53 -04:00
|
|
|
prometheus_listen_addr: prometheus_listen_addr
|
|
|
|
}.compact
|
2020-12-16 22:10:36 -05:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
storage_path = Rails.root.join('tmp', 'tests', 'second_storage').to_s
|
|
|
|
storages << { name: 'test_second_storage', path: storage_path }
|
|
|
|
end
|
2018-01-05 06:31:12 -05:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
config[:storage] = storages
|
2019-11-06 19:06:18 -05:00
|
|
|
|
2021-03-03 13:11:16 -05:00
|
|
|
internal_socket_dir = options[:internal_socket_dir] || File.join(gitaly_dir, 'internal_sockets')
|
2020-03-25 14:08:10 -04:00
|
|
|
FileUtils.mkdir(internal_socket_dir) unless File.exist?(internal_socket_dir)
|
|
|
|
config[:internal_socket_dir] = internal_socket_dir
|
2019-11-06 19:06:18 -05:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
config[:'gitaly-ruby'] = { dir: File.join(gitaly_dir, 'ruby') } if gitaly_ruby
|
|
|
|
config[:'gitlab-shell'] = { dir: Gitlab.config.gitlab_shell.path }
|
2021-05-24 14:10:28 -04:00
|
|
|
config[:bin_dir] = File.join(gitaly_dir, '_build', 'bin') # binaries by default are in `_build/bin`
|
2020-06-02 05:08:01 -04:00
|
|
|
config[:gitlab] = { url: Gitlab.config.gitlab.url }
|
2020-10-27 08:08:33 -04:00
|
|
|
config[:logging] = { dir: Rails.root.join('log').to_s }
|
2019-06-06 10:11:44 -04:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
TomlRB.dump(config)
|
2019-06-06 10:11:44 -04:00
|
|
|
end
|
2018-01-05 06:31:12 -05:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
private
|
|
|
|
|
2020-12-16 22:10:36 -05:00
|
|
|
def get_config_path(dir, options)
|
|
|
|
config_filename = options[:config_filename] || 'config.toml'
|
|
|
|
File.join(dir, config_filename)
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
2018-01-05 06:31:12 -05:00
|
|
|
end
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Praefect
|
|
|
|
extend Gitlab::SetupHelper
|
|
|
|
class << self
|
2020-12-16 22:10:36 -05:00
|
|
|
def configuration_toml(gitaly_dir, _, _)
|
2020-03-25 14:08:10 -04:00
|
|
|
nodes = [{ storage: 'default', address: "unix:#{gitaly_dir}/gitaly.socket", primary: true, token: 'secret' }]
|
2020-12-16 22:10:36 -05:00
|
|
|
second_storage_nodes = [{ storage: 'test_second_storage', address: "unix:#{gitaly_dir}/gitaly2.socket", primary: true, token: 'secret' }]
|
|
|
|
|
|
|
|
storages = [{ name: 'default', node: nodes }, { name: 'test_second_storage', node: second_storage_nodes }]
|
2021-05-31 11:11:12 -04:00
|
|
|
failover = { enabled: false, election_strategy: 'local' }
|
|
|
|
config = {
|
|
|
|
i_understand_my_election_strategy_is_unsupported_and_will_be_removed_without_warning: true,
|
|
|
|
socket_path: "#{gitaly_dir}/praefect.socket",
|
|
|
|
memory_queue_enabled: true,
|
|
|
|
virtual_storage: storages,
|
|
|
|
failover: failover
|
|
|
|
}
|
2020-03-25 14:08:10 -04:00
|
|
|
config[:token] = 'secret' if Rails.env.test?
|
|
|
|
|
|
|
|
TomlRB.dump(config)
|
|
|
|
end
|
2018-01-05 06:31:12 -05:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
private
|
2018-01-05 06:31:12 -05:00
|
|
|
|
2020-12-16 22:10:36 -05:00
|
|
|
def get_config_path(dir, _)
|
2020-03-25 14:08:10 -04:00
|
|
|
File.join(dir, 'praefect.config.toml')
|
2018-01-05 06:31:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|