Commit 13e88c93 authored by Gabriel Mazetto's avatar Gabriel Mazetto
Browse files

Refactor gitlab:app:checks to use SystemCheck

parent 156fba5146dc
module SystemCheck
module App
class DatabaseConfigExistsCheck < SystemCheck::BaseCheck
set_name 'Database config exists?'
def check?
database_config_file = Rails.root.join('config', 'database.yml')
File.exist?(database_config_file)
end
def show_error
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'
)
fix_and_rerun
end
private
def see_database_guide
'doc/install/databases.md'
end
end
end
end
module SystemCheck
module App
class GitConfigCheck < SystemCheck::BaseCheck
OPTIONS = {
'core.autocrlf' => 'input'
}.freeze
set_name 'Git configured with autocrlf=input?'
def check?
correct_options = OPTIONS.map do |name, value|
run_command(%W(#{Gitlab.config.git.bin_path} config --global --get #{name})).try(:squish) == value
end
correct_options.all?
end
def repair!
auto_fix_git_config(OPTIONS)
end
def show_error
try_fixing_it(
sudo_gitlab("\"#{Gitlab.config.git.bin_path}\" config --global core.autocrlf \"#{OPTIONS['core.autocrlf']}\"")
)
for_more_information(
see_installation_guide_section 'GitLab'
)
end
private
# Tries to configure git itself
#
# Returns true if all subcommands were successfull (according to their exit code)
# Returns false if any or all subcommands failed.
def auto_fix_git_config(options)
if !@warned_user_not_gitlab
command_success = options.map do |name, value|
system(*%W(#{Gitlab.config.git.bin_path} config --global #{name} #{value}))
end
command_success.all?
else
false
end
end
end
end
end
module SystemCheck
module App
class GitVersionCheck < SystemCheck::BaseCheck
set_name -> { "Git version >= #{self.required_version} ?" }
set_check_pass -> { "yes (#{self.current_version})" }
def self.required_version
@required_version ||= Gitlab::VersionInfo.new(2, 7, 3)
end
def self.current_version
@current_version ||= Gitlab::VersionInfo.parse(run_command(%W(#{Gitlab.config.git.bin_path} --version)))
end
def check?
self.class.current_version.valid? && self.class.required_version <= self.class.current_version
end
def show_error
puts "Your git bin path is \"#{Gitlab.config.git.bin_path}\""
try_fixing_it(
"Update your git to a version >= #{self.class.required_version} from #{self.class.current_version}"
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class GitlabConfigExistsCheck < SystemCheck::BaseCheck
set_name 'GitLab config exists?'
def check?
gitlab_config_file = Rails.root.join('config', 'gitlab.yml')
File.exist?(gitlab_config_file)
end
def show_error
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'
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class GitlabConfigNotOutdatedCheck < SystemCheck::BaseCheck
set_name 'GitLab config outdated?'
set_check_pass 'no'
set_check_fail 'yes'
set_skip_reason "can't check because of previous errors"
def skip?
gitlab_config_file = Rails.root.join('config', 'gitlab.yml')
!File.exist?(gitlab_config_file)
end
def check?
# omniauth or ldap could have been deleted from the file
!Gitlab.config['git_host']
end
def show_error
try_fixing_it(
'Backup your config/gitlab.yml',
'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'
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class InitScriptExistsCheck < SystemCheck::BaseCheck
set_name 'Init script exists?'
set_skip_reason 'skipped (omnibus-gitlab has no init script)'
def skip?
omnibus_gitlab?
end
def check?
script_path = '/etc/init.d/gitlab'
File.exist?(script_path)
end
def show_error
try_fixing_it(
'Install the init script'
)
for_more_information(
see_installation_guide_section 'Install Init Script'
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class InitScriptUpToDateCheck < SystemCheck::BaseCheck
SCRIPT_PATH = '/etc/init.d/gitlab'.freeze
set_name 'Init script up-to-date?'
set_skip_reason 'skipped (omnibus-gitlab has no init script)'
def skip?
omnibus_gitlab?
end
def multi_check
recipe_path = Rails.root.join('lib/support/init.d/', 'gitlab')
unless File.exist?(SCRIPT_PATH)
puts "can't check because of previous errors".color(:magenta)
return
end
recipe_content = File.read(recipe_path)
script_content = File.read(SCRIPT_PATH)
if recipe_content == script_content
puts 'yes'.color(:green)
else
puts 'no'.color(:red)
show_error
end
end
def show_error
try_fixing_it(
'Re-download the init script'
)
for_more_information(
see_installation_guide_section 'Install Init Script'
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class LogWritableCheck < SystemCheck::BaseCheck
set_name 'Log directory writable?'
def check?
File.writable?(log_path)
end
def show_error
try_fixing_it(
"sudo chown -R gitlab #{log_path}",
"sudo chmod -R u+rwX #{log_path}"
)
for_more_information(
see_installation_guide_section 'GitLab'
)
fix_and_rerun
end
private
def log_path
Rails.root.join('log')
end
end
end
end
module SystemCheck
module App
class MigrationsAreUpCheck < SystemCheck::BaseCheck
set_name 'All migrations up?'
def check?
migration_status, _ = Gitlab::Popen.popen(%w(bundle exec rake db:migrate:status))
migration_status !~ /down\s+\d{14}/
end
def show_error
try_fixing_it(
sudo_gitlab('bundle exec rake db:migrate RAILS_ENV=production')
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class OrphanedGroupMembersCheck < SystemCheck::BaseCheck
set_name 'Database contains orphaned GroupMembers?'
set_check_pass 'no'
set_check_fail 'yes'
def check?
!GroupMember.where('user_id not in (select id from users)').exists?
end
def show_error
try_fixing_it(
'You can delete the orphaned records using something along the lines of:',
sudo_gitlab("bundle exec rails runner -e production 'GroupMember.where(\"user_id NOT IN (SELECT id FROM users)\").delete_all'")
)
end
end
end
end
module SystemCheck
module App
class ProjectsHaveNamespaceCheck < SystemCheck::BaseCheck
set_name 'projects have namespace: '
set_skip_reason "can't check, you have no projects"
def skip?
!Project.exists?
end
def multi_check
puts ''
Project.find_each(batch_size: 100) do |project|
print sanitized_message(project)
if project.namespace
puts 'yes'.color(:green)
else
puts 'no'.color(:red)
show_error
end
end
end
def show_error
try_fixing_it(
"Migrate global projects"
)
for_more_information(
"doc/update/5.4-to-6.0.md in section \"#global-projects\""
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class RedisVersionCheck < SystemCheck::BaseCheck
MIN_REDIS_VERSION = '2.8.0'.freeze
set_name "Redis version >= #{MIN_REDIS_VERSION}?"
def check?
redis_version = run_command(%w(redis-cli --version))
redis_version = redis_version.try(:match, /redis-cli (\d+\.\d+\.\d+)/)
redis_version && (Gem::Version.new(redis_version[1]) > Gem::Version.new(MIN_REDIS_VERSION))
end
def show_error
try_fixing_it(
"Update your redis server to a version >= #{MIN_REDIS_VERSION}"
)
for_more_information(
'gitlab-public-wiki/wiki/Trouble-Shooting-Guide in section sidekiq'
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class RubyVersionCheck < SystemCheck::BaseCheck
set_name -> { "Ruby version >= #{self.required_version} ?" }
set_check_pass -> { "yes (#{self.current_version})" }
def self.required_version
@required_version ||= Gitlab::VersionInfo.new(2, 1, 0)
end
def self.current_version
@current_version ||= Gitlab::VersionInfo.parse(run_command(%w(ruby --version)))
end
def check?
self.class.current_version.valid? && self.class.required_version <= self.class.current_version
end
def show_error
try_fixing_it(
"Update your ruby to a version >= #{self.class.required_version} from #{self.class.current_version}"
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class TmpWritableCheck < SystemCheck::BaseCheck
set_name 'Tmp directory writable?'
def check?
File.writable?(tmp_path)
end
def show_error
try_fixing_it(
"sudo chown -R gitlab #{tmp_path}",
"sudo chmod -R u+rwX #{tmp_path}"
)
for_more_information(
see_installation_guide_section 'GitLab'
)
fix_and_rerun
end
private
def tmp_path
Rails.root.join('tmp')
end
end
end
end
module SystemCheck
module App
class UploadsDirectoryExistsCheck < SystemCheck::BaseCheck
set_name 'Uploads directory exists?'
def check?
File.directory?(Rails.root.join('public/uploads'))
end
def show_error
try_fixing_it(
"sudo -u #{gitlab_user} mkdir #{Rails.root}/public/uploads"
)
for_more_information(
see_installation_guide_section 'GitLab'
)
fix_and_rerun
end
end
end
end
module SystemCheck
module App
class UploadsPathPermissionCheck < SystemCheck::BaseCheck
set_name 'Uploads directory has correct permissions?'
set_skip_reason 'skipped (no uploads folder found)'
def skip?
!File.directory?(rails_uploads_path)
end
def check?
File.stat(uploads_fullpath).mode == 040700
end
def show_error
try_fixing_it(
"sudo chmod 700 #{uploads_fullpath}"
)
for_more_information(
see_installation_guide_section 'GitLab'
)
fix_and_rerun
end
private
def rails_uploads_path
Rails.root.join('public/uploads')
end
def uploads_fullpath
File.realpath(rails_uploads_path)
end
end
end
end
module SystemCheck
module App
class UploadsPathTmpPermissionCheck < SystemCheck::BaseCheck
set_name 'Uploads directory tmp has correct permissions?'
set_skip_reason 'skipped (no tmp uploads folder yet)'
def skip?
!File.directory?(uploads_fullpath) || !Dir.exist?(upload_path_tmp)
end
def check?
# If tmp upload dir has incorrect permissions, assume others do as well
# Verify drwx------ permissions
File.stat(upload_path_tmp).mode == 040700 && File.owned?(upload_path_tmp)
end
def show_error
try_fixing_it(
"sudo chown -R #{gitlab_user} #{uploads_fullpath}",
"sudo find #{uploads_fullpath} -type f -exec chmod 0644 {} \\;",
"sudo find #{uploads_fullpath} -type d -not -path #{uploads_fullpath} -exec chmod 0700 {} \\;"
)
for_more_information(
see_installation_guide_section 'GitLab'
)
fix_and_rerun
end
private
def upload_path_tmp
File.join(uploads_fullpath, 'tmp')
end
def uploads_fullpath
File.realpath(Rails.root.join('public/uploads'))
end
end
end
end
......@@ -2,16 +2,103 @@ module SystemCheck
# Base class for Checks. You must inherit from here
# and implement the methods below when necessary
class BaseCheck
include ::Gitlab::TaskHelpers
include Helpers
# Define a custom term for when check passed
#
# @param [String] term used when check passed (default: 'yes')
def self.set_check_pass(term)
@check_pass = term
end
# Define a custom term for when check failed
#
# @param [String] term used when check failed (default: 'no')
def self.set_check_fail(term)
@check_fail = term
end
# Define the name of the SystemCheck that will be displayed during execution
#
# @param [String] name of the check
def self.set_name(name)
@name = name
end
# Define the reason why we skipped the SystemCheck
#
# This is only used if subclass implements `#skip?`
#
# @param [String] reason to be displayed
def self.set_skip_reason(reason)
@skip_reason = reason
end
# Term to be displayed when check passed
#
# @return [String] term when check passed ('yes' if not re-defined in a subclass)
def self.check_pass
call_or_return(@check_pass) || 'yes'
end
## Term to be displayed when check failed
#
# @return [String] term when check failed ('no' if not re-defined in a subclass)
def self.check_fail
call_or_return(@check_fail) || 'no'
end
# Name of the SystemCheck defined by the subclass
#
# @return [String] the name
def self.display_name
call_or_return(@name) || self.name
end
# Skip reason defined by the subclass
#
# @return [String] the reason
def self.skip_reason
call_or_return(@skip_reason) || 'skipped'
end