guess_patroni_node_role

in files/gitlab-ctl-commands/pg-upgrade.rb [651:699]


def guess_patroni_node_role
  failure_cause = :none
  unless GitlabCtl::Util.progress_message('Attempting to detect the role of this Patroni node') do
    begin
      node = Patroni::Client.new
      scope = @attributes.dig(:patroni, :scope)
      node_name = @attributes.dig(:patroni, :name)
      consul_binary = @attributes.dig(:consul, :binary_path)

      if node.up?
        @instance_type = :patroni_leader if node.leader?
        @instance_type = :patroni_replica if node.replica?
        @instance_type = :patroni_standby_leader if node.standby_leader?
        failure_cause = :patroni_running_on_replica if @instance_type == :patroni_replica
        @instance_type == :patroni_leader || @instance_type == :patroni_standby_leader
      else
        leader_name = GitlabCtl::Util.get_command_output("#{consul_binary} kv get /service/#{scope}/leader").strip
        @instance_type = node_name == leader_name ? :patroni_leader : :patroni_replica unless leader_name.nil? || leader_name.empty?
        failure_cause = :patroni_stopped_on_leader if @instance_type == :patroni_leader
        @instance_type == :patroni_replica
      end
    rescue GitlabCtl::Errors::ExecutionError => e
      log 'Unable to get the role of the Patroni node from Consul'
      log "STDOUT: #{e.stdout}"
      log "STDERR: #{e.stderr}"
      false
    end
  end
    case failure_cause
    when :patroni_running_on_replica
      log 'Looks like that this is a replica node but the Patroni service is still running.'
      log 'Try to stop the Patroni service before attempting the upgrade.'

      die 'Patroni service is still running on the replica node.'
    when :patroni_stopped_on_leader
      log 'Looks like that this is the leader node but the Patroni is not running.'
      log 'Try to start the Patroni service before attempting the upgrade.'

      die 'Patroni service is not running on the leader node.'
    else
      log 'Unable to detect the role of this Patroni node.'
      log 'Try to use --leader, --replica or --standby-leader switches to specify the role manually.'
      log 'See: https://docs.gitlab.com/ee/administration/postgresql/replication_and_failover.html#upgrading-postgresql-major-version-in-a-patroni-cluster'

      die 'Unable to detect the role of this Patroni node.'
    end
  end
end