2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module Gitlab
|
|
|
|
class Upgrader
|
|
|
|
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"
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
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 ||= Gitlab::VersionInfo.parse(current_version_raw)
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest_version
|
|
|
|
@latest_version ||= Gitlab::VersionInfo.parse(latest_version_raw)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_version_raw
|
|
|
|
File.read(File.join(gitlab_path, "VERSION")).strip
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest_version_raw
|
2015-09-11 14:41:01 +05:30
|
|
|
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) }
|
2017-08-17 22:00:37 +05:30
|
|
|
"v#{git_versions.sort.last}"
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_git_tags
|
2015-11-26 14:37:03 +05:30
|
|
|
remote_tags, _ = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} ls-remote --tags https://gitlab.com/gitlab-org/gitlab-ce.git))
|
2018-03-17 18:26:18 +05:30
|
|
|
remote_tags.split("\n").grep(%r{tags/v#{current_version.major}})
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def update_commands
|
|
|
|
{
|
2015-11-26 14:37:03 +05:30
|
|
|
"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}),
|
2017-08-17 22:00:37 +05:30
|
|
|
"Install gems" => %w(bundle),
|
|
|
|
"Migrate DB" => %w(bundle exec rake db:migrate),
|
|
|
|
"Recompile assets" => %w(bundle exec rake yarn:install gitlab:assets:clean gitlab:assets:compile),
|
|
|
|
"Clear cache" => %w(bundle exec rake cache:clear)
|
2014-09-02 18:07:02 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def env
|
2017-08-17 22:00:37 +05:30
|
|
|
{
|
|
|
|
'RAILS_ENV' => 'production',
|
|
|
|
'NODE_ENV' => 'production'
|
|
|
|
}
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def upgrade
|
|
|
|
update_commands.each do |title, cmd|
|
|
|
|
puts title
|
|
|
|
puts " -> #{cmd.join(' ')}"
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|