Commit adc44d24 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Project model and repo backups: apply Rubocop rules

--HG--
branch : heptapod
parent 30aed99d5f07
...@@ -39,7 +39,7 @@ class Project < ApplicationRecord ...@@ -39,7 +39,7 @@ class Project < ApplicationRecord
BoardLimitExceeded = Class.new(StandardError) BoardLimitExceeded = Class.new(StandardError)
MERCURIAL_VCS_TYPES = ["hg", "hg_git"].freeze MERCURIAL_VCS_TYPES = %w[hg hg_git].freeze
STATISTICS_ATTRIBUTE = 'repositories_count' STATISTICS_ATTRIBUTE = 'repositories_count'
UNKNOWN_IMPORT_URL = 'http://unknown.git' UNKNOWN_IMPORT_URL = 'http://unknown.git'
# Hashed Storage versions handle rolling out new storage to project and dependents models: # Hashed Storage versions handle rolling out new storage to project and dependents models:
...@@ -888,7 +888,7 @@ def unlink_forks_upon_visibility_decrease_enabled? ...@@ -888,7 +888,7 @@ def unlink_forks_upon_visibility_decrease_enabled?
end end
def mercurial? def mercurial?
return MERCURIAL_VCS_TYPES.include?(vcs_type) MERCURIAL_VCS_TYPES.include?(vcs_type)
end end
def empty_repo? def empty_repo?
......
...@@ -17,11 +17,9 @@ def initialize(progress) ...@@ -17,11 +17,9 @@ def initialize(progress)
# (better for logging anyway), so any existing directory # (better for logging anyway), so any existing directory
# that's *not* also an hg repo (that excludes Rails root) would do. # that's *not* also an hg repo (that excludes Rails root) would do.
def hg_cmd_details def hg_cmd_details
[Gitlab.config.mercurial.bin_path, [Gitlab.config.mercurial.bin_path,
backup_repos_path, # guaranteed to exist and not an hg repo backup_repos_path, # guaranteed to exist and not an hg repo
{ "HGRCPATH" => Gitlab::Mercurial.hgrc_path, { "HGRCPATH" => Gitlab::Mercurial.hgrc_path }]
},
]
end end
def dump(max_concurrency:, max_storage_concurrency:) def dump(max_concurrency:, max_storage_concurrency:)
...@@ -60,14 +58,17 @@ def backup_project(project) ...@@ -60,14 +58,17 @@ def backup_project(project)
# TODO make a project method out of this # TODO make a project method out of this
has_git = ["git", "hg_git"].include?(project.vcs_type) has_git = ["git", "hg_git"].include?(project.vcs_type)
if has_git if has_git
Gitlab::GitalyClient::RepositoryService.new(project.repository) Gitlab::GitalyClient::RepositoryService.new(project.repository)
.create_bundle(path_to_project_bundle) .create_bundle(path_to_project_bundle)
backup_custom_hooks(project) backup_custom_hooks(project)
end end
if project.mercurial? if project.mercurial?
backup_hg_repo(project) backup_hg_repo(project)
end end
rescue => e rescue => e
progress_warn(project, e, 'Failed to backup repo') progress_warn(project, e, 'Failed to backup repo')
end end
...@@ -83,22 +84,21 @@ def backup_hg_repo(project) ...@@ -83,22 +84,21 @@ def backup_hg_repo(project)
# In Heptapod 0.12, we don't need to backup the `.hg/git`, # In Heptapod 0.12, we don't need to backup the `.hg/git`,
# which is the only binary (platform dependent) thing we have here # which is the only binary (platform dependent) thing we have here
cmd = ['tar', cmd = ['tar',
'-cf', path_to_hg_tars(project, "config-and-cache"), '-cf', path_to_hg_tars(project, "config-and-cache"),
'-C', path_to_hg_repo, '-C', path_to_hg_repo,
'--ignore-failed-read', '--ignore-failed-read',
'.hg/hgrc', '.hg/hgrc',
'.hg/hgrc.managed', '.hg/hgrc.managed',
'.hg/git-mapfile', '.hg/git-mapfile']
]
output, status = Gitlab::Popen.popen(cmd) output, status = Gitlab::Popen.popen(cmd)
progress_warn(project, cmd.join(' '), output) unless status.zero? progress_warn(project, cmd.join(' '), output) unless status == 0
# Backup the hg repository itself # Backup the hg repository itself
cmd = [hg_exe, "-R", path_to_hg_repo, cmd = [hg_exe, "-R", path_to_hg_repo,
"bundle", "--all", path_to_hg_project_bundle] "bundle", "--all", path_to_hg_project_bundle]
output, status = Gitlab::Popen.popen(cmd, hg_cwd, hg_env) output, status = Gitlab::Popen.popen(cmd, hg_cwd, hg_env)
unless status.zero? unless status == 0
progress_warn(project, cmd.join(' '), output) progress_warn(project, cmd.join(' '), output)
raise StandardError, output.strip raise StandardError, output.strip
end end
...@@ -154,9 +154,11 @@ def restore ...@@ -154,9 +154,11 @@ def restore
wiki.repository.create_from_bundle(path_to_wiki_bundle) wiki.repository.create_from_bundle(path_to_wiki_bundle)
restore_custom_hooks(wiki) restore_custom_hooks(wiki)
end end
if project.mercurial? if project.mercurial?
try_restore_hg_repo(wiki, !has_git) try_restore_hg_repo(wiki, !has_git)
end end
progress.puts "[DONE]".color(:green) progress.puts "[DONE]".color(:green)
rescue => e rescue => e
progress.puts "[Failed] restoring #{wiki.full_path} wiki".color(:red) progress.puts "[Failed] restoring #{wiki.full_path} wiki".color(:red)
...@@ -177,7 +179,8 @@ def restore ...@@ -177,7 +179,8 @@ def restore
# a Git restoration happens first or not. # a Git restoration happens first or not.
def try_restore_hg_repo(project, create_if_missing = true) def try_restore_hg_repo(project, create_if_missing = true)
path_to_hg_project_bundle = path_to_hg_bundle(project) path_to_hg_project_bundle = path_to_hg_bundle(project)
if !File.exist?(path_to_hg_project_bundle)
unless File.exist?(path_to_hg_project_bundle)
project.repository.create_repository if create_if_missing project.repository.create_repository if create_if_missing
return return
end end
...@@ -188,21 +191,20 @@ def try_restore_hg_repo(project, create_if_missing = true) ...@@ -188,21 +191,20 @@ def try_restore_hg_repo(project, create_if_missing = true)
# Create the Mercurial repository # Create the Mercurial repository
cmd = [hg_exe, "init", path_to_hg_repo] cmd = [hg_exe, "init", path_to_hg_repo]
output, status = Gitlab::Popen.popen(cmd, hg_cwd, hg_env) output, status = Gitlab::Popen.popen(cmd, hg_cwd, hg_env)
raise StandardError, output.strip unless status.zero? raise StandardError, output.strip unless status == 0
# Untar the configuration and necessary caches # Untar the configuration and necessary caches
cmd = %W(tar -xf #{path_to_hg_tars(project, "config-and-cache")} -C #{path_to_hg_repo}) cmd = %W(tar -xf #{path_to_hg_tars(project, "config-and-cache")} -C #{path_to_hg_repo})
output, status = Gitlab::Popen.popen(cmd) output, status = Gitlab::Popen.popen(cmd)
raise StandardError, output.strip unless status.zero? raise StandardError, output.strip unless status == 0
# Unbundle the backup # Unbundle the backup
cmd = [hg_exe, "-R", path_to_hg_repo, cmd = [hg_exe, "-R", path_to_hg_repo,
"unbundle", path_to_hg_project_bundle] "unbundle", path_to_hg_project_bundle]
output, status = Gitlab::Popen.popen(cmd, hg_cwd, hg_env) output, status = Gitlab::Popen.popen(cmd, hg_cwd, hg_env)
raise StandardError, output.strip unless status.zero? raise StandardError, output.strip unless status == 0
end end
protected protected
def path_to_hg_repo(project) def path_to_hg_repo(project)
......
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