Class: Gitlab::Upgrader

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/upgrader.rb

Instance Method Summary collapse

Instance Method Details

#current_versionObject


33
34
35
# File 'lib/gitlab/upgrader.rb', line 33

def current_version
  @current_version ||= Gitlab::VersionInfo.parse(current_version_raw)
end

#current_version_rawObject


41
42
43
# File 'lib/gitlab/upgrader.rb', line 41

def current_version_raw
  File.read(File.join(gitlab_path, "VERSION")).strip
end

#envObject


69
70
71
# File 'lib/gitlab/upgrader.rb', line 69

def env
  { 'RAILS_ENV' => 'production' }
end

#executeObject


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab/upgrader.rb', line 6

def execute
  puts "GitLab #{current_version.major} upgrade tool"
  puts "Your version is #{current_version}"
  puts "Latest available version for GitLab #{current_version.major} is #{latest_version}"

  if latest_version?
    puts "You are using the latest GitLab version"
  else
    puts "Newer GitLab version is available"
    answer = if ARGV.first == "-y"
               "yes"
             else
               prompt("Do you want to upgrade (yes/no)? ", %w{yes no})
             end

    if answer == "yes"
      upgrade
    else
      exit 0
    end
  end
end

#fetch_git_tagsObject


52
53
54
55
# File 'lib/gitlab/upgrader.rb', line 52

def fetch_git_tags
  remote_tags, _ = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} ls-remote --tags https://gitlab.com/gitlab-org/gitlab-ce.git))
  remote_tags.split("\n").grep(/tags\/v#{current_version.major}/)
end

#gitlab_pathObject


89
90
91
# File 'lib/gitlab/upgrader.rb', line 89

def gitlab_path
  File.expand_path(File.join(File.dirname(__FILE__), '../..'))
end

#latest_versionObject


37
38
39
# File 'lib/gitlab/upgrader.rb', line 37

def latest_version
  @latest_version ||= Gitlab::VersionInfo.parse(latest_version_raw)
end

#latest_version?Boolean


29
30
31
# File 'lib/gitlab/upgrader.rb', line 29

def latest_version?
  current_version >= latest_version
end

#latest_version_rawObject


45
46
47
48
49
50
# File 'lib/gitlab/upgrader.rb', line 45

def latest_version_raw
  git_tags = fetch_git_tags
  git_tags = git_tags.select { |version| version =~ /v\d+\.\d+\.\d+\Z/ }
  git_versions = git_tags.map { |tag| Gitlab::VersionInfo.parse(tag.match(/v\d+\.\d+\.\d+/).to_s) }
  "v#{git_versions.sort.last.to_s}"
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


99
100
101
102
103
104
105
# File 'lib/gitlab/upgrader.rb', line 99

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

#update_commandsObject


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitlab/upgrader.rb', line 57

def update_commands
  {
    "Stash changed files" => %W(#{Gitlab.config.git.bin_path} stash),
    "Get latest code" => %W(#{Gitlab.config.git.bin_path} fetch),
    "Switch to new version" => %W(#{Gitlab.config.git.bin_path} checkout v#{latest_version}),
    "Install gems" => %W(bundle),
    "Migrate DB" => %W(bundle exec rake db:migrate),
    "Recompile assets" => %W(bundle exec rake assets:clean assets:precompile),
    "Clear cache" => %W(bundle exec rake cache:clear)
  }
end

#upgradeObject


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gitlab/upgrader.rb', line 73

def upgrade
  update_commands.each do |title, cmd|
    puts title
    puts " -> #{cmd.join(' ')}"
    if system(env, *cmd)
      puts " -> OK"
    else
      puts " -> FAILED"
      puts "Failed to upgrade. Try to repeat task or proceed with upgrade manually "
      exit 1
    end
  end

  puts "Done"
end