Module: Gitlab::TaskHelpers

Extended by:
TaskHelpers
Includes:
Utils::StrongMemoize
Included in:
TaskHelpers, SystemCheck::Helpers
Defined in:
lib/gitlab/task_helpers.rb

Instance Method Summary collapse

Instance Method Details

#ask_to_continueObject

Ask if the user wants to continue

Returns “yes” the user chose to continue Raises Gitlab::TaskAbortedByUserError if the user chose not to continue



26
27
28
29
30
31
# File 'lib/gitlab/task_helpers.rb', line 26

def ask_to_continue
  return if Gitlab::Utils.to_boolean(ENV['GITLAB_ASSUME_YES'])

  answer = prompt("Do you want to continue (yes/no)? ".color(:blue), %w[yes no])
  raise Gitlab::TaskAbortedByUserError unless answer == "yes"
end

#checkout_or_clone_version(version:, repo:, target_dir:, clone_opts: []) ⇒ Object



162
163
164
165
# File 'lib/gitlab/task_helpers.rb', line 162

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)
  checkout_version(get_version(version), target_dir)
end

#checkout_version(version, target_dir) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/gitlab/task_helpers.rb', line 180

def checkout_version(version, target_dir)
  # 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])
  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 --])
end

#clone_repo(repo, target_dir, clone_opts: []) ⇒ Object



176
177
178
# File 'lib/gitlab/task_helpers.rb', line 176

def clone_repo(repo, target_dir, clone_opts: [])
  run_command!(%W[#{Gitlab.config.git.bin_path} clone] + clone_opts + %W[-- #{repo} #{target_dir}])
end

#get_version(component_version) ⇒ Object

this function implements the same logic we have in omnibus for dealing with components version



168
169
170
171
172
173
174
# File 'lib/gitlab/task_helpers.rb', line 168

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}"
end

#gid_for(group_name) ⇒ Object



121
122
123
124
125
# File 'lib/gitlab/task_helpers.rb', line 121

def gid_for(group_name)
  Etc.getgrnam(group_name).gid
rescue ArgumentError # no group
  "group #{group_name} doesn't exist"
end

#gitlab_userObject



127
128
129
# File 'lib/gitlab/task_helpers.rb', line 127

def gitlab_user
  Gitlab.config.gitlab.user
end

#gitlab_user?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'lib/gitlab/task_helpers.rb', line 131

def gitlab_user?
  strong_memoize(:is_gitlab_user) do
    current_user = run_command(%w[whoami]).chomp
    current_user == gitlab_user
  end
end

#invoke_and_time_task(task) ⇒ Object



16
17
18
19
20
# File 'lib/gitlab/task_helpers.rb', line 16

def invoke_and_time_task(task)
  start = Time.now
  Rake::Task[task].invoke
  puts "`#{task}` finished in #{Time.now - start} seconds"
end

#os_nameObject

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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gitlab/task_helpers.rb', line 37

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

  os_name.try(:squish)
end

#prompt(message, choices = nil) ⇒ Object

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



61
62
63
64
65
66
67
# File 'lib/gitlab/task_helpers.rb', line 61

def prompt(message, choices = nil)
  begin
    print(message)
    answer = $stdin.gets.chomp
  end while choices.present? && !choices.include?(answer)
  answer
end

#prompt_for_password(message = 'Enter password: ') ⇒ Object

Prompt the user to input a password

message - custom message to display before input



72
73
74
75
76
77
78
79
# File 'lib/gitlab/task_helpers.rb', line 72

def prompt_for_password(message = 'Enter password: ')
  unless $stdin.tty?
    print(message)
    return $stdin.gets.chomp
  end

  $stdin.getpass(message)
end

#repository_storage_paths_argsObject



152
153
154
155
156
# File 'lib/gitlab/task_helpers.rb', line 152

def repository_storage_paths_args
  Gitlab::GitalyClient::StorageSettings.allow_disk_access do
    Gitlab.config.repositories.storages.values.map { |rs| rs.legacy_disk_path }
  end
end

#run_and_match(command, regexp) ⇒ Object

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



88
89
90
# File 'lib/gitlab/task_helpers.rb', line 88

def run_and_match(command, regexp)
  run_command(command).try(:match, regexp)
end

#run_command(command) ⇒ Object

Runs the given command

Returns ” if the command was not found Returns the output of the command otherwise

see also #run_and_match



98
99
100
101
102
103
# File 'lib/gitlab/task_helpers.rb', line 98

def run_command(command)
  output, _ = Gitlab::Popen.popen(command)
  output
rescue Errno::ENOENT
  '' # if the command does not exist, return an empty string
end

#run_command!(command) ⇒ Object

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



109
110
111
112
113
114
115
# File 'lib/gitlab/task_helpers.rb', line 109

def run_command!(command)
  output, status = Gitlab::Popen.popen(command)

  raise Gitlab::TaskFailedError, output unless status == 0

  output
end

#uid_for(user_name) ⇒ Object



117
118
119
# File 'lib/gitlab/task_helpers.rb', line 117

def uid_for(user_name)
  run_command(%W[id -u #{user_name}]).chomp.to_i
end

#user_homeObject



158
159
160
# File 'lib/gitlab/task_helpers.rb', line 158

def user_home
  Rails.env.test? ? Rails.root.join('tmp/tests') : Gitlab.config.gitlab.user_home
end

#warn_user_is_not_gitlabObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gitlab/task_helpers.rb', line 138

def warn_user_is_not_gitlab
  return if gitlab_user?

  strong_memoize(:warned_user_not_gitlab) do
    current_user = run_command(%w[whoami]).chomp

    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 ""
  end
end