gitlab-org--gitlab-foss/lib/tasks/gitlab/check.rake

689 lines
18 KiB
Ruby
Raw Normal View History

2012-04-03 18:02:54 -04:00
namespace :gitlab do
2012-12-10 08:50:55 -05:00
desc "GITLAB | Check the configuration of GitLab and its environment"
task check: %w{gitlab:env:check
2013-02-11 12:16:59 -05:00
gitlab:gitlab_shell:check
2013-01-09 18:08:15 -05:00
gitlab:sidekiq:check
2012-12-13 06:11:41 -05:00
gitlab:app:check}
2012-12-10 08:50:55 -05:00
2012-04-03 18:02:54 -04:00
namespace :app do
2012-12-10 08:50:55 -05:00
desc "GITLAB | Check the configuration of the GitLab Rails app"
task check: :environment do
2012-12-13 06:11:41 -05:00
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
check_redis_version
2013-05-06 08:08:53 -04:00
check_git_version
2012-12-13 06:11:41 -05:00
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"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_database_is_not_sqlite
2012-12-23 14:47:55 -05:00
print "Database is SQLite ... "
2012-12-13 06:11:41 -05:00
database_config_file = Rails.root.join("config", "database.yml")
unless File.read(database_config_file) =~ /adapter:\s+sqlite/
2012-12-23 14:47:55 -05:00
puts "no".green
2012-12-13 06:11:41 -05:00
else
2012-12-23 14:47:55 -05:00
puts "yes".red
2012-12-13 06:11:41 -05:00
for_more_information(
"https://github.com/gitlabhq/gitlabhq/wiki/Migrate-from-SQLite-to-MySQL",
see_database_guide
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
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"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_gitlab_config_not_outdated
2012-12-20 10:51:14 -05:00
print "GitLab config outdated? ... "
2012-12-13 06:11:41 -05:00
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
2013-01-07 13:47:53 -05:00
unless Gitlab.config['git_host']
2012-12-20 10:51:14 -05:00
puts "no".green
2012-12-13 06:11:41 -05:00
else
2012-12-20 10:51:14 -05:00
puts "yes".red
2012-12-13 06:11:41 -05:00
try_fixing_it(
2012-12-20 10:51:14 -05:00
"Backup your config/gitlab.yml",
2012-12-13 06:11:41 -05:00
"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"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
2012-04-03 18:02:54 -04:00
2012-12-13 06:11:41 -05:00
def check_init_script_exists
print "Init script exists? ... "
script_path = "/etc/init.d/gitlab"
if File.exists?(script_path)
puts "yes".green
2012-09-26 07:18:10 -04:00
else
2012-12-13 06:11:41 -05:00
puts "no".red
try_fixing_it(
"Install the init script"
)
for_more_information(
see_installation_guide_section "Install Init Script"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_init_script_up_to_date
print "Init script up-to-date? ... "
recipe_path = Rails.root.join("lib/support/init.d/", "gitlab")
2012-12-13 06:11:41 -05:00
script_path = "/etc/init.d/gitlab"
2012-12-13 06:11:41 -05:00
unless File.exists?(script_path)
puts "can't check because of previous errors".magenta
2012-04-03 18:02:54 -04:00
return
end
recipe_content = File.read(recipe_path)
2012-12-13 06:11:41 -05:00
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"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
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
2012-04-03 18:02:54 -04:00
else
2012-12-13 06:11:41 -05:00
puts "no".red
try_fixing_it(
sudo_gitlab("bundle exec rake db:migrate RAILS_ENV=production")
2012-12-13 06:11:41 -05:00
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_satellites_exist
print "Projects have satellites? ... "
unless Project.count > 0
puts "can't check, you have no projects".magenta
2012-04-03 18:02:54 -04:00
return
end
2012-12-13 06:11:41 -05:00
puts ""
Project.find_each(batch_size: 100) do |project|
print "#{project.name_with_namespace.yellow} ... "
2012-12-13 06:11:41 -05:00
if project.satellite.exists?
puts "yes".green
elsif project.empty_repo?
puts "can't create, repository is empty".magenta
2012-12-13 06:11:41 -05:00
else
puts "no".red
try_fixing_it(
sudo_gitlab("bundle exec rake gitlab:satellites:create RAILS_ENV=production"),
"If necessary, remove the tmp/repo_satellites directory ...",
"... and rerun the above command"
2012-12-13 06:11:41 -05:00
)
for_more_information(
"doc/raketasks/maintenance.md "
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
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}",
2013-05-27 15:30:54 -04:00
"sudo chmod -R u+rwX #{log_path}"
2012-12-13 06:11:41 -05:00
)
for_more_information(
see_installation_guide_section "GitLab"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
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}",
2013-05-27 15:30:54 -04:00
"sudo chmod -R u+rwX #{tmp_path}"
2012-12-13 06:11:41 -05:00
)
for_more_information(
see_installation_guide_section "GitLab"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
2012-12-10 08:50:55 -05:00
end
2013-04-20 08:56:13 -04:00
def check_redis_version
2013-04-20 08:56:13 -04:00
print "Redis version >= 2.0.0? ... "
if run_and_match("redis-cli --version", /redis-cli 2.\d.\d/)
puts "yes".green
2013-04-20 08:56:13 -04:00
else
puts "no".red
try_fixing_it(
2013-04-20 08:56:13 -04:00
"Update your redis server to a version >= 2.0.0"
)
for_more_information(
"gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq"
)
fix_and_rerun
end
2013-04-20 08:56:13 -04:00
end
2012-12-10 08:50:55 -05:00
end
2012-12-13 06:11:41 -05:00
2012-12-10 08:50:55 -05:00
namespace :env do
desc "GITLAB | Check the configuration of the environment"
task check: :environment do
2012-12-13 06:11:41 -05:00
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
gitlab_user = Gitlab.config.gitlab.user
print "Git configured for #{gitlab_user} user? ... "
2012-12-13 06:11:41 -05:00
options = {
"user.name" => "GitLab",
"user.email" => Gitlab.config.gitlab.email_from
2012-12-13 06:11:41 -05:00
}
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(
sudo_gitlab("git config --global user.name \"#{options["user.name"]}\""),
sudo_gitlab("git config --global user.email \"#{options["user.email"]}\"")
2012-12-13 06:11:41 -05:00
)
for_more_information(
see_installation_guide_section "GitLab"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
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"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
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"
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
2012-12-10 08:50:55 -05:00
end
end
2012-12-13 06:11:41 -05:00
2013-02-11 12:16:59 -05:00
namespace :gitlab_shell do
2013-05-19 05:13:39 -04:00
desc "GITLAB | Check the configuration of GitLab Shell"
2012-12-10 08:50:55 -05:00
task check: :environment do
2012-12-13 06:11:41 -05:00
warn_user_is_not_gitlab
2013-05-19 05:13:39 -04:00
start_checking "GitLab Shell"
2012-12-13 06:11:41 -05:00
check_gitlab_shell
2012-12-13 06:11:41 -05:00
check_repo_base_exists
check_repo_base_is_not_symlink
2012-12-13 06:11:41 -05:00
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
2013-05-19 05:13:39 -04:00
finished_checking "GitLab Shell"
2012-12-13 06:11:41 -05:00
end
# Checks
########################
def check_post_receive_hook_is_up_to_date
print "post-receive hook up-to-date? ... "
hook_file = "post-receive"
gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
2013-02-11 12:16:59 -05:00
gitlab_shell_hook_file = File.join(gitlab_shell_hooks_path, hook_file)
gitlab_shell_ssh_user = Gitlab.config.gitlab_shell.ssh_user
2012-12-13 06:11:41 -05:00
2013-02-11 12:16:59 -05:00
unless File.exists?(gitlab_shell_hook_file)
2012-12-13 06:11:41 -05:00
puts "can't check because of previous errors".magenta
2012-04-03 18:02:54 -04:00
return
end
puts "yes".green
2012-12-13 06:11:41 -05:00
end
def check_repo_base_exists
print "Repo base directory exists? ... "
2013-02-11 12:16:59 -05:00
repo_base_path = Gitlab.config.gitlab_shell.repos_path
2012-12-13 06:11:41 -05:00
if File.exists?(repo_base_path)
puts "yes".green
else
puts "no".red
puts "#{repo_base_path} is missing".red
try_fixing_it(
2013-05-19 05:13:39 -04:00
"This should have been created when setting up GitLab Shell.",
2012-12-13 06:11:41 -05:00
"Make sure it's set correctly in config/gitlab.yml",
2013-05-19 05:13:39 -04:00
"Make sure GitLab Shell is installed correctly."
2012-12-13 06:11:41 -05:00
)
for_more_information(
2013-05-19 05:13:39 -04:00
see_installation_guide_section "GitLab Shell"
2012-12-13 06:11:41 -05:00
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_repo_base_is_not_symlink
print "Repo base directory is a symlink? ... "
2013-02-11 12:16:59 -05:00
repo_base_path = Gitlab.config.gitlab_shell.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
2012-12-13 06:11:41 -05:00
def check_repo_base_permissions
print "Repo base access is drwxrws---? ... "
2012-12-13 06:11:41 -05:00
2013-02-11 12:16:59 -05:00
repo_base_path = Gitlab.config.gitlab_shell.repos_path
2012-12-13 06:11:41 -05:00
unless File.exists?(repo_base_path)
puts "can't check because of previous errors".magenta
2012-04-03 18:02:54 -04:00
return
end
if File.stat(repo_base_path).mode.to_s(8).ends_with?("2770")
2012-12-13 06:11:41 -05:00
puts "yes".green
else
puts "no".red
try_fixing_it(
"sudo chmod -R ug+rwX,o-rwx #{repo_base_path}",
"sudo chmod -R ug-s #{repo_base_path}",
"find #{repo_base_path} -type d -print0 | sudo xargs -0 chmod g+s"
2012-12-13 06:11:41 -05:00
)
for_more_information(
2013-05-19 05:13:39 -04:00
see_installation_guide_section "GitLab Shell"
2012-12-13 06:11:41 -05:00
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_repo_base_user_and_group
2013-02-11 12:16:59 -05:00
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}? ... "
2012-12-13 06:11:41 -05:00
2013-02-11 12:16:59 -05:00
repo_base_path = Gitlab.config.gitlab_shell.repos_path
2012-12-13 06:11:41 -05:00
unless File.exists?(repo_base_path)
puts "can't check because of previous errors".magenta
return
end
2013-02-11 12:16:59 -05:00
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)
2012-12-13 06:11:41 -05:00
puts "yes".green
2012-04-03 18:02:54 -04:00
else
2012-12-13 06:11:41 -05:00
puts "no".red
try_fixing_it(
2013-02-11 12:16:59 -05:00
"sudo chown -R #{gitlab_shell_ssh_user}:#{gitlab_shell_owner_group} #{repo_base_path}"
2012-12-13 06:11:41 -05:00
)
for_more_information(
2013-05-19 05:13:39 -04:00
see_installation_guide_section "GitLab Shell"
2012-12-13 06:11:41 -05:00
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
def check_repos_post_receive_hooks_is_link
print "post-receive hooks in repos are links: ... "
hook_file = "post-receive"
gitlab_shell_hooks_path = Gitlab.config.gitlab_shell.hooks_path
2013-02-11 12:16:59 -05:00
gitlab_shell_hook_file = File.join(gitlab_shell_hooks_path, hook_file)
gitlab_shell_ssh_user = Gitlab.config.gitlab_shell.ssh_user
2012-12-13 06:11:41 -05:00
2013-02-11 12:16:59 -05:00
unless File.exists?(gitlab_shell_hook_file)
2012-12-13 06:11:41 -05:00
puts "can't check because of previous errors".magenta
return
end
2012-12-13 06:11:41 -05:00
unless Project.count > 0
puts "can't check, you have no projects".magenta
return
end
puts ""
2012-12-13 06:11:41 -05:00
Project.find_each(batch_size: 100) do |project|
print "#{project.name_with_namespace.yellow} ... "
2013-02-01 15:18:09 -05:00
if project.empty_repo?
puts "repository is empty".magenta
2012-12-13 06:11:41 -05:00
else
2013-02-01 15:18:09 -05:00
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(
2013-02-11 12:16:59 -05:00
"sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
2013-02-01 15:18:09 -05:00
)
for_more_information(
2013-02-12 12:49:11 -05:00
"#{gitlab_shell_user_home}/gitlab-shell/support/rewrite-hooks.sh"
2013-02-01 15:18:09 -05:00
)
fix_and_rerun
next
end
if File.lstat(project_hook_file).symlink? &&
2013-02-11 12:16:59 -05:00
File.realpath(project_hook_file) == File.realpath(gitlab_shell_hook_file)
2013-02-01 15:18:09 -05:00
puts "ok".green
else
2013-05-19 05:13:39 -04:00
puts "not a link to GitLab Shell's hook".red
2013-02-01 15:18:09 -05:00
try_fixing_it(
2013-02-11 12:16:59 -05:00
"sudo -u #{gitlab_shell_ssh_user} ln -sf #{gitlab_shell_hook_file} #{project_hook_file}"
2013-02-01 15:18:09 -05:00
)
for_more_information(
"lib/support/rewrite-hooks.sh"
)
fix_and_rerun
end
end
end
2012-12-10 08:50:55 -05:00
end
2012-12-13 06:11:41 -05:00
# Helper methods
########################
2013-02-11 12:16:59 -05:00
def gitlab_shell_user_home
File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
2012-12-13 06:11:41 -05:00
end
2013-02-11 12:16:59 -05:00
def gitlab_shell_version
2013-02-12 12:49:11 -05:00
gitlab_shell_version_file = "#{gitlab_shell_user_home}/gitlab-shell/VERSION"
2013-02-11 12:16:59 -05:00
if File.readable?(gitlab_shell_version_file)
File.read(gitlab_shell_version_file)
2012-12-13 06:11:41 -05:00
end
end
2013-02-11 12:16:59 -05:00
def has_gitlab_shell3?
gitlab_shell_version.try(:start_with?, "v3.")
2012-12-13 06:11:41 -05:00
end
2012-12-10 08:50:55 -05:00
end
2012-12-13 06:11:41 -05:00
2013-01-09 18:08:15 -05:00
namespace :sidekiq do
2013-01-09 01:14:05 -05:00
desc "GITLAB | Check the configuration of Sidekiq"
2012-12-10 08:50:55 -05:00
task check: :environment do
2012-12-13 06:11:41 -05:00
warn_user_is_not_gitlab
2013-01-09 18:08:15 -05:00
start_checking "Sidekiq"
2012-12-13 06:11:41 -05:00
2013-01-09 18:08:15 -05:00
check_sidekiq_running
2012-12-13 06:11:41 -05:00
2013-01-09 18:08:15 -05:00
finished_checking "Sidekiq"
2012-12-13 06:11:41 -05:00
end
# Checks
########################
2013-01-09 18:08:15 -05:00
def check_sidekiq_running
2012-12-13 06:11:41 -05:00
print "Running? ... "
if run_and_match("ps aux | grep -i sidekiq", /sidekiq \d+\.\d+\.\d+.+$/)
2012-12-13 06:11:41 -05:00
puts "yes".green
else
puts "no".red
try_fixing_it(
sudo_gitlab("bundle exec rake sidekiq:start RAILS_ENV=production")
2012-12-13 06:11:41 -05:00
)
for_more_information(
see_installation_guide_section("Install Init Script"),
2013-01-09 01:14:05 -05:00
"see log/sidekiq.log for possible errors"
2012-12-13 06:11:41 -05:00
)
2012-12-22 19:33:58 -05:00
fix_and_rerun
2012-12-13 06:11:41 -05:00
end
end
end
# Helper methods
##########################
2012-12-22 19:33:58 -05:00
def fix_and_rerun
2012-12-13 06:11:41 -05:00
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
def sudo_gitlab(command)
gitlab_user = Gitlab.config.gitlab.user
"sudo -u #{gitlab_user} -H #{command}"
end
2012-12-13 06:11:41 -05:00
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
def check_gitlab_shell
required_version = Gitlab::VersionInfo.new(1, 7, 0)
current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
2013-04-30 07:49:35 -04:00
print "GitLab Shell version >= #{required_version} ? ... "
if current_version.valid? && required_version <= current_version
puts "OK (#{current_version})".green
else
puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
end
end
2013-05-06 08:08:53 -04:00
def check_git_version
required_version = Gitlab::VersionInfo.new(1, 7, 10)
current_version = Gitlab::VersionInfo.parse(run("#{Gitlab.config.git.bin_path} --version"))
puts "Your git bin path is \"#{Gitlab.config.git.bin_path}\""
print "Git version >= #{required_version} ? ... "
if current_version.valid? && required_version <= current_version
puts "yes (#{current_version})".green
2013-05-06 08:08:53 -04:00
else
puts "no".red
try_fixing_it(
"Update your git to a version >= #{required_version} from #{current_version}"
2013-05-06 08:08:53 -04:00
)
fix_and_rerun
end
end
2012-04-03 18:02:54 -04:00
end