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
where(
arel_table[:repository_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))
)
end
......
......@@ -170,5 +170,24 @@ module Geo
def last_synced_at
registry.public_send("last_#{type}_synced_at") # rubocop:disable GitlabSecurity/PublicSend
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
require 'tmpdir'
module Geo
class RepositorySyncService < BaseSyncService
include Gitlab::ShellAdapter
self.type = :repository
private
def sync_repository(with_backup = false)
fetch_project_repository(with_backup)
def sync_repository(redownload = false)
fetch_project_repository(redownload)
expire_repository_caches
end
def fetch_project_repository(with_backup)
def fetch_project_repository(redownload)
log_info('Trying to fetch project repository')
update_registry(started_at: DateTime.now)
if with_backup
log_info('Backup enabled')
actual_path = project.repository.path_to_repo
backup_path = File.join(Dir.mktmpdir, project.path)
# Creating a backup copy and removing the main repo
FileUtils.mv(actual_path, backup_path)
end
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)
if redownload
log_info('Redownloading repository')
clean_up_temporary_repository
fetch_geo_mirror(build_temporary_repository)
set_temp_repository_as_main
else
project.ensure_repository
fetch_geo_mirror(project.repository)
end
update_registry(finished_at: DateTime.now)
......@@ -44,12 +37,6 @@ module Geo
log_error('Invalid repository', e)
registry.update(force_to_redownload_repository: true)
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
def expire_repository_caches
......@@ -60,5 +47,9 @@ module Geo
def ssh_url_to_repo
"#{primary_ssh_path_prefix}#{project.full_path}.git"
end
def repository
project.repository
end
end
end
module Geo
class WikiSyncService < BaseSyncService
include Gitlab::ShellAdapter
self.type = :wiki
private
def sync_repository(with_backup = false)
fetch_wiki_repository(with_backup)
def sync_repository(redownload = false)
fetch_wiki_repository(redownload)
end
def fetch_wiki_repository(with_backup)
def fetch_wiki_repository(redownload)
log_info('Fetching wiki repository')
update_registry(started_at: DateTime.now)
if with_backup
log_info('Backup enabled')
actual_path = project.wiki.path_to_repo
backup_path = File.join(Dir.mktmpdir, 'wiki')
# Creating a backup copy and removing the main wiki
FileUtils.mv(actual_path, backup_path)
end
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)
if redownload
log_info('Redownloading wiki')
clean_up_temporary_repository
fetch_geo_mirror(build_temporary_repository)
set_temp_repository_as_main
else
project.wiki.ensure_repository
fetch_geo_mirror(project.wiki.repository)
end
update_registry(finished_at: DateTime.now)
......@@ -42,15 +37,14 @@ module Geo
rescue Gitlab::Git::Repository::NoRepository => e
log_error('Invalid wiki', e)
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
def ssh_url_to_wiki
"#{primary_ssh_path_prefix}#{project.full_path}.wiki.git"
end
def repository
project.wiki.repository
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