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

Add `gitlab-ctl promotion-preflight-checks` command

It will run preflight checks for promoting to
a primary node. `promote-to-primary-node` command
can include or optionally skip these checks.
parent cfa5c5de
No related branches found
No related tags found
No related merge requests found
Showing with 157 additions and 87 deletions
---
title: Add command promotion-preflight-checks to run before promoting to primary node
merge_request: 4246
author:
type: added
Loading
Loading
@@ -27,26 +27,7 @@ module Geo
def run_preflight_checks
return true if @options[:skip_preflight_checks]
 
puts
puts 'Ensure you have completed the following manual '\
'preflight checks:'
puts '- Check if you need to migrate to Object Storage'
puts '- Review configuration of each secondary node'
puts '- Run system checks'
puts '- Check that secrets match between nodes'
puts '- Ensure Geo replication is up-to-date'
puts '- Verify the integrity of replicated data'
puts '- Notify users of scheduled maintenance'
puts 'Please read https://docs.gitlab.com/ee/administration/geo/'\
'disaster_recovery/planned_failover.html#preflight-checks'
puts
puts 'Did you perform all manual preflight checks (y/n)?'.color(:green)
return if STDIN.gets.chomp.casecmp('y').zero?
raise 'ERROR: Manual preflight checks were not performed! '\
'Please read https://docs.gitlab.com/ee/administration/geo/'\
'disaster_recovery/planned_failover.html#preflight-checks'.color(:red)
PromotionPreflightChecks.new.execute
end
 
def make_sure_primary_is_down
Loading
Loading
require 'io/console'
require 'rainbow/ext/string'
module Geo
class PromotionPreflightChecks
def execute
confirm_manual_checks
rescue RuntimeError => e
puts e.message
exit 1
end
private
def confirm_manual_checks
puts
puts 'Ensure you have completed the following manual '\
'preflight checks:'
puts '- Check if you need to migrate to Object Storage'
puts '- Review configuration of each secondary node'
puts '- Run system checks'
puts '- Check that secrets match between nodes'
puts '- Ensure Geo replication is up-to-date'
puts '- Verify the integrity of replicated data'
puts '- Notify users of scheduled maintenance'
puts 'Please read https://docs.gitlab.com/ee/administration/geo/'\
'disaster_recovery/planned_failover.html#preflight-checks'
puts
puts 'Did you perform all manual preflight checks (y/n)?'.color(:green)
return if STDIN.gets.chomp.casecmp('y').zero?
raise 'ERROR: Manual preflight checks were not performed! '\
'Please read https://docs.gitlab.com/ee/administration/geo/'\
'disaster_recovery/planned_failover.html#preflight-checks'.color(:red)
end
end
end
require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo/promotion_preflight_checks"
add_command_under_category('promotion-preflight-checks', 'gitlab-geo', 'Run preflight checks for promotion to primary node', 2) do |cmd_name|
Geo::PromotionPreflightChecks.new.execute
end
require 'spec_helper'
$LOAD_PATH << './files/gitlab-ctl-commands-ee/lib'
$LOAD_PATH << './files/gitlab-ctl-commands/lib'
require 'fileutils'
require 'geo/promote_to_primary_node'
require 'geo/promotion_preflight_checks'
require 'gitlab_ctl/util'
 
describe Geo::PromoteToPrimaryNode, '#execute' do
subject(:command) { described_class.new(nil, { skip_preflight_checks: true }) }
let(:options) { { skip_preflight_checks: true } }
subject(:command) { described_class.new(nil, options) }
 
let(:temp_directory) { Dir.mktmpdir }
let(:gitlab_config_path) { File.join(temp_directory, 'gitlab.rb') }
Loading
Loading
@@ -23,49 +22,29 @@ describe Geo::PromoteToPrimaryNode, '#execute' do
end
 
describe '#run_preflight_checks' do
subject(:run_preflight_checks) do
described_class.new(nil, options).send(:run_preflight_checks)
end
let(:confirmation) { 'y' }
before do
allow(STDIN).to receive(:gets).and_return(confirmation)
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)
allow(command).to receive(:success_message).and_return(true)
end
 
context 'when `--skip-preflight-checks` is passed' do
let(:options) { { skip_preflight_checks: true } }
let(:confirmation) { 'n' }
it 'does not run execute promotion preflight checks' do
expect(Geo::PromotionPreflightChecks).not_to receive(:execute)
 
it 'does not raise error' do
expect { run_preflight_checks }.not_to raise_error
command.execute
end
end
 
context 'when `--skip-preflight-checks` is not passed' do
let(:options) { {} }
 
it 'prints preflight check instructions' do
expect { run_preflight_checks }.to output(
/Ensure you have completed the following manual preflight checks/)
.to_stdout
end
context 'when confirmation is accepted' do
it 'does not raise an error' do
expect { run_preflight_checks }.to_not raise_error
end
end
context 'when confirmation is not accepted' do
let(:confirmation) { 'n' }
it 'runs promotion preflight checks' do
expect_any_instance_of(Geo::PromotionPreflightChecks).to receive(:execute)
 
it 'raises error' do
expect { run_preflight_checks }.to raise_error(
RuntimeError,
/ERROR: Manual preflight checks were not performed/
)
end
command.execute
end
end
end
Loading
Loading
require 'spec_helper'
require 'fileutils'
require 'geo/promotion_preflight_checks'
require 'gitlab_ctl/util'
describe Geo::PromotionPreflightChecks, '#execute' do
subject(:command) { described_class.new }
let(:confirmation) { 'y' }
before do
allow(STDIN).to receive(:gets).and_return(confirmation)
end
it 'prints preflight check instructions' do
expect { command.execute }.to output(
/Ensure you have completed the following manual preflight checks/)
.to_stdout
end
context 'when confirmation is accepted' do
it 'does not raise an error' do
expect { command.execute }.to_not raise_error
end
end
context 'when confirmation is not accepted' do
let(:confirmation) { 'n' }
around do |example|
example.run
rescue SystemExit
end
it 'print error message' do
expect { command.execute }.to output(
/ERROR: Manual preflight checks were not performed/
).to_stdout
end
end
end
require 'spec_helper'
require 'omnibus-ctl'
require 'geo/promote_to_primary_node'
 
describe 'gitlab-ctl promote-to-primary-node' do
subject(:ctl) { Omnibus::Ctl.new('testing-ctl') }
before do
allow_any_instance_of(Omnibus::Ctl).to receive(:require).and_call_original
allow_any_instance_of(Omnibus::Ctl).to receive(:require).with(
'/opt/testing-ctl/embedded/service/omnibus-ctl-ee/lib/geo/promote_to_primary_node'
) do
require_relative('../../files/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node')
end
ctl.load_file('files/gitlab-ctl-commands-ee/promote_to_primary_node.rb')
end
it 'appends a geo replication command' do
expect(subject.get_all_commands_hash).to include('promote-to-primary-node')
end
it 'executes the command when called' do
# ARGV contains the commands that were passed to rspec, which are
# invalid for the omnibus-ctl commands
oldargv = ARGV
ARGV = [] # rubocop:disable Style/MutableConstant
expect_any_instance_of(Geo::PromoteToPrimaryNode).to receive(:execute)
ctl.promote_to_primary_node
ARGV = oldargv
end
include_examples 'gitlab geo commands',
'promote-to-primary-node',
Geo::PromoteToPrimaryNode,
'promote_to_primary_node'
end
require 'spec_helper'
require 'geo/promotion_preflight_checks'
describe 'gitlab-ctl promotion-preflight-checks' do
include_examples 'gitlab geo commands',
'promotion-preflight-checks',
Geo::PromotionPreflightChecks,
'promotion_preflight_checks'
end
Loading
Loading
@@ -7,7 +7,10 @@ require 'gitlab/util'
require 'rspec-parameterized'
 
# Load support libraries to provide common convenience methods for our tests
Dir[File.join(__dir__, 'support/*.rb')].each { |f| require f }
Dir["./spec/support/**/*.rb"].each { |f| require f }
$LOAD_PATH << './files/gitlab-ctl-commands-ee/lib'
$LOAD_PATH << './files/gitlab-ctl-commands/lib'
 
Knapsack::Adapters::RSpecAdapter.bind if Gitlab::Util.get_env('USE_KNAPSACK')
 
Loading
Loading
require 'spec_helper'
require 'omnibus-ctl'
RSpec.shared_examples 'gitlab geo commands' do |command_name, klass, command_script|
subject(:ctl) { Omnibus::Ctl.new('testing-ctl') }
before do
allow_any_instance_of(Omnibus::Ctl).to receive(:require).and_call_original
allow_any_instance_of(Omnibus::Ctl).to receive(:require).with(
"/opt/testing-ctl/embedded/service/omnibus-ctl-ee/lib/geo/#{command_script}"
) do
require_relative("../../../files/gitlab-ctl-commands-ee/lib/geo/#{command_script}")
end
ctl.load_file("files/gitlab-ctl-commands-ee/#{command_script}.rb")
end
it 'appends a geo replication command' do
expect(subject.get_all_commands_hash).to include(command_name)
end
it 'executes the command when called' do
# ARGV contains the commands that were passed to rspec, which are
# invalid for the omnibus-ctl commands
oldargv = ARGV
ARGV = [] # rubocop:disable Style/MutableConstant
expect_any_instance_of(klass).to receive(:execute)
ctl.send(command_script)
ARGV = oldargv
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