Merge branch 'rerun_db_migrate' into 'master'

Rerun db migrate, run on revision change

Different take on !698 

See merge request !700
This commit is contained in:
Marin Jankovski 2016-03-31 14:14:41 +00:00
commit 3b42520a34
4 changed files with 40 additions and 45 deletions

View File

@ -51,6 +51,7 @@ build do
# source code to include the Git revision of the code included in the omnibus
# build.
command "sed -i \"s/.*REVISION.*/REVISION = '$(git log --pretty=format:'%h' -n 1)'/\" config/initializers/2_app.rb"
command "echo $(git log --pretty=format:'%h' -n 1) > REVISION"
bundle_without = %w{development test}
bundle_without << "mysql" unless EE

View File

@ -1,35 +0,0 @@
#
# Copyright:: Copyright (c) 2014 GitLab B.V.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
define :migrate_database, :command => nil, :action => :run, :restarts => [] do
bash "migrate #{params[:name]} database" do
code <<-EOH
set -e
log_file="/tmp/#{params[:name]}-db-migrate-$(date +%s)-$$/output.log"
umask 077
mkdir $(dirname ${log_file})
#{params[:command]} 2>& 1 | tee ${log_file}
exit ${PIPESTATUS[0]}
EOH
action params[:action]
notifies :run, 'execute[enable pg_trgm extension]', :before unless OmnibusHelper.not_listening?("posgresql") || !node['gitlab']['postgresql']['enable']
notifies :run, "execute[clear the #{params[:name]} cache]", :immediately unless OmnibusHelper.not_listening?("redis")
params[:restarts].each do |svc|
notifies :restart, svc, :immediately
end
end
end

View File

@ -21,6 +21,13 @@ dependent_services = []
dependent_services << "service[unicorn]" if OmnibusHelper.should_notify?("unicorn")
dependent_services << "service[sidekiq]" if OmnibusHelper.should_notify?("sidekiq")
revision_file = ::File.join(node['gitlab']['gitlab-rails']['dir'], "REVISION")
if ::File.exist?(revision_file)
revision = IO.read(revision_file).chomp
end
upgrade_status_dir = ::File.join(node['gitlab']['gitlab-rails']['dir'], "upgrade-status")
db_migrate_status_file = ::File.join(upgrade_status_dir, "db-migrate-#{revision}")
execute "initialize gitlab-rails database" do
command "/opt/gitlab/bin/gitlab-rake db:schema:load db:seed_fu"
environment ({'GITLAB_ROOT_PASSWORD' => initial_root_password }) if initial_root_password
@ -28,8 +35,24 @@ execute "initialize gitlab-rails database" do
notifies :run, 'execute[enable pg_trgm extension]', :before unless OmnibusHelper.not_listening?("posgresql") || !node['gitlab']['postgresql']['enable']
end
migrate_database 'gitlab-rails' do
command '/opt/gitlab/bin/gitlab-rake db:migrate'
action :nothing
restarts dependent_services
# TODO: Refactor this into a resource
# Currently blocked due to a bug in Chef 12.6.0
# https://github.com/chef/chef/issues/4537
bash "migrate gitlab-rails database" do
code <<-EOH
set -e
log_file="/tmp/gitlab-rails-db-migrate-$(date +%s)-$$/output.log"
umask 077
mkdir $(dirname ${log_file})
/opt/gitlab/bin/gitlab-rake db:migrate 2>& 1 | tee ${log_file}
STATUS=${PIPESTATUS[0]}
echo $STATUS > #{db_migrate_status_file}
exit $STATUS
EOH
notifies :run, 'execute[enable pg_trgm extension]', :before unless OmnibusHelper.not_listening?("postgresql") || !node['gitlab']['postgresql']['enable']
notifies :run, "execute[clear the gitlab-rails cache]", :immediately unless OmnibusHelper.not_listening?("redis")
dependent_services.each do |svc|
notifies :restart, svc, :immediately
end
not_if "(test -f #{db_migrate_status_file}) && (cat #{db_migrate_status_file} | grep -Fx 0)"
end

View File

@ -28,6 +28,7 @@ gitlab_rails_public_uploads_dir = node['gitlab']['gitlab-rails']['uploads_direct
gitlab_rails_log_dir = node['gitlab']['gitlab-rails']['log_directory']
gitlab_ci_dir = node['gitlab']['gitlab-ci']['dir']
gitlab_ci_builds_dir = node['gitlab']['gitlab-ci']['builds_directory']
upgrade_status_dir = File.join(gitlab_rails_dir, "upgrade-status")
ssh_dir = File.join(node['gitlab']['user']['home'], ".ssh")
known_hosts = File.join(ssh_dir, "known_hosts")
@ -82,6 +83,7 @@ end
gitlab_rails_working_dir,
gitlab_rails_tmp_dir,
node['gitlab']['gitlab-rails']['gitlab_repository_downloads_path'],
upgrade_status_dir,
gitlab_rails_log_dir
].compact.each do |dir_name|
directory dir_name do
@ -286,15 +288,16 @@ file "/opt/gitlab/embedded/service/gitlab-rails/db/schema.rb" do
owner gitlab_user
end
# Only run `rake db:migrate` when the gitlab-rails version has changed
# Link the VERSION file just for easier administration
remote_file File.join(gitlab_rails_dir, 'VERSION') do
source "file:///opt/gitlab/embedded/service/gitlab-rails/VERSION"
notifies :run, 'bash[migrate gitlab-rails database]' unless postgresql_not_listening
notifies :run, 'execute[clear the gitlab-rails cache]' unless redis_not_listening
end
# Only run `rake db:migrate` when the gitlab-rails version has changed
# Or migration failed for some reason
remote_file File.join(gitlab_rails_dir, 'REVISION') do
source "file:///opt/gitlab/embedded/service/gitlab-rails/REVISION"
notifies :run, 'bash[generate assets]' if node['gitlab']['gitlab-rails']['gitlab_relative_url']
dependent_services.each do |sv|
notifies :restart, sv
end
end
# If a version of ruby changes restart unicorn. If not, unicorn will fail to
@ -325,6 +328,9 @@ bash "generate assets" do
EOS
# We have to precompile assets as root because of permissions and ownership of files
environment ({ 'NO_PRIVILEGE_DROP' => 'true', 'USE_DB' => 'false' })
dependent_services.each do |sv|
notifies :restart, sv
end
action :nothing
end