Skip to content
Snippets Groups Projects
Commit 140507ec authored by Aakriti Gupta's avatar Aakriti Gupta
Browse files

Geo: Add force mode to promote-to-primary-node

It will allow promotion even if preflight checks
fail.
parent 0cf10944
No related branches found
No related tags found
No related merge requests found
---
title: "Geo: Add force mode to promote-to-primary-node command"
merge_request: 4321
author:
type: other
Loading
Loading
@@ -25,7 +25,25 @@ module Geo
def run_preflight_checks
return true if @options[:skip_preflight_checks]
 
PromotionPreflightChecks.new(@base_path, @options).execute
begin
PromotionPreflightChecks.new(@base_path, @options).execute
rescue SystemExit => e
raise e unless @options[:force]
confirm_proceed_after_preflight_checks_fail
end
end
def confirm_proceed_after_preflight_checks_fail
puts
puts 'WARNING: Preflight checks failed but you are running this in '\
'force mode. If you proceed data loss may happen. '\
'This may be desired in case of an actual disaster.'\
'Are you sure you want to proceed? (y/n)'.color(:yellow)
return if STDIN.gets.chomp.casecmp('y').zero?
exit 1
end
 
def promote_postgresql_to_primary
Loading
Loading
Loading
Loading
@@ -37,6 +37,10 @@ def get_ctl_options
opts.on('-m', '--skip-preflight-checks', 'Do not ask for confirmation if manual checks ran') do |m|
options[:skip_preflight_checks] = m
end
opts.on('-f', '--force', 'Proceed even if preflight checks fail') do |f|
options[:force] = f
end
end.parse!(ARGV.dup)
 
options
Loading
Loading
Loading
Loading
@@ -15,23 +15,18 @@ describe Geo::PromoteToPrimaryNode, '#execute' do
before do
allow(command).to receive(:puts)
allow(command).to receive(:print)
allow(command).to receive(:run_command).with(any_args)
end
 
after do
FileUtils.rm_rf(temp_directory)
end
 
shared_examples 'runs promotion preflight checks' do |expected_args|
it do
expect_any_instance_of(Geo::PromotionPreflightChecks).to receive(:execute)
command.execute
end
end
describe '#run_preflight_checks' do
before do
allow(STDIN).to receive(:gets).and_return('y')
allow(command).to receive(:promote_postgresql_to_primary).and_return(true)
allow(command).to receive(:reconfigure).and_return(true)
allow(command).to receive(:promote_to_primary).and_return(true)
Loading
Loading
@@ -47,25 +42,24 @@ describe Geo::PromoteToPrimaryNode, '#execute' do
end
 
context 'when `--skip-preflight-checks` is not passed' do
context 'when --confirm-primary-is-down is not passed' do
let(:options) { {} }
let(:options) { { confirm_primary_is_down: true } }
 
it_behaves_like 'runs promotion preflight checks',
'--no-confirm-primary-is-down'
before do
allow_any_instance_of(Geo::PromotionPreflightChecks).to receive(
:execute).and_return(true)
end
 
context 'when --no-confirm-primary-is-down is passed' do
let(:options) { { confirm_primary_is_down: false } }
it 'runs preflight checks' do
expect_any_instance_of(Geo::PromotionPreflightChecks).to receive(:execute)
 
it_behaves_like 'runs promotion preflight checks',
'--no-confirm-primary-is-down'
command.execute
end
 
context 'when --confirm-primary-is-down is passed' do
let(:options) { { confirm_primary_is_down: true } }
it 'passes given options to preflight checks command' do
expect(Geo::PromotionPreflightChecks).to receive(:new).with(
nil, options).and_call_original
 
it_behaves_like 'runs promotion preflight checks',
'--confirm-primary-is-down'
command.execute
end
end
end
Loading
Loading
@@ -87,12 +81,56 @@ describe Geo::PromoteToPrimaryNode, '#execute' do
end
 
context 'when preflight checks fail' do
around do |example|
example.run
rescue SystemExit
end
before do
allow(STDIN).to receive(:gets).and_return('n')
allow(command).to receive(:promote_postgresql_to_primary).and_return(true)
allow(command).to receive(:reconfigure).and_return(true)
allow(command).to receive(:promote_to_primary).and_return(true)
allow_any_instance_of(Geo::PromotionPreflightChecks).to receive(
:execute).and_raise(SystemExit)
end
 
it 'raises an error' do
expect { command.execute }.to raise_error
context 'when running in force mode' do
let(:options) { { force: true } }
it 'asks for confirmation' do
expect { command.execute }.to output(
/Are you sure you want to proceed?/
).to_stdout
end
it 'exits with 1 if user denies' do
allow(STDIN).to receive(:gets).and_return('n')
expect { command.execute }.to raise_error(SystemExit) do |error|
expect(error.status).to eq(1)
end
end
it 'calls all the subcommands if user affirms' do
allow(STDIN).to receive(:gets).and_return('y')
is_expected.to receive(:promote_postgresql_to_primary)
is_expected.to receive(:reconfigure)
is_expected.to receive(:promote_to_primary)
command.execute
end
end
context 'when not running in force mode' do
let(:options) { { force: false } }
it 'exits with 1' do
expect { command.execute }.to raise_error(SystemExit)
end
end
end
end
Loading
Loading
@@ -23,4 +23,8 @@ describe 'gitlab-ctl promote-to-primary-node' do
it_behaves_like 'geo promotion command accepts option',
'--skip-preflight-checks',
{ skip_preflight_checks: true }
it_behaves_like 'geo promotion command accepts option',
'--force',
{ force: true }
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