OSDN Git Service

Fix sidekiq chech and added script/check
[wvm/gitlab.git] / lib / tasks / gitlab / check.rake
1 namespace :gitlab do
2   desc "GITLAB | Check the configuration of GitLab and its environment"
3   task check: %w{gitlab:env:check
4                  gitlab:gitolite:check
5                  gitlab:resque:check
6                  gitlab:app:check}
7
8
9
10   namespace :app do
11     desc "GITLAB | Check the configuration of the GitLab Rails app"
12     task check: :environment  do
13       warn_user_is_not_gitlab
14       start_checking "GitLab"
15
16       check_database_config_exists
17       check_database_is_not_sqlite
18       check_migrations_are_up
19       check_gitlab_config_exists
20       check_gitlab_config_not_outdated
21       check_log_writable
22       check_tmp_writable
23       check_init_script_exists
24       check_init_script_up_to_date
25       check_satellites_exist
26
27       finished_checking "GitLab"
28     end
29
30
31     # Checks
32     ########################
33
34     def check_database_config_exists
35       print "Database config exists? ... "
36
37       database_config_file = Rails.root.join("config", "database.yml")
38
39       if File.exists?(database_config_file)
40         puts "yes".green
41       else
42         puts "no".red
43         try_fixing_it(
44           "Copy config/database.yml.<your db> to config/database.yml",
45           "Check that the information in config/database.yml is correct"
46         )
47         for_more_information(
48           see_database_guide,
49           "http://guides.rubyonrails.org/getting_started.html#configuring-a-database"
50         )
51         fix_and_rerun
52       end
53     end
54
55     def check_database_is_not_sqlite
56       print "Database is SQLite ... "
57
58       database_config_file = Rails.root.join("config", "database.yml")
59
60       unless File.read(database_config_file) =~ /adapter:\s+sqlite/
61         puts "no".green
62       else
63         puts "yes".red
64         for_more_information(
65           "https://github.com/gitlabhq/gitlabhq/wiki/Migrate-from-SQLite-to-MySQL",
66           see_database_guide
67         )
68         fix_and_rerun
69       end
70     end
71
72     def check_gitlab_config_exists
73       print "GitLab config exists? ... "
74
75       gitlab_config_file = Rails.root.join("config", "gitlab.yml")
76
77       if File.exists?(gitlab_config_file)
78         puts "yes".green
79       else
80         puts "no".red
81         try_fixing_it(
82           "Copy config/gitlab.yml.example to config/gitlab.yml",
83           "Update config/gitlab.yml to match your setup"
84         )
85         for_more_information(
86           see_installation_guide_section "GitLab"
87         )
88         fix_and_rerun
89       end
90     end
91
92     def check_gitlab_config_not_outdated
93       print "GitLab config outdated? ... "
94
95       gitlab_config_file = Rails.root.join("config", "gitlab.yml")
96       unless File.exists?(gitlab_config_file)
97         puts "can't check because of previous errors".magenta
98       end
99
100       # omniauth or ldap could have been deleted from the file
101       unless Gitlab.config['git_host']
102         puts "no".green
103       else
104         puts "yes".red
105         try_fixing_it(
106           "Backup your config/gitlab.yml",
107           "Copy config/gitlab.yml.example to config/gitlab.yml",
108           "Update config/gitlab.yml to match your setup"
109         )
110         for_more_information(
111           see_installation_guide_section "GitLab"
112         )
113         fix_and_rerun
114       end
115     end
116
117     def check_init_script_exists
118       print "Init script exists? ... "
119
120       script_path = "/etc/init.d/gitlab"
121
122       if File.exists?(script_path)
123         puts "yes".green
124       else
125         puts "no".red
126         try_fixing_it(
127           "Install the init script"
128         )
129         for_more_information(
130           see_installation_guide_section "Install Init Script"
131         )
132         fix_and_rerun
133       end
134     end
135
136     def check_init_script_up_to_date
137       print "Init script up-to-date? ... "
138
139       script_path = "/etc/init.d/gitlab"
140       unless File.exists?(script_path)
141         puts "can't check because of previous errors".magenta
142         return
143       end
144
145       recipe_content = `curl https://raw.github.com/gitlabhq/gitlab-recipes/master/init.d/gitlab 2>/dev/null`
146       script_content = File.read(script_path)
147
148       if recipe_content == script_content
149         puts "yes".green
150       else
151         puts "no".red
152         try_fixing_it(
153           "Redownload the init script"
154         )
155         for_more_information(
156           see_installation_guide_section "Install Init Script"
157         )
158         fix_and_rerun
159       end
160     end
161
162     def check_migrations_are_up
163       print "All migrations up? ... "
164
165       migration_status =  `bundle exec rake db:migrate:status`
166
167       unless migration_status =~ /down\s+\d{14}/
168         puts "yes".green
169       else
170         puts "no".red
171         try_fixing_it(
172           "sudo -u gitlab -H bundle exec rake db:migrate"
173         )
174         fix_and_rerun
175       end
176     end
177
178     def check_satellites_exist
179       print "Projects have satellites? ... "
180
181       unless Project.count > 0
182         puts "can't check, you have no projects".magenta
183         return
184       end
185       puts ""
186
187       Project.find_each(batch_size: 100) do |project|
188         print "#{project.name_with_namespace.yellow} ... "
189
190         if project.satellite.exists?
191           puts "yes".green
192         elsif project.empty_repo?
193           puts "can't create, repository is empty".magenta
194         else
195           puts "no".red
196           try_fixing_it(
197             "sudo -u gitlab -H bundle exec rake gitlab:satellites:create",
198             "If necessary, remove the tmp/repo_satellites directory ...",
199             "... and rerun the above command"
200           )
201           for_more_information(
202             "doc/raketasks/maintenance.md "
203           )
204           fix_and_rerun
205         end
206       end
207     end
208
209     def check_log_writable
210       print "Log directory writable? ... "
211
212       log_path = Rails.root.join("log")
213
214       if File.writable?(log_path)
215         puts "yes".green
216       else
217         puts "no".red
218         try_fixing_it(
219           "sudo chown -R gitlab #{log_path}",
220           "sudo chmod -R rwX #{log_path}"
221         )
222         for_more_information(
223           see_installation_guide_section "GitLab"
224         )
225         fix_and_rerun
226       end
227     end
228
229     def check_tmp_writable
230       print "Tmp directory writable? ... "
231
232       tmp_path = Rails.root.join("tmp")
233
234       if File.writable?(tmp_path)
235         puts "yes".green
236       else
237         puts "no".red
238         try_fixing_it(
239           "sudo chown -R gitlab #{tmp_path}",
240           "sudo chmod -R rwX #{tmp_path}"
241         )
242         for_more_information(
243           see_installation_guide_section "GitLab"
244         )
245         fix_and_rerun
246       end
247     end
248   end
249
250
251
252   namespace :env do
253     desc "GITLAB | Check the configuration of the environment"
254     task check: :environment  do
255       warn_user_is_not_gitlab
256       start_checking "Environment"
257
258       check_gitlab_in_git_group
259       check_issue_1059_shell_profile_error
260       check_gitlab_git_config
261       check_python2_exists
262       check_python2_version
263
264       finished_checking "Environment"
265     end
266
267
268     # Checks
269     ########################
270
271     def check_gitlab_git_config
272       print "Git configured for gitlab user? ... "
273
274       options = {
275         "user.name"  => "GitLab",
276         "user.email" => Gitlab.config.gitlab.email_from
277       }
278       correct_options = options.map do |name, value|
279         run("git config --global --get #{name}").try(:squish) == value
280       end
281
282       if correct_options.all?
283         puts "yes".green
284       else
285         puts "no".red
286         try_fixing_it(
287           "sudo -u gitlab -H git config --global user.name  \"#{options["user.name"]}\"",
288           "sudo -u gitlab -H git config --global user.email \"#{options["user.email"]}\""
289         )
290         for_more_information(
291           see_installation_guide_section "GitLab"
292         )
293         fix_and_rerun
294       end
295     end
296
297     def check_gitlab_in_git_group
298       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
299       print "gitlab user is in #{gitolite_ssh_user} group? ... "
300
301       if run_and_match("id -rnG", /\Wgit\W/)
302         puts "yes".green
303       else
304         puts "no".red
305         try_fixing_it(
306           "sudo usermod -a -G #{gitolite_ssh_user} gitlab"
307         )
308         for_more_information(
309           see_installation_guide_section "System Users"
310         )
311         fix_and_rerun
312       end
313     end
314
315     # see https://github.com/gitlabhq/gitlabhq/issues/1059
316     def check_issue_1059_shell_profile_error
317       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
318       print "Has no \"-e\" in ~#{gitolite_ssh_user}/.profile ... "
319
320       profile_file = File.join(gitolite_home, ".profile")
321
322       unless File.read(profile_file) =~ /^-e PATH/
323         puts "yes".green
324       else
325         puts "no".red
326         try_fixing_it(
327           "Open #{profile_file}",
328           "Find the line starting with \"-e PATH\"",
329           "Remove \"-e \" so the line starts with PATH"
330         )
331         for_more_information(
332           see_installation_guide_section("Gitolite"),
333           "https://github.com/gitlabhq/gitlabhq/issues/1059"
334         )
335         fix_and_rerun
336       end
337     end
338
339     def check_python2_exists
340       print "Has python2? ... "
341
342       # Python prints its version to STDERR
343       # so we can't just use run("python2 --version")
344       if run_and_match("which python2", /python2$/)
345         puts "yes".green
346       else
347         puts "no".red
348         try_fixing_it(
349           "Make sure you have Python 2.5+ installed",
350           "Link it to python2"
351         )
352         for_more_information(
353           see_installation_guide_section "Packages / Dependencies"
354         )
355         fix_and_rerun
356       end
357     end
358
359     def check_python2_version
360       print "python2 is supported version? ... "
361
362       # Python prints its version to STDERR
363       # so we can't just use run("python2 --version")
364
365       unless run_and_match("which python2", /python2$/)
366         puts "can't check because of previous errors".magenta
367         return
368       end
369
370       if `python2 --version 2>&1` =~ /2\.[567]\.\d/
371         puts "yes".green
372       else
373         puts "no".red
374         try_fixing_it(
375           "Make sure you have Python 2.5+ installed",
376           "Link it to python2"
377         )
378         for_more_information(
379           see_installation_guide_section "Packages / Dependencies"
380         )
381         fix_and_rerun
382       end
383     end
384   end
385
386
387
388   namespace :gitolite do
389     desc "GITLAB | Check the configuration of Gitolite"
390     task check: :environment  do
391       warn_user_is_not_gitlab
392       start_checking "Gitolite"
393
394       check_gitolite_is_up_to_date
395       check_gitoliterc_repo_umask
396       check_gitoliterc_git_config_keys
397       check_dot_gitolite_exists
398       check_dot_gitolite_user_and_group
399       check_dot_gitolite_permissions
400       check_repo_base_exists
401       check_repo_base_is_not_symlink
402       check_repo_base_user_and_group
403       check_repo_base_permissions
404       check_can_clone_gitolite_admin
405       check_can_commit_to_gitolite_admin
406       check_post_receive_hook_exists
407       check_post_receive_hook_is_up_to_date
408       check_repos_post_receive_hooks_is_link
409       check_repos_git_config
410
411       finished_checking "Gitolite"
412     end
413
414
415     # Checks
416     ########################
417
418     def check_can_clone_gitolite_admin
419       print "Can clone gitolite-admin? ... "
420
421       test_path = "/tmp/gitlab_gitolite_admin_test"
422       FileUtils.rm_rf(test_path)
423       `git clone -q #{Gitlab.config.gitolite.admin_uri} #{test_path}`
424       raise unless $?.success?
425
426       puts "yes".green
427     rescue
428       puts "no".red
429       try_fixing_it(
430         "Make sure the \"admin_uri\" is set correctly in config/gitlab.yml",
431         "Try cloning it yourself with:",
432         "  git clone -q #{Gitlab.config.gitolite.admin_uri} /tmp/gitolite-admin",
433         "Make sure Gitolite is installed correctly."
434       )
435       for_more_information(
436         see_installation_guide_section "Gitolite"
437       )
438       fix_and_rerun
439     end
440
441     # assumes #check_can_clone_gitolite_admin has been run before
442     def check_can_commit_to_gitolite_admin
443       print "Can commit to gitolite-admin? ... "
444
445       test_path = "/tmp/gitlab_gitolite_admin_test"
446       unless File.exists?(test_path)
447         puts "can't check because of previous errors".magenta
448         return
449       end
450
451       Dir.chdir(test_path) do
452         `touch foo && git add foo && git commit -qm foo`
453         raise unless $?.success?
454       end
455
456       puts "yes".green
457     rescue
458       puts "no".red
459       try_fixing_it(
460         "Try committing to it yourself with:",
461         "  git clone -q #{Gitlab.config.gitolite.admin_uri} /tmp/gitolite-admin",
462         "  touch foo",
463         "  git add foo",
464         "  git commit -m \"foo\"",
465         "Make sure Gitolite is installed correctly."
466       )
467       for_more_information(
468         see_installation_guide_section "Gitolite"
469       )
470       fix_and_rerun
471     ensure
472       FileUtils.rm_rf("/tmp/gitolite_gitlab_test")
473     end
474
475     def check_dot_gitolite_exists
476       print "Config directory exists? ... "
477
478       gitolite_config_path = File.join(gitolite_home, ".gitolite")
479
480       if File.directory?(gitolite_config_path)
481         puts "yes".green
482       else
483         puts "no".red
484         puts "#{gitolite_config_path} is missing".red
485         try_fixing_it(
486           "This should have been created when setting up Gitolite.",
487           "Make sure Gitolite is installed correctly."
488         )
489         for_more_information(
490           see_installation_guide_section "Gitolite"
491         )
492         fix_and_rerun
493       end
494     end
495
496     def check_dot_gitolite_permissions
497       print "Config directory access is drwxr-x---? ... "
498
499       gitolite_config_path = File.join(gitolite_home, ".gitolite")
500       unless File.exists?(gitolite_config_path)
501         puts "can't check because of previous errors".magenta
502         return
503       end
504
505       if `stat --printf %a #{gitolite_config_path}` == "750"
506         puts "yes".green
507       else
508         puts "no".red
509         try_fixing_it(
510           "sudo chmod 750 #{gitolite_config_path}"
511         )
512         for_more_information(
513           see_installation_guide_section "Gitolite"
514         )
515         fix_and_rerun
516       end
517     end
518
519     def check_dot_gitolite_user_and_group
520       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
521       print "Config directory owned by #{gitolite_ssh_user}:#{gitolite_ssh_user} ... "
522
523       gitolite_config_path = File.join(gitolite_home, ".gitolite")
524       unless File.exists?(gitolite_config_path)
525         puts "can't check because of previous errors".magenta
526         return
527       end
528
529       if `stat --printf %U #{gitolite_config_path}` == gitolite_ssh_user && # user
530          `stat --printf %G #{gitolite_config_path}` == gitolite_ssh_user #group
531         puts "yes".green
532       else
533         puts "no".red
534         puts "#{gitolite_config_path} is not owned by #{gitolite_ssh_user}".red
535         try_fixing_it(
536           "sudo chown -R #{gitolite_ssh_user}:#{gitolite_ssh_user} #{gitolite_config_path}"
537         )
538         for_more_information(
539           see_installation_guide_section "Gitolite"
540         )
541         fix_and_rerun
542       end
543     end
544
545     def check_gitolite_is_up_to_date
546       print "Using recommended version ... "
547       if gitolite_version.try(:start_with?, "v3.2")
548         puts "yes".green
549       else
550         puts "no".red
551         try_fixing_it(
552           "We strongly recommend using the version pointed out in the installation guide."
553         )
554         for_more_information(
555           see_installation_guide_section "Gitolite"
556         )
557         # this is not a "hard" failure
558       end
559     end
560
561     def check_gitoliterc_git_config_keys
562       gitoliterc_path = File.join(gitolite_home, ".gitolite.rc")
563
564       print "Allow all Git config keys in .gitolite.rc ... "
565       option_name = if has_gitolite3?
566                       # see https://github.com/sitaramc/gitolite/blob/v3.04/src/lib/Gitolite/Rc.pm#L329
567                       "GIT_CONFIG_KEYS"
568                     else
569                       # assume older version
570                       # see https://github.com/sitaramc/gitolite/blob/v2.3/conf/example.gitolite.rc#L49
571                       "$GL_GITCONFIG_KEYS"
572                     end
573       option_value = ".*"
574       if open(gitoliterc_path).grep(/#{option_name}\s*=[>]?\s*["']#{option_value}["']/).any?
575         puts "yes".green
576       else
577         puts "no".red
578         try_fixing_it(
579           "Open #{gitoliterc_path}",
580           "Find the \"#{option_name}\" option",
581           "Change its value to \".*\""
582         )
583         for_more_information(
584           see_installation_guide_section "Gitolite"
585         )
586         fix_and_rerun
587       end
588     end
589
590     def check_gitoliterc_repo_umask
591       gitoliterc_path = File.join(gitolite_home, ".gitolite.rc")
592
593       print "Repo umask is 0007 in .gitolite.rc? ... "
594       option_name = if has_gitolite3?
595                       # see https://github.com/sitaramc/gitolite/blob/v3.04/src/lib/Gitolite/Rc.pm#L328
596                       "UMASK"
597                     else
598                       # assume older version
599                       # see https://github.com/sitaramc/gitolite/blob/v2.3/conf/example.gitolite.rc#L32
600                       "$REPO_UMASK"
601                     end
602       option_value = "0007"
603       if open(gitoliterc_path).grep(/#{option_name}\s*=[>]?\s*#{option_value}/).any?
604         puts "yes".green
605       else
606         puts "no".red
607         try_fixing_it(
608           "Open #{gitoliterc_path}",
609           "Find the \"#{option_name}\" option",
610           "Change its value to \"0007\""
611         )
612         for_more_information(
613           see_installation_guide_section "Gitolite"
614         )
615         fix_and_rerun
616       end
617     end
618
619     def check_post_receive_hook_exists
620       print "post-receive hook exists? ... "
621
622       hook_file = "post-receive"
623       gitolite_hooks_path = File.join(Gitlab.config.gitolite.hooks_path, "common")
624       gitolite_hook_file = File.join(gitolite_hooks_path, hook_file)
625       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
626
627       gitlab_hook_file = Rails.root.join.join("lib", "hooks", hook_file)
628
629       if File.exists?(gitolite_hook_file)
630         puts "yes".green
631       else
632         puts "no".red
633         try_fixing_it(
634           "sudo -u #{gitolite_ssh_user} cp #{gitlab_hook_file} #{gitolite_hook_file}"
635         )
636         for_more_information(
637           see_installation_guide_section "Setup GitLab Hooks"
638         )
639         fix_and_rerun
640       end
641     end
642
643     def check_post_receive_hook_is_up_to_date
644       print "post-receive hook up-to-date? ... "
645
646       hook_file = "post-receive"
647       gitolite_hooks_path = File.join(Gitlab.config.gitolite.hooks_path, "common")
648       gitolite_hook_file  = File.join(gitolite_hooks_path, hook_file)
649       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
650
651       unless File.exists?(gitolite_hook_file)
652         puts "can't check because of previous errors".magenta
653         return
654       end
655
656       gitolite_hook_content = File.read(gitolite_hook_file)
657       gitlab_hook_file = Rails.root.join.join("lib", "hooks", hook_file)
658       gitlab_hook_content = File.read(gitlab_hook_file)
659
660       if gitolite_hook_content == gitlab_hook_content
661         puts "yes".green
662       else
663         puts "no".red
664         try_fixing_it(
665           "sudo -u #{gitolite_ssh_user} cp #{gitlab_hook_file} #{gitolite_hook_file}"
666         )
667         for_more_information(
668           see_installation_guide_section "Setup GitLab Hooks"
669         )
670         fix_and_rerun
671       end
672     end
673
674     def check_repo_base_exists
675       print "Repo base directory exists? ... "
676
677       repo_base_path = Gitlab.config.gitolite.repos_path
678
679       if File.exists?(repo_base_path)
680         puts "yes".green
681       else
682         puts "no".red
683         puts "#{repo_base_path} is missing".red
684         try_fixing_it(
685           "This should have been created when setting up Gitolite.",
686           "Make sure it's set correctly in config/gitlab.yml",
687           "Make sure Gitolite is installed correctly."
688         )
689         for_more_information(
690           see_installation_guide_section "Gitolite"
691         )
692         fix_and_rerun
693       end
694     end
695
696     def check_repo_base_is_not_symlink
697       print "Repo base directory is a symlink? ... "
698
699       repo_base_path = Gitlab.config.gitolite.repos_path
700       unless File.exists?(repo_base_path)
701         puts "can't check because of previous errors".magenta
702         return
703       end
704
705       unless File.symlink?(repo_base_path)
706         puts "no".green
707       else
708         puts "yes".red
709         try_fixing_it(
710           "Make sure it's set to the real directory in config/gitlab.yml"
711         )
712         fix_and_rerun
713       end
714     end
715
716     def check_repo_base_permissions
717       print "Repo base access is drwsrws---? ... "
718
719       repo_base_path = Gitlab.config.gitolite.repos_path
720       unless File.exists?(repo_base_path)
721         puts "can't check because of previous errors".magenta
722         return
723       end
724
725       if `stat --printf %a #{repo_base_path}` == "6770"
726         puts "yes".green
727       else
728         puts "no".red
729         puts "#{repo_base_path} is not writable".red
730         try_fixing_it(
731           "sudo chmod -R ug+rwXs,o-rwx #{repo_base_path}"
732         )
733         for_more_information(
734           see_installation_guide_section "Gitolite"
735         )
736         fix_and_rerun
737       end
738     end
739
740     def check_repo_base_user_and_group
741       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
742       print "Repo base owned by #{gitolite_ssh_user}:#{gitolite_ssh_user}? ... "
743
744       repo_base_path = Gitlab.config.gitolite.repos_path
745       unless File.exists?(repo_base_path)
746         puts "can't check because of previous errors".magenta
747         return
748       end
749
750       if `stat --printf %U #{repo_base_path}` == gitolite_ssh_user && # user
751          `stat --printf %G #{repo_base_path}` == gitolite_ssh_user #group
752         puts "yes".green
753       else
754         puts "no".red
755         puts "#{repo_base_path} is not owned by #{gitolite_ssh_user}".red
756         try_fixing_it(
757           "sudo chown -R #{gitolite_ssh_user}:#{gitolite_ssh_user} #{repo_base_path}"
758         )
759         for_more_information(
760           see_installation_guide_section "Gitolite"
761         )
762         fix_and_rerun
763       end
764     end
765
766     def check_repos_git_config
767       print "Git config in repos: ... "
768
769       unless Project.count > 0
770         puts "can't check, you have no projects".magenta
771         return
772       end
773       puts ""
774
775       options = {
776         "core.sharedRepository" => "0660",
777       }
778
779       Project.find_each(batch_size: 100) do |project|
780         print "#{project.name_with_namespace.yellow} ... "
781
782         correct_options = options.map do |name, value|
783           run("git --git-dir=\"#{project.repository.path_to_repo}\" config --get #{name}").try(:chomp) == value
784         end
785
786         if correct_options.all?
787           puts "ok".green
788         else
789           puts "wrong or missing".red
790           try_fixing_it(
791             "sudo -u gitlab -H bundle exec rake gitlab:gitolite:update_repos"
792           )
793           for_more_information(
794             "doc/raketasks/maintenance.md"
795           )
796           fix_and_rerun
797         end
798       end
799     end
800
801     def check_repos_post_receive_hooks_is_link
802       print "post-receive hooks in repos are links: ... "
803
804       hook_file = "post-receive"
805       gitolite_hooks_path = File.join(Gitlab.config.gitolite.hooks_path, "common")
806       gitolite_hook_file  = File.join(gitolite_hooks_path, hook_file)
807       gitolite_ssh_user = Gitlab.config.gitolite.ssh_user
808
809       unless File.exists?(gitolite_hook_file)
810         puts "can't check because of previous errors".magenta
811         return
812       end
813
814       unless Project.count > 0
815         puts "can't check, you have no projects".magenta
816         return
817       end
818       puts ""
819
820       Project.find_each(batch_size: 100) do |project|
821         print "#{project.name_with_namespace.yellow} ... "
822         project_hook_file = File.join(project.repository.path_to_repo, "hooks", hook_file)
823
824         unless File.exists?(project_hook_file)
825           puts "missing".red
826           try_fixing_it(
827             "sudo -u #{gitolite_ssh_user} ln -sf #{gitolite_hook_file} #{project_hook_file}"
828           )
829           for_more_information(
830             "lib/support/rewrite-hooks.sh"
831           )
832           fix_and_rerun
833           next
834         end
835
836         if run_and_match("stat --format %N #{project_hook_file}", /#{hook_file}.+->.+#{gitolite_hook_file}/)
837           puts "ok".green
838         else
839           puts "not a link to Gitolite's hook".red
840           try_fixing_it(
841             "sudo -u #{gitolite_ssh_user} ln -sf #{gitolite_hook_file} #{project_hook_file}"
842           )
843           for_more_information(
844             "lib/support/rewrite-hooks.sh"
845           )
846           fix_and_rerun
847         end
848       end
849     end
850
851
852     # Helper methods
853     ########################
854
855     def gitolite_home
856       File.expand_path("~#{Gitlab.config.gitolite.ssh_user}")
857     end
858
859     def gitolite_version
860       gitolite_version_file = "#{gitolite_home}/gitolite/src/VERSION"
861       if File.readable?(gitolite_version_file)
862         File.read(gitolite_version_file)
863       end
864     end
865
866     def has_gitolite3?
867       gitolite_version.try(:start_with?, "v3.")
868     end
869   end
870
871
872
873   namespace :resque do
874     desc "GITLAB | Check the configuration of Sidekiq"
875     task check: :environment  do
876       warn_user_is_not_gitlab
877       start_checking "Resque"
878
879       check_resque_running
880
881       finished_checking "Resque"
882     end
883
884
885     # Checks
886     ########################
887
888     def check_resque_running
889       print "Running? ... "
890
891       if run_and_match("ps aux | grep -i sidekiq", /sidekiq \d\.\d\.\d.+$/)
892         puts "yes".green
893       else
894         puts "no".red
895         try_fixing_it(
896           "sudo service gitlab restart",
897           "or",
898           "sudo /etc/init.d/gitlab restart"
899         )
900         for_more_information(
901           see_installation_guide_section("Install Init Script"),
902           "see log/sidekiq.log for possible errors"
903         )
904         fix_and_rerun
905       end
906     end
907   end
908
909
910   # Helper methods
911   ##########################
912
913   def fix_and_rerun
914     puts "  Please #{"fix the error above"} and rerun the checks.".red
915   end
916
917   def for_more_information(*sources)
918     sources = sources.shift if sources.first.is_a?(Array)
919
920     puts "  For more information see:".blue
921     sources.each do |source|
922       puts "  #{source}"
923     end
924   end
925
926   def finished_checking(component)
927     puts ""
928     puts "Checking #{component.yellow} ... #{"Finished".green}"
929     puts ""
930   end
931
932   def see_database_guide
933     "doc/install/databases.md"
934   end
935
936   def see_installation_guide_section(section)
937     "doc/install/installation.md in section \"#{section}\""
938   end
939
940   def start_checking(component)
941     puts "Checking #{component.yellow} ..."
942     puts ""
943   end
944
945   def try_fixing_it(*steps)
946     steps = steps.shift if steps.first.is_a?(Array)
947
948     puts "  Try fixing it:".blue
949     steps.each do |step|
950       puts "  #{step}"
951     end
952   end
953 end