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


R
Riyad Preukschas 已提交
9

10
  namespace :app do
R
Riyad Preukschas 已提交
11 12
    desc "GITLAB | Check the configuration of the GitLab Rails app"
    task check: :environment  do
R
Riyad Preukschas 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      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
      check_satellites_exist

      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 已提交
51
        fix_and_rerun
R
Riyad Preukschas 已提交
52 53 54 55
      end
    end

    def check_database_is_not_sqlite
R
Riyad Preukschas 已提交
56
      print "Database is SQLite ... "
R
Riyad Preukschas 已提交
57 58 59

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

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

    def check_gitlab_config_not_outdated
R
Riyad Preukschas 已提交
93
      print "GitLab config outdated? ... "
R
Riyad Preukschas 已提交
94 95 96 97 98 99 100

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

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

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

      script_path = "/etc/init.d/gitlab"
      unless File.exists?(script_path)
        puts "can't check because of previous errors".magenta
142 143 144
        return
      end

R
Riyad Preukschas 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
      recipe_content = `curl https://raw.github.com/gitlabhq/gitlab-recipes/master/init.d/gitlab 2>/dev/null`
      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 已提交
158
        fix_and_rerun
R
Riyad Preukschas 已提交
159 160 161 162 163 164 165 166 167 168
      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
169
      else
R
Riyad Preukschas 已提交
170 171
        puts "no".red
        try_fixing_it(
172
          sudo_gitlab("bundle exec rake db:migrate RAILS_ENV=production")
R
Riyad Preukschas 已提交
173
        )
R
Riyad Preukschas 已提交
174
        fix_and_rerun
R
Riyad Preukschas 已提交
175 176 177 178 179 180 181 182
      end
    end

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

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
183 184
        return
      end
R
Riyad Preukschas 已提交
185 186 187
      puts ""

      Project.find_each(batch_size: 100) do |project|
188
        print "#{project.name_with_namespace.yellow} ... "
R
Riyad Preukschas 已提交
189 190 191

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

R
Riyad Preukschas 已提交
250 251


R
Riyad Preukschas 已提交
252 253 254
  namespace :env do
    desc "GITLAB | Check the configuration of the environment"
    task check: :environment  do
R
Riyad Preukschas 已提交
255 256 257 258
      warn_user_is_not_gitlab
      start_checking "Environment"

      check_gitlab_in_git_group
259
      check_issue_1059_shell_profile_error
R
Riyad Preukschas 已提交
260 261 262 263 264 265 266 267 268 269 270 271
      check_gitlab_git_config
      check_python2_exists
      check_python2_version

      finished_checking "Environment"
    end


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

    def check_gitlab_git_config
272 273
      gitlab_user = Gitlab.config.gitlab.user
      print "Git configured for #{gitlab_user} user? ... "
R
Riyad Preukschas 已提交
274 275 276

      options = {
        "user.name"  => "GitLab",
277
        "user.email" => Gitlab.config.gitlab.email_from
R
Riyad Preukschas 已提交
278 279 280 281 282 283 284 285 286 287
      }
      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(
288 289
          sudo_gitlab("git config --global user.name  \"#{options["user.name"]}\""),
          sudo_gitlab("git config --global user.email \"#{options["user.email"]}\"")
R
Riyad Preukschas 已提交
290 291 292 293
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
R
Riyad Preukschas 已提交
294
        fix_and_rerun
R
Riyad Preukschas 已提交
295 296 297 298
      end
    end

    def check_gitlab_in_git_group
299
      gitlab_user = Gitlab.config.gitlab.user
300 301
      gitolite_owner_group = Gitlab.config.gitolite.owner_group
      print "#{gitlab_user} user is in #{gitolite_owner_group} group? ... "
R
Riyad Preukschas 已提交
302

303
      if run_and_match("id -rnG", /^#{gitolite_owner_group}\W|\W#{gitolite_owner_group}\W|\W#{gitolite_owner_group}$/)
R
Riyad Preukschas 已提交
304 305 306 307
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
308
          "sudo usermod -a -G #{gitolite_owner_group} #{gitlab_user}"
R
Riyad Preukschas 已提交
309 310 311 312
        )
        for_more_information(
          see_installation_guide_section "System Users"
        )
R
Riyad Preukschas 已提交
313
        fix_and_rerun
R
Riyad Preukschas 已提交
314 315 316 317
      end
    end

    # see https://github.com/gitlabhq/gitlabhq/issues/1059
318
    def check_issue_1059_shell_profile_error
319
      gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
320
      print "Has no \"-e\" in ~#{gitolite_ssh_user}/.profile ... "
R
Riyad Preukschas 已提交
321

322
      profile_file = File.join(gitolite_user_home, ".profile")
R
Riyad Preukschas 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336

      unless File.read(profile_file) =~ /^-e PATH/
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Open #{profile_file}",
          "Find the line starting with \"-e PATH\"",
          "Remove \"-e \" so the line starts with PATH"
        )
        for_more_information(
          see_installation_guide_section("Gitolite"),
          "https://github.com/gitlabhq/gitlabhq/issues/1059"
        )
R
Riyad Preukschas 已提交
337
        fix_and_rerun
R
Riyad Preukschas 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      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 已提交
357
        fix_and_rerun
R
Riyad Preukschas 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
      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 已提交
383
        fix_and_rerun
R
Riyad Preukschas 已提交
384
      end
R
Riyad Preukschas 已提交
385 386 387
    end
  end

R
Riyad Preukschas 已提交
388 389


R
Riyad Preukschas 已提交
390 391 392
  namespace :gitolite do
    desc "GITLAB | Check the configuration of Gitolite"
    task check: :environment  do
R
Riyad Preukschas 已提交
393 394 395 396 397 398 399 400 401 402
      warn_user_is_not_gitlab
      start_checking "Gitolite"

      check_gitolite_is_up_to_date
      check_gitoliterc_repo_umask
      check_gitoliterc_git_config_keys
      check_dot_gitolite_exists
      check_dot_gitolite_user_and_group
      check_dot_gitolite_permissions
      check_repo_base_exists
403
      check_repo_base_is_not_symlink
R
Riyad Preukschas 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
      check_repo_base_user_and_group
      check_repo_base_permissions
      check_can_clone_gitolite_admin
      check_can_commit_to_gitolite_admin
      check_post_receive_hook_exists
      check_post_receive_hook_is_up_to_date
      check_repos_post_receive_hooks_is_link
      check_repos_git_config

      finished_checking "Gitolite"
    end


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

    def check_can_clone_gitolite_admin
      print "Can clone gitolite-admin? ... "

      test_path = "/tmp/gitlab_gitolite_admin_test"
      FileUtils.rm_rf(test_path)
425
      `git clone -q #{Gitlab.config.gitolite.admin_uri} #{test_path}`
R
Riyad Preukschas 已提交
426 427 428 429 430 431 432 433
      raise unless $?.success?

      puts "yes".green
    rescue
      puts "no".red
      try_fixing_it(
        "Make sure the \"admin_uri\" is set correctly in config/gitlab.yml",
        "Try cloning it yourself with:",
434
        "  git clone -q #{Gitlab.config.gitolite.admin_uri} /tmp/gitolite-admin",
R
Riyad Preukschas 已提交
435 436 437 438 439
        "Make sure Gitolite is installed correctly."
      )
      for_more_information(
        see_installation_guide_section "Gitolite"
      )
R
Riyad Preukschas 已提交
440
      fix_and_rerun
R
Riyad Preukschas 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    end

    # assumes #check_can_clone_gitolite_admin has been run before
    def check_can_commit_to_gitolite_admin
      print "Can commit to gitolite-admin? ... "

      test_path = "/tmp/gitlab_gitolite_admin_test"
      unless File.exists?(test_path)
        puts "can't check because of previous errors".magenta
        return
      end

      Dir.chdir(test_path) do
        `touch foo && git add foo && git commit -qm foo`
        raise unless $?.success?
      end

      puts "yes".green
    rescue
      puts "no".red
      try_fixing_it(
        "Try committing to it yourself with:",
463
        "  git clone -q #{Gitlab.config.gitolite.admin_uri} /tmp/gitolite-admin",
R
Riyad Preukschas 已提交
464 465 466 467 468 469 470 471
        "  touch foo",
        "  git add foo",
        "  git commit -m \"foo\"",
        "Make sure Gitolite is installed correctly."
      )
      for_more_information(
        see_installation_guide_section "Gitolite"
      )
R
Riyad Preukschas 已提交
472
      fix_and_rerun
R
Riyad Preukschas 已提交
473 474 475 476 477 478 479
    ensure
      FileUtils.rm_rf("/tmp/gitolite_gitlab_test")
    end

    def check_dot_gitolite_exists
      print "Config directory exists? ... "

480
      gitolite_config_path = File.join(gitolite_user_home, ".gitolite")
R
Riyad Preukschas 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493

      if File.directory?(gitolite_config_path)
        puts "yes".green
      else
        puts "no".red
        puts "#{gitolite_config_path} is missing".red
        try_fixing_it(
          "This should have been created when setting up Gitolite.",
          "Make sure Gitolite is installed correctly."
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
494
        fix_and_rerun
R
Riyad Preukschas 已提交
495 496 497 498 499
      end
    end

    def check_dot_gitolite_permissions
      print "Config directory access is drwxr-x---? ... "
500

501
      gitolite_config_path = File.join(gitolite_user_home, ".gitolite")
R
Riyad Preukschas 已提交
502 503 504 505 506
      unless File.exists?(gitolite_config_path)
        puts "can't check because of previous errors".magenta
        return
      end

507
      if File.stat(gitolite_config_path).mode.to_s(8).ends_with?("750")
R
Riyad Preukschas 已提交
508
        puts "yes".green
N
Nihad Abbasov 已提交
509
      else
R
Riyad Preukschas 已提交
510 511 512 513 514 515 516
        puts "no".red
        try_fixing_it(
          "sudo chmod 750 #{gitolite_config_path}"
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
517
        fix_and_rerun
R
Riyad Preukschas 已提交
518 519 520 521
      end
    end

    def check_dot_gitolite_user_and_group
522
      gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
523 524
      gitolite_owner_group = Gitlab.config.gitolite.owner_group
      print "Config directory owned by #{gitolite_ssh_user}:#{gitolite_owner_group} ... "
R
Riyad Preukschas 已提交
525

526
      gitolite_config_path = File.join(gitolite_user_home, ".gitolite")
R
Riyad Preukschas 已提交
527 528
      unless File.exists?(gitolite_config_path)
        puts "can't check because of previous errors".magenta
529 530 531
        return
      end

532
      if File.stat(gitolite_config_path).uid == uid_for(gitolite_ssh_user) &&
533
         File.stat(gitolite_config_path).gid == gid_for(gitolite_owner_group)
R
Riyad Preukschas 已提交
534 535 536 537
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
538
          "sudo chown -R #{gitolite_ssh_user}:#{gitolite_owner_group} #{gitolite_config_path}"
R
Riyad Preukschas 已提交
539 540 541 542
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
543
        fix_and_rerun
R
Riyad Preukschas 已提交
544 545 546 547 548
      end
    end

    def check_gitolite_is_up_to_date
      print "Using recommended version ... "
549
      if gitolite_version.try(:start_with?, "v3.2")
R
Riyad Preukschas 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "We strongly recommend using the version pointed out in the installation guide."
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
        # this is not a "hard" failure
      end
    end

    def check_gitoliterc_git_config_keys
564
      gitoliterc_path = File.join(gitolite_user_home, ".gitolite.rc")
R
Riyad Preukschas 已提交
565 566 567 568 569 570 571 572

      print "Allow all Git config keys in .gitolite.rc ... "
      option_name = if has_gitolite3?
                      # see https://github.com/sitaramc/gitolite/blob/v3.04/src/lib/Gitolite/Rc.pm#L329
                      "GIT_CONFIG_KEYS"
                    else
                      # assume older version
                      # see https://github.com/sitaramc/gitolite/blob/v2.3/conf/example.gitolite.rc#L49
573
                      "\\$GL_GITCONFIG_KEYS"
R
Riyad Preukschas 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587
                    end
      option_value = ".*"
      if open(gitoliterc_path).grep(/#{option_name}\s*=[>]?\s*["']#{option_value}["']/).any?
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Open #{gitoliterc_path}",
          "Find the \"#{option_name}\" option",
          "Change its value to \".*\""
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
588
        fix_and_rerun
R
Riyad Preukschas 已提交
589 590 591 592
      end
    end

    def check_gitoliterc_repo_umask
593
      gitoliterc_path = File.join(gitolite_user_home, ".gitolite.rc")
R
Riyad Preukschas 已提交
594 595 596 597 598 599 600 601

      print "Repo umask is 0007 in .gitolite.rc? ... "
      option_name = if has_gitolite3?
                      # see https://github.com/sitaramc/gitolite/blob/v3.04/src/lib/Gitolite/Rc.pm#L328
                      "UMASK"
                    else
                      # assume older version
                      # see https://github.com/sitaramc/gitolite/blob/v2.3/conf/example.gitolite.rc#L32
602
                      "\\$REPO_UMASK"
R
Riyad Preukschas 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616
                    end
      option_value = "0007"
      if open(gitoliterc_path).grep(/#{option_name}\s*=[>]?\s*#{option_value}/).any?
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
          "Open #{gitoliterc_path}",
          "Find the \"#{option_name}\" option",
          "Change its value to \"0007\""
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
617
        fix_and_rerun
R
Riyad Preukschas 已提交
618 619 620 621 622 623 624
      end
    end

    def check_post_receive_hook_exists
      print "post-receive hook exists? ... "

      hook_file = "post-receive"
625
      gitolite_hooks_path = File.join(Gitlab.config.gitolite.hooks_path, "common")
R
Riyad Preukschas 已提交
626
      gitolite_hook_file = File.join(gitolite_hooks_path, hook_file)
627
      gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
R
Riyad Preukschas 已提交
628 629 630 631 632

      gitlab_hook_file = Rails.root.join.join("lib", "hooks", hook_file)

      if File.exists?(gitolite_hook_file)
        puts "yes".green
633
      else
R
Riyad Preukschas 已提交
634 635
        puts "no".red
        try_fixing_it(
636
          "sudo -u #{gitolite_ssh_user} cp #{gitlab_hook_file} #{gitolite_hook_file}"
R
Riyad Preukschas 已提交
637 638 639 640
        )
        for_more_information(
          see_installation_guide_section "Setup GitLab Hooks"
        )
R
Riyad Preukschas 已提交
641
        fix_and_rerun
R
Riyad Preukschas 已提交
642 643 644 645 646 647 648
      end
    end

    def check_post_receive_hook_is_up_to_date
      print "post-receive hook up-to-date? ... "

      hook_file = "post-receive"
649
      gitolite_hooks_path = File.join(Gitlab.config.gitolite.hooks_path, "common")
R
Riyad Preukschas 已提交
650
      gitolite_hook_file  = File.join(gitolite_hooks_path, hook_file)
651
      gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
R
Riyad Preukschas 已提交
652 653 654

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

658
      gitolite_hook_content = File.read(gitolite_hook_file)
R
Riyad Preukschas 已提交
659 660 661 662 663 664 665 666
      gitlab_hook_file = Rails.root.join.join("lib", "hooks", hook_file)
      gitlab_hook_content = File.read(gitlab_hook_file)

      if gitolite_hook_content == gitlab_hook_content
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
667
          "sudo -u #{gitolite_ssh_user} cp #{gitlab_hook_file} #{gitolite_hook_file}"
R
Riyad Preukschas 已提交
668 669 670 671
        )
        for_more_information(
          see_installation_guide_section "Setup GitLab Hooks"
        )
R
Riyad Preukschas 已提交
672
        fix_and_rerun
R
Riyad Preukschas 已提交
673 674 675 676 677 678
      end
    end

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

679
      repo_base_path = Gitlab.config.gitolite.repos_path
R
Riyad Preukschas 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693

      if File.exists?(repo_base_path)
        puts "yes".green
      else
        puts "no".red
        puts "#{repo_base_path} is missing".red
        try_fixing_it(
          "This should have been created when setting up Gitolite.",
          "Make sure it's set correctly in config/gitlab.yml",
          "Make sure Gitolite is installed correctly."
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
694
        fix_and_rerun
R
Riyad Preukschas 已提交
695 696 697
      end
    end

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

      repo_base_path = Gitlab.config.gitolite.repos_path
      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 已提交
718
    def check_repo_base_permissions
719
      print "Repo base access is drwxrws---? ... "
R
Riyad Preukschas 已提交
720

721
      repo_base_path = Gitlab.config.gitolite.repos_path
R
Riyad Preukschas 已提交
722 723
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
724 725 726
        return
      end

727
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
R
Riyad Preukschas 已提交
728 729 730 731
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
732 733
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
          "sudo chmod -R u-s #{repo_base_path}",
734
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
R
Riyad Preukschas 已提交
735 736 737 738
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
739
        fix_and_rerun
R
Riyad Preukschas 已提交
740 741 742 743
      end
    end

    def check_repo_base_user_and_group
744
      gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
745 746
      gitolite_owner_group = Gitlab.config.gitolite.owner_group
      print "Repo base owned by #{gitolite_ssh_user}:#{gitolite_owner_group}? ... "
R
Riyad Preukschas 已提交
747

748
      repo_base_path = Gitlab.config.gitolite.repos_path
R
Riyad Preukschas 已提交
749 750
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
751 752 753
        return
      end

754
      if File.stat(repo_base_path).uid == uid_for(gitolite_ssh_user) &&
755
         File.stat(repo_base_path).gid == gid_for(gitolite_owner_group)
R
Riyad Preukschas 已提交
756
        puts "yes".green
757
      else
R
Riyad Preukschas 已提交
758 759
        puts "no".red
        try_fixing_it(
760
          "sudo chown -R #{gitolite_ssh_user}:#{gitolite_owner_group} #{repo_base_path}"
R
Riyad Preukschas 已提交
761 762 763 764
        )
        for_more_information(
          see_installation_guide_section "Gitolite"
        )
R
Riyad Preukschas 已提交
765
        fix_and_rerun
R
Riyad Preukschas 已提交
766 767 768 769 770 771 772 773
      end
    end

    def check_repos_git_config
      print "Git config in repos: ... "

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
774 775
        return
      end
R
Riyad Preukschas 已提交
776
      puts ""
777

R
Riyad Preukschas 已提交
778 779 780 781 782
      options = {
        "core.sharedRepository" => "0660",
      }

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

        correct_options = options.map do |name, value|
786
          run("git --git-dir=\"#{project.repository.path_to_repo}\" config --get #{name}").try(:chomp) == value
R
Riyad Preukschas 已提交
787 788 789 790
        end

        if correct_options.all?
          puts "ok".green
791
        else
R
Riyad Preukschas 已提交
792 793
          puts "wrong or missing".red
          try_fixing_it(
794
            sudo_gitlab("bundle exec rake gitlab:gitolite:update_repos RAILS_ENV=production")
R
Riyad Preukschas 已提交
795 796 797 798
          )
          for_more_information(
            "doc/raketasks/maintenance.md"
          )
R
Riyad Preukschas 已提交
799
          fix_and_rerun
800 801
        end
      end
R
Riyad Preukschas 已提交
802 803 804 805 806 807
    end

    def check_repos_post_receive_hooks_is_link
      print "post-receive hooks in repos are links: ... "

      hook_file = "post-receive"
808
      gitolite_hooks_path = File.join(Gitlab.config.gitolite.hooks_path, "common")
R
Riyad Preukschas 已提交
809
      gitolite_hook_file  = File.join(gitolite_hooks_path, hook_file)
810
      gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
R
Riyad Preukschas 已提交
811 812 813 814 815

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

R
Riyad Preukschas 已提交
817 818 819 820 821
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
822

R
Riyad Preukschas 已提交
823
      Project.find_each(batch_size: 100) do |project|
824
        print "#{project.name_with_namespace.yellow} ... "
825
        project_hook_file = File.join(project.repository.path_to_repo, "hooks", hook_file)
826

R
Riyad Preukschas 已提交
827 828 829
        unless File.exists?(project_hook_file)
          puts "missing".red
          try_fixing_it(
830
            "sudo -u #{gitolite_ssh_user} ln -sf #{gitolite_hook_file} #{project_hook_file}"
R
Riyad Preukschas 已提交
831 832 833 834
          )
          for_more_information(
            "lib/support/rewrite-hooks.sh"
          )
R
Riyad Preukschas 已提交
835
          fix_and_rerun
R
Riyad Preukschas 已提交
836 837
          next
        end
838

839 840
        if File.lstat(project_hook_file).symlink? &&
            File.realpath(project_hook_file) == File.realpath(gitolite_hook_file)
R
Riyad Preukschas 已提交
841 842 843 844
          puts "ok".green
        else
          puts "not a link to Gitolite's hook".red
          try_fixing_it(
845
            "sudo -u #{gitolite_ssh_user} ln -sf #{gitolite_hook_file} #{project_hook_file}"
R
Riyad Preukschas 已提交
846 847 848 849
          )
          for_more_information(
            "lib/support/rewrite-hooks.sh"
          )
R
Riyad Preukschas 已提交
850
          fix_and_rerun
851 852
        end
      end
R
Riyad Preukschas 已提交
853
    end
R
Riyad Preukschas 已提交
854 855 856 857 858


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

859
    def gitolite_user_home
860
      File.expand_path("~#{Gitlab.config.gitolite.ssh_user}")
R
Riyad Preukschas 已提交
861 862 863
    end

    def gitolite_version
864
      gitolite_version_file = "#{gitolite_user_home}/gitolite/src/VERSION"
R
Riyad Preukschas 已提交
865 866 867 868 869 870 871 872
      if File.readable?(gitolite_version_file)
        File.read(gitolite_version_file)
      end
    end

    def has_gitolite3?
      gitolite_version.try(:start_with?, "v3.")
    end
R
Riyad Preukschas 已提交
873
  end
874

R
Riyad Preukschas 已提交
875 876


R
Riyad Preukschas 已提交
877
  namespace :sidekiq do
D
Dmitriy Zaporozhets 已提交
878
    desc "GITLAB | Check the configuration of Sidekiq"
R
Riyad Preukschas 已提交
879
    task check: :environment  do
R
Riyad Preukschas 已提交
880
      warn_user_is_not_gitlab
R
Riyad Preukschas 已提交
881
      start_checking "Sidekiq"
R
Riyad Preukschas 已提交
882

R
Riyad Preukschas 已提交
883
      check_sidekiq_running
R
Riyad Preukschas 已提交
884

R
Riyad Preukschas 已提交
885
      finished_checking "Sidekiq"
R
Riyad Preukschas 已提交
886 887 888 889 890 891
    end


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

R
Riyad Preukschas 已提交
892
    def check_sidekiq_running
R
Riyad Preukschas 已提交
893 894
      print "Running? ... "

895
      if run_and_match("ps aux | grep -i sidekiq", /sidekiq \d\.\d\.\d.+$/)
R
Riyad Preukschas 已提交
896 897 898 899
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
900
          sudo_gitlab("bundle exec rake sidekiq:start RAILS_ENV=production")
R
Riyad Preukschas 已提交
901 902 903
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
D
Dmitriy Zaporozhets 已提交
904
          "see log/sidekiq.log for possible errors"
R
Riyad Preukschas 已提交
905
        )
R
Riyad Preukschas 已提交
906
        fix_and_rerun
R
Riyad Preukschas 已提交
907 908 909 910 911 912 913 914
      end
    end
  end


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

R
Riyad Preukschas 已提交
915
  def fix_and_rerun
R
Riyad Preukschas 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    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

942 943 944 945 946
  def sudo_gitlab(command)
    gitlab_user = Gitlab.config.gitlab.user
    "sudo -u #{gitlab_user} -H #{command}"
  end

R
Riyad Preukschas 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959
  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
960
end