check.rake 24.0 KB
Newer Older
1
namespace :gitlab do
2
  desc "GitLab | Check the configuration of GitLab and its environment"
M
Marin Jankovski 已提交
3
  task check: %w{gitlab:gitlab_shell:check
R
Riyad Preukschas 已提交
4
                 gitlab:sidekiq:check
5
                 gitlab:reply_by_email:check
6
                 gitlab:ldap:check
R
Riyad Preukschas 已提交
7 8 9
                 gitlab:app:check}


R
Riyad Preukschas 已提交
10

11
  namespace :app do
12
    desc "GitLab | Check the configuration of the GitLab Rails app"
R
Riyad Preukschas 已提交
13
    task check: :environment  do
R
Riyad Preukschas 已提交
14 15 16
      warn_user_is_not_gitlab
      start_checking "GitLab"

17
      check_git_config
R
Riyad Preukschas 已提交
18 19 20
      check_database_config_exists
      check_database_is_not_sqlite
      check_migrations_are_up
21
      check_orphaned_group_members
R
Riyad Preukschas 已提交
22 23 24 25 26 27
      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
H
Hiroyuki Sato 已提交
28
      check_projects_have_namespace
29
      check_redis_version
30
      check_ruby_version
31
      check_git_version
32
      check_active_users
R
Riyad Preukschas 已提交
33 34 35 36 37 38 39 40

      finished_checking "GitLab"
    end


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

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    def check_git_config
      print "Git configured with autocrlf=input? ... "

      options = {
        "core.autocrlf" => "input"
      }

      correct_options = options.map do |name, value|
        run(%W(#{Gitlab.config.git.bin_path} config --global --get #{name})).try(:squish) == value
      end

      if correct_options.all?
        puts "yes".green
      else
        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 core.autocrlf \"#{options["core.autocrlf"]}\"")
          )
          for_more_information(
            see_installation_guide_section "GitLab"
          )
       end
      end
    end

R
Riyad Preukschas 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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"
        )
R
Riyad Preukschas 已提交
88
        fix_and_rerun
R
Riyad Preukschas 已提交
89 90 91 92
      end
    end

    def check_database_is_not_sqlite
R
Riyad Preukschas 已提交
93
      print "Database is SQLite ... "
R
Riyad Preukschas 已提交
94 95 96

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

97
      unless File.read(database_config_file) =~ /adapter:\s+sqlite/
R
Riyad Preukschas 已提交
98
        puts "no".green
R
Riyad Preukschas 已提交
99
      else
R
Riyad Preukschas 已提交
100
        puts "yes".red
101
        puts "Please fix this by removing the SQLite entry from the database.yml".blue
R
Riyad Preukschas 已提交
102 103 104 105
        for_more_information(
          "https://github.com/gitlabhq/gitlabhq/wiki/Migrate-from-SQLite-to-MySQL",
          see_database_guide
        )
R
Riyad Preukschas 已提交
106
        fix_and_rerun
R
Riyad Preukschas 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      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"
        )
R
Riyad Preukschas 已提交
126
        fix_and_rerun
R
Riyad Preukschas 已提交
127 128 129 130
      end
    end

    def check_gitlab_config_not_outdated
R
Riyad Preukschas 已提交
131
      print "GitLab config outdated? ... "
R
Riyad Preukschas 已提交
132 133 134 135 136 137 138

      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
139
      unless Gitlab.config['git_host']
R
Riyad Preukschas 已提交
140
        puts "no".green
R
Riyad Preukschas 已提交
141
      else
R
Riyad Preukschas 已提交
142
        puts "yes".red
R
Riyad Preukschas 已提交
143
        try_fixing_it(
R
Riyad Preukschas 已提交
144
          "Backup your config/gitlab.yml",
R
Riyad Preukschas 已提交
145 146 147 148 149 150
          "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"
        )
R
Riyad Preukschas 已提交
151
        fix_and_rerun
R
Riyad Preukschas 已提交
152 153
      end
    end
154

R
Riyad Preukschas 已提交
155 156 157
    def check_init_script_exists
      print "Init script exists? ... "

158 159 160 161 162
      if omnibus_gitlab?
        puts 'skipped (omnibus-gitlab has no init script)'.magenta
        return
      end

R
Riyad Preukschas 已提交
163 164 165 166
      script_path = "/etc/init.d/gitlab"

      if File.exists?(script_path)
        puts "yes".green
N
Nihad Abbasov 已提交
167
      else
R
Riyad Preukschas 已提交
168 169 170 171 172 173 174
        puts "no".red
        try_fixing_it(
          "Install the init script"
        )
        for_more_information(
          see_installation_guide_section "Install Init Script"
        )
R
Riyad Preukschas 已提交
175
        fix_and_rerun
R
Riyad Preukschas 已提交
176 177 178 179 180 181
      end
    end

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

182 183 184 185 186
      if omnibus_gitlab?
        puts 'skipped (omnibus-gitlab has no init script)'.magenta
        return
      end

187
      recipe_path = Rails.root.join("lib/support/init.d/", "gitlab")
R
Riyad Preukschas 已提交
188
      script_path = "/etc/init.d/gitlab"
189

R
Riyad Preukschas 已提交
190 191
      unless File.exists?(script_path)
        puts "can't check because of previous errors".magenta
192 193 194
        return
      end

195
      recipe_content = File.read(recipe_path)
R
Riyad Preukschas 已提交
196 197 198 199 200 201 202 203 204 205 206 207
      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"
        )
R
Riyad Preukschas 已提交
208
        fix_and_rerun
R
Riyad Preukschas 已提交
209 210 211 212 213 214
      end
    end

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

215
      migration_status, _ = Gitlab::Popen.popen(%W(bundle exec rake db:migrate:status))
R
Riyad Preukschas 已提交
216 217 218

      unless migration_status =~ /down\s+\d{14}/
        puts "yes".green
219
      else
R
Riyad Preukschas 已提交
220 221
        puts "no".red
        try_fixing_it(
222
          sudo_gitlab("bundle exec rake db:migrate RAILS_ENV=production")
R
Riyad Preukschas 已提交
223
        )
R
Riyad Preukschas 已提交
224
        fix_and_rerun
R
Riyad Preukschas 已提交
225 226 227
      end
    end

228
    def check_orphaned_group_members
229 230
      print "Database contains orphaned GroupMembers? ... "
      if GroupMember.where("user_id not in (select id from users)").count > 0
231
        puts "yes".red
232 233
        try_fixing_it(
          "You can delete the orphaned records using something along the lines of:",
234
          sudo_gitlab("bundle exec rails runner -e production 'GroupMember.where(\"user_id NOT IN (SELECT id FROM users)\").delete_all'")
235
        )
236 237 238 239 240
      else
        puts "no".green
      end
    end

R
Riyad Preukschas 已提交
241 242 243 244 245 246 247 248 249 250 251
    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}",
B
bassrock 已提交
252
          "sudo chmod -R u+rwX #{log_path}"
R
Riyad Preukschas 已提交
253 254 255 256
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
257
        fix_and_rerun
R
Riyad Preukschas 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271
      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}",
B
bassrock 已提交
272
          "sudo chmod -R u+rwX #{tmp_path}"
R
Riyad Preukschas 已提交
273 274 275 276
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
277
        fix_and_rerun
R
Riyad Preukschas 已提交
278
      end
R
Riyad Preukschas 已提交
279
    end
280

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

T
tonic 已提交
284 285
      redis_version = run(%W(redis-cli --version))
      if redis_version.try(:match, /redis-cli 2.\d.\d/) || redis_version.try(:match, /redis-cli 3.\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
R
Riyad Preukschas 已提交
298 299
  end

300
  namespace :gitlab_shell do
301
    desc "GitLab | Check the configuration of GitLab Shell"
R
Riyad Preukschas 已提交
302
    task check: :environment  do
R
Riyad Preukschas 已提交
303
      warn_user_is_not_gitlab
B
Ben Bodenmiller 已提交
304
      start_checking "GitLab Shell"
R
Riyad Preukschas 已提交
305

306
      check_gitlab_shell
R
Riyad Preukschas 已提交
307
      check_repo_base_exists
308
      check_repo_base_is_not_symlink
R
Riyad Preukschas 已提交
309 310
      check_repo_base_user_and_group
      check_repo_base_permissions
311
      check_repos_hooks_directory_is_link
312
      check_gitlab_shell_self_test
R
Riyad Preukschas 已提交
313

B
Ben Bodenmiller 已提交
314
      finished_checking "GitLab Shell"
R
Riyad Preukschas 已提交
315 316 317 318 319 320 321 322 323
    end


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

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

324
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
325 326 327 328 329 330 331

      if File.exists?(repo_base_path)
        puts "yes".green
      else
        puts "no".red
        puts "#{repo_base_path} is missing".red
        try_fixing_it(
B
Ben Bodenmiller 已提交
332
          "This should have been created when setting up GitLab Shell.",
R
Riyad Preukschas 已提交
333
          "Make sure it's set correctly in config/gitlab.yml",
B
Ben Bodenmiller 已提交
334
          "Make sure GitLab Shell is installed correctly."
R
Riyad Preukschas 已提交
335 336
        )
        for_more_information(
B
Ben Bodenmiller 已提交
337
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
338
        )
R
Riyad Preukschas 已提交
339
        fix_and_rerun
R
Riyad Preukschas 已提交
340 341 342
      end
    end

343 344 345
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

346
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
      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

R
Riyad Preukschas 已提交
363
    def check_repo_base_permissions
364
      print "Repo base access is drwxrws---? ... "
R
Riyad Preukschas 已提交
365

366
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
367 368
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
369 370 371
        return
      end

372
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
R
Riyad Preukschas 已提交
373 374 375 376
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
377
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
378
          "sudo chmod -R ug-s #{repo_base_path}",
379
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
R
Riyad Preukschas 已提交
380 381
        )
        for_more_information(
B
Ben Bodenmiller 已提交
382
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
383
        )
R
Riyad Preukschas 已提交
384
        fix_and_rerun
R
Riyad Preukschas 已提交
385 386 387 388
      end
    end

    def check_repo_base_user_and_group
389 390 391
      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}? ... "
R
Riyad Preukschas 已提交
392

393
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
394 395
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
396 397 398
        return
      end

399 400 401
      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
R
Riyad Preukschas 已提交
402
        puts "yes".green
403
      else
R
Riyad Preukschas 已提交
404
        puts "no".red
405
        puts "  User id for #{gitlab_shell_ssh_user}: #{uid}. Groupd id for #{gitlab_shell_owner_group}: #{gid}".blue
R
Riyad Preukschas 已提交
406
        try_fixing_it(
407
          "sudo chown -R #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group} #{repo_base_path}"
R
Riyad Preukschas 已提交
408 409
        )
        for_more_information(
B
Ben Bodenmiller 已提交
410
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
411
        )
R
Riyad Preukschas 已提交
412
        fix_and_rerun
R
Riyad Preukschas 已提交
413 414 415
      end
    end

416 417
    def check_repos_hooks_directory_is_link
      print "hooks directories in repos are links: ... "
R
Riyad Preukschas 已提交
418

419
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
420

R
Riyad Preukschas 已提交
421 422 423 424 425
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
426

R
Riyad Preukschas 已提交
427
      Project.find_each(batch_size: 100) do |project|
M
Marin Jankovski 已提交
428
        print sanitized_message(project)
429
        project_hook_directory = File.join(project.repository.path_to_repo, "hooks")
430

R
Riyad Preukschas 已提交
431 432
        if project.empty_repo?
          puts "repository is empty".magenta
433 434
        elsif File.directory?(project_hook_directory) && File.directory?(gitlab_shell_hooks_path) &&
            (File.realpath(project_hook_directory) == File.realpath(gitlab_shell_hooks_path))
435
          puts 'ok'.green
R
Riyad Preukschas 已提交
436
        else
437 438 439 440 441 442 443 444 445 446
          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
447
        end
448

449
      end
R
Riyad Preukschas 已提交
450
    end
R
Riyad Preukschas 已提交
451

452
    def check_gitlab_shell_self_test
453
      gitlab_shell_repo_base = gitlab_shell_path
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
      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

H
Hiroyuki Sato 已提交
469 470 471 472 473 474 475 476 477 478
    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|
M
Marin Jankovski 已提交
479
        print sanitized_message(project)
H
Hiroyuki Sato 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

        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
R
Riyad Preukschas 已提交
495 496 497 498

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

499 500
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
R
Riyad Preukschas 已提交
501 502
    end

503
    def gitlab_shell_version
504
      Gitlab::Shell.new.version
R
Riyad Preukschas 已提交
505 506
    end

507
    def gitlab_shell_major_version
508
      Gitlab::Shell.version_required.split('.')[0].to_i
509 510 511
    end

    def gitlab_shell_minor_version
512
      Gitlab::Shell.version_required.split('.')[1].to_i
513 514 515
    end

    def gitlab_shell_patch_version
516
      Gitlab::Shell.version_required.split('.')[2].to_i
517
    end
R
Riyad Preukschas 已提交
518
  end
519

R
Riyad Preukschas 已提交
520 521


R
Riyad Preukschas 已提交
522
  namespace :sidekiq do
523
    desc "GitLab | Check the configuration of Sidekiq"
R
Riyad Preukschas 已提交
524
    task check: :environment  do
R
Riyad Preukschas 已提交
525
      warn_user_is_not_gitlab
R
Riyad Preukschas 已提交
526
      start_checking "Sidekiq"
R
Riyad Preukschas 已提交
527

R
Riyad Preukschas 已提交
528
      check_sidekiq_running
529
      only_one_sidekiq_running
R
Riyad Preukschas 已提交
530

R
Riyad Preukschas 已提交
531
      finished_checking "Sidekiq"
R
Riyad Preukschas 已提交
532 533 534 535 536 537
    end


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

R
Riyad Preukschas 已提交
538
    def check_sidekiq_running
R
Riyad Preukschas 已提交
539 540
      print "Running? ... "

541
      if sidekiq_process_count > 0
R
Riyad Preukschas 已提交
542 543 544 545
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
546
          sudo_gitlab("RAILS_ENV=production bin/background_jobs start")
R
Riyad Preukschas 已提交
547 548 549
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
D
Dmitriy Zaporozhets 已提交
550
          "see log/sidekiq.log for possible errors"
R
Riyad Preukschas 已提交
551
        )
R
Riyad Preukschas 已提交
552
        fix_and_rerun
R
Riyad Preukschas 已提交
553 554
      end
    end
555 556

    def only_one_sidekiq_running
557 558
      process_count = sidekiq_process_count
      return if process_count.zero?
559 560

      print 'Number of Sidekiq processes ... '
561
      if process_count == 1
562 563
        puts '1'.green
      else
564
        puts "#{process_count}".red
565 566
        try_fixing_it(
          'sudo service gitlab stop',
567 568
          "sudo pkill -u #{gitlab_user} -f sidekiq",
          "sleep 10 && sudo pkill -9 -u #{gitlab_user} -f sidekiq",
569 570 571 572 573 574
          'sudo service gitlab start'
        )
        fix_and_rerun
      end
    end

575
    def sidekiq_process_count
576 577
      ps_ux, _ = Gitlab::Popen.popen(%W(ps ux))
      ps_ux.scan(/sidekiq \d+\.\d+\.\d+/).count
578
    end
R
Riyad Preukschas 已提交
579 580
  end

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

  namespace :reply_by_email do
    desc "GitLab | Check the configuration of Reply by email"
    task check: :environment  do
      warn_user_is_not_gitlab
      start_checking "Reply by email"

      if Gitlab.config.reply_by_email.enabled
        check_address_formatted_correctly
        check_mail_room_config_exists
        check_imap_authentication
        check_initd_configured_correctly
        check_mail_room_running
      else
        puts 'Reply by email is disabled in config/gitlab.yml'
      end

      finished_checking "Reply by email"
    end


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

    def check_address_formatted_correctly
      print "Address formatted correctly? ... "

      if Gitlab::ReplyByEmail.address_formatted_correctly?
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Make sure that the address in config/gitlab.yml includes the '%{reply_key}' placeholder."
        )
        fix_and_rerun
      end
    end

    def check_initd_configured_correctly
      print "Init.d configured correctly? ... "

      path = "/etc/default/gitlab"

      if File.exist?(path) && File.read(path).include?("mail_room_enabled=true")
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Enable mail_room in the init.d configuration."
        )
        for_more_information(
          "doc/reply_by_email/README.md"
        )
        fix_and_rerun
      end
    end

    def check_mail_room_running
      print "MailRoom running? ... "

      path = "/etc/default/gitlab"

      unless File.exist?(path) && File.read(path).include?("mail_room_enabled=true")
        puts "can't check because of previous errors".magenta
        return
      end

D
Douwe Maan 已提交
648
      if mail_room_running?
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          sudo_gitlab("RAILS_ENV=production bin/mail_room start")
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
          "see log/mail_room.log for possible errors"
        )
        fix_and_rerun
      end
    end

    def check_mail_room_config_exists
      print "MailRoom config exists? ... "

      mail_room_config_file = Rails.root.join("config", "mail_room.yml")

      if File.exists?(mail_room_config_file)
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Copy config/mail_room.yml.example to config/mail_room.yml",
          "Check that the information in config/mail_room.yml is correct"
        )
        for_more_information(
          "doc/reply_by_email/README.md"
        )
        fix_and_rerun
      end
    end

    def check_imap_authentication
      print "IMAP server credentials are correct? ... "

      mail_room_config_file = Rails.root.join("config", "mail_room.yml")

      unless File.exists?(mail_room_config_file)
        puts "can't check because of previous errors".magenta
        return
      end

      config = YAML.load_file(mail_room_config_file)[:mailboxes].first rescue nil

      if config
        begin
          imap = Net::IMAP.new(config[:host], port: config[:port], ssl: config[:ssl])
          imap.login(config[:email], config[:password])
          connected = true
        rescue
          connected = false
        end
      end

      if connected
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Check that the information in config/mail_room.yml is correct"
        )
        for_more_information(
          "doc/reply_by_email/README.md"
        )
        fix_and_rerun
      end
    end

D
Douwe Maan 已提交
719
    def mail_room_running?
720
      ps_ux, _ = Gitlab::Popen.popen(%W(ps ux))
D
Douwe Maan 已提交
721
      ps_ux.include?("mail_room")
722 723 724
    end
  end

725
  namespace :ldap do
726
    task :check, [:limit] => :environment do |t, args|
727 728
      # Only show up to 100 results because LDAP directories can be very big.
      # This setting only affects the `rake gitlab:check` script.
729
      args.with_defaults(limit: 100)
730 731 732
      warn_user_is_not_gitlab
      start_checking "LDAP"

D
Dmitriy Zaporozhets 已提交
733
      if Gitlab::LDAP::Config.enabled?
734
        print_users(args.limit)
735 736 737
      else
        puts 'LDAP is disabled in config/gitlab.yml'
      end
738 739 740 741

      finished_checking "LDAP"
    end

742
    def print_users(limit)
743
      puts "LDAP users with access to your GitLab server (only showing the first #{limit} results)"
744

745
      servers = Gitlab::LDAP::Config.providers
746

D
Dmitriy Zaporozhets 已提交
747 748
      servers.each do |server|
        puts "Server: #{server}"
749
        Gitlab::LDAP::Adapter.open(server) do |adapter|
D
Dmitriy Zaporozhets 已提交
750 751 752 753 754
          users = adapter.users(adapter.config.uid, '*', 100)
          users.each do |user|
            puts "\tDN: #{user.dn}\t #{adapter.config.uid}: #{user.uid}"
          end
        end
755
      end
756 757
    end
  end
R
Riyad Preukschas 已提交
758

V
Vinnie Okada 已提交
759
  namespace :repo do
760
    desc "GitLab | Check the integrity of the repositories managed by GitLab"
V
Vinnie Okada 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
    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

R
Riyad Preukschas 已提交
776 777 778
  # Helper methods
  ##########################

R
Riyad Preukschas 已提交
779
  def fix_and_rerun
R
Riyad Preukschas 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    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

806 807 808 809
  def sudo_gitlab(command)
    "sudo -u #{gitlab_user} -H #{command}"
  end

810 811 812 813
  def gitlab_user
    Gitlab.config.gitlab.user
  end

R
Riyad Preukschas 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826
  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
827 828

  def check_gitlab_shell
829
    required_version = Gitlab::VersionInfo.new(gitlab_shell_major_version, gitlab_shell_minor_version, gitlab_shell_patch_version)
830
    current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
831

832
    print "GitLab Shell version >= #{required_version} ? ... "
S
Sato Hiroyuki 已提交
833
    if current_version.valid? && required_version <= current_version
834
      puts "OK (#{current_version})".green
835
    else
836
      puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
837 838
    end
  end
839

840
  def check_ruby_version
841
    required_version = Gitlab::VersionInfo.new(2, 1, 0)
842 843 844 845 846
    current_version = Gitlab::VersionInfo.parse(run(%W(ruby --version)))

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

    if current_version.valid? && required_version <= current_version
847
      puts "yes (#{current_version})".green
848 849 850 851 852 853 854 855 856
    else
      puts "no".red
      try_fixing_it(
        "Update your ruby to a version >= #{required_version} from #{current_version}"
      )
      fix_and_rerun
    end
  end

857
  def check_git_version
858
    required_version = Gitlab::VersionInfo.new(1, 7, 10)
859
    current_version = Gitlab::VersionInfo.parse(run(%W(#{Gitlab.config.git.bin_path} --version)))
S
Sato Hiroyuki 已提交
860

861
    puts "Your git bin path is \"#{Gitlab.config.git.bin_path}\""
S
Sato Hiroyuki 已提交
862 863
    print "Git version >= #{required_version} ? ... "

S
Sato Hiroyuki 已提交
864
    if current_version.valid? && required_version <= current_version
865
      puts "yes (#{current_version})".green
866 867 868
    else
      puts "no".red
      try_fixing_it(
S
Sato Hiroyuki 已提交
869
        "Update your git to a version >= #{required_version} from #{current_version}"
870 871 872 873
      )
      fix_and_rerun
    end
  end
874

875 876 877 878
  def check_active_users
    puts "Active users: #{User.active.count}"
  end

879 880 881
  def omnibus_gitlab?
    Dir.pwd == '/opt/gitlab/embedded/service/gitlab-rails'
  end
882

M
Marin Jankovski 已提交
883
  def sanitized_message(project)
884
    if should_sanitize?
M
Marin Jankovski 已提交
885 886 887 888 889 890
      "#{project.namespace_id.to_s.yellow}/#{project.id.to_s.yellow} ... "
    else
      "#{project.name_with_namespace.yellow} ... "
    end
  end

891
  def should_sanitize?
892 893 894 895 896 897
    if ENV['SANITIZE'] == "true"
      true
    else
      false
    end
  end
898
end