Commit 440f3f384f1ef03a8911b6d6ff3e4a21c3308adf
1 parent
2ca4e708
Exists in
master
and in
4 other branches
Minor version upgrader
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
3 changed files
with
123 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,96 @@ | @@ -0,0 +1,96 @@ | ||
1 | +require "colored" | ||
2 | +require_relative "version_info" | ||
3 | + | ||
4 | +module Gitlab | ||
5 | + class Upgrader | ||
6 | + def execute | ||
7 | + puts "GitLab #{current_version.major} upgrade tool".yellow | ||
8 | + puts "Your version is #{current_version}" | ||
9 | + puts "Latest available version for GitLab #{current_version.major} is #{latest_version}" | ||
10 | + | ||
11 | + if latest_version? | ||
12 | + puts "You use latest GitLab version" | ||
13 | + else | ||
14 | + puts "Newer GitLab version is available" | ||
15 | + answer = prompt("Do you want to upgrade (yes/no)? ".blue, %w{yes no}) | ||
16 | + | ||
17 | + if answer == "yes" | ||
18 | + upgrade | ||
19 | + else | ||
20 | + exit 0 | ||
21 | + end | ||
22 | + end | ||
23 | + end | ||
24 | + | ||
25 | + def latest_version? | ||
26 | + current_version >= latest_version | ||
27 | + end | ||
28 | + | ||
29 | + def current_version | ||
30 | + @current_version ||= Gitlab::VersionInfo.parse(current_version_raw) | ||
31 | + end | ||
32 | + | ||
33 | + def latest_version | ||
34 | + @latest_version ||= Gitlab::VersionInfo.parse(latest_version_raw) | ||
35 | + end | ||
36 | + | ||
37 | + def current_version_raw | ||
38 | + File.read(File.join(gitlab_path, "VERSION")).strip | ||
39 | + end | ||
40 | + | ||
41 | + def latest_version_raw | ||
42 | + git_tags = `git ls-remote --tags origin | grep tags\/v#{current_version.major}` | ||
43 | + git_tags = git_tags.lines.to_a.select { |version| version =~ /v\d\.\d\.\d\Z/ } | ||
44 | + last_tag = git_tags.last.match(/v\d\.\d\.\d/).to_s | ||
45 | + end | ||
46 | + | ||
47 | + def git_las_tags | ||
48 | + end | ||
49 | + | ||
50 | + def update_commands | ||
51 | + { | ||
52 | + "Stash changed files" => "git stash", | ||
53 | + "Get latest code" => "git fetch", | ||
54 | + "Switch to new version" => "git checkout v#{latest_version}", | ||
55 | + "Install gems" => "bundle", | ||
56 | + "Migrate DB" => "bundle exec rake db:migrate RAILS_ENV=production", | ||
57 | + "Recompile assets" => "bundle exec rake assets:clean assets:precompile RAILS_ENV=production", | ||
58 | + "Clear cache" => "bundle exec rake cache:clear RAILS_ENV=production" | ||
59 | + } | ||
60 | + end | ||
61 | + | ||
62 | + def upgrade | ||
63 | + update_commands.each do |title, cmd| | ||
64 | + puts title.yellow | ||
65 | + puts " -> #{cmd}" | ||
66 | + if system(cmd) | ||
67 | + puts " -> OK".green | ||
68 | + else | ||
69 | + puts " -> FAILED".red | ||
70 | + puts "Failed to upgrade. Try to repeat task or proceed with upgrade manually " | ||
71 | + exit 1 | ||
72 | + end | ||
73 | + end | ||
74 | + | ||
75 | + puts "Done" | ||
76 | + end | ||
77 | + | ||
78 | + def gitlab_path | ||
79 | + File.expand_path(File.join(File.dirname(__FILE__), '../..')) | ||
80 | + end | ||
81 | + | ||
82 | + # Prompt the user to input something | ||
83 | + # | ||
84 | + # message - the message to display before input | ||
85 | + # choices - array of strings of acceptable answers or nil for any answer | ||
86 | + # | ||
87 | + # Returns the user's answer | ||
88 | + def prompt(message, choices = nil) | ||
89 | + begin | ||
90 | + print(message) | ||
91 | + answer = STDIN.gets.chomp | ||
92 | + end while !choices.include?(answer) | ||
93 | + answer | ||
94 | + end | ||
95 | + end | ||
96 | +end |
@@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe Gitlab::Upgrader do | ||
4 | + let(:upgrader) { Gitlab::Upgrader.new } | ||
5 | + let(:current_version) { Gitlab::VERSION } | ||
6 | + | ||
7 | + describe 'current_version_raw' do | ||
8 | + it { upgrader.current_version_raw.should == current_version } | ||
9 | + end | ||
10 | + | ||
11 | + describe 'latest_version?' do | ||
12 | + it 'should be true if newest version' do | ||
13 | + upgrader.stub(latest_version_raw: current_version) | ||
14 | + upgrader.latest_version?.should be_true | ||
15 | + end | ||
16 | + end | ||
17 | + | ||
18 | + describe 'latest_version_raw' do | ||
19 | + it 'should be latest version for GitLab 5' do | ||
20 | + upgrader.stub(current_version_raw: "5.3.0") | ||
21 | + upgrader.latest_version_raw.should == "v5.4.2" | ||
22 | + end | ||
23 | + end | ||
24 | +end |