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


R
Riyad Preukschas 已提交
10

11
  namespace :app do
R
Riyad Preukschas 已提交
12 13
    desc "GITLAB | Check the configuration of the GitLab Rails app"
    task check: :environment  do
R
Riyad Preukschas 已提交
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_users_groups
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_git_version
R
Riyad Preukschas 已提交
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

      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"
        )
R
Riyad Preukschas 已提交
56
        fix_and_rerun
R
Riyad Preukschas 已提交
57 58 59 60
      end
    end

    def check_database_is_not_sqlite
R
Riyad Preukschas 已提交
61
      print "Database is SQLite ... "
R
Riyad Preukschas 已提交
62 63 64

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

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

    def check_gitlab_config_not_outdated
R
Riyad Preukschas 已提交
99
      print "GitLab config outdated? ... "
R
Riyad Preukschas 已提交
100 101 102 103 104 105 106

      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
107
      unless Gitlab.config['git_host']
R
Riyad Preukschas 已提交
108
        puts "no".green
R
Riyad Preukschas 已提交
109
      else
R
Riyad Preukschas 已提交
110
        puts "yes".red
R
Riyad Preukschas 已提交
111
        try_fixing_it(
R
Riyad Preukschas 已提交
112
          "Backup your config/gitlab.yml",
R
Riyad Preukschas 已提交
113 114 115 116 117 118
          "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 已提交
119
        fix_and_rerun
R
Riyad Preukschas 已提交
120 121
      end
    end
122

R
Riyad Preukschas 已提交
123 124 125 126 127 128 129
    def check_init_script_exists
      print "Init script exists? ... "

      script_path = "/etc/init.d/gitlab"

      if File.exists?(script_path)
        puts "yes".green
N
Nihad Abbasov 已提交
130
      else
R
Riyad Preukschas 已提交
131 132 133 134 135 136 137
        puts "no".red
        try_fixing_it(
          "Install the init script"
        )
        for_more_information(
          see_installation_guide_section "Install Init Script"
        )
R
Riyad Preukschas 已提交
138
        fix_and_rerun
R
Riyad Preukschas 已提交
139 140 141 142 143 144
      end
    end

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

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

R
Riyad Preukschas 已提交
148 149
      unless File.exists?(script_path)
        puts "can't check because of previous errors".magenta
150 151 152
        return
      end

153
      recipe_content = File.read(recipe_path)
R
Riyad Preukschas 已提交
154 155 156 157 158 159 160 161 162 163 164 165
      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 已提交
166
        fix_and_rerun
R
Riyad Preukschas 已提交
167 168 169 170 171 172
      end
    end

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

173
      migration_status, _ = Gitlab::Popen.popen(%W(bundle exec rake db:migrate:status))
R
Riyad Preukschas 已提交
174 175 176

      unless migration_status =~ /down\s+\d{14}/
        puts "yes".green
177
      else
R
Riyad Preukschas 已提交
178 179
        puts "no".red
        try_fixing_it(
180
          sudo_gitlab("bundle exec rake db:migrate RAILS_ENV=production")
R
Riyad Preukschas 已提交
181
        )
R
Riyad Preukschas 已提交
182
        fix_and_rerun
R
Riyad Preukschas 已提交
183 184 185
      end
    end

186 187 188 189
    def check_orphaned_users_groups
      print "Database contains orphaned UsersGroups? ... "
      if UsersGroup.where("user_id not in (select id from users)").count > 0
        puts "yes".red
190 191 192 193
        try_fixing_it(
          "You can delete the orphaned records using something along the lines of:",
          sudo_gitlab("bundle exec rails runner -e production 'UsersGroup.where(\"user_id NOT IN (SELECT id FROM users)\").delete_all'")
        )
194 195 196 197 198
      else
        puts "no".green
      end
    end

R
Riyad Preukschas 已提交
199 200 201 202 203
    def check_satellites_exist
      print "Projects have satellites? ... "

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
204 205
        return
      end
R
Riyad Preukschas 已提交
206 207 208
      puts ""

      Project.find_each(batch_size: 100) do |project|
209
        print "#{project.name_with_namespace.yellow} ... "
R
Riyad Preukschas 已提交
210 211 212

        if project.satellite.exists?
          puts "yes".green
213 214
        elsif project.empty_repo?
          puts "can't create, repository is empty".magenta
R
Riyad Preukschas 已提交
215 216 217
        else
          puts "no".red
          try_fixing_it(
218
            sudo_gitlab("bundle exec rake gitlab:satellites:create RAILS_ENV=production"),
219 220
            "If necessary, remove the tmp/repo_satellites directory ...",
            "... and rerun the above command"
R
Riyad Preukschas 已提交
221 222 223 224
          )
          for_more_information(
            "doc/raketasks/maintenance.md "
          )
R
Riyad Preukschas 已提交
225
          fix_and_rerun
R
Riyad Preukschas 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        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 已提交
241
          "sudo chmod -R u+rwX #{log_path}"
R
Riyad Preukschas 已提交
242 243 244 245
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
246
        fix_and_rerun
R
Riyad Preukschas 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260
      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 已提交
261
          "sudo chmod -R u+rwX #{tmp_path}"
R
Riyad Preukschas 已提交
262 263 264 265
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
266
        fix_and_rerun
R
Riyad Preukschas 已提交
267
      end
R
Riyad Preukschas 已提交
268
    end
269

270
    def check_redis_version
271 272
      print "Redis version >= 2.0.0? ... "

273
      if run_and_match(%W(redis-cli --version), /redis-cli 2.\d.\d/)
274
        puts "yes".green
275
      else
276 277
        puts "no".red
        try_fixing_it(
278
          "Update your redis server to a version >= 2.0.0"
279 280 281 282 283 284
        )
        for_more_information(
          "gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq"
        )
        fix_and_rerun
      end
285
    end
R
Riyad Preukschas 已提交
286 287
  end

R
Riyad Preukschas 已提交
288 289


R
Riyad Preukschas 已提交
290 291 292
  namespace :env do
    desc "GITLAB | Check the configuration of the environment"
    task check: :environment  do
R
Riyad Preukschas 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305
      warn_user_is_not_gitlab
      start_checking "Environment"

      check_gitlab_git_config

      finished_checking "Environment"
    end


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

    def check_gitlab_git_config
306
      print "Git configured for #{gitlab_user} user? ... "
R
Riyad Preukschas 已提交
307 308 309

      options = {
        "user.name"  => "GitLab",
310
        "user.email" => Gitlab.config.gitlab.email_from
R
Riyad Preukschas 已提交
311 312
      }
      correct_options = options.map do |name, value|
313
        run(%W(git config --global --get #{name})).try(:squish) == value
R
Riyad Preukschas 已提交
314 315 316 317 318 319 320
      end

      if correct_options.all?
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
321 322
          sudo_gitlab("git config --global user.name  \"#{options["user.name"]}\""),
          sudo_gitlab("git config --global user.email \"#{options["user.email"]}\"")
R
Riyad Preukschas 已提交
323 324 325 326
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
327
        fix_and_rerun
R
Riyad Preukschas 已提交
328 329
      end
    end
R
Riyad Preukschas 已提交
330 331
  end

R
Riyad Preukschas 已提交
332 333


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

340
      check_gitlab_shell
R
Riyad Preukschas 已提交
341
      check_repo_base_exists
342
      check_repo_base_is_not_symlink
R
Riyad Preukschas 已提交
343 344
      check_repo_base_user_and_group
      check_repo_base_permissions
345 346
      check_update_hook_is_up_to_date
      check_repos_update_hooks_is_link
347
      check_gitlab_shell_self_test
R
Riyad Preukschas 已提交
348

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


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


357 358
    def check_update_hook_is_up_to_date
      print "update hook up-to-date? ... "
R
Riyad Preukschas 已提交
359

360
      hook_file = "update"
361
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
362
      gitlab_shell_hook_file  = File.join(gitlab_shell_hooks_path, hook_file)
R
Riyad Preukschas 已提交
363

364 365 366 367 368 369 370 371 372 373 374 375
      if File.exists?(gitlab_shell_hook_file)
        puts "yes".green
      else
        puts "no".red
        puts "Could not find #{gitlab_shell_hook_file}"
        try_fixing_it(
          'Check the hooks_path in config/gitlab.yml',
          'Check your gitlab-shell installation'
        )
        for_more_information(
          see_installation_guide_section "GitLab Shell"
        )
376
      end
R
Riyad Preukschas 已提交
377 378 379 380 381
    end

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

382
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
383 384 385 386 387 388 389

      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 已提交
390
          "This should have been created when setting up GitLab Shell.",
R
Riyad Preukschas 已提交
391
          "Make sure it's set correctly in config/gitlab.yml",
B
Ben Bodenmiller 已提交
392
          "Make sure GitLab Shell is installed correctly."
R
Riyad Preukschas 已提交
393 394
        )
        for_more_information(
B
Ben Bodenmiller 已提交
395
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
396
        )
R
Riyad Preukschas 已提交
397
        fix_and_rerun
R
Riyad Preukschas 已提交
398 399 400
      end
    end

401 402 403
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

404
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
      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 已提交
421
    def check_repo_base_permissions
422
      print "Repo base access is drwxrws---? ... "
R
Riyad Preukschas 已提交
423

424
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
425 426
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
427 428 429
        return
      end

430
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
R
Riyad Preukschas 已提交
431 432 433 434
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
435
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
436
          "sudo chmod -R ug-s #{repo_base_path}",
437
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
R
Riyad Preukschas 已提交
438 439
        )
        for_more_information(
B
Ben Bodenmiller 已提交
440
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
441
        )
R
Riyad Preukschas 已提交
442
        fix_and_rerun
R
Riyad Preukschas 已提交
443 444 445 446
      end
    end

    def check_repo_base_user_and_group
447 448 449
      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 已提交
450

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

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

474 475
    def check_repos_update_hooks_is_link
      print "update hooks in repos are links: ... "
R
Riyad Preukschas 已提交
476

477
      hook_file = "update"
478
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
479 480
      gitlab_shell_hook_file  = File.join(gitlab_shell_hooks_path, hook_file)
      gitlab_shell_ssh_user = Gitlab.config.gitlab_shell.ssh_user
R
Riyad Preukschas 已提交
481

482
      unless File.exists?(gitlab_shell_hook_file)
R
Riyad Preukschas 已提交
483 484 485
        puts "can't check because of previous errors".magenta
        return
      end
486

R
Riyad Preukschas 已提交
487 488 489 490 491
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
492

R
Riyad Preukschas 已提交
493
      Project.find_each(batch_size: 100) do |project|
494
        print "#{project.name_with_namespace.yellow} ... "
495

R
Riyad Preukschas 已提交
496 497
        if project.empty_repo?
          puts "repository is empty".magenta
R
Riyad Preukschas 已提交
498
        else
R
Riyad Preukschas 已提交
499 500 501 502 503
          project_hook_file = File.join(project.repository.path_to_repo, "hooks", hook_file)

          unless File.exists?(project_hook_file)
            puts "missing".red
            try_fixing_it(
504
              "sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
R
Riyad Preukschas 已提交
505 506
            )
            for_more_information(
507
              "#{gitlab_shell_path}/support/rewrite-hooks.sh"
R
Riyad Preukschas 已提交
508 509 510 511 512 513
            )
            fix_and_rerun
            next
          end

          if File.lstat(project_hook_file).symlink? &&
514
              File.realpath(project_hook_file) == File.realpath(gitlab_shell_hook_file)
R
Riyad Preukschas 已提交
515 516
            puts "ok".green
          else
B
Ben Bodenmiller 已提交
517
            puts "not a link to GitLab Shell's hook".red
R
Riyad Preukschas 已提交
518
            try_fixing_it(
519
              "sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
R
Riyad Preukschas 已提交
520 521 522 523 524 525
            )
            for_more_information(
              "lib/support/rewrite-hooks.sh"
            )
            fix_and_rerun
          end
526 527
        end
      end
R
Riyad Preukschas 已提交
528
    end
R
Riyad Preukschas 已提交
529

530
    def check_gitlab_shell_self_test
531
      gitlab_shell_repo_base = gitlab_shell_path
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
      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 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    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|
        print "#{project.name_with_namespace.yellow} ... "

        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 已提交
573 574 575 576

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

577 578
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
R
Riyad Preukschas 已提交
579 580
    end

581
    def gitlab_shell_version
582
      Gitlab::Shell.new.version
R
Riyad Preukschas 已提交
583 584
    end

585 586
    def has_gitlab_shell3?
      gitlab_shell_version.try(:start_with?, "v3.")
R
Riyad Preukschas 已提交
587
    end
R
Riyad Preukschas 已提交
588
  end
589

R
Riyad Preukschas 已提交
590 591


R
Riyad Preukschas 已提交
592
  namespace :sidekiq do
D
Dmitriy Zaporozhets 已提交
593
    desc "GITLAB | Check the configuration of Sidekiq"
R
Riyad Preukschas 已提交
594
    task check: :environment  do
R
Riyad Preukschas 已提交
595
      warn_user_is_not_gitlab
R
Riyad Preukschas 已提交
596
      start_checking "Sidekiq"
R
Riyad Preukschas 已提交
597

R
Riyad Preukschas 已提交
598
      check_sidekiq_running
599
      only_one_sidekiq_running
R
Riyad Preukschas 已提交
600

R
Riyad Preukschas 已提交
601
      finished_checking "Sidekiq"
R
Riyad Preukschas 已提交
602 603 604 605 606 607
    end


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

R
Riyad Preukschas 已提交
608
    def check_sidekiq_running
R
Riyad Preukschas 已提交
609 610
      print "Running? ... "

611
      if sidekiq_process_count > 0
R
Riyad Preukschas 已提交
612 613 614 615
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
616
          sudo_gitlab("RAILS_ENV=production script/background_jobs start")
R
Riyad Preukschas 已提交
617 618 619
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
D
Dmitriy Zaporozhets 已提交
620
          "see log/sidekiq.log for possible errors"
R
Riyad Preukschas 已提交
621
        )
R
Riyad Preukschas 已提交
622
        fix_and_rerun
R
Riyad Preukschas 已提交
623 624
      end
    end
625 626

    def only_one_sidekiq_running
627 628
      process_count = sidekiq_process_count
      return if process_count.zero?
629 630

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

645
    def sidekiq_process_count
646 647
      ps_ux, _ = Gitlab::Popen.popen(%W(ps ux))
      ps_ux.scan(/sidekiq \d+\.\d+\.\d+/).count
648
    end
R
Riyad Preukschas 已提交
649 650
  end

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

659
      if ldap_config.enabled
660
        print_users(args.limit)
661 662 663
      else
        puts 'LDAP is disabled in config/gitlab.yml'
      end
664 665 666 667

      finished_checking "LDAP"
    end

668
    def print_users(limit)
669
      puts "LDAP users with access to your GitLab server (only showing the first #{limit} results)"
670
      ldap.search(attributes: attributes, filter: filter, size: limit, return_result: false) do |entry|
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
        puts "DN: #{entry.dn}\t#{ldap_config.uid}: #{entry[ldap_config.uid]}"
      end
    end

    def attributes
      [ldap_config.uid]
    end

    def filter
      Net::LDAP::Filter.present?(ldap_config.uid)
    end

    def ldap
      @ldap ||= OmniAuth::LDAP::Adaptor.new(ldap_config).connection
    end

    def ldap_config
      @ldap_config ||= Gitlab.config.ldap
    end
  end
R
Riyad Preukschas 已提交
691 692 693 694

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

R
Riyad Preukschas 已提交
695
  def fix_and_rerun
R
Riyad Preukschas 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    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

722 723 724 725
  def sudo_gitlab(command)
    "sudo -u #{gitlab_user} -H #{command}"
  end

726 727 728 729
  def gitlab_user
    Gitlab.config.gitlab.user
  end

R
Riyad Preukschas 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742
  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
743 744

  def check_gitlab_shell
745
    required_version = Gitlab::VersionInfo.new(1, 9, 1)
746
    current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
747

748
    print "GitLab Shell version >= #{required_version} ? ... "
S
Sato Hiroyuki 已提交
749
    if current_version.valid? && required_version <= current_version
750
      puts "OK (#{current_version})".green
751
    else
752
      puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
753 754
    end
  end
755 756

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

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

S
Sato Hiroyuki 已提交
763
    if current_version.valid? && required_version <= current_version
764
        puts "yes (#{current_version})".green
765 766 767
    else
      puts "no".red
      try_fixing_it(
S
Sato Hiroyuki 已提交
768
        "Update your git to a version >= #{required_version} from #{current_version}"
769 770 771 772
      )
      fix_and_rerun
    end
  end
773
end