2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-13 08:18:57 -05:00
|
|
|
module Gitlab
|
|
|
|
class Upgrader
|
|
|
|
def execute
|
2013-12-14 07:10:24 -05:00
|
|
|
puts "GitLab #{current_version.major} upgrade tool"
|
2013-12-13 08:18:57 -05:00
|
|
|
puts "Your version is #{current_version}"
|
|
|
|
puts "Latest available version for GitLab #{current_version.major} is #{latest_version}"
|
|
|
|
|
|
|
|
if latest_version?
|
2014-01-25 18:33:55 -05:00
|
|
|
puts "You are using the latest GitLab version"
|
2013-12-13 08:18:57 -05:00
|
|
|
else
|
|
|
|
puts "Newer GitLab version is available"
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2013-12-13 09:23:38 -05:00
|
|
|
answer = if ARGV.first == "-y"
|
2013-12-13 09:17:54 -05:00
|
|
|
"yes"
|
|
|
|
else
|
2013-12-14 07:11:31 -05:00
|
|
|
prompt("Do you want to upgrade (yes/no)? ", %w{yes no})
|
2013-12-13 09:17:54 -05:00
|
|
|
end
|
2013-12-13 08:18:57 -05:00
|
|
|
|
|
|
|
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-05-23 06:11:23 -04:00
|
|
|
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-02-22 13:18:40 -05:00
|
|
|
"v#{git_versions.sort.last}"
|
2015-05-23 06:11:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_git_tags
|
2015-11-03 17:10:38 -05:00
|
|
|
remote_tags, _ = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} ls-remote --tags https://gitlab.com/gitlab-org/gitlab-ce.git))
|
2018-01-27 00:35:53 -05:00
|
|
|
remote_tags.split("\n").grep(%r{tags/v#{current_version.major}})
|
2013-12-13 08:18:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_commands
|
|
|
|
{
|
2015-11-03 17:10:38 -05:00
|
|
|
"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-02-22 13:18:40 -05:00
|
|
|
"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)
|
2013-12-13 08:18:57 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-02-25 06:15:18 -05:00
|
|
|
def env
|
2017-02-16 02:05:31 -05:00
|
|
|
{
|
|
|
|
'RAILS_ENV' => 'production',
|
|
|
|
'NODE_ENV' => 'production'
|
|
|
|
}
|
2014-02-25 06:15:18 -05:00
|
|
|
end
|
|
|
|
|
2013-12-13 08:18:57 -05:00
|
|
|
def upgrade
|
|
|
|
update_commands.each do |title, cmd|
|
2013-12-14 07:10:24 -05:00
|
|
|
puts title
|
2014-02-25 06:15:18 -05:00
|
|
|
puts " -> #{cmd.join(' ')}"
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2014-02-25 06:15:18 -05:00
|
|
|
if system(env, *cmd)
|
2013-12-14 07:10:24 -05:00
|
|
|
puts " -> OK"
|
2013-12-13 08:18:57 -05:00
|
|
|
else
|
2013-12-14 07:10:24 -05:00
|
|
|
puts " -> FAILED"
|
2013-12-13 08:18:57 -05:00
|
|
|
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
|