Skip to content
Snippets Groups Projects
Commit 6c560bbc963a authored by DJ Mountney's avatar DJ Mountney
Browse files

Merge branch 'jc-praefect-track-repository' into 'master'

Introduce praefect track-repository command

Closes gitaly#3811

See merge request gitlab-org/omnibus-gitlab!5658
No related branches found
No related tags found
2 merge requests!55Intermediate build after shift of stable series,!54GitLab 14.4
......@@ -10,6 +10,7 @@
COMMANDS:
remove-repository Remove repository from Gitaly cluster
track-repository Tells Gitaly cluster to track a repository
EOS
def self.parse_options!(args)
......@@ -29,8 +30,6 @@
'remove-repository' => OptionParser.new do |opts|
opts.banner = "Usage: gitlab-ctl praefect remove-repository [options]"
opts.on("-h", "--help", "Prints this help") do
Kernel.puts opts
Kernel.exit 0
end
parse_common_options!(options, opts)
end,
......@@ -36,5 +35,4 @@
opts.on('--virtual-storage-name NAME', 'Name of the virtual storage where the repository resides (mandatory)') do |virtual_storage_name|
options[:virtual_storage_name] = virtual_storage_name
end
'track-repository' => OptionParser.new do |opts|
opts.banner = "Usage: gitlab-ctl praefect track-repository [options]"
......@@ -40,5 +38,3 @@
opts.on('--repository-relative-path PATH', 'Relative path to the repository on the disk (mandatory)') do |repository_relative_path|
options[:repository_relative_path] = repository_relative_path
end
parse_common_options!(options, opts)
......@@ -44,6 +40,6 @@
opts.on('--dir DIR', 'Directory in which Praefect is installed') do |dir|
options[:dir] = dir
opts.on('--authoritative-storage STORAGE-NAME', 'The storage to use as the primary for this repository (optional)') do |authoritative_storage|
options[:authoritative_storage] = authoritative_storage
end
end
}
......@@ -60,6 +56,7 @@
commands[command].parse!(args)
# common arguments
raise OptionParser::ParseError, "Option --virtual-storage-name must be specified" \
unless options.key?(:virtual_storage_name)
......@@ -70,9 +67,28 @@
options
end
def self.parse_common_options!(options, option_parser)
option_parser.on("-h", "--help", "Prints this help") do
Kernel.puts option_parser
Kernel.exit 0
end
option_parser.on('--dir DIR', 'Directory in which Praefect is installed') do |dir|
options[:dir] = dir
end
option_parser.on('--virtual-storage-name NAME', 'Name of the virtual storage where the repository resides (mandatory)') do |virtual_storage_name|
options[:virtual_storage_name] = virtual_storage_name
end
option_parser.on('--repository-relative-path PATH', 'Relative path to the repository on the disk (mandatory)') do |repository_relative_path|
options[:repository_relative_path] = repository_relative_path
end
end
def self.execute(options)
config_file_path = File.join(options.fetch(:dir, DIR_PATH), 'config.toml')
[EXEC_PATH, config_file_path].each do |path|
next if File.exist?(path)
......@@ -73,9 +89,9 @@
def self.execute(options)
config_file_path = File.join(options.fetch(:dir, DIR_PATH), 'config.toml')
[EXEC_PATH, config_file_path].each do |path|
next if File.exist?(path)
Kernel.abort "Could not find '#{path}' file. Is your package installed correctly?"
Kernel.abort "Could not find '#{path}' file. Is this command being run on a Praefect node?"
end
......@@ -80,6 +96,7 @@
end
# common arguments
command = [EXEC_PATH, "-config", config_file_path, options[:command]]
command += ["-virtual-storage", options[:virtual_storage_name]]
command += ["-repository", options[:repository_relative_path]]
......@@ -82,7 +99,10 @@
command = [EXEC_PATH, "-config", config_file_path, options[:command]]
command += ["-virtual-storage", options[:virtual_storage_name]]
command += ["-repository", options[:repository_relative_path]]
# command specific arguments
command += ["-authoritative-storage", options[:authoritative_storage]] if options[:command] == 'track-repository' && options.key?(:authoritative_storage)
status = Kernel.system(*command)
Kernel.exit!(1) unless status
end
......
......@@ -17,7 +17,16 @@
expect { Praefect.parse_options!(%w(praefect unknown-command)) }.to raise_error(OptionParser::ParseError, /Unknown Praefect command: unknown-command/)
end
it 'throws an error when unknown option is specified' do
expect { Praefect.parse_options!(%w(praefect remove-repository --unknown)) }.to raise_error(OptionParser::InvalidOption, /unknown/)
end
shared_examples 'parses common required options' do
it 'throws an error when unknown option is specified' do
expect { Praefect.parse_options!(%W(praefect #{command} --unknown)) }.to raise_error(OptionParser::InvalidOption, /unknown/)
end
it 'throws an error when an argument for --virtual-storage-name is not specified' do
expect { Praefect.parse_options!(%W(praefect #{command} --virtual-storage-name)) }.to raise_error(OptionParser::MissingArgument, /virtual-storage-name/)
end
it 'throws an error when --virtual-storage-name is not specified' do
expect { Praefect.parse_options!(%W(praefect #{command})) }.to raise_error(OptionParser::ParseError, /Option --virtual-storage-name must be specified/)
end
......@@ -23,5 +32,5 @@
it 'throws an error when an argument for --virtual-storage-name is not specified' do
expect { Praefect.parse_options!(%w(praefect remove-repository --virtual-storage-name)) }.to raise_error(OptionParser::MissingArgument, /virtual-storage-name/)
end
it 'throws an error when an argument for --repository-relative-path is not specified' do
expect { Praefect.parse_options!(%W(praefect #{command} --repository-relative-path)) }.to raise_error(OptionParser::MissingArgument, /repository-relative-path/)
end
......@@ -27,5 +36,12 @@
it 'throws an error when --virtual-storage-name is not specified' do
expect { Praefect.parse_options!(%w(praefect remove-repository)) }.to raise_error(OptionParser::ParseError, /Option --virtual-storage-name must be specified/)
it 'throws an error when --repository-relative-path is not specified' do
expect { Praefect.parse_options!(%W(praefect #{command} --virtual-storage-name name)) }.to raise_error(OptionParser::ParseError, /Option --repository-relative-path must be specified/)
end
it 'successfully parses correct params' do
expected_options = { command: command, virtual_storage_name: 'name', repository_relative_path: 'path' }
expect(Praefect.parse_options!(%W(praefect #{command} --virtual-storage-name name --repository-relative-path path))).to eq(expected_options)
end
end
......@@ -30,6 +46,8 @@
end
it 'throws an error when an argument for --repository-relative-path is not specified' do
expect { Praefect.parse_options!(%w(praefect remove-repository --repository-relative-path)) }.to raise_error(OptionParser::MissingArgument, /repository-relative-path/)
context 'when command is remove-repository' do
let(:command) { 'remove-repository' }
it_behaves_like 'parses common required options'
end
......@@ -34,6 +52,7 @@
end
it 'throws an error when --repository-relative-path is not specified' do
expect { Praefect.parse_options!(%w(praefect remove-repository --virtual-storage-name name)) }.to raise_error(OptionParser::ParseError, /Option --repository-relative-path must be specified/)
end
context 'when command is track-repository' do
let(:command) { 'track-repository' }
it_behaves_like 'parses common required options'
......@@ -39,7 +58,12 @@
it 'successfully parses correct params' do
expected_options = { command: 'remove-repository', virtual_storage_name: 'name', repository_relative_path: 'path' }
expect(Praefect.parse_options!(%w(praefect remove-repository --virtual-storage-name name --repository-relative-path path))).to eq(expected_options)
it 'successfully parses authoritative-storage' do
expected_options = { command: command, virtual_storage_name: 'name', repository_relative_path: 'path', authoritative_storage: 'storage-1' }
expect(Praefect.parse_options!(%W(praefect #{command}
--virtual-storage-name name
--repository-relative-path path
--authoritative-storage storage-1))).to eq(expected_options)
end
end
context 'when help option is passed' do
......@@ -51,6 +75,6 @@
expect { Praefect.parse_options!(%w(praefect -h)) }.to raise_error('Kernel.exit(0)')
end
['remove-repository'].each do |cmd|
['remove-repository', 'track-repository'].each do |cmd|
it "shows help message and exit for #{cmd} help option" do
expect(Kernel).to receive(:puts) do |msg|
......@@ -55,6 +79,6 @@
it "shows help message and exit for #{cmd} help option" do
expect(Kernel).to receive(:puts) do |msg|
expect(msg.to_s).to match(/gitlab-ctl praefect remove-repository/)
expect(msg.to_s).to match(/gitlab-ctl praefect #{cmd}/)
end
expect { Praefect.parse_options!(%W(praefect #{cmd} -h)) }.to raise_error('Kernel.exit(0)')
......@@ -73,7 +97,24 @@
end
end
it 'executes the command' do
allow(File).to receive(:exist?).and_return(true)
expect(Kernel).not_to receive(:abort)
shared_examples 'executes the command' do
it 'successfully executes' do
allow(File).to receive(:exist?).and_return(true)
expect(Kernel).not_to receive(:abort)
args = [
Praefect::EXEC_PATH, '-config', 'dir/config.toml', command,
'-virtual-storage', 'storage-name', '-repository', 'repository-path'
]
args += command_args
expect(Kernel).to receive(:system).with(*args).and_return('result!')
expect(Kernel).not_to receive(:exit!)
common_options = {
virtual_storage_name: 'storage-name',
repository_relative_path: 'repository-path',
command: command,
dir: 'dir'
}
......@@ -79,7 +120,13 @@
expect(Kernel).to receive(:system).with(
Praefect::EXEC_PATH, '-config', 'dir/config.toml', 'cmd',
'-virtual-storage', 'storage-name', '-repository', 'repository-path'
).and_return('result!')
expect(Kernel).not_to receive(:exit!)
Praefect.execute(common_options.merge(command_options))
end
end
context 'remove-repository command' do
let(:command) { 'remove-repository' }
let(:command_args) { [] }
let(:command_options) { {} }
it_behaves_like 'executes the command'
end
......@@ -85,11 +132,17 @@
options = {
virtual_storage_name: 'storage-name',
repository_relative_path: 'repository-path',
command: 'cmd',
dir: 'dir'
}
Praefect.execute(options)
context 'track-repository command' do
let(:command) { 'track-repository' }
let(:command_args) { [] }
let(:command_options) { {} }
it_behaves_like 'executes the command'
context 'with authoritative-storage' do
let(:command_options) { { authoritative_storage: 'storage' } }
let(:command_args) { ['-authoritative-storage', 'storage'] }
it_behaves_like 'executes the command'
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment