BigW Consortium Gitlab

task_helpers.rake 3.99 KB
Newer Older
1 2 3 4
module Gitlab
  class TaskAbortedByUserError < StandardError; end
end

5 6
String.disable_colorization = true unless STDOUT.isatty

7 8 9
# Prevent StateMachine warnings from outputting during a cron task
StateMachines::Machine.ignore_method_conflicts = true if ENV['CRON']

10 11
namespace :gitlab do

12 13 14 15 16 17 18 19 20
  # 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

21 22 23
  # Check which OS is running
  #
  # It will primarily use lsb_relase to determine the OS.
24
  # It has fallbacks to Debian, SuSE, OS X and systems running systemd.
25
  def os_name
26
    os_name = run(%W(lsb_release -irs))
27 28 29 30 31 32 33 34 35 36
    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
37
    os_name ||= if os_x_version = run(%W(sw_vers -productVersion))
38 39
                  "Mac OS X #{os_x_version}"
                end
40 41 42
    os_name ||= if File.readable?('/etc/os-release')
                  File.read('/etc/os-release').match(/PRETTY_NAME=\"(.+)\"/)[1]
                end
43 44 45
    os_name.try(:squish!)
  end

46 47 48
  # Prompt the user to input something
  #
  # message - the message to display before input
Kevin Lyda committed
49
  # choices - array of strings of acceptable answers or nil for any answer
50 51 52 53 54 55 56 57 58 59
  #
  # 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

Kevin Lyda committed
60
  # Runs the given command and matches the output against the given pattern
61 62
  #
  # Returns nil if nothing matched
Johannes Schleifenbaum committed
63
  # Returns the MatchData if the pattern matched
64 65 66 67 68 69 70 71 72 73 74 75 76 77
  #
  # 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)
78 79
    output, _ = Gitlab::Popen.popen(command)
    output
80 81
  rescue Errno::ENOENT
    '' # if the command does not exist, return an empty string
82 83
  end

84
  def uid_for(user_name)
85
    run(%W(id -u #{user_name})).chomp.to_i
86 87 88
  end

  def gid_for(group_name)
89 90 91 92 93
    begin
      Etc.getgrnam(group_name).gid
    rescue ArgumentError # no group
      "group #{group_name} doesn't exist"
    end
94 95
  end

96 97
  def warn_user_is_not_gitlab
    unless @warned_user_not_gitlab
98
      gitlab_user = Gitlab.config.gitlab.user
99
      current_user = run(%W(whoami)).chomp
100
      unless current_user == gitlab_user
101
        puts " Warning ".colorize(:black).on_yellow
102 103
        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."
104
        puts "  For correct results you should run this as user #{gitlab_user.magenta}."
105 106 107 108 109
        puts ""
      end
      @warned_user_not_gitlab = true
    end
  end
110 111 112 113 114 115

  # 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)
116
    if !@warned_user_not_gitlab
117
      command_success = options.map do |name, value|
118
        system(*%W(#{Gitlab.config.git.bin_path} config --global #{name} #{value}))
119 120 121 122 123 124 125
      end

      command_success.all?
    else
      false
    end
  end
126 127

  def all_repos
128 129 130 131 132
    IO.popen(%W(find #{Gitlab.config.gitlab_shell.repos_path} -mindepth 2 -maxdepth 2 -type d -name *.git)) do |find|
      find.each_line do |path|
        yield path.chomp
      end
    end
133
  end
134
end