debian-mirror-gitlab/lib/gitlab/task_helpers.rb

190 lines
6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'rainbow/ext/string'
2023-05-27 22:25:52 +05:30
require_relative 'utils/strong_memoize'
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
# rubocop:disable Rails/Output
2017-08-17 22:00:37 +05:30
module Gitlab
TaskFailedError = Class.new(StandardError)
TaskAbortedByUserError = Class.new(StandardError)
module TaskHelpers
2018-03-17 18:26:18 +05:30
include Gitlab::Utils::StrongMemoize
extend self
2019-03-02 22:35:43 +05:30
def invoke_and_time_task(task)
start = Time.now
Rake::Task[task].invoke
puts "`#{task}` finished in #{Time.now - start} seconds"
end
2017-08-17 22:00:37 +05:30
# 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
2020-11-24 15:15:51 +05:30
return if Gitlab::Utils.to_boolean(ENV['GITLAB_ASSUME_YES'])
2017-08-17 22:00:37 +05:30
answer = prompt("Do you want to continue (yes/no)? ".color(: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
os_name = run_command(%w(lsb_release -irs))
os_name ||=
if File.readable?('/etc/system-release')
File.read('/etc/system-release')
elsif File.readable?('/etc/debian_version')
"Debian #{File.read('/etc/debian_version')}"
elsif File.readable?('/etc/SuSE-release')
File.read('/etc/SuSE-release')
elsif os_x_version = run_command(%w(sw_vers -productVersion))
"Mac OS X #{os_x_version}"
elsif File.readable?('/etc/os-release')
File.read('/etc/os-release').match(/PRETTY_NAME=\"(.+)\"/)[1]
end
2018-12-13 13:39:08 +05:30
os_name.try(:squish)
2017-08-17 22:00:37 +05:30
end
# 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
#
# Returns the user's answer
def prompt(message, choices = nil)
begin
print(message)
2021-09-04 01:27:46 +05:30
answer = $stdin.gets.chomp
2017-08-17 22:00:37 +05:30
end while choices.present? && !choices.include?(answer)
answer
end
2021-03-11 19:13:27 +05:30
# Prompt the user to input a password
#
# message - custom message to display before input
def prompt_for_password(message = 'Enter password: ')
2021-09-04 01:27:46 +05:30
unless $stdin.tty?
2021-03-11 19:13:27 +05:30
print(message)
2021-09-04 01:27:46 +05:30
return $stdin.gets.chomp
2021-03-11 19:13:27 +05:30
end
2021-09-04 01:27:46 +05:30
$stdin.getpass(message)
2021-03-11 19:13:27 +05:30
end
2017-08-17 22:00:37 +05:30
# Runs the given command and matches the output against the given pattern
#
# Returns nil if nothing matched
# Returns the MatchData if the pattern matched
#
# see also #run_command
# see also String#match
def run_and_match(command, regexp)
run_command(command).try(:match, regexp)
end
# Runs the given command
#
# Returns '' if the command was not found
# Returns the output of the command otherwise
#
# see also #run_and_match
def run_command(command)
output, _ = Gitlab::Popen.popen(command)
output
rescue Errno::ENOENT
'' # if the command does not exist, return an empty string
end
# Runs the given command and raises a Gitlab::TaskFailedError exception if
# the command does not exit with 0
#
# Returns the output of the command otherwise
def run_command!(command)
output, status = Gitlab::Popen.popen(command)
2021-06-08 01:23:25 +05:30
raise Gitlab::TaskFailedError, output unless status == 0
2017-08-17 22:00:37 +05:30
output
end
def uid_for(user_name)
run_command(%W(id -u #{user_name})).chomp.to_i
end
def gid_for(group_name)
2018-03-17 18:26:18 +05:30
Etc.getgrnam(group_name).gid
rescue ArgumentError # no group
"group #{group_name} doesn't exist"
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
def gitlab_user
Gitlab.config.gitlab.user
end
2018-03-17 18:26:18 +05:30
def gitlab_user?
strong_memoize(:is_gitlab_user) do
current_user = run_command(%w(whoami)).chomp
current_user == gitlab_user
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
def warn_user_is_not_gitlab
2018-03-17 18:26:18 +05:30
return if gitlab_user?
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
strong_memoize(:warned_user_not_gitlab) do
2017-08-17 22:00:37 +05:30
current_user = run_command(%w(whoami)).chomp
2017-09-10 17:25:29 +05:30
puts " Warning ".color(:black).background(:yellow)
puts " You are running as user #{current_user.color(: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.color(:magenta)}."
puts ""
2017-08-17 22:00:37 +05:30
end
end
def repository_storage_paths_args
2018-11-08 19:23:39 +05:30
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
Gitlab.config.repositories.storages.values.map { |rs| rs.legacy_disk_path }
end
2017-08-17 22:00:37 +05:30
end
def user_home
Rails.env.test? ? Rails.root.join('tmp/tests') : Gitlab.config.gitlab.user_home
end
2020-05-24 23:13:21 +05:30
def checkout_or_clone_version(version:, repo:, target_dir:, clone_opts: [])
clone_repo(repo, target_dir, clone_opts: clone_opts) unless Dir.exist?(target_dir)
2019-12-26 22:10:19 +05:30
checkout_version(get_version(version), target_dir)
end
# this function implements the same logic we have in omnibus for dealing with components version
def get_version(component_version)
# If not a valid version string following SemVer it is probably a branch name or a SHA
# commit of one of our own component so it doesn't need `v` prepended
return component_version unless /^\d+\.\d+\.\d+(-rc\d+)?$/.match?(component_version)
"v#{component_version}"
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
def clone_repo(repo, target_dir, clone_opts: [])
run_command!(%W[#{Gitlab.config.git.bin_path} clone] + clone_opts + %W[-- #{repo} #{target_dir}])
2017-08-17 22:00:37 +05:30
end
def checkout_version(version, target_dir)
2022-01-26 12:08:38 +05:30
# Explicitly setting the git protocol version to v2 allows older Git binaries
# to do have a shallow clone obtain objects by object ID.
run_command!(%W[#{Gitlab.config.git.bin_path} -C #{target_dir} config protocol.version 2])
2017-09-10 17:25:29 +05:30
run_command!(%W[#{Gitlab.config.git.bin_path} -C #{target_dir} fetch --quiet origin #{version}])
run_command!(%W[#{Gitlab.config.git.bin_path} -C #{target_dir} checkout -f --quiet FETCH_HEAD --])
2017-08-17 22:00:37 +05:30
end
end
end
2022-06-21 17:19:12 +05:30
# rubocop:enable Rails/Output