check.rake 18.2 KB
Newer Older
1
namespace :gitlab do
2 3
  desc "GITLAB | Check the configuration of GitLab and its environment"
  task check: %w{gitlab:env:check
4
                 gitlab:gitlab_shell:check
Riyad Preukschas's avatar
Riyad Preukschas committed
5
                 gitlab:sidekiq:check
Riyad Preukschas's avatar
Riyad Preukschas committed
6 7 8
                 gitlab:app:check}


9

10
  namespace :app do
11 12
    desc "GITLAB | Check the configuration of the GitLab Rails app"
    task check: :environment  do
Riyad Preukschas's avatar
Riyad Preukschas committed
13 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
      check_satellites_exist
26
      check_redis_version
27
      check_git_version
Riyad Preukschas's avatar
Riyad Preukschas committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

      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"
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
53
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
54 55 56 57
      end
    end

    def check_database_is_not_sqlite
58
      print "Database is SQLite ... "
Riyad Preukschas's avatar
Riyad Preukschas committed
59 60 61

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

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

    def check_gitlab_config_not_outdated
Riyad Preukschas's avatar
Riyad Preukschas committed
95
      print "GitLab config outdated? ... "
Riyad Preukschas's avatar
Riyad Preukschas committed
96 97 98 99 100 101 102

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

Riyad Preukschas's avatar
Riyad Preukschas committed
119 120 121 122 123 124 125
    def check_init_script_exists
      print "Init script exists? ... "

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

      if File.exists?(script_path)
        puts "yes".green
Nihad Abbasov's avatar
Nihad Abbasov committed
126
      else
Riyad Preukschas's avatar
Riyad Preukschas committed
127 128 129 130 131 132 133
        puts "no".red
        try_fixing_it(
          "Install the init script"
        )
        for_more_information(
          see_installation_guide_section "Install Init Script"
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
134
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
135 136 137 138 139 140 141 142 143
      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
144 145 146
        return
      end

Riyad Preukschas's avatar
Riyad Preukschas committed
147 148 149 150 151 152 153 154 155 156 157 158 159
      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"
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
160
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
161 162 163 164 165 166 167 168 169 170
      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
171
      else
Riyad Preukschas's avatar
Riyad Preukschas committed
172 173
        puts "no".red
        try_fixing_it(
174
          sudo_gitlab("bundle exec rake db:migrate RAILS_ENV=production")
Riyad Preukschas's avatar
Riyad Preukschas committed
175
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
176
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
177 178 179 180 181 182 183 184
      end
    end

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

      unless Project.count > 0
        puts "can't check, you have no projects".magenta
185 186
        return
      end
Riyad Preukschas's avatar
Riyad Preukschas committed
187 188 189
      puts ""

      Project.find_each(batch_size: 100) do |project|
190
        print "#{project.name_with_namespace.yellow} ... "
Riyad Preukschas's avatar
Riyad Preukschas committed
191 192 193

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

251
    def check_redis_version
252 253
      print "Redis version >= 2.0.0? ... "

254 255
      if run_and_match("redis-cli --version", /redis-cli 2.\d.\d/)
        puts "yes".green
256
      else
257 258
        puts "no".red
        try_fixing_it(
259
          "Update your redis server to a version >= 2.0.0"
260 261 262 263 264 265
        )
        for_more_information(
          "gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq"
        )
        fix_and_rerun
      end
266
    end
267 268
  end

Riyad Preukschas's avatar
Riyad Preukschas committed
269 270


271 272 273
  namespace :env do
    desc "GITLAB | Check the configuration of the environment"
    task check: :environment  do
Riyad Preukschas's avatar
Riyad Preukschas committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      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
289 290
      gitlab_user = Gitlab.config.gitlab.user
      print "Git configured for #{gitlab_user} user? ... "
Riyad Preukschas's avatar
Riyad Preukschas committed
291 292 293

      options = {
        "user.name"  => "GitLab",
294
        "user.email" => Gitlab.config.gitlab.email_from
Riyad Preukschas's avatar
Riyad Preukschas committed
295 296 297 298 299 300 301 302 303 304
      }
      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(
305 306
          sudo_gitlab("git config --global user.name  \"#{options["user.name"]}\""),
          sudo_gitlab("git config --global user.email \"#{options["user.email"]}\"")
Riyad Preukschas's avatar
Riyad Preukschas committed
307 308 309 310
        )
        for_more_information(
          see_installation_guide_section "GitLab"
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
311
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      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"
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
331
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      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"
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
357
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
358
      end
359 360 361
    end
  end

Riyad Preukschas's avatar
Riyad Preukschas committed
362 363


364
  namespace :gitlab_shell do
Ben Bodenmiller's avatar
Ben Bodenmiller committed
365
    desc "GITLAB | Check the configuration of GitLab Shell"
366
    task check: :environment  do
Riyad Preukschas's avatar
Riyad Preukschas committed
367
      warn_user_is_not_gitlab
Ben Bodenmiller's avatar
Ben Bodenmiller committed
368
      start_checking "GitLab Shell"
Riyad Preukschas's avatar
Riyad Preukschas committed
369

370
      check_gitlab_shell
Riyad Preukschas's avatar
Riyad Preukschas committed
371
      check_repo_base_exists
372
      check_repo_base_is_not_symlink
Riyad Preukschas's avatar
Riyad Preukschas committed
373 374 375 376 377
      check_repo_base_user_and_group
      check_repo_base_permissions
      check_post_receive_hook_is_up_to_date
      check_repos_post_receive_hooks_is_link

Ben Bodenmiller's avatar
Ben Bodenmiller committed
378
      finished_checking "GitLab Shell"
Riyad Preukschas's avatar
Riyad Preukschas committed
379 380 381 382 383 384 385 386 387 388 389
    end


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


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

      hook_file = "post-receive"
390
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
391 392
      gitlab_shell_hook_file  = File.join(gitlab_shell_hooks_path, hook_file)
      gitlab_shell_ssh_user = Gitlab.config.gitlab_shell.ssh_user
Riyad Preukschas's avatar
Riyad Preukschas committed
393

394
      unless File.exists?(gitlab_shell_hook_file)
Riyad Preukschas's avatar
Riyad Preukschas committed
395
        puts "can't check because of previous errors".magenta
396 397 398
        return
      end

399
      puts "yes".green
Riyad Preukschas's avatar
Riyad Preukschas committed
400 401 402 403 404
    end

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

405
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
Riyad Preukschas's avatar
Riyad Preukschas committed
406 407 408 409 410 411 412

      if File.exists?(repo_base_path)
        puts "yes".green
      else
        puts "no".red
        puts "#{repo_base_path} is missing".red
        try_fixing_it(
Ben Bodenmiller's avatar
Ben Bodenmiller committed
413
          "This should have been created when setting up GitLab Shell.",
Riyad Preukschas's avatar
Riyad Preukschas committed
414
          "Make sure it's set correctly in config/gitlab.yml",
Ben Bodenmiller's avatar
Ben Bodenmiller committed
415
          "Make sure GitLab Shell is installed correctly."
Riyad Preukschas's avatar
Riyad Preukschas committed
416 417
        )
        for_more_information(
Ben Bodenmiller's avatar
Ben Bodenmiller committed
418
          see_installation_guide_section "GitLab Shell"
Riyad Preukschas's avatar
Riyad Preukschas committed
419
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
420
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
421 422 423
      end
    end

424 425 426
    def check_repo_base_is_not_symlink
      print "Repo base directory is a symlink? ... "

427
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
      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

Riyad Preukschas's avatar
Riyad Preukschas committed
444
    def check_repo_base_permissions
445
      print "Repo base access is drwxrws---? ... "
Riyad Preukschas's avatar
Riyad Preukschas committed
446

447
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
Riyad Preukschas's avatar
Riyad Preukschas committed
448 449
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
450 451 452
        return
      end

453
      if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
Riyad Preukschas's avatar
Riyad Preukschas committed
454 455 456 457
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
458
          "sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
459
          "sudo chmod -R ug-s #{repo_base_path}",
460
          "find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
Riyad Preukschas's avatar
Riyad Preukschas committed
461 462
        )
        for_more_information(
Ben Bodenmiller's avatar
Ben Bodenmiller committed
463
          see_installation_guide_section "GitLab Shell"
Riyad Preukschas's avatar
Riyad Preukschas committed
464
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
465
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
466 467 468 469
      end
    end

    def check_repo_base_user_and_group
470 471 472
      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}? ... "
Riyad Preukschas's avatar
Riyad Preukschas committed
473

474
      repo_base_path = Gitlab.config.gitlab_shell.repos_path
Riyad Preukschas's avatar
Riyad Preukschas committed
475 476
      unless File.exists?(repo_base_path)
        puts "can't check because of previous errors".magenta
477 478 479
        return
      end

480 481
      if File.stat(repo_base_path).uid == uid_for(gitlab_shell_ssh_user) &&
         File.stat(repo_base_path).gid == gid_for(gitlab_shell_owner_group)
Riyad Preukschas's avatar
Riyad Preukschas committed
482
        puts "yes".green
483
      else
Riyad Preukschas's avatar
Riyad Preukschas committed
484 485
        puts "no".red
        try_fixing_it(
486
          "sudo chown -R #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group} #{repo_base_path}"
Riyad Preukschas's avatar
Riyad Preukschas committed
487 488
        )
        for_more_information(
Ben Bodenmiller's avatar
Ben Bodenmiller committed
489
          see_installation_guide_section "GitLab Shell"
Riyad Preukschas's avatar
Riyad Preukschas committed
490
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
491
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
492 493 494 495 496 497 498
      end
    end

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

      hook_file = "post-receive"
499
      gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
500 501
      gitlab_shell_hook_file  = File.join(gitlab_shell_hooks_path, hook_file)
      gitlab_shell_ssh_user = Gitlab.config.gitlab_shell.ssh_user
Riyad Preukschas's avatar
Riyad Preukschas committed
502

503
      unless File.exists?(gitlab_shell_hook_file)
Riyad Preukschas's avatar
Riyad Preukschas committed
504 505 506
        puts "can't check because of previous errors".magenta
        return
      end
507

Riyad Preukschas's avatar
Riyad Preukschas committed
508 509 510 511 512
      unless Project.count > 0
        puts "can't check, you have no projects".magenta
        return
      end
      puts ""
513

Riyad Preukschas's avatar
Riyad Preukschas committed
514
      Project.find_each(batch_size: 100) do |project|
515
        print "#{project.name_with_namespace.yellow} ... "
516

Riyad Preukschas's avatar
Riyad Preukschas committed
517 518
        if project.empty_repo?
          puts "repository is empty".magenta
Riyad Preukschas's avatar
Riyad Preukschas committed
519
        else
Riyad Preukschas's avatar
Riyad Preukschas committed
520 521 522 523 524
          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(
525
              "sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
Riyad Preukschas's avatar
Riyad Preukschas committed
526 527
            )
            for_more_information(
Martin Bastien's avatar
Martin Bastien committed
528
              "#{gitlab_shell_user_home}/gitlab-shell/support/rewrite-hooks.sh"
Riyad Preukschas's avatar
Riyad Preukschas committed
529 530 531 532 533 534
            )
            fix_and_rerun
            next
          end

          if File.lstat(project_hook_file).symlink? &&
535
              File.realpath(project_hook_file) == File.realpath(gitlab_shell_hook_file)
Riyad Preukschas's avatar
Riyad Preukschas committed
536 537
            puts "ok".green
          else
Ben Bodenmiller's avatar
Ben Bodenmiller committed
538
            puts "not a link to GitLab Shell's hook".red
Riyad Preukschas's avatar
Riyad Preukschas committed
539
            try_fixing_it(
540
              "sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
Riyad Preukschas's avatar
Riyad Preukschas committed
541 542 543 544 545 546
            )
            for_more_information(
              "lib/support/rewrite-hooks.sh"
            )
            fix_and_rerun
          end
547 548
        end
      end
549
    end
Riyad Preukschas's avatar
Riyad Preukschas committed
550 551 552 553 554


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

555 556
    def gitlab_shell_user_home
      File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
Riyad Preukschas's avatar
Riyad Preukschas committed
557 558
    end

559
    def gitlab_shell_version
Martin Bastien's avatar
Martin Bastien committed
560
      gitlab_shell_version_file = "#{gitlab_shell_user_home}/gitlab-shell/VERSION"
561 562
      if File.readable?(gitlab_shell_version_file)
        File.read(gitlab_shell_version_file)
Riyad Preukschas's avatar
Riyad Preukschas committed
563 564 565
      end
    end

566 567
    def has_gitlab_shell3?
      gitlab_shell_version.try(:start_with?, "v3.")
Riyad Preukschas's avatar
Riyad Preukschas committed
568
    end
569
  end
570

Riyad Preukschas's avatar
Riyad Preukschas committed
571 572


Riyad Preukschas's avatar
Riyad Preukschas committed
573
  namespace :sidekiq do
574
    desc "GITLAB | Check the configuration of Sidekiq"
575
    task check: :environment  do
Riyad Preukschas's avatar
Riyad Preukschas committed
576
      warn_user_is_not_gitlab
Riyad Preukschas's avatar
Riyad Preukschas committed
577
      start_checking "Sidekiq"
Riyad Preukschas's avatar
Riyad Preukschas committed
578

Riyad Preukschas's avatar
Riyad Preukschas committed
579
      check_sidekiq_running
Riyad Preukschas's avatar
Riyad Preukschas committed
580

Riyad Preukschas's avatar
Riyad Preukschas committed
581
      finished_checking "Sidekiq"
Riyad Preukschas's avatar
Riyad Preukschas committed
582 583 584 585 586 587
    end


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

Riyad Preukschas's avatar
Riyad Preukschas committed
588
    def check_sidekiq_running
Riyad Preukschas's avatar
Riyad Preukschas committed
589 590
      print "Running? ... "

591
      if run_and_match("ps aux | grep -i sidekiq", /sidekiq \d+\.\d+\.\d+.+$/)
Riyad Preukschas's avatar
Riyad Preukschas committed
592 593 594 595
        puts "yes".green
      else
        puts "no".red
        try_fixing_it(
596
          sudo_gitlab("bundle exec rake sidekiq:start RAILS_ENV=production")
Riyad Preukschas's avatar
Riyad Preukschas committed
597 598 599
        )
        for_more_information(
          see_installation_guide_section("Install Init Script"),
600
          "see log/sidekiq.log for possible errors"
Riyad Preukschas's avatar
Riyad Preukschas committed
601
        )
Riyad Preukschas's avatar
Riyad Preukschas committed
602
        fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
603 604 605 606 607 608 609 610
      end
    end
  end


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

Riyad Preukschas's avatar
Riyad Preukschas committed
611
  def fix_and_rerun
Riyad Preukschas's avatar
Riyad Preukschas committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    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

638 639 640 641 642
  def sudo_gitlab(command)
    gitlab_user = Gitlab.config.gitlab.user
    "sudo -u #{gitlab_user} -H #{command}"
  end

Riyad Preukschas's avatar
Riyad Preukschas committed
643 644 645 646 647 648 649 650 651 652 653 654 655
  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
656 657

  def check_gitlab_shell
658 659
    required_version = Gitlab::VersionInfo.new(1, 4, 0)
    current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
660

661
    print "GitLab Shell version >= #{required_version} ? ... "
Sato Hiroyuki's avatar
Sato Hiroyuki committed
662
    if current_version.valid? && required_version <= current_version
663
      puts "OK (#{current_version})".green
664
    else
665
      puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
666 667
    end
  end
668 669

  def check_git_version
670
    required_version = Gitlab::VersionInfo.new(1, 7, 10)
671
    current_version = Gitlab::VersionInfo.parse(run("#{Gitlab.config.git.bin_path} --version"))
Sato Hiroyuki's avatar
Sato Hiroyuki committed
672

673
    puts "Your git bin path is \"#{Gitlab.config.git.bin_path}\""
Sato Hiroyuki's avatar
Sato Hiroyuki committed
674 675
    print "Git version >= #{required_version} ? ... "

Sato Hiroyuki's avatar
Sato Hiroyuki committed
676
    if current_version.valid? && required_version <= current_version
677
        puts "yes (#{current_version})".green
678 679 680
    else
      puts "no".red
      try_fixing_it(
Sato Hiroyuki's avatar
Sato Hiroyuki committed
681
        "Update your git to a version >= #{required_version} from #{current_version}"
682 683 684 685
      )
      fix_and_rerun
    end
  end
686
end