check.rake 21.4 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 20 21 22 23 24 25
      warn_user_is_not_gitlab
      start_checking "GitLab"

      check_database_config_exists
      check_database_is_not_sqlite
      check_migrations_are_up
      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 已提交
26
      check_projects_have_namespace
R
Riyad Preukschas 已提交
27
      check_satellites_exist
28
      check_redis_version
29
      check_git_version
R
Riyad Preukschas 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

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

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

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

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

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

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

R
Riyad Preukschas 已提交
121 122 123 124 125 126 127
    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 已提交
128
      else
R
Riyad Preukschas 已提交
129 130 131 132 133 134 135
        puts "no".red
        try_fixing_it(
          "Install the init script"
        )
        for_more_information(
          see_installation_guide_section "Install Init Script"
        )
R
Riyad Preukschas 已提交
136
        fix_and_rerun
R
Riyad Preukschas 已提交
137 138 139 140 141 142
      end
    end

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

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

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

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

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

      migration_status =  `bundle exec rake db:migrate:status`

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

    def check_satellites_exist
      print "Projects have satellites? ... "

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
189 190
        return
      end
R
Riyad Preukschas 已提交
191 192 193
      puts ""

      Project.find_each(batch_size: 100) do |project|
194
        print "#{project.name_with_namespace.yellow} ... "
R
Riyad Preukschas 已提交
195 196 197

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

255
    def check_redis_version
256 257
      print "Redis version >= 2.0.0? ... "

258 259
      if run_and_match("redis-cli --version", /redis-cli 2.\d.\d/)
        puts "yes".green
260
      else
261 262
        puts "no".red
        try_fixing_it(
263
          "Update your redis server to a version >= 2.0.0"
264 265 266 267 268 269
        )
        for_more_information(
          "gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq"
        )
        fix_and_rerun
      end
270
    end
R
Riyad Preukschas 已提交
271 272
  end

R
Riyad Preukschas 已提交
273 274


R
Riyad Preukschas 已提交
275 276 277
  namespace :env do
    desc "GITLAB | Check the configuration of the environment"
    task check: :environment  do
R
Riyad Preukschas 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
      warn_user_is_not_gitlab
      start_checking "Environment"

      check_gitlab_git_config
      check_python2_exists
      check_python2_version

      finished_checking "Environment"
    end


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

    def check_gitlab_git_config
293
      print "Git configured for #{gitlab_user} user? ... "
R
Riyad Preukschas 已提交
294 295 296

      options = {
        "user.name"  => "GitLab",
297
        "user.email" => Gitlab.config.gitlab.email_from
R
Riyad Preukschas 已提交
298 299 300 301 302 303 304 305 306 307
      }
      correct_options = options.map do |name, value|
        run("git config --global --get #{name}").try(:squish) == value
      end

      if correct_options.all?
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
308 309
          sudo_gitlab("git config --global user.name  \"#{options["user.name"]}\""),
          sudo_gitlab("git config --global user.email \"#{options["user.email"]}\"")
R
Riyad Preukschas 已提交
310 311 312 313
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
314
        fix_and_rerun
R
Riyad Preukschas 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      end
    end

    def check_python2_exists
      print "Has python2? ... "

      # Python prints its version to STDERR
      # so we can't just use run("python2 --version")
      if run_and_match("which python2", /python2$/)
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Make sure you have Python 2.5+ installed",
          "Link it to python2"
        )
        for_more_information(
          see_installation_guide_section "Packages / Dependencies"
        )
R
Riyad Preukschas 已提交
334
        fix_and_rerun
R
Riyad Preukschas 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      end
    end

    def check_python2_version
      print "python2 is supported version? ... "

      # Python prints its version to STDERR
      # so we can't just use run("python2 --version")

      unless run_and_match("which python2", /python2$/)
        puts "can't check because of previous errors".magenta
        return
      end

      if `python2 --version 2>&1` =~ /2\.[567]\.\d/
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Make sure you have Python 2.5+ installed",
          "Link it to python2"
        )
        for_more_information(
          see_installation_guide_section "Packages / Dependencies"
        )
R
Riyad Preukschas 已提交
360
        fix_and_rerun
R
Riyad Preukschas 已提交
361
      end
R
Riyad Preukschas 已提交
362 363 364
    end
  end

R
Riyad Preukschas 已提交
365 366


367
  namespace :gitlab_shell do
B
Ben Bodenmiller 已提交
368
    desc "GITLAB | Check the configuration of GitLab Shell"
R
Riyad Preukschas 已提交
369
    task check: :environment  do
R
Riyad Preukschas 已提交
370
      warn_user_is_not_gitlab
B
Ben Bodenmiller 已提交
371
      start_checking "GitLab Shell"
R
Riyad Preukschas 已提交
372

373
      check_gitlab_shell
R
Riyad Preukschas 已提交
374
      check_repo_base_exists
375
      check_repo_base_is_not_symlink
R
Riyad Preukschas 已提交
376 377
      check_repo_base_user_and_group
      check_repo_base_permissions
378 379
      check_update_hook_is_up_to_date
      check_repos_update_hooks_is_link
380
      check_gitlab_shell_self_test
R
Riyad Preukschas 已提交
381

B
Ben Bodenmiller 已提交
382
      finished_checking "GitLab Shell"
R
Riyad Preukschas 已提交
383 384 385 386 387 388 389
    end


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


390 391
    def check_update_hook_is_up_to_date
      print "update hook up-to-date? ... "
R
Riyad Preukschas 已提交
392

393
      hook_file = "update"
394
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
395
      gitlab_shell_hook_file  = File.join(gitlab_shell_hooks_path, hook_file)
R
Riyad Preukschas 已提交
396

397 398 399 400 401 402 403 404 405 406 407 408
      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"
        )
409
      end
R
Riyad Preukschas 已提交
410 411 412 413 414
    end

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

415
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
416 417 418 419 420 421 422

      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 已提交
423
          "This should have been created when setting up GitLab Shell.",
R
Riyad Preukschas 已提交
424
          "Make sure it's set correctly in config/gitlab.yml",
B
Ben Bodenmiller 已提交
425
          "Make sure GitLab Shell is installed correctly."
R
Riyad Preukschas 已提交
426 427
        )
        for_more_information(
B
Ben Bodenmiller 已提交
428
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
429
        )
R
Riyad Preukschas 已提交
430
        fix_and_rerun
R
Riyad Preukschas 已提交
431 432 433
      end
    end

434 435 436
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

437
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      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 已提交
454
    def check_repo_base_permissions
455
      print "Repo base access is drwxrws---? ... "
R
Riyad Preukschas 已提交
456

457
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
458 459
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
460 461 462
        return
      end

463
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
R
Riyad Preukschas 已提交
464 465 466 467
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
468
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
469
          "sudo chmod -R ug-s #{repo_base_path}",
470
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
R
Riyad Preukschas 已提交
471 472
        )
        for_more_information(
B
Ben Bodenmiller 已提交
473
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
474
        )
R
Riyad Preukschas 已提交
475
        fix_and_rerun
R
Riyad Preukschas 已提交
476 477 478 479
      end
    end

    def check_repo_base_user_and_group
480 481 482
      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 已提交
483

484
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
R
Riyad Preukschas 已提交
485 486
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
487 488 489
        return
      end

490 491 492
      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 已提交
493
        puts "yes".green
494
      else
R
Riyad Preukschas 已提交
495
        puts "no".red
496
        puts "  User id for #{gitlab_shell_ssh_user}: #{uid}. Groupd id for #{gitlab_shell_owner_group}: #{gid}".blue
R
Riyad Preukschas 已提交
497
        try_fixing_it(
498
          "sudo chown -R #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group} #{repo_base_path}"
R
Riyad Preukschas 已提交
499 500
        )
        for_more_information(
B
Ben Bodenmiller 已提交
501
          see_installation_guide_section "GitLab Shell"
R
Riyad Preukschas 已提交
502
        )
R
Riyad Preukschas 已提交
503
        fix_and_rerun
R
Riyad Preukschas 已提交
504 505 506
      end
    end

507 508
    def check_repos_update_hooks_is_link
      print "update hooks in repos are links: ... "
R
Riyad Preukschas 已提交
509

510
      hook_file = "update"
511
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
512 513
      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 已提交
514

515
      unless File.exists?(gitlab_shell_hook_file)
R
Riyad Preukschas 已提交
516 517 518
        puts "can't check because of previous errors".magenta
        return
      end
519

R
Riyad Preukschas 已提交
520 521 522 523 524
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
525

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

R
Riyad Preukschas 已提交
529 530
        if project.empty_repo?
          puts "repository is empty".magenta
R
Riyad Preukschas 已提交
531
        else
R
Riyad Preukschas 已提交
532 533 534 535 536
          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(
537
              "sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
R
Riyad Preukschas 已提交
538 539
            )
            for_more_information(
M
Martin Bastien 已提交
540
              "#{gitlab_shell_user_home}/gitlab-shell/support/rewrite-hooks.sh"
R
Riyad Preukschas 已提交
541 542 543 544 545 546
            )
            fix_and_rerun
            next
          end

          if File.lstat(project_hook_file).symlink? &&
547
              File.realpath(project_hook_file) == File.realpath(gitlab_shell_hook_file)
R
Riyad Preukschas 已提交
548 549
            puts "ok".green
          else
B
Ben Bodenmiller 已提交
550
            puts "not a link to GitLab Shell's hook".red
R
Riyad Preukschas 已提交
551
            try_fixing_it(
552
              "sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
R
Riyad Preukschas 已提交
553 554 555 556 557 558
            )
            for_more_information(
              "lib/support/rewrite-hooks.sh"
            )
            fix_and_rerun
          end
559 560
        end
      end
R
Riyad Preukschas 已提交
561
    end
R
Riyad Preukschas 已提交
562

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    def check_gitlab_shell_self_test
      gitlab_shell_repo_base = File.expand_path('gitlab-shell', gitlab_shell_user_home)
      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 已提交
580 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
    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 已提交
606 607 608 609

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

610 611
    def gitlab_shell_user_home
      File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
R
Riyad Preukschas 已提交
612 613
    end

614
    def gitlab_shell_version
615
      Gitlab::Shell.new.version
R
Riyad Preukschas 已提交
616 617
    end

618 619
    def has_gitlab_shell3?
      gitlab_shell_version.try(:start_with?, "v3.")
R
Riyad Preukschas 已提交
620
    end
R
Riyad Preukschas 已提交
621
  end
622

R
Riyad Preukschas 已提交
623 624


R
Riyad Preukschas 已提交
625
  namespace :sidekiq do
D
Dmitriy Zaporozhets 已提交
626
    desc "GITLAB | Check the configuration of Sidekiq"
R
Riyad Preukschas 已提交
627
    task check: :environment  do
R
Riyad Preukschas 已提交
628
      warn_user_is_not_gitlab
R
Riyad Preukschas 已提交
629
      start_checking "Sidekiq"
R
Riyad Preukschas 已提交
630

R
Riyad Preukschas 已提交
631
      check_sidekiq_running
632
      only_one_sidekiq_running
R
Riyad Preukschas 已提交
633

R
Riyad Preukschas 已提交
634
      finished_checking "Sidekiq"
R
Riyad Preukschas 已提交
635 636 637 638 639 640
    end


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

R
Riyad Preukschas 已提交
641
    def check_sidekiq_running
R
Riyad Preukschas 已提交
642 643
      print "Running? ... "

644
      if sidekiq_process_count > 0
R
Riyad Preukschas 已提交
645 646 647 648
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
649
          sudo_gitlab("RAILS_ENV=production script/background_jobs start")
R
Riyad Preukschas 已提交
650 651 652
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
D
Dmitriy Zaporozhets 已提交
653
          "see log/sidekiq.log for possible errors"
R
Riyad Preukschas 已提交
654
        )
R
Riyad Preukschas 已提交
655
        fix_and_rerun
R
Riyad Preukschas 已提交
656 657
      end
    end
658 659

    def only_one_sidekiq_running
660 661
      process_count = sidekiq_process_count
      return if process_count.zero?
662 663

      print 'Number of Sidekiq processes ... '
664
      if process_count == 1
665 666
        puts '1'.green
      else
667
        puts "#{process_count}".red
668 669
        try_fixing_it(
          'sudo service gitlab stop',
670 671
          "sudo pkill -u #{gitlab_user} -f sidekiq",
          "sleep 10 && sudo pkill -9 -u #{gitlab_user} -f sidekiq",
672 673 674 675 676 677
          'sudo service gitlab start'
        )
        fix_and_rerun
      end
    end

678 679
    def sidekiq_process_count
      `ps ux`.scan(/sidekiq \d+\.\d+\.\d+/).count
680
    end
R
Riyad Preukschas 已提交
681 682
  end

683
  namespace :ldap do
684
    task :check, [:limit] => :environment do |t, args|
685 686
      # Only show up to 100 results because LDAP directories can be very big.
      # This setting only affects the `rake gitlab:check` script.
687
      args.with_defaults(limit: 100)
688 689 690
      warn_user_is_not_gitlab
      start_checking "LDAP"

691
      if ldap_config.enabled
692
        print_users(args.limit)
693 694 695
      else
        puts 'LDAP is disabled in config/gitlab.yml'
      end
696 697 698 699

      finished_checking "LDAP"
    end

700 701 702
    def print_users(limit)
      puts "LDAP users with access to your GitLab server (limit: #{limit}):"
      ldap.search(attributes: attributes, filter: filter, size: limit, return_result: false) do |entry|
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        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 已提交
723 724 725 726

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

R
Riyad Preukschas 已提交
727
  def fix_and_rerun
R
Riyad Preukschas 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    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

754 755 756 757
  def sudo_gitlab(command)
    "sudo -u #{gitlab_user} -H #{command}"
  end

758 759 760 761
  def gitlab_user
    Gitlab.config.gitlab.user
  end

R
Riyad Preukschas 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774
  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
775 776

  def check_gitlab_shell
777
    required_version = Gitlab::VersionInfo.new(1, 7, 9)
778
    current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
779

780
    print "GitLab Shell version >= #{required_version} ? ... "
S
Sato Hiroyuki 已提交
781
    if current_version.valid? && required_version <= current_version
782
      puts "OK (#{current_version})".green
783
    else
784
      puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
785 786
    end
  end
787 788

  def check_git_version
789
    required_version = Gitlab::VersionInfo.new(1, 7, 10)
790
    current_version = Gitlab::VersionInfo.parse(run("#{Gitlab.config.git.bin_path} --version"))
S
Sato Hiroyuki 已提交
791

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

S
Sato Hiroyuki 已提交
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(
S
Sato Hiroyuki 已提交
800
        "Update your git to a version >= #{required_version} from #{current_version}"
801 802 803 804
      )
      fix_and_rerun
    end
  end
805
end