diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | app/views/admin/runners/index.html.haml | 2 | ||||
-rw-r--r-- | app/views/helps/show.html.haml | 4 | ||||
-rw-r--r-- | config/initializers/2_app.rb | 6 | ||||
-rw-r--r-- | lib/api/helpers.rb | 2 | ||||
-rw-r--r-- | lib/upgrader.rb | 94 | ||||
-rw-r--r-- | lib/version_info.rb | 52 | ||||
-rw-r--r-- | script/upgrade.rb | 3 | ||||
-rw-r--r-- | spec/lib/upgrader_spec.rb | 24 | ||||
-rw-r--r-- | spec/requests/runners_spec.rb | 2 |
10 files changed, 182 insertions, 8 deletions
@@ -16,3 +16,4 @@ coverage/* <<<<<<< HEAD .secret /vendor +.DS_Store diff --git a/app/views/admin/runners/index.html.haml b/app/views/admin/runners/index.html.haml index ea62b3d..ebe12d0 100644 --- a/app/views/admin/runners/index.html.haml +++ b/app/views/admin/runners/index.html.haml @@ -8,7 +8,7 @@ %p.lead %span To register new runner you should use next token - %code #{GitlabCi::RunnersToken} + %code #{GitlabCi::RUNNERS_TOKEN} .bs-callout %p diff --git a/app/views/helps/show.html.haml b/app/views/helps/show.html.haml index e37f565..996d16b 100644 --- a/app/views/helps/show.html.haml +++ b/app/views/helps/show.html.haml @@ -1,8 +1,8 @@ .jumbotron %h2 GitLab CI - %span= GitlabCi::Version - %small= GitlabCi::Revision + %span= GitlabCi::VERSION + %small= GitlabCi::REVISION %p GitLab CI integrates with your GitLab installation and run tests for your projects. %br diff --git a/config/initializers/2_app.rb b/config/initializers/2_app.rb index 22a95c9..8730519 100644 --- a/config/initializers/2_app.rb +++ b/config/initializers/2_app.rb @@ -1,7 +1,7 @@ module GitlabCi - Version = File.read(Rails.root.join("VERSION")) - Revision = `git log --pretty=format:'%h' -n 1` - RunnersToken = SecureRandom.hex(10) + VERSION = File.read(Rails.root.join("VERSION")).strip + REVISION = `git log --pretty=format:'%h' -n 1` + RUNNERS_TOKEN = SecureRandom.hex(10) def self.config Settings diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 28a3f2b..fb57938 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -22,7 +22,7 @@ module API end def authenticate_runners! - forbidden! unless params[:token] == GitlabCi::RunnersToken + forbidden! unless params[:token] == GitlabCi::RUNNERS_TOKEN end def authenticate_runner! diff --git a/lib/upgrader.rb b/lib/upgrader.rb new file mode 100644 index 0000000..3dff6a8 --- /dev/null +++ b/lib/upgrader.rb @@ -0,0 +1,94 @@ +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 = `git ls-remote --tags origin | grep tags\/v#{current_version.major}` + git_tags = git_tags.lines.to_a.select { |version| version =~ /v\d\.\d\.\d\Z/ } + last_tag = git_tags.last.match(/v\d\.\d\.\d/).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 +end diff --git a/lib/version_info.rb b/lib/version_info.rb new file mode 100644 index 0000000..2a87c91 --- /dev/null +++ b/lib/version_info.rb @@ -0,0 +1,52 @@ +class VersionInfo + include Comparable + + attr_reader :major, :minor, :patch + + def self.parse(str) + if str && m = str.match(/(\d+)\.(\d+)\.(\d+)/) + VersionInfo.new(m[1].to_i, m[2].to_i, m[3].to_i) + else + VersionInfo.new + end + end + + def initialize(major = 0, minor = 0, patch = 0) + @major = major + @minor = minor + @patch = patch + end + + def <=>(other) + return unless other.is_a? VersionInfo + return unless valid? && other.valid? + + if other.major < @major + 1 + elsif @major < other.major + -1 + elsif other.minor < @minor + 1 + elsif @minor < other.minor + -1 + elsif other.patch < @patch + 1 + elsif @patch < other.patch + -1 + else + 0 + end + end + + def to_s + if valid? + "%d.%d.%d" % [@major, @minor, @patch] + else + "Unknown" + end + end + + def valid? + @major >= 0 && @minor >= 0 && @patch >= 0 && @major + @minor + @patch > 0 + end +end diff --git a/script/upgrade.rb b/script/upgrade.rb new file mode 100644 index 0000000..86cbf6e --- /dev/null +++ b/script/upgrade.rb @@ -0,0 +1,3 @@ +require_relative "../lib/upgrader" + +Upgrader.new.execute diff --git a/spec/lib/upgrader_spec.rb b/spec/lib/upgrader_spec.rb new file mode 100644 index 0000000..9674907 --- /dev/null +++ b/spec/lib/upgrader_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe Upgrader do + let(:upgrader) { Upgrader.new } + let(:current_version) { GitlabCi::VERSION } + + describe 'current_version_raw' do + it { upgrader.current_version_raw.should == current_version } + end + + describe 'latest_version?' do + it 'should be true if newest version' do + upgrader.stub(latest_version_raw: current_version) + upgrader.latest_version?.should be_true + end + end + + describe 'latest_version_raw' do + it 'should be latest version for GitLab 5' do + upgrader.stub(current_version_raw: "3.0.0") + upgrader.latest_version_raw.should == "v3.2.0" + end + end +end diff --git a/spec/requests/runners_spec.rb b/spec/requests/runners_spec.rb index dcbe0ff..a6b836a 100644 --- a/spec/requests/runners_spec.rb +++ b/spec/requests/runners_spec.rb @@ -40,7 +40,7 @@ describe API::API do describe "POST /runners/register" do it "should create a runner if token provided" do - post api("/runners/register"), token: GitlabCi::RunnersToken, public_key: 'sha-rsa ....' + post api("/runners/register"), token: GitlabCi::RUNNERS_TOKEN, public_key: 'sha-rsa ....' response.status.should == 201 end |