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

114 lines
3.2 KiB
Ruby
Raw Normal View History

2013-01-17 20:19:36 +00:00
module Gitlab
class TaskAbortedByUserError < StandardError; end
end
unless STDOUT.isatty
module Colored
extend self
def colorize(string, options={})
string
end
end
end
2012-12-23 02:20:13 +00:00
namespace :gitlab do
2013-01-17 20:19:36 +00: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
answer = prompt("Do you want to continue (yes/no)? ".blue, %w{yes no})
raise Gitlab::TaskAbortedByUserError unless answer == "yes"
end
# Check which OS is running
#
# It will primarily use lsb_relase to determine the OS.
# It has fallbacks to Debian, SuSE, OS X and systems running systemd.
def os_name
2014-02-25 11:12:09 +00:00
os_name = run(%W(lsb_release -irs))
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 11:12:09 +00:00
os_name ||= if os_x_version = run(%W(sw_vers -productVersion))
"Mac OS X #{os_x_version}"
end
os_name ||= if File.readable?('/etc/os-release')
File.read('/etc/os-release').match(/PRETTY_NAME=\"(.+)\"/)[1]
end
os_name.try(:squish!)
end
2013-01-17 20:19:36 +00:00
# Prompt the user to input something
#
# message - the message to display before input
# choices - array of strings of acceptable answers or nil for any answer
2013-01-17 20:19:36 +00: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
# Runs the given command and matches the output against the given pattern
2012-12-23 02:20:13 +00:00
#
# Returns nil if nothing matched
2013-07-29 10:46:00 +00:00
# Returns the MatchData if the pattern matched
2012-12-23 02:20:13 +00: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 11:12:09 +00:00
output, _ = Gitlab::Popen.popen(command)
output
2012-12-23 02:20:13 +00:00
end
def uid_for(user_name)
2014-02-25 11:12:09 +00:00
run(%W(id -u #{user_name})).chomp.to_i
end
def gid_for(group_name)
2013-09-12 13:28:46 +00:00
begin
Etc.getgrnam(group_name).gid
rescue ArgumentError # no group
"group #{group_name} doesn't exist"
end
end
2012-12-23 02:20:13 +00:00
def warn_user_is_not_gitlab
unless @warned_user_not_gitlab
gitlab_user = Gitlab.config.gitlab.user
2014-02-25 11:12:09 +00:00
current_user = run(%W(whoami)).chomp
unless current_user == gitlab_user
2012-12-23 02:20:13 +00:00
puts "#{Colored.color(:black)+Colored.color(:on_yellow)} Warning #{Colored.extra(:clear)}"
puts " You are running as user #{current_user.magenta}, we hope you know what you are doing."
puts " Things may work\/fail for the wrong reasons."
puts " For correct results you should run this as user #{gitlab_user.magenta}."
2012-12-23 02:20:13 +00:00
puts ""
end
@warned_user_not_gitlab = true
end
end
end