2013-01-17 15:19:36 -05:00
|
|
|
module Gitlab
|
|
|
|
class TaskAbortedByUserError < StandardError; end
|
|
|
|
end
|
|
|
|
|
2016-06-01 18:37:15 -04:00
|
|
|
require 'rainbow/ext/string'
|
2016-01-12 21:34:23 -05:00
|
|
|
|
2016-01-15 17:47:28 -05:00
|
|
|
# Prevent StateMachine warnings from outputting during a cron task
|
|
|
|
StateMachines::Machine.ignore_method_conflicts = true if ENV['CRON']
|
|
|
|
|
2012-12-22 21:20:13 -05:00
|
|
|
namespace :gitlab do
|
|
|
|
|
2013-01-17 15:19:36 -05:00
|
|
|
# Ask if the user wants to continue
|
|
|
|
#
|
|
|
|
# Returns "yes" the user chose to continue
|
|
|
|
# Raises Gitlab::TaskAbortedByUserError if the user chose *not* to continue
|
|
|
|
def ask_to_continue
|
2016-06-01 18:37:15 -04:00
|
|
|
answer = prompt("Do you want to continue (yes/no)? ".color(:blue), %w{yes no})
|
2013-01-17 15:19:36 -05:00
|
|
|
raise Gitlab::TaskAbortedByUserError unless answer == "yes"
|
|
|
|
end
|
|
|
|
|
2013-01-11 19:24:51 -05:00
|
|
|
# Check which OS is running
|
|
|
|
#
|
|
|
|
# It will primarily use lsb_relase to determine the OS.
|
2013-04-15 11:05:48 -04:00
|
|
|
# It has fallbacks to Debian, SuSE, OS X and systems running systemd.
|
2013-01-11 19:24:51 -05:00
|
|
|
def os_name
|
2014-02-25 06:12:09 -05:00
|
|
|
os_name = run(%W(lsb_release -irs))
|
2013-01-11 19:24:51 -05:00
|
|
|
os_name ||= if File.readable?('/etc/system-release')
|
|
|
|
File.read('/etc/system-release')
|
|
|
|
end
|
|
|
|
os_name ||= if File.readable?('/etc/debian_version')
|
|
|
|
debian_version = File.read('/etc/debian_version')
|
|
|
|
"Debian #{debian_version}"
|
|
|
|
end
|
|
|
|
os_name ||= if File.readable?('/etc/SuSE-release')
|
|
|
|
File.read('/etc/SuSE-release')
|
|
|
|
end
|
2014-02-25 06:12:09 -05:00
|
|
|
os_name ||= if os_x_version = run(%W(sw_vers -productVersion))
|
2013-01-11 19:24:51 -05:00
|
|
|
"Mac OS X #{os_x_version}"
|
|
|
|
end
|
2013-04-15 11:05:48 -04:00
|
|
|
os_name ||= if File.readable?('/etc/os-release')
|
|
|
|
File.read('/etc/os-release').match(/PRETTY_NAME=\"(.+)\"/)[1]
|
|
|
|
end
|
2013-01-11 19:24:51 -05:00
|
|
|
os_name.try(:squish!)
|
|
|
|
end
|
|
|
|
|
2013-01-17 15:19:36 -05:00
|
|
|
# Prompt the user to input something
|
|
|
|
#
|
|
|
|
# message - the message to display before input
|
2013-03-17 15:46:54 -04:00
|
|
|
# choices - array of strings of acceptable answers or nil for any answer
|
2013-01-17 15:19:36 -05:00
|
|
|
#
|
|
|
|
# Returns the user's answer
|
|
|
|
def prompt(message, choices = nil)
|
|
|
|
begin
|
|
|
|
print(message)
|
|
|
|
answer = STDIN.gets.chomp
|
|
|
|
end while choices.present? && !choices.include?(answer)
|
|
|
|
answer
|
|
|
|
end
|
|
|
|
|
2013-03-17 15:46:54 -04:00
|
|
|
# Runs the given command and matches the output against the given pattern
|
2012-12-22 21:20:13 -05:00
|
|
|
#
|
|
|
|
# Returns nil if nothing matched
|
2013-07-29 06:46:00 -04:00
|
|
|
# Returns the MatchData if the pattern matched
|
2012-12-22 21:20:13 -05:00
|
|
|
#
|
|
|
|
# see also #run
|
|
|
|
# see also String#match
|
|
|
|
def run_and_match(command, regexp)
|
|
|
|
run(command).try(:match, regexp)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Runs the given command
|
|
|
|
#
|
|
|
|
# Returns nil if the command was not found
|
|
|
|
# Returns the output of the command otherwise
|
|
|
|
#
|
|
|
|
# see also #run_and_match
|
|
|
|
def run(command)
|
2014-02-25 06:12:09 -05:00
|
|
|
output, _ = Gitlab::Popen.popen(command)
|
|
|
|
output
|
2014-03-10 13:14:59 -04:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
'' # if the command does not exist, return an empty string
|
2012-12-22 21:20:13 -05:00
|
|
|
end
|
|
|
|
|
2013-01-11 18:44:40 -05:00
|
|
|
def uid_for(user_name)
|
2014-02-25 06:12:09 -05:00
|
|
|
run(%W(id -u #{user_name})).chomp.to_i
|
2013-01-11 18:44:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def gid_for(group_name)
|
2013-09-12 09:28:46 -04:00
|
|
|
begin
|
|
|
|
Etc.getgrnam(group_name).gid
|
|
|
|
rescue ArgumentError # no group
|
|
|
|
"group #{group_name} doesn't exist"
|
|
|
|
end
|
2013-01-11 18:44:40 -05:00
|
|
|
end
|
|
|
|
|
2012-12-22 21:20:13 -05:00
|
|
|
def warn_user_is_not_gitlab
|
|
|
|
unless @warned_user_not_gitlab
|
2013-01-16 18:09:08 -05:00
|
|
|
gitlab_user = Gitlab.config.gitlab.user
|
2014-02-25 06:12:09 -05:00
|
|
|
current_user = run(%W(whoami)).chomp
|
2013-01-16 18:09:08 -05:00
|
|
|
unless current_user == gitlab_user
|
2016-06-01 18:37:15 -04:00
|
|
|
puts " Warning ".color(:black).background(:yellow)
|
|
|
|
puts " You are running as user #{current_user.color(:magenta)}, we hope you know what you are doing."
|
2012-12-22 21:20:13 -05:00
|
|
|
puts " Things may work\/fail for the wrong reasons."
|
2016-06-01 18:37:15 -04:00
|
|
|
puts " For correct results you should run this as user #{gitlab_user.color(:magenta)}."
|
2012-12-22 21:20:13 -05:00
|
|
|
puts ""
|
|
|
|
end
|
|
|
|
@warned_user_not_gitlab = true
|
|
|
|
end
|
|
|
|
end
|
2015-03-12 06:30:46 -04:00
|
|
|
|
|
|
|
# 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)
|
2015-06-03 09:42:22 -04:00
|
|
|
if !@warned_user_not_gitlab
|
2015-03-12 06:30:46 -04:00
|
|
|
command_success = options.map do |name, value|
|
2015-06-03 09:42:22 -04:00
|
|
|
system(*%W(#{Gitlab.config.git.bin_path} config --global #{name} #{value}))
|
2015-03-12 06:30:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
command_success.all?
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2015-09-22 08:04:14 -04:00
|
|
|
|
|
|
|
def all_repos
|
2016-06-22 17:04:51 -04:00
|
|
|
Gitlab.config.repositories.storages.each do |name, path|
|
|
|
|
IO.popen(%W(find #{path} -mindepth 2 -maxdepth 2 -type d -name *.git)) do |find|
|
|
|
|
find.each_line do |path|
|
|
|
|
yield path.chomp
|
|
|
|
end
|
2015-10-01 07:34:41 -04:00
|
|
|
end
|
|
|
|
end
|
2015-09-22 08:04:14 -04:00
|
|
|
end
|
2016-06-22 17:04:51 -04:00
|
|
|
|
|
|
|
def repository_storage_paths_args
|
|
|
|
Gitlab.config.repositories.storages.values
|
|
|
|
end
|
2012-12-22 21:20:13 -05:00
|
|
|
end
|