check.rake 21.7 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:ldap:check
R
Riyad Preukschas 已提交
6 7 8
                 gitlab:app:check}


R
Riyad Preukschas 已提交
9

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

16
      check_git_config
R
Riyad Preukschas 已提交
17 18 19
      check_database_config_exists
      check_database_is_not_sqlite
      check_migrations_are_up
20
      check_orphaned_group_members
R
Riyad Preukschas 已提交
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
H
Hiroyuki Sato 已提交
27
      check_projects_have_namespace
R
Riyad Preukschas 已提交
28
      check_satellites_exist
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
    def check_satellites_exist
      print "Projects have satellites? ... "

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
246 247
        return
      end
R
Riyad Preukschas 已提交
248 249 250
      puts ""

      Project.find_each(batch_size: 100) do |project|
M
Marin Jankovski 已提交
251
        print sanitized_message(project)
R
Riyad Preukschas 已提交
252 253 254

        if project.satellite.exists?
          puts "yes".green
255 256
        elsif project.empty_repo?
          puts "can't create, repository is empty".magenta
R
Riyad Preukschas 已提交
257 258 259
        else
          puts "no".red
          try_fixing_it(
260
            sudo_gitlab("bundle exec rake gitlab:satellites:create RAILS_ENV=production"),
261 262
            "If necessary, remove the tmp/repo_satellites directory ...",
            "... and rerun the above command"
R
Riyad Preukschas 已提交
263 264 265 266
          )
          for_more_information(
            "doc/raketasks/maintenance.md "
          )
R
Riyad Preukschas 已提交
267
          fix_and_rerun
R
Riyad Preukschas 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        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}",
B
bassrock 已提交
283
          "sudo chmod -R u+rwX #{log_path}"
R
Riyad Preukschas 已提交
284 285 286 287
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
288
        fix_and_rerun
R
Riyad Preukschas 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302
      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 已提交
303
          "sudo chmod -R u+rwX #{tmp_path}"
R
Riyad Preukschas 已提交
304 305 306 307
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
308
        fix_and_rerun
R
Riyad Preukschas 已提交
309
      end
R
Riyad Preukschas 已提交
310
    end
311

312
    def check_redis_version
313 314
      print "Redis version >= 2.0.0? ... "

T
tonic 已提交
315 316
      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/)
317
        puts "yes".green
318
      else
319 320
        puts "no".red
        try_fixing_it(
321
          "Update your redis server to a version >= 2.0.0"
322 323 324 325 326 327
        )
        for_more_information(
          "gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq"
        )
        fix_and_rerun
      end
328
    end
R
Riyad Preukschas 已提交
329 330
  end

331
  namespace :gitlab_shell do
332
    desc "GitLab | Check the configuration of GitLab Shell"
R
Riyad Preukschas 已提交
333
    task check: :environment  do
R
Riyad Preukschas 已提交
334
      warn_user_is_not_gitlab
B
Ben Bodenmiller 已提交
335
      start_checking "GitLab Shell"
R
Riyad Preukschas 已提交
336

337
      check_gitlab_shell
R
Riyad Preukschas 已提交
338
      check_repo_base_exists
339
      check_repo_base_is_not_symlink
R
Riyad Preukschas 已提交
340 341
      check_repo_base_user_and_group
      check_repo_base_permissions
342
      check_satellites_permissions
343
      check_repos_hooks_directory_is_link
344
      check_gitlab_shell_self_test
R
Riyad Preukschas 已提交
345

B
Ben Bodenmiller 已提交
346
      finished_checking "GitLab Shell"
R
Riyad Preukschas 已提交
347 348 349 350 351 352 353 354 355
    end


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

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

356
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
357 358 359 360 361 362 363

      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 已提交
364
          "This should have been created when setting up GitLab Shell.",
R
Riyad Preukschas 已提交
365
          "Make sure it's set correctly in config/gitlab.yml",
B
Ben Bodenmiller 已提交
366
          "Make sure GitLab Shell is installed correctly."
R
Riyad Preukschas 已提交
367 368
        )
        for_more_information(
B
Ben Bodenmiller 已提交
369
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
370
        )
R
Riyad Preukschas 已提交
371
        fix_and_rerun
R
Riyad Preukschas 已提交
372 373 374
      end
    end

375 376 377
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

378
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      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 已提交
395
    def check_repo_base_permissions
396
      print "Repo base access is drwxrws---? ... "
R
Riyad Preukschas 已提交
397

398
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
399 400
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
401 402 403
        return
      end

404
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
R
Riyad Preukschas 已提交
405 406 407 408
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
409
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
410
          "sudo chmod -R ug-s #{repo_base_path}",
411
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
R
Riyad Preukschas 已提交
412 413
        )
        for_more_information(
B
Ben Bodenmiller 已提交
414
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
415
        )
R
Riyad Preukschas 已提交
416
        fix_and_rerun
R
Riyad Preukschas 已提交
417 418 419
      end
    end

420 421 422 423 424 425 426 427 428 429 430 431 432 433
    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(
M
Michael Krane 已提交
434
          "sudo chmod u+rwx,g=rx,o-rwx #{satellites_path}",
435 436 437 438 439 440 441 442
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
        fix_and_rerun
      end
    end

R
Riyad Preukschas 已提交
443
    def check_repo_base_user_and_group
444 445 446
      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 已提交
447

448
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
449 450
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
451 452 453
        return
      end

454 455 456
      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 已提交
457
        puts "yes".green
458
      else
R
Riyad Preukschas 已提交
459
        puts "no".red
460
        puts "  User id for #{gitlab_shell_ssh_user}: #{uid}. Groupd id for #{gitlab_shell_owner_group}: #{gid}".blue
R
Riyad Preukschas 已提交
461
        try_fixing_it(
462
          "sudo chown -R #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group} #{repo_base_path}"
R
Riyad Preukschas 已提交
463 464
        )
        for_more_information(
B
Ben Bodenmiller 已提交
465
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
466
        )
R
Riyad Preukschas 已提交
467
        fix_and_rerun
R
Riyad Preukschas 已提交
468 469 470
      end
    end

471 472
    def check_repos_hooks_directory_is_link
      print "hooks directories in repos are links: ... "
R
Riyad Preukschas 已提交
473

474
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
475

R
Riyad Preukschas 已提交
476 477 478 479 480
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
481

R
Riyad Preukschas 已提交
482
      Project.find_each(batch_size: 100) do |project|
M
Marin Jankovski 已提交
483
        print sanitized_message(project)
484
        project_hook_directory = File.join(project.repository.path_to_repo, "hooks")
485

R
Riyad Preukschas 已提交
486 487
        if project.empty_repo?
          puts "repository is empty".magenta
488 489
        elsif File.realpath(project_hook_directory) == File.realpath(gitlab_shell_hooks_path)
          puts 'ok'.green
R
Riyad Preukschas 已提交
490
        else
491 492 493 494 495 496 497 498 499 500
          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
501
        end
502

503
      end
R
Riyad Preukschas 已提交
504
    end
R
Riyad Preukschas 已提交
505

506
    def check_gitlab_shell_self_test
507
      gitlab_shell_repo_base = gitlab_shell_path
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      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 已提交
523 524 525 526 527 528 529 530 531 532
    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 已提交
533
        print sanitized_message(project)
H
Hiroyuki Sato 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

        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 已提交
549 550 551 552

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

553 554
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
R
Riyad Preukschas 已提交
555 556
    end

557
    def gitlab_shell_version
558
      Gitlab::Shell.new.version
R
Riyad Preukschas 已提交
559 560
    end

561
    def gitlab_shell_major_version
562
      Gitlab::Shell.version_required.split('.')[0].to_i
563 564 565
    end

    def gitlab_shell_minor_version
566
      Gitlab::Shell.version_required.split('.')[1].to_i
567 568 569
    end

    def gitlab_shell_patch_version
570
      Gitlab::Shell.version_required.split('.')[2].to_i
571
    end
R
Riyad Preukschas 已提交
572
  end
573

R
Riyad Preukschas 已提交
574 575


R
Riyad Preukschas 已提交
576
  namespace :sidekiq do
577
    desc "GitLab | Check the configuration of Sidekiq"
R
Riyad Preukschas 已提交
578
    task check: :environment  do
R
Riyad Preukschas 已提交
579
      warn_user_is_not_gitlab
R
Riyad Preukschas 已提交
580
      start_checking "Sidekiq"
R
Riyad Preukschas 已提交
581

R
Riyad Preukschas 已提交
582
      check_sidekiq_running
583
      only_one_sidekiq_running
R
Riyad Preukschas 已提交
584

R
Riyad Preukschas 已提交
585
      finished_checking "Sidekiq"
R
Riyad Preukschas 已提交
586 587 588 589 590 591
    end


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

R
Riyad Preukschas 已提交
592
    def check_sidekiq_running
R
Riyad Preukschas 已提交
593 594
      print "Running? ... "

595
      if sidekiq_process_count > 0
R
Riyad Preukschas 已提交
596 597 598 599
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
600
          sudo_gitlab("RAILS_ENV=production bin/background_jobs start")
R
Riyad Preukschas 已提交
601 602 603
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
D
Dmitriy Zaporozhets 已提交
604
          "see log/sidekiq.log for possible errors"
R
Riyad Preukschas 已提交
605
        )
R
Riyad Preukschas 已提交
606
        fix_and_rerun
R
Riyad Preukschas 已提交
607 608
      end
    end
609 610

    def only_one_sidekiq_running
611 612
      process_count = sidekiq_process_count
      return if process_count.zero?
613 614

      print 'Number of Sidekiq processes ... '
615
      if process_count == 1
616 617
        puts '1'.green
      else
618
        puts "#{process_count}".red
619 620
        try_fixing_it(
          'sudo service gitlab stop',
621 622
          "sudo pkill -u #{gitlab_user} -f sidekiq",
          "sleep 10 && sudo pkill -9 -u #{gitlab_user} -f sidekiq",
623 624 625 626 627 628
          'sudo service gitlab start'
        )
        fix_and_rerun
      end
    end

629
    def sidekiq_process_count
630 631
      ps_ux, _ = Gitlab::Popen.popen(%W(ps ux))
      ps_ux.scan(/sidekiq \d+\.\d+\.\d+/).count
632
    end
R
Riyad Preukschas 已提交
633 634
  end

635
  namespace :ldap do
636
    task :check, [:limit] => :environment do |t, args|
637 638
      # Only show up to 100 results because LDAP directories can be very big.
      # This setting only affects the `rake gitlab:check` script.
639
      args.with_defaults(limit: 100)
640 641 642
      warn_user_is_not_gitlab
      start_checking "LDAP"

D
Dmitriy Zaporozhets 已提交
643
      if Gitlab::LDAP::Config.enabled?
644
        print_users(args.limit)
645 646 647
      else
        puts 'LDAP is disabled in config/gitlab.yml'
      end
648 649 650 651

      finished_checking "LDAP"
    end

652
    def print_users(limit)
653
      puts "LDAP users with access to your GitLab server (only showing the first #{limit} results)"
654

655
      servers = Gitlab::LDAP::Config.providers
656

D
Dmitriy Zaporozhets 已提交
657 658
      servers.each do |server|
        puts "Server: #{server}"
659
        Gitlab::LDAP::Adapter.open(server) do |adapter|
D
Dmitriy Zaporozhets 已提交
660 661 662 663 664
          users = adapter.users(adapter.config.uid, '*', 100)
          users.each do |user|
            puts "\tDN: #{user.dn}\t #{adapter.config.uid}: #{user.uid}"
          end
        end
665
      end
666 667
    end
  end
R
Riyad Preukschas 已提交
668

V
Vinnie Okada 已提交
669
  namespace :repo do
670
    desc "GitLab | Check the integrity of the repositories managed by GitLab"
V
Vinnie Okada 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    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 已提交
686 687 688
  # Helper methods
  ##########################

R
Riyad Preukschas 已提交
689
  def fix_and_rerun
R
Riyad Preukschas 已提交
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
    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

716 717 718 719
  def sudo_gitlab(command)
    "sudo -u #{gitlab_user} -H #{command}"
  end

720 721 722 723
  def gitlab_user
    Gitlab.config.gitlab.user
  end

R
Riyad Preukschas 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736
  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
737 738

  def check_gitlab_shell
739
    required_version = Gitlab::VersionInfo.new(gitlab_shell_major_version, gitlab_shell_minor_version, gitlab_shell_patch_version)
740
    current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
741

742
    print "GitLab Shell version >= #{required_version} ? ... "
S
Sato Hiroyuki 已提交
743
    if current_version.valid? && required_version <= current_version
744
      puts "OK (#{current_version})".green
745
    else
746
      puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
747 748
    end
  end
749

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
  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

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

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

S
Sato Hiroyuki 已提交
774
    if current_version.valid? && required_version <= current_version
775
        puts "yes (#{current_version})".green
776 777 778
    else
      puts "no".red
      try_fixing_it(
S
Sato Hiroyuki 已提交
779
        "Update your git to a version >= #{required_version} from #{current_version}"
780 781 782 783
      )
      fix_and_rerun
    end
  end
784

785 786 787 788
  def check_active_users
    puts "Active users: #{User.active.count}"
  end

789 790 791
  def omnibus_gitlab?
    Dir.pwd == '/opt/gitlab/embedded/service/gitlab-rails'
  end
792

M
Marin Jankovski 已提交
793
  def sanitized_message(project)
794
    if should_sanitize?
M
Marin Jankovski 已提交
795 796 797 798 799 800
      "#{project.namespace_id.to_s.yellow}/#{project.id.to_s.yellow} ... "
    else
      "#{project.name_with_namespace.yellow} ... "
    end
  end

801
  def should_sanitize?
802 803 804 805 806 807
    if ENV['SANITIZE'] == "true"
      true
    else
      false
    end
  end
808
end
809