BigW Consortium Gitlab

check.rake 22.2 KB
Newer Older
1
namespace :gitlab do
2 3
  desc "GITLAB | Check the configuration of GitLab and its environment"
  task check: %w{gitlab:env:check
4
                 gitlab:gitlab_shell:check
5
                 gitlab:sidekiq:check
6
                 gitlab:ldap:check
Riyad Preukschas committed
7 8 9
                 gitlab:app:check}


10

11
  namespace :app do
12 13
    desc "GITLAB | Check the configuration of the GitLab Rails app"
    task check: :environment  do
Riyad Preukschas committed
14 15 16 17 18 19
      warn_user_is_not_gitlab
      start_checking "GitLab"

      check_database_config_exists
      check_database_is_not_sqlite
      check_migrations_are_up
20
      check_orphaned_group_members
Riyad Preukschas committed
21 22 23 24 25 26
      check_gitlab_config_exists
      check_gitlab_config_not_outdated
      check_log_writable
      check_tmp_writable
      check_init_script_exists
      check_init_script_up_to_date
Hiroyuki Sato committed
27
      check_projects_have_namespace
Riyad Preukschas committed
28
      check_satellites_exist
29
      check_redis_version
30
      check_ruby_version
31
      check_git_version
32
      check_active_users
Riyad Preukschas committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

      finished_checking "GitLab"
    end


    # Checks
    ########################

    def check_database_config_exists
      print "Database config exists? ... "

      database_config_file = Rails.root.join("config", "database.yml")

      if File.exists?(database_config_file)
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Copy config/database.yml.<your db> to config/database.yml",
          "Check that the information in config/database.yml is correct"
        )
        for_more_information(
          see_database_guide,
          "http://guides.rubyonrails.org/getting_started.html#configuring-a-database"
        )
Riyad Preukschas committed
58
        fix_and_rerun
Riyad Preukschas committed
59 60 61 62
      end
    end

    def check_database_is_not_sqlite
63
      print "Database is SQLite ... "
Riyad Preukschas committed
64 65 66

      database_config_file = Rails.root.join("config", "database.yml")

67
      unless File.read(database_config_file) =~ /adapter:\s+sqlite/
68
        puts "no".green
Riyad Preukschas committed
69
      else
70
        puts "yes".red
71
        puts "Please fix this by removing the SQLite entry from the database.yml".blue
Riyad Preukschas committed
72 73 74 75
        for_more_information(
          "https://github.com/gitlabhq/gitlabhq/wiki/Migrate-from-SQLite-to-MySQL",
          see_database_guide
        )
Riyad Preukschas committed
76
        fix_and_rerun
Riyad Preukschas committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      end
    end

    def check_gitlab_config_exists
      print "GitLab config exists? ... "

      gitlab_config_file = Rails.root.join("config", "gitlab.yml")

      if File.exists?(gitlab_config_file)
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Copy config/gitlab.yml.example to config/gitlab.yml",
          "Update config/gitlab.yml to match your setup"
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
Riyad Preukschas committed
96
        fix_and_rerun
Riyad Preukschas committed
97 98 99 100
      end
    end

    def check_gitlab_config_not_outdated
101
      print "GitLab config outdated? ... "
Riyad Preukschas committed
102 103 104 105 106 107 108

      gitlab_config_file = Rails.root.join("config", "gitlab.yml")
      unless File.exists?(gitlab_config_file)
        puts "can't check because of previous errors".magenta
      end

      # omniauth or ldap could have been deleted from the file
109
      unless Gitlab.config['git_host']
110
        puts "no".green
Riyad Preukschas committed
111
      else
112
        puts "yes".red
Riyad Preukschas committed
113
        try_fixing_it(
114
          "Backup your config/gitlab.yml",
Riyad Preukschas committed
115 116 117 118 119 120
          "Copy config/gitlab.yml.example to config/gitlab.yml",
          "Update config/gitlab.yml to match your setup"
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
Riyad Preukschas committed
121
        fix_and_rerun
Riyad Preukschas committed
122 123
      end
    end
124

Riyad Preukschas committed
125 126 127
    def check_init_script_exists
      print "Init script exists? ... "

128 129 130 131 132
      if omnibus_gitlab?
        puts 'skipped (omnibus-gitlab has no init script)'.magenta
        return
      end

Riyad Preukschas committed
133 134 135 136
      script_path = "/etc/init.d/gitlab"

      if File.exists?(script_path)
        puts "yes".green
Nihad Abbasov committed
137
      else
Riyad Preukschas committed
138 139 140 141 142 143 144
        puts "no".red
        try_fixing_it(
          "Install the init script"
        )
        for_more_information(
          see_installation_guide_section "Install Init Script"
        )
Riyad Preukschas committed
145
        fix_and_rerun
Riyad Preukschas committed
146 147 148 149 150 151
      end
    end

    def check_init_script_up_to_date
      print "Init script up-to-date? ... "

152 153 154 155 156
      if omnibus_gitlab?
        puts 'skipped (omnibus-gitlab has no init script)'.magenta
        return
      end

157
      recipe_path = Rails.root.join("lib/support/init.d/", "gitlab")
Riyad Preukschas committed
158
      script_path = "/etc/init.d/gitlab"
159

Riyad Preukschas committed
160 161
      unless File.exists?(script_path)
        puts "can't check because of previous errors".magenta
162 163 164
        return
      end

165
      recipe_content = File.read(recipe_path)
Riyad Preukschas committed
166 167 168 169 170 171 172 173 174 175 176 177
      script_content = File.read(script_path)

      if recipe_content == script_content
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Redownload the init script"
        )
        for_more_information(
          see_installation_guide_section "Install Init Script"
        )
Riyad Preukschas committed
178
        fix_and_rerun
Riyad Preukschas committed
179 180 181 182 183 184
      end
    end

    def check_migrations_are_up
      print "All migrations up? ... "

185
      migration_status, _ = Gitlab::Popen.popen(%W(bundle exec rake db:migrate:status))
Riyad Preukschas committed
186 187 188

      unless migration_status =~ /down\s+\d{14}/
        puts "yes".green
189
      else
Riyad Preukschas committed
190 191
        puts "no".red
        try_fixing_it(
192
          sudo_gitlab("bundle exec rake db:migrate RAILS_ENV=production")
Riyad Preukschas committed
193
        )
Riyad Preukschas committed
194
        fix_and_rerun
Riyad Preukschas committed
195 196 197
      end
    end

198
    def check_orphaned_group_members
199 200
      print "Database contains orphaned GroupMembers? ... "
      if GroupMember.where("user_id not in (select id from users)").count > 0
201
        puts "yes".red
202 203
        try_fixing_it(
          "You can delete the orphaned records using something along the lines of:",
204
          sudo_gitlab("bundle exec rails runner -e production 'GroupMember.where(\"user_id NOT IN (SELECT id FROM users)\").delete_all'")
205
        )
206 207 208 209 210
      else
        puts "no".green
      end
    end

Riyad Preukschas committed
211 212 213 214 215
    def check_satellites_exist
      print "Projects have satellites? ... "

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
216 217
        return
      end
Riyad Preukschas committed
218 219 220
      puts ""

      Project.find_each(batch_size: 100) do |project|
221
        print sanitized_message(project)
Riyad Preukschas committed
222 223 224

        if project.satellite.exists?
          puts "yes".green
225 226
        elsif project.empty_repo?
          puts "can't create, repository is empty".magenta
Riyad Preukschas committed
227 228 229
        else
          puts "no".red
          try_fixing_it(
230
            sudo_gitlab("bundle exec rake gitlab:satellites:create RAILS_ENV=production"),
231 232
            "If necessary, remove the tmp/repo_satellites directory ...",
            "... and rerun the above command"
Riyad Preukschas committed
233 234 235 236
          )
          for_more_information(
            "doc/raketasks/maintenance.md "
          )
Riyad Preukschas committed
237
          fix_and_rerun
Riyad Preukschas committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        end
      end
    end

    def check_log_writable
      print "Log directory writable? ... "

      log_path = Rails.root.join("log")

      if File.writable?(log_path)
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "sudo chown -R gitlab #{log_path}",
bassrock committed
253
          "sudo chmod -R u+rwX #{log_path}"
Riyad Preukschas committed
254 255 256 257
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
Riyad Preukschas committed
258
        fix_and_rerun
Riyad Preukschas committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272
      end
    end

    def check_tmp_writable
      print "Tmp directory writable? ... "

      tmp_path = Rails.root.join("tmp")

      if File.writable?(tmp_path)
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "sudo chown -R gitlab #{tmp_path}",
bassrock committed
273
          "sudo chmod -R u+rwX #{tmp_path}"
Riyad Preukschas committed
274 275 276 277
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
Riyad Preukschas committed
278
        fix_and_rerun
Riyad Preukschas committed
279
      end
280
    end
281

282
    def check_redis_version
283 284
      print "Redis version >= 2.0.0? ... "

285
      if run_and_match(%W(redis-cli --version), /redis-cli 2.\d.\d/)
286
        puts "yes".green
287
      else
288 289
        puts "no".red
        try_fixing_it(
290
          "Update your redis server to a version >= 2.0.0"
291 292 293 294 295 296
        )
        for_more_information(
          "gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq"
        )
        fix_and_rerun
      end
297
    end
298 299
  end

Riyad Preukschas committed
300 301


302 303 304
  namespace :env do
    desc "GITLAB | Check the configuration of the environment"
    task check: :environment  do
Riyad Preukschas committed
305 306 307 308 309 310 311 312 313 314 315 316 317
      warn_user_is_not_gitlab
      start_checking "Environment"

      check_gitlab_git_config

      finished_checking "Environment"
    end


    # Checks
    ########################

    def check_gitlab_git_config
318
      print "Git configured for #{gitlab_user} user? ... "
Riyad Preukschas committed
319 320 321

      options = {
        "user.name"  => "GitLab",
322 323
        "user.email" => Gitlab.config.gitlab.email_from,
        "core.autocrlf" => "input"
Riyad Preukschas committed
324 325
      }
      correct_options = options.map do |name, value|
326
        run(%W(#{Gitlab.config.git.bin_path} config --global --get #{name})).try(:squish) == value
Riyad Preukschas committed
327 328 329 330 331
      end

      if correct_options.all?
        puts "yes".green
      else
332 333 334 335 336 337 338 339 340 341 342 343 344 345
        print "Trying to fix Git error automatically. ..."
        if auto_fix_git_config(options)
          puts "Success".green
        else
          puts "Failed".red
          try_fixing_it(
            sudo_gitlab("\"#{Gitlab.config.git.bin_path}\" config --global user.name  \"#{options["user.name"]}\""),
            sudo_gitlab("\"#{Gitlab.config.git.bin_path}\" config --global user.email \"#{options["user.email"]}\""),
            sudo_gitlab("\"#{Gitlab.config.git.bin_path}\" config --global core.autocrlf \"#{options["core.autocrlf"]}\"")
          )
          for_more_information(
            see_installation_guide_section "GitLab"
          )
        end
Riyad Preukschas committed
346 347
      end
    end
348 349
  end

Riyad Preukschas committed
350 351


352
  namespace :gitlab_shell do
353
    desc "GITLAB | Check the configuration of GitLab Shell"
354
    task check: :environment  do
Riyad Preukschas committed
355
      warn_user_is_not_gitlab
356
      start_checking "GitLab Shell"
Riyad Preukschas committed
357

358
      check_gitlab_shell
Riyad Preukschas committed
359
      check_repo_base_exists
360
      check_repo_base_is_not_symlink
Riyad Preukschas committed
361 362
      check_repo_base_user_and_group
      check_repo_base_permissions
363
      check_satellites_permissions
364
      check_repos_hooks_directory_is_link
365
      check_gitlab_shell_self_test
Riyad Preukschas committed
366

367
      finished_checking "GitLab Shell"
Riyad Preukschas committed
368 369 370 371 372 373 374 375 376
    end


    # Checks
    ########################

    def check_repo_base_exists
      print "Repo base directory exists? ... "

377
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
Riyad Preukschas committed
378 379 380 381 382 383 384

      if File.exists?(repo_base_path)
        puts "yes".green
      else
        puts "no".red
        puts "#{repo_base_path} is missing".red
        try_fixing_it(
385
          "This should have been created when setting up GitLab Shell.",
Riyad Preukschas committed
386
          "Make sure it's set correctly in config/gitlab.yml",
387
          "Make sure GitLab Shell is installed correctly."
Riyad Preukschas committed
388 389
        )
        for_more_information(
390
          see_installation_guide_section "GitLab Shell"
Riyad Preukschas committed
391
        )
Riyad Preukschas committed
392
        fix_and_rerun
Riyad Preukschas committed
393 394 395
      end
    end

396 397 398
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

399
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
        return
      end

      unless File.symlink?(repo_base_path)
        puts "no".green
      else
        puts "yes".red
        try_fixing_it(
          "Make sure it's set to the real directory in config/gitlab.yml"
        )
        fix_and_rerun
      end
    end

Riyad Preukschas committed
416
    def check_repo_base_permissions
417
      print "Repo base access is drwxrws---? ... "
Riyad Preukschas committed
418

419
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
Riyad Preukschas committed
420 421
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
422 423 424
        return
      end

425
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
Riyad Preukschas committed
426 427 428 429
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
430
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
431
          "sudo chmod -R ug-s #{repo_base_path}",
432
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
Riyad Preukschas committed
433 434
        )
        for_more_information(
435
          see_installation_guide_section "GitLab Shell"
Riyad Preukschas committed
436
        )
Riyad Preukschas committed
437
        fix_and_rerun
Riyad Preukschas committed
438 439 440
      end
    end

441 442 443 444 445 446 447 448 449 450 451 452 453 454
    def check_satellites_permissions
      print "Satellites access is drwxr-x---? ... "

      satellites_path = Gitlab.config.satellites.path
      unless File.exists?(satellites_path)
        puts "can't check because of previous errors".magenta
        return
      end

      if File.stat(satellites_path).mode.to_s(8).ends_with?("0750")
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
Michael Krane committed
455
          "sudo chmod u+rwx,g=rx,o-rwx #{satellites_path}",
456 457 458 459 460 461 462 463
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
        fix_and_rerun
      end
    end

Riyad Preukschas committed
464
    def check_repo_base_user_and_group
465 466 467
      gitlab_shell_ssh_user = Gitlab.config.gitlab_shell.ssh_user
      gitlab_shell_owner_group = Gitlab.config.gitlab_shell.owner_group
      print "Repo base owned by #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group}? ... "
Riyad Preukschas committed
468

469
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
Riyad Preukschas committed
470 471
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
472 473 474
        return
      end

475 476 477
      uid = uid_for(gitlab_shell_ssh_user)
      gid = gid_for(gitlab_shell_owner_group)
      if File.stat(repo_base_path).uid == uid && File.stat(repo_base_path).gid == gid
Riyad Preukschas committed
478
        puts "yes".green
479
      else
Riyad Preukschas committed
480
        puts "no".red
481
        puts "  User id for #{gitlab_shell_ssh_user}: #{uid}. Groupd id for #{gitlab_shell_owner_group}: #{gid}".blue
Riyad Preukschas committed
482
        try_fixing_it(
483
          "sudo chown -R #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group} #{repo_base_path}"
Riyad Preukschas committed
484 485
        )
        for_more_information(
486
          see_installation_guide_section "GitLab Shell"
Riyad Preukschas committed
487
        )
Riyad Preukschas committed
488
        fix_and_rerun
Riyad Preukschas committed
489 490 491
      end
    end

492 493
    def check_repos_hooks_directory_is_link
      print "hooks directories in repos are links: ... "
Riyad Preukschas committed
494

495
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
496

Riyad Preukschas committed
497 498 499 500 501
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
502

Riyad Preukschas committed
503
      Project.find_each(batch_size: 100) do |project|
504
        print sanitized_message(project)
505
        project_hook_directory = File.join(project.repository.path_to_repo, "hooks")
506

507 508
        if project.empty_repo?
          puts "repository is empty".magenta
509 510
        elsif File.realpath(project_hook_directory) == File.realpath(gitlab_shell_hooks_path)
          puts 'ok'.green
Riyad Preukschas committed
511
        else
512 513 514 515 516 517 518 519 520 521
          puts "wrong or missing hooks".red
          try_fixing_it(
            sudo_gitlab("#{gitlab_shell_path}/bin/create-hooks"),
            'Check the hooks_path in config/gitlab.yml',
            'Check your gitlab-shell installation'
          )
          for_more_information(
            see_installation_guide_section "GitLab Shell"
          )
          fix_and_rerun
522
        end
523

524
      end
525
    end
Riyad Preukschas committed
526

527
    def check_gitlab_shell_self_test
528
      gitlab_shell_repo_base = gitlab_shell_path
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
      check_cmd = File.expand_path('bin/check', gitlab_shell_repo_base)
      puts "Running #{check_cmd}"
      if system(check_cmd, chdir: gitlab_shell_repo_base)
        puts 'gitlab-shell self-check successful'.green
      else
        puts 'gitlab-shell self-check failed'.red
        try_fixing_it(
          'Make sure GitLab is running;',
          'Check the gitlab-shell configuration file:',
          sudo_gitlab("editor #{File.expand_path('config.yml', gitlab_shell_repo_base)}")
        )
        fix_and_rerun
      end
    end

Hiroyuki Sato committed
544 545 546 547 548 549 550 551 552 553
    def check_projects_have_namespace
      print "projects have namespace: ... "

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""

      Project.find_each(batch_size: 100) do |project|
554
        print sanitized_message(project)
Hiroyuki Sato committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

        if project.namespace
          puts "yes".green
        else
          puts "no".red
          try_fixing_it(
            "Migrate global projects"
          )
          for_more_information(
            "doc/update/5.4-to-6.0.md in section \"#global-projects\""
          )
          fix_and_rerun
        end
      end
    end
Riyad Preukschas committed
570 571 572 573

    # Helper methods
    ########################

574 575
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
Riyad Preukschas committed
576 577
    end

578
    def gitlab_shell_version
579
      Gitlab::Shell.new.version
Riyad Preukschas committed
580 581
    end

582
    def gitlab_shell_major_version
583
      Gitlab::Shell.version_required.split('.')[0].to_i
584 585 586
    end

    def gitlab_shell_minor_version
587
      Gitlab::Shell.version_required.split('.')[1].to_i
588 589 590
    end

    def gitlab_shell_patch_version
591
      Gitlab::Shell.version_required.split('.')[2].to_i
592
    end
593
  end
594

Riyad Preukschas committed
595 596


597
  namespace :sidekiq do
598
    desc "GITLAB | Check the configuration of Sidekiq"
599
    task check: :environment  do
Riyad Preukschas committed
600
      warn_user_is_not_gitlab
601
      start_checking "Sidekiq"
Riyad Preukschas committed
602

603
      check_sidekiq_running
604
      only_one_sidekiq_running
Riyad Preukschas committed
605

606
      finished_checking "Sidekiq"
Riyad Preukschas committed
607 608 609 610 611 612
    end


    # Checks
    ########################

613
    def check_sidekiq_running
Riyad Preukschas committed
614 615
      print "Running? ... "

616
      if sidekiq_process_count > 0
Riyad Preukschas committed
617 618 619 620
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
621
          sudo_gitlab("RAILS_ENV=production bin/background_jobs start")
Riyad Preukschas committed
622 623 624
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
625
          "see log/sidekiq.log for possible errors"
Riyad Preukschas committed
626
        )
Riyad Preukschas committed
627
        fix_and_rerun
Riyad Preukschas committed
628 629
      end
    end
630 631

    def only_one_sidekiq_running
632 633
      process_count = sidekiq_process_count
      return if process_count.zero?
634 635

      print 'Number of Sidekiq processes ... '
636
      if process_count == 1
637 638
        puts '1'.green
      else
639
        puts "#{process_count}".red
640 641
        try_fixing_it(
          'sudo service gitlab stop',
642 643
          "sudo pkill -u #{gitlab_user} -f sidekiq",
          "sleep 10 && sudo pkill -9 -u #{gitlab_user} -f sidekiq",
644 645 646 647 648 649
          'sudo service gitlab start'
        )
        fix_and_rerun
      end
    end

650
    def sidekiq_process_count
651 652
      ps_ux, _ = Gitlab::Popen.popen(%W(ps ux))
      ps_ux.scan(/sidekiq \d+\.\d+\.\d+/).count
653
    end
Riyad Preukschas committed
654 655
  end

656
  namespace :ldap do
657
    task :check, [:limit] => :environment do |t, args|
658 659
      # Only show up to 100 results because LDAP directories can be very big.
      # This setting only affects the `rake gitlab:check` script.
660
      args.with_defaults(limit: 100)
661 662 663
      warn_user_is_not_gitlab
      start_checking "LDAP"

664
      if Gitlab::LDAP::Config.enabled?
665
        print_users(args.limit)
666 667 668
      else
        puts 'LDAP is disabled in config/gitlab.yml'
      end
669 670 671 672

      finished_checking "LDAP"
    end

673
    def print_users(limit)
674
      puts "LDAP users with access to your GitLab server (only showing the first #{limit} results)"
675

676
      servers = Gitlab::LDAP::Config.providers
677

678 679
      servers.each do |server|
        puts "Server: #{server}"
680
        Gitlab::LDAP::Adapter.open(server) do |adapter|
681 682 683 684 685
          users = adapter.users(adapter.config.uid, '*', 100)
          users.each do |user|
            puts "\tDN: #{user.dn}\t #{adapter.config.uid}: #{user.uid}"
          end
        end
686
      end
687 688
    end
  end
Riyad Preukschas committed
689

Vinnie Okada committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
  namespace :repo do
    desc "GITLAB | Check the integrity of the repositories managed by GitLab"
    task check: :environment do
      namespace_dirs = Dir.glob(
        File.join(Gitlab.config.gitlab_shell.repos_path, '*')
      )

      namespace_dirs.each do |namespace_dir|
        repo_dirs = Dir.glob(File.join(namespace_dir, '*'))
        repo_dirs.each do |dir|
          puts "\nChecking repo at #{dir}"
          system(*%w(git fsck), chdir: dir)
        end
      end
    end
  end

Riyad Preukschas committed
707 708 709
  # Helper methods
  ##########################

Riyad Preukschas committed
710
  def fix_and_rerun
Riyad Preukschas committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    puts "  Please #{"fix the error above"} and rerun the checks.".red
  end

  def for_more_information(*sources)
    sources = sources.shift if sources.first.is_a?(Array)

    puts "  For more information see:".blue
    sources.each do |source|
      puts "  #{source}"
    end
  end

  def finished_checking(component)
    puts ""
    puts "Checking #{component.yellow} ... #{"Finished".green}"
    puts ""
  end

  def see_database_guide
    "doc/install/databases.md"
  end

  def see_installation_guide_section(section)
    "doc/install/installation.md in section \"#{section}\""
  end

737 738 739 740
  def sudo_gitlab(command)
    "sudo -u #{gitlab_user} -H #{command}"
  end

741 742 743 744
  def gitlab_user
    Gitlab.config.gitlab.user
  end

Riyad Preukschas committed
745 746 747 748 749 750 751 752 753 754 755 756 757
  def start_checking(component)
    puts "Checking #{component.yellow} ..."
    puts ""
  end

  def try_fixing_it(*steps)
    steps = steps.shift if steps.first.is_a?(Array)

    puts "  Try fixing it:".blue
    steps.each do |step|
      puts "  #{step}"
    end
  end
758 759

  def check_gitlab_shell
760
    required_version = Gitlab::VersionInfo.new(gitlab_shell_major_version, gitlab_shell_minor_version, gitlab_shell_patch_version)
761
    current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
762

763
    print "GitLab Shell version >= #{required_version} ? ... "
764
    if current_version.valid? && required_version <= current_version
765
      puts "OK (#{current_version})".green
766
    else
767
      puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
768 769
    end
  end
770

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
  def check_ruby_version
    required_version = Gitlab::VersionInfo.new(2, 0, 0)
    current_version = Gitlab::VersionInfo.parse(run(%W(ruby --version)))

    print "Ruby version >= #{required_version} ? ... "

    if current_version.valid? && required_version <= current_version
        puts "yes (#{current_version})".green
    else
      puts "no".red
      try_fixing_it(
        "Update your ruby to a version >= #{required_version} from #{current_version}"
      )
      fix_and_rerun
    end
  end

788
  def check_git_version
789
    required_version = Gitlab::VersionInfo.new(1, 7, 10)
790
    current_version = Gitlab::VersionInfo.parse(run(%W(#{Gitlab.config.git.bin_path} --version)))
Sato Hiroyuki committed
791

792
    puts "Your git bin path is \"#{Gitlab.config.git.bin_path}\""
Sato Hiroyuki committed
793 794
    print "Git version >= #{required_version} ? ... "

795
    if current_version.valid? && required_version <= current_version
796
        puts "yes (#{current_version})".green
797 798 799
    else
      puts "no".red
      try_fixing_it(
Sato Hiroyuki committed
800
        "Update your git to a version >= #{required_version} from #{current_version}"
801 802 803 804
      )
      fix_and_rerun
    end
  end
805

806 807 808 809
  def check_active_users
    puts "Active users: #{User.active.count}"
  end

810 811 812
  def omnibus_gitlab?
    Dir.pwd == '/opt/gitlab/embedded/service/gitlab-rails'
  end
813

814
  def sanitized_message(project)
815
    if should_sanitize?
816 817 818 819 820 821
      "#{project.namespace_id.to_s.yellow}/#{project.id.to_s.yellow} ... "
    else
      "#{project.name_with_namespace.yellow} ... "
    end
  end

822
  def should_sanitize?
823 824 825 826 827 828
    if ENV['SANITIZE'] == "true"
      true
    else
      false
    end
  end
829
end
830