summaryrefslogtreecommitdiff
path: root/lib/upgrader.rb
blob: b1599e23fb00c68898af877342ae8aa2fe04356e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require_relative "version_info"

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

    if latest_version?
      puts "You are using the latest GitLab CI 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

  def latest_version?
    current_version >= latest_version
  end

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

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

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

  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| VersionInfo.parse(tag.match(/v\d+\.\d+\.\d+/).to_s) }
    "v#{git_versions.sort.last.to_s}"
  end

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

  def upgrade
    update_commands.each do |title, cmd|
      puts title
      puts " -> #{cmd}"
      if system(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

  def gitlab_path
    File.expand_path(File.join(File.dirname(__FILE__), '..'))
  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)
      answer = STDIN.gets.chomp
    end while !choices.include?(answer)
    answer
  end

  def fetch_git_tags
    tags = `git ls-remote --tags origin | grep tags\/v#{current_version.major}`
    tags.lines.to_a.select { |version| version =~ /v\d+\.\d+\.\d+\Z/ }
  end
end