summaryrefslogtreecommitdiff
path: root/lib/tasks/gitlab/praefect.rake
blob: 346df3e0c7585763478441bf3532bf7d868d1960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

namespace :gitlab do
  namespace :praefect do
    def int?(string)
      true if Integer(string) rescue false
    end

    def print_checksums(header, row)
      header.each_with_index do |val, i|
        width = [val.length, row[i].length].max
        header[i] = header[i].ljust(width)
        row[i] = row[i].ljust(width)
      end

      header_str = header.join(' | ')
      puts header_str
      puts '-' * header_str.length
      puts row.join(' | ')
    end

    desc 'GitLab | Praefect | Check replicas'
    task :replicas, [:project_id] => :gitlab_environment do |t, args|
      warn_user_is_not_gitlab

      unless int?(args.project_id)
        puts 'argument must be a valid project_id'
        next
      end

      project = Project.find_by_id(args.project_id)
      if project.nil?
        puts 'No project was found with that id'
        next
      end

      begin
        replicas_resp = project.repository.replicas

        sorted_replicas = replicas_resp.replicas.sort_by { |r| r.repository.storage_name }

        header = ['Project name'] << "#{replicas_resp.primary.repository.storage_name} (primary)"
        header.concat(sorted_replicas.map { |r| r.repository.storage_name })

        row = [project.name] << replicas_resp.primary.checksum
        row.concat(sorted_replicas.map {|r| r.checksum})
      rescue
        puts 'Something went wrong when getting replicas.'
        next
      end

      puts "\n"
      print_checksums(header, row)
    end
  end
end