Skip to content
Snippets Groups Projects
Commit 723997ca authored by Ian Baum's avatar Ian Baum
Browse files

Merge branch 'ag-consistent-naming' into 'master'

Make naming for promote script code consistent

See merge request gitlab-org/omnibus-gitlab!4247
parents e09bc77d 811ee4c7
No related branches found
No related tags found
No related merge requests found
require 'io/console'
require 'rainbow/ext/string'
module Geo
class PromoteToPrimary
def initialize(base_path, options)
@base_path = base_path
@options = options
end
def execute
run_preflight_checks
make_sure_primary_is_down
promote_postgresql_to_primary
reconfigure
promote_to_primary
success_message
end
private
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)
end
def make_sure_primary_is_down
return true if @options[:confirm_primary_is_down]
puts
puts '---------------------------------------'.color(:yellow)
puts 'WARNING: Make sure your primary is down'.color(:yellow)
puts 'If you have more than one secondary please see https://docs.gitlab.com/ee/gitlab-geo/disaster-recovery.html#promoting-secondary-geo-replica-in-multi-secondary-configurations'.color(:yellow)
puts 'There may be data saved to the primary that was not been replicated to the secondary before the primary went offline. This data should be treated as lost if you proceed.'.color(:yellow)
puts '---------------------------------------'.color(:yellow)
puts
print '*** Are you sure? (N/y): '.color(:green)
raise 'Exited because primary node must be down' unless STDIN.gets.chomp.casecmp('y').zero?
end
def promote_postgresql_to_primary
puts
puts 'Promoting the PostgreSQL to primary...'.color(:yellow)
puts
run_command("/opt/gitlab/embedded/bin/gitlab-pg-ctl promote", live: true).error!
end
def reconfigure
puts
puts 'Reconfiguring...'.color(:yellow)
puts
run_command('gitlab-ctl reconfigure', live: true)
end
def promote_to_primary
puts
puts 'Running gitlab-rake geo:set_secondary_as_primary...'.color(:yellow)
puts
run_command('gitlab-rake geo:set_secondary_as_primary', live: true)
end
def success_message
puts
puts 'You successfully promoted this node!'.color(:green)
end
def run_command(cmd, live: false)
GitlabCtl::Util.run_command(cmd, live: live)
end
end
end
require 'io/console'
require 'rainbow/ext/string'
module Geo
class PromoteToPrimaryNode
def initialize(base_path, options)
@base_path = base_path
@options = options
end
def execute
run_preflight_checks
make_sure_primary_is_down
promote_postgresql_to_primary
reconfigure
promote_to_primary
success_message
end
private
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)
end
def make_sure_primary_is_down
return true if @options[:confirm_primary_is_down]
puts
puts '---------------------------------------'.color(:yellow)
puts 'WARNING: Make sure your primary is down'.color(:yellow)
puts 'If you have more than one secondary please see https://docs.gitlab.com/ee/gitlab-geo/disaster-recovery.html#promoting-secondary-geo-replica-in-multi-secondary-configurations'.color(:yellow)
puts 'There may be data saved to the primary that was not been replicated to the secondary before the primary went offline. This data should be treated as lost if you proceed.'.color(:yellow)
puts '---------------------------------------'.color(:yellow)
puts
print '*** Are you sure? (N/y): '.color(:green)
raise 'Exited because primary node must be down' unless STDIN.gets.chomp.casecmp('y').zero?
end
def promote_postgresql_to_primary
puts
puts 'Promoting the PostgreSQL to primary...'.color(:yellow)
puts
run_command("/opt/gitlab/embedded/bin/gitlab-pg-ctl promote", live: true).error!
end
def reconfigure
puts
puts 'Reconfiguring...'.color(:yellow)
puts
run_command('gitlab-ctl reconfigure', live: true)
end
def promote_to_primary
puts
puts 'Running gitlab-rake geo:set_secondary_as_primary...'.color(:yellow)
puts
run_command('gitlab-rake geo:set_secondary_as_primary', live: true)
end
def success_message
puts
puts 'You successfully promoted this node!'.color(:green)
end
def run_command(cmd, live: false)
GitlabCtl::Util.run_command(cmd, live: live)
end
end
end
require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo/promote_to_primary"
require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo/promote_to_primary_node"
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
......@@ -18,7 +18,7 @@
#
add_command_under_category('promote-to-primary-node', 'gitlab-geo', 'Promote to primary node', 2) do |cmd_name, *args|
Geo::PromoteToPrimary.new(base_path, get_ctl_options).execute
Geo::PromoteToPrimaryNode.new(base_path, get_ctl_options).execute
end
def get_ctl_options
......
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 'gitlab_ctl/util'
describe Geo::PromoteToPrimaryNode, '#execute' do
subject(:command) { described_class.new(nil, { skip_preflight_checks: true }) }
let(:temp_directory) { Dir.mktmpdir }
let(:gitlab_config_path) { File.join(temp_directory, 'gitlab.rb') }
before do
allow(command).to receive(:puts)
allow(command).to receive(:print)
end
after do
FileUtils.rm_rf(temp_directory)
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)
end
context 'when `--skip-preflight-checks` is passed' do
let(:options) { { skip_preflight_checks: true } }
let(:confirmation) { 'n' }
it 'does not raise error' do
expect { run_preflight_checks }.not_to raise_error
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 'raises error' do
expect { run_preflight_checks }.to raise_error(
RuntimeError,
/ERROR: Manual preflight checks were not performed/
)
end
end
end
end
context 'when confirmation is accepted' do
before do
allow(STDIN).to receive(:gets).and_return('y')
end
it 'calls all the subcommands' do
is_expected.to receive(:run_command).with('gitlab-ctl reconfigure', live: true).once
is_expected.to receive(:run_command).with('gitlab-rake geo:set_secondary_as_primary', live: true).once
shell_out_object = double.tap { |shell_out_object| expect(shell_out_object).to receive(:error!) }
is_expected.to receive(:run_command).with("/opt/gitlab/embedded/bin/gitlab-pg-ctl promote", live: true).once.and_return(shell_out_object)
command.execute
end
end
context 'when confirmation is refused' do
before do
allow(STDIN).to receive(:gets).and_return('n')
end
it 'calls all the subcommands' do
is_expected.not_to receive(:run_command)
expect { command.execute }.to raise_error RuntimeError, 'Exited because primary node must be down'
end
end
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'
require 'gitlab_ctl/util'
describe Geo::PromoteToPrimary, '#execute' do
subject(:command) { described_class.new(nil, { skip_preflight_checks: true }) }
let(:temp_directory) { Dir.mktmpdir }
let(:gitlab_config_path) { File.join(temp_directory, 'gitlab.rb') }
before do
allow(command).to receive(:puts)
allow(command).to receive(:print)
end
after do
FileUtils.rm_rf(temp_directory)
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)
end
context 'when `--skip-preflight-checks` is passed' do
let(:options) { { skip_preflight_checks: true } }
let(:confirmation) { 'n' }
it 'does not raise error' do
expect { run_preflight_checks }.not_to raise_error
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 'raises error' do
expect { run_preflight_checks }.to raise_error(
RuntimeError,
/ERROR: Manual preflight checks were not performed/
)
end
end
end
end
context 'when confirmation is accepted' do
before do
allow(STDIN).to receive(:gets).and_return('y')
end
it 'calls all the subcommands' do
is_expected.to receive(:run_command).with('gitlab-ctl reconfigure', live: true).once
is_expected.to receive(:run_command).with('gitlab-rake geo:set_secondary_as_primary', live: true).once
shell_out_object = double.tap { |shell_out_object| expect(shell_out_object).to receive(:error!) }
is_expected.to receive(:run_command).with("/opt/gitlab/embedded/bin/gitlab-pg-ctl promote", live: true).once.and_return(shell_out_object)
command.execute
end
end
context 'when confirmation is refused' do
before do
allow(STDIN).to receive(:gets).and_return('n')
end
it 'calls all the subcommands' do
is_expected.not_to receive(:run_command)
expect { command.execute }.to raise_error RuntimeError, 'Exited because primary node must be down'
end
end
end
......@@ -7,5 +7,5 @@
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'
'/opt/testing-ctl/embedded/service/omnibus-ctl-ee/lib/geo/promote_to_primary_node'
) do
......@@ -11,5 +11,5 @@
) do
require_relative('../../files/gitlab-ctl-commands-ee/lib/geo/promote_to_primary')
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')
......@@ -25,7 +25,7 @@
oldargv = ARGV
ARGV = [] # rubocop:disable Style/MutableConstant
expect_any_instance_of(Geo::PromoteToPrimary).to receive(:execute)
expect_any_instance_of(Geo::PromoteToPrimaryNode).to receive(:execute)
ctl.promote_to_primary_node
......
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