Commit 5754557d authored by Valery Sizov's avatar Valery Sizov

Geo: Implement a download to a temporary repo

parent 06fb09da
...@@ -19,7 +19,7 @@ class Geo::ProjectRegistry < Geo::BaseRegistry ...@@ -19,7 +19,7 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
where( where(
arel_table[:repository_retry_at].lt(Time.now) arel_table[:repository_retry_at].lt(Time.now)
.or(arel_table[:wiki_retry_at].lt(Time.now)) .or(arel_table[:wiki_retry_at].lt(Time.now))
.or(arel_table[:wiki_retry_at].eq(nil)) .or(arel_table[:repository_retry_at].eq(nil))
.or(arel_table[:wiki_retry_at].eq(nil)) .or(arel_table[:wiki_retry_at].eq(nil))
) )
end end
......
...@@ -170,5 +170,24 @@ module Geo ...@@ -170,5 +170,24 @@ module Geo
def last_synced_at def last_synced_at
registry.public_send("last_#{type}_synced_at") # rubocop:disable GitlabSecurity/PublicSend registry.public_send("last_#{type}_synced_at") # rubocop:disable GitlabSecurity/PublicSend
end end
def disk_path_temp
"#{repository.disk_path}_temp"
end
def build_temporary_repository
gitlab_shell.add_repository(project.repository_storage, disk_path_temp)
repository.clone.tap{|repo| repo.disk_path = disk_path_temp}
end
def clean_up_temporary_repository
gitlab_shell.remove_repository(project.repository_storage_path, disk_path_temp)
end
def set_temp_repository_as_main
log_info("Setting newly downloaded #{type} as a main one")
gitlab_shell.remove_repository(project.repository_storage_path, repository.disk_path)
gitlab_shell.mv_repository(project.repository_storage_path, disk_path_temp, repository.disk_path)
end
end end
end end
require 'tmpdir'
module Geo module Geo
class RepositorySyncService < BaseSyncService class RepositorySyncService < BaseSyncService
include Gitlab::ShellAdapter
self.type = :repository self.type = :repository
private private
def sync_repository(with_backup = false) def sync_repository(redownload = false)
fetch_project_repository(with_backup) fetch_project_repository(redownload)
expire_repository_caches expire_repository_caches
end end
def fetch_project_repository(with_backup) def fetch_project_repository(redownload)
log_info('Trying to fetch project repository') log_info('Trying to fetch project repository')
update_registry(started_at: DateTime.now) update_registry(started_at: DateTime.now)
if with_backup if redownload
log_info('Backup enabled') log_info('Redownloading repository')
actual_path = project.repository.path_to_repo clean_up_temporary_repository
backup_path = File.join(Dir.mktmpdir, project.path) fetch_geo_mirror(build_temporary_repository)
set_temp_repository_as_main
# Creating a backup copy and removing the main repo else
FileUtils.mv(actual_path, backup_path) project.ensure_repository
end fetch_geo_mirror(project.repository)
project.ensure_repository
fetch_geo_mirror(project.repository)
if with_backup
log_info('Removing backup copy as the repository was redownloaded successfully')
FileUtils.rm_rf(backup_path)
end end
update_registry(finished_at: DateTime.now) update_registry(finished_at: DateTime.now)
...@@ -44,12 +37,6 @@ module Geo ...@@ -44,12 +37,6 @@ module Geo
log_error('Invalid repository', e) log_error('Invalid repository', e)
registry.update(force_to_redownload_repository: true) registry.update(force_to_redownload_repository: true)
expire_repository_caches expire_repository_caches
ensure
# Backup can only exist if redownload was unsuccessful
if with_backup && File.exist?(backup_path)
FileUtils.rm_rf(actual_path)
FileUtils.mv(backup_path, actual_path)
end
end end
def expire_repository_caches def expire_repository_caches
...@@ -60,5 +47,9 @@ module Geo ...@@ -60,5 +47,9 @@ module Geo
def ssh_url_to_repo def ssh_url_to_repo
"#{primary_ssh_path_prefix}#{project.full_path}.git" "#{primary_ssh_path_prefix}#{project.full_path}.git"
end end
def repository
project.repository
end
end end
end end
module Geo module Geo
class WikiSyncService < BaseSyncService class WikiSyncService < BaseSyncService
include Gitlab::ShellAdapter
self.type = :wiki self.type = :wiki
private private
def sync_repository(with_backup = false) def sync_repository(redownload = false)
fetch_wiki_repository(with_backup) fetch_wiki_repository(redownload)
end end
def fetch_wiki_repository(with_backup) def fetch_wiki_repository(redownload)
log_info('Fetching wiki repository') log_info('Fetching wiki repository')
update_registry(started_at: DateTime.now) update_registry(started_at: DateTime.now)
if with_backup if redownload
log_info('Backup enabled') log_info('Redownloading wiki')
actual_path = project.wiki.path_to_repo clean_up_temporary_repository
backup_path = File.join(Dir.mktmpdir, 'wiki') fetch_geo_mirror(build_temporary_repository)
set_temp_repository_as_main
# Creating a backup copy and removing the main wiki else
FileUtils.mv(actual_path, backup_path) project.wiki.ensure_repository
end fetch_geo_mirror(project.wiki.repository)
project.wiki.ensure_repository
fetch_geo_mirror(project.wiki.repository)
if with_backup
log_info('Removing backup copy as the repository was redownloaded successfully')
FileUtils.rm_r(backup_path)
end end
update_registry(finished_at: DateTime.now) update_registry(finished_at: DateTime.now)
...@@ -42,15 +37,14 @@ module Geo ...@@ -42,15 +37,14 @@ module Geo
rescue Gitlab::Git::Repository::NoRepository => e rescue Gitlab::Git::Repository::NoRepository => e
log_error('Invalid wiki', e) log_error('Invalid wiki', e)
registry.update(force_to_redownload_wiki: true) registry.update(force_to_redownload_wiki: true)
ensure
# Backup can only exist if redownload was unsuccessful
if with_backup && File.exist?(backup_path)
FileUtils.mv(backup_path, actual_path)
end
end end
def ssh_url_to_wiki def ssh_url_to_wiki
"#{primary_ssh_path_prefix}#{project.full_path}.wiki.git" "#{primary_ssh_path_prefix}#{project.full_path}.wiki.git"
end end
def repository
project.wiki.repository
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment