diff --git a/files/gitlab-ctl-commands-ee/geo.rb b/files/gitlab-ctl-commands-ee/geo.rb index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9nZW8ucmI=..582166d5390365fd25d6538a406435f6eea8f60b_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9nZW8ucmI= 100644 --- a/files/gitlab-ctl-commands-ee/geo.rb +++ b/files/gitlab-ctl-commands-ee/geo.rb @@ -1,5 +1,6 @@ require 'optparse' +require "#{base_path}/embedded/cookbooks/package/libraries/gitlab_cluster" require "#{base_path}/embedded/service/omnibus-ctl/lib/gitlab_ctl" require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo" require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo/promote" diff --git a/files/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node.rb b/files/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node.rb deleted file mode 100644 index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9saWIvZ2VvL3Byb21vdGVfdG9fcHJpbWFyeV9ub2RlLnJi..0000000000000000000000000000000000000000 --- a/files/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node.rb +++ /dev/null @@ -1,105 +0,0 @@ -require 'io/console' -require 'rainbow/ext/string' - -module Geo - class PromoteToPrimaryNode - attr_accessor :ctl, :base_path - - def initialize(ctl, options) - @ctl = ctl - @base_path = @ctl.base_path - @options = options - end - - def execute - run_preflight_checks - final_confirmation - toggle_geo_roles - promote_postgresql_to_primary - reconfigure - promote_to_primary - success_message - end - - private - - def run_preflight_checks - return true if @options[:skip_preflight_checks] - - 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 final_confirmation - return if @options[:force] - - puts - puts 'WARNING: Secondary will now be promoted to primary. '\ - 'Are you sure you want to proceed? (y/n)'.color(:yellow) - - return if STDIN.gets.chomp.casecmp('y').zero? - - exit 1 - end - - def toggle_geo_roles - puts - puts 'Disabling the secondary role and enabling the primary in the cluster configuration file...'.color(:yellow) - puts - - GitlabCluster.config.set('primary', true) - GitlabCluster.config.set('secondary', false) - - return if GitlabCluster.config.save - - puts "ERROR: Could not write to #{GitlabCluster::JSON_FILE}.".color(:red) - exit 1 - end - - def promote_postgresql_to_primary - Geo::PromoteDb.new(ctl).execute - 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 diff --git a/files/gitlab-ctl-commands-ee/promote_db.rb b/files/gitlab-ctl-commands-ee/promote_db.rb deleted file mode 100644 index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9wcm9tb3RlX2RiLnJi..0000000000000000000000000000000000000000 --- a/files/gitlab-ctl-commands-ee/promote_db.rb +++ /dev/null @@ -1,32 +0,0 @@ -require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo/promote_db" - -# -# Copyright:: Copyright (c) 2020 GitLab Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -add_command_under_category('promote-db', 'gitlab-geo', 'Promote secondary PostgreSQL database', 2) do |cmd_name, *args| - print_deprecation_message - - Geo::PromoteDb.new(self).execute -end - -def print_deprecation_message - puts - puts 'WARNING: As of GitLab 14.5, this command is deprecated in favor of ' \ - 'gitlab-ctl geo promote. This command will be removed in GitLab 15.0. ' \ - 'Please see https://docs.gitlab.com/ee/administration/geo/disaster_recovery/planned_failover.html ' \ - 'for more details.'.color(:red) -end diff --git a/files/gitlab-ctl-commands-ee/promote_to_primary_node.rb b/files/gitlab-ctl-commands-ee/promote_to_primary_node.rb deleted file mode 100644 index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9wcm9tb3RlX3RvX3ByaW1hcnlfbm9kZS5yYg==..0000000000000000000000000000000000000000 --- a/files/gitlab-ctl-commands-ee/promote_to_primary_node.rb +++ /dev/null @@ -1,54 +0,0 @@ -require "#{base_path}/embedded/cookbooks/package/libraries/gitlab_cluster" -require "#{base_path}/embedded/service/omnibus-ctl-ee/lib/geo/promote_to_primary_node" - -# -# Copyright:: Copyright (c) 2017 GitLab Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -add_command_under_category('promote-to-primary-node', 'gitlab-geo', 'Promote to primary node', 2) do |cmd_name, *args| - def get_ctl_options - options = {} - OptionParser.new do |opts| - opts.banner = "Usage: gitlab-ctl promote-to-primary-node [options]" - - opts.on('-p', '--[no-]confirm-primary-is-down', 'Do not ask for confirmation that primary is down') do |p| - options[:confirm_primary_is_down] = p - end - - opts.on('-m', '--skip-preflight-checks', 'Perform promotion without running any preflight checks') 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 - end - - print_deprecation_message - - Geo::PromoteToPrimaryNode.new(self, get_ctl_options).execute -end - -def print_deprecation_message - puts - puts 'WARNING: As of GitLab 14.5, this command is deprecated in favor of ' \ - 'gitlab-ctl geo promote. This command will be removed in GitLab 15.0. ' \ - 'Please see https://docs.gitlab.com/ee/administration/geo/disaster_recovery/planned_failover.html ' \ - 'for more details.'.color(:red) -end diff --git a/spec/chef/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node_spec.rb b/spec/chef/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node_spec.rb deleted file mode 100644 index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL2dlby9wcm9tb3RlX3RvX3ByaW1hcnlfbm9kZV9zcGVjLnJi..0000000000000000000000000000000000000000 --- a/spec/chef/gitlab-ctl-commands-ee/lib/geo/promote_to_primary_node_spec.rb +++ /dev/null @@ -1,275 +0,0 @@ -require 'spec_helper' -require 'fileutils' -require 'geo/promote_to_primary_node' -require 'geo/promote_db' -require 'geo/promotion_preflight_checks' -require 'gitlab_ctl/util' - -RSpec.describe Geo::PromoteToPrimaryNode, '#execute' do - let(:options) { { skip_preflight_checks: true } } - - let(:instance) { double(base_path: '/opt/gitlab/embedded', data_path: '/var/opt/gitlab/postgresql/data') } - - subject(:command) { described_class.new(instance, options) } - - let(:config_path) { Dir.mktmpdir } - - before do - allow($stdout).to receive(:puts) - allow($stdout).to receive(:print) - - allow(command).to receive(:run_command).with(any_args) - end - - after do - FileUtils.rm_rf(config_path) - end - - describe '#promote_postgresql_to_primary' do - before do - allow(STDIN).to receive(:gets).and_return('y') - - allow(command).to receive(:toggle_geo_roles).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 - - it 'promotes the database' do - expect_any_instance_of(Geo::PromoteDb).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(:toggle_geo_roles).and_return(true) - 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 - it 'does not run execute promotion preflight checks' do - expect_any_instance_of(Geo::PromotionPreflightChecks).not_to receive(:execute) - - command.execute - end - end - - context 'when `--skip-preflight-checks` is not passed' do - let(:options) { { confirm_primary_is_down: true } } - - before do - allow_any_instance_of(Geo::PromotionPreflightChecks).to receive( - :execute).and_return(true) - end - - it 'runs preflight checks' do - expect_any_instance_of(Geo::PromotionPreflightChecks).to receive(:execute) - - command.execute - end - - it 'passes given options to preflight checks command' do - expect(Geo::PromotionPreflightChecks).to receive(:new).with( - '/opt/gitlab/embedded', options).and_call_original - - command.execute - end - end - end - - describe '#toggle_geo_roles' do - let(:gitlab_cluster_config_path) { File.join(config_path, 'gitlab-cluster.json') } - - before do - stub_const('GitlabCluster::CONFIG_PATH', config_path) - stub_const('GitlabCluster::JSON_FILE', gitlab_cluster_config_path) - - allow(STDIN).to receive(:gets).and_return('y') - - allow(command).to receive(:run_preflight_checks).and_return(true) - 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 the cluster configuration file does not exist' do - it 'creates the file with the Geo primary role enabled and secondary role disabled' do - command.execute - - expect(File.exist?(gitlab_cluster_config_path)).to eq(true) - expect(read_file_content(gitlab_cluster_config_path)).to eq("primary" => true, "secondary" => false) - end - end - - context 'when the cluster configuration file exists' do - it 'disables the Geo secondary role' do - write_file_content(gitlab_cluster_config_path, primary: false, secondary: true) - - command.execute - - expect(read_file_content(gitlab_cluster_config_path)).to eq("primary" => true, "secondary" => false) - end - end - - def read_file_content(fullpath) - JSON.parse(File.read(fullpath)) - end - - def write_file_content(fullpath, content) - File.open(fullpath, 'w') do |f| - f.write(content.to_json) - f.chmod(0600) - end - end - end - - context 'when preflight checks pass' do - before do - allow(STDIN).to receive(:gets).and_return('y') - - allow_any_instance_of(Geo::PromotionPreflightChecks).to receive( - :execute).and_return(true) - - allow(command).to receive(:toggle_geo_roles).and_return(true) - 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 running in force mode' do - let(:options) { { force: true } } - - it 'does not ask for final confirmation' do - expect { command.execute }.not_to output( - /WARNING\: Secondary will now be promoted to primary./).to_stdout - end - end - - context 'when not running in force mode' do - let(:options) { { force: false } } - - it 'asks for confirmation' do - expect { command.execute }.to output( - /WARNING\: Secondary will now be promoted to primary./).to_stdout - end - - context 'when final confirmation is given' do - it 'calls all the subcommands' do - expect(command).to receive(:toggle_geo_roles) - expect(command).to receive(:promote_postgresql_to_primary) - expect(command).to receive(:reconfigure) - expect(command).to receive(:promote_to_primary) - expect(command).to receive(:success_message) - - command.execute - end - end - end - 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 - - 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(:toggle_geo_roles) - is_expected.to receive(:promote_postgresql_to_primary) - is_expected.to receive(:reconfigure) - is_expected.to receive(:promote_to_primary) - is_expected.to receive(:success_message) - - 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 - - context 'when writing to the cluster configuration file fail' do - around do |example| - example.run - rescue SystemExit - end - - before do - allow(STDIN).to receive(:gets).and_return('y') - - allow(command).to receive(:run_preflight_checks).and_return(true) - - allow(GitlabCluster.config).to receive(:save).and_return(false) - end - - it 'exits with 1' do - expect { command.execute }.to raise_error(SystemExit) - end - end - - context 'when writing to the cluster configuration file succeed' 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) - allow(command).to receive(:success_message).and_return(true) - - allow(GitlabCluster.config).to receive(:save).and_return(true) - end - - it 'calls all the subcommands' do - expect(command).to receive(:promote_postgresql_to_primary) - expect(command).to receive(:reconfigure) - expect(command).to receive(:promote_to_primary) - expect(command).to receive(:success_message) - - command.execute - end - end -end diff --git a/spec/chef/gitlab-ctl-commands-ee/lib/geo/replication_toggle_command_spec.rb b/spec/chef/gitlab-ctl-commands-ee/lib/geo/replication_toggle_command_spec.rb index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL2dlby9yZXBsaWNhdGlvbl90b2dnbGVfY29tbWFuZF9zcGVjLnJi..582166d5390365fd25d6538a406435f6eea8f60b_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL2dlby9yZXBsaWNhdGlvbl90b2dnbGVfY29tbWFuZF9zcGVjLnJi 100644 --- a/spec/chef/gitlab-ctl-commands-ee/lib/geo/replication_toggle_command_spec.rb +++ b/spec/chef/gitlab-ctl-commands-ee/lib/geo/replication_toggle_command_spec.rb @@ -4,7 +4,6 @@ $LOAD_PATH << './files/gitlab-ctl-commands/lib' require 'geo/replication_toggle_command' -require 'geo/promote_to_primary_node' require 'gitlab_ctl/util' RSpec.describe Geo::ReplicationToggleCommand do diff --git a/spec/chef/gitlab-ctl-commands-ee/promote_db_spec.rb b/spec/chef/gitlab-ctl-commands-ee/promote_db_spec.rb deleted file mode 100644 index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvcHJvbW90ZV9kYl9zcGVjLnJi..0000000000000000000000000000000000000000 --- a/spec/chef/gitlab-ctl-commands-ee/promote_db_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -require 'chef_helper' -require 'geo/promote_db' - -RSpec.describe 'gitlab-ctl promote-db' do - let(:klass) { Geo::PromoteDb } - let(:command_name) { 'promote-db' } - let(:command_script) { 'promote_db' } - - include_context 'ctl' - - it_behaves_like 'gitlab geo promotion commands', 'promote-db' - - # rubocop:disable Style/MutableConstant, Lint/ConstantDefinitionInBlock - it 'prints the deprecation message' do - # ARGV contains the commands that were passed to rspec, which are - # invalid for the omnibus-ctl commands - oldargv = ARGV - ARGV = [] - - expect_any_instance_of(klass).to receive(:execute) - - expect { ctl.send(command_script) }.to output( - /WARNING: As of GitLab 14.5, this command is deprecated/).to_stdout - - ARGV = oldargv - end - # rubocop:enable Style/MutableConstant, Lint/ConstantDefinitionInBlock -end diff --git a/spec/chef/gitlab-ctl-commands-ee/promote_to_primary_node_spec.rb b/spec/chef/gitlab-ctl-commands-ee/promote_to_primary_node_spec.rb deleted file mode 100644 index 0c64f5dc3122c08ffea3e60e139897ddbcbfc0c7_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvcHJvbW90ZV90b19wcmltYXJ5X25vZGVfc3BlYy5yYg==..0000000000000000000000000000000000000000 --- a/spec/chef/gitlab-ctl-commands-ee/promote_to_primary_node_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'geo/promote_to_primary_node' - -RSpec.describe 'gitlab-ctl promote-to-primary-node' do - let(:klass) { Geo::PromoteToPrimaryNode } - let(:command_name) { 'promote-to-primary-node' } - let(:command_script) { 'promote_to_primary_node' } - - include_context 'ctl' - - it_behaves_like 'gitlab geo promotion commands', 'promote-to-primary-node' - - it_behaves_like 'geo promotion command accepts option', - '--confirm-primary-is-down', - { confirm_primary_is_down: true } - - 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 } - - # rubocop:disable Style/MutableConstant, Lint/ConstantDefinitionInBlock - it 'prints the deprecation message' 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) - - expect { ctl.send(command_script) }.to output( - /WARNING: As of GitLab 14.5, this command is deprecated/).to_stdout - - ARGV = oldargv - end - # rubocop:enable Style/MutableConstant, Lint/ConstantDefinitionInBlock -end