Commit 2f552fe3 authored by Alex Kalderimis's avatar Alex Kalderimis

Read root from config, simpler orphan finding

This uses Gitlab.config to establish the root, and simplifies the
method of finding orphans.

Output messages are made more informative.
parent b212f825
# frozen_string_literal: true # frozen_string_literal: true
require 'find'
module Gitlab module Gitlab
module HashedStorage module HashedStorage
module RakeHelper module RakeHelper
...@@ -96,34 +94,28 @@ module Gitlab ...@@ -96,34 +94,28 @@ module Gitlab
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def self.prune(relation_name, relation) def self.prune(relation_name, relation, dry_run: true, root: nil)
root = ENV['GDK_REPOSITORY_ROOT'].presence || '../repositories' root ||= '../repositories'
dry_run = !ENV['FORCE'].present?
known_paths = Set.new known_paths = Set.new
listing(relation_name, relation) { |p| known_paths << "#{root}/#{p.repository.disk_path}" } listing(relation_name, relation) { |p| known_paths << "#{root}/#{p.repository.disk_path}" }
marked_for_deletion = Set.new marked_for_deletion = Set.new(Dir["#{root}/@hashed/*/*/*"])
prefix_length = Pathname.new(root).ascend.count marked_for_deletion.reject! do |path|
base = path.gsub(/\.(\w+\.)?git$/, '')
Find.find("#{root}/@hashed") do |path| known_paths.include?(base)
path = Pathname.new(path)
next unless path.directory?
path.ascend do |p|
base = p.to_s.gsub(/\.(\w+\.)?git$/, '')
Find.prune if known_paths.include?(base)
end
if path.ascend.count == prefix_length + 4
marked_for_deletion << path
Find.prune
end
end end
$stdout.puts "Dry run. We would have deleted:" if dry_run if marked_for_deletion.empty?
$stdout.puts "No orphaned directories found. Nothing to do!"
else
n = marked_for_deletion.size
$stdout.puts "Found #{n} orphaned #{'directory'.pluralize(n)}"
$stdout.puts "Dry run. (Run again with FORCE=1 to delete). We would have deleted:" if dry_run
end
marked_for_deletion.each do |p| marked_for_deletion.each do |p|
p = Pathname.new(p)
if dry_run if dry_run
$stdout.puts " - #{p}" $stdout.puts " - #{p}"
else else
......
require 'find'
namespace :gitlab do namespace :gitlab do
namespace :storage do namespace :storage do
desc 'GitLab | Storage | Migrate existing projects to Hashed Storage' desc 'GitLab | Storage | Migrate existing projects to Hashed Storage'
...@@ -119,7 +117,7 @@ namespace :gitlab do ...@@ -119,7 +117,7 @@ namespace :gitlab do
end end
desc 'Gitlab | Storage | Prune projects using Hashed Storage. Remove all hashed directories that do not have a project associated' desc 'Gitlab | Storage | Prune projects using Hashed Storage. Remove all hashed directories that do not have a project associated'
task prune_hashed_projects: :environment do task prune_hashed_projects: [:environment, :gitlab_environment] do
if Rails.env.production? if Rails.env.production?
abort('This destructive action may only be run in development') abort('This destructive action may only be run in development')
end end
...@@ -127,8 +125,10 @@ namespace :gitlab do ...@@ -127,8 +125,10 @@ namespace :gitlab do
helper = Gitlab::HashedStorage::RakeHelper helper = Gitlab::HashedStorage::RakeHelper
name = 'projects using Hashed Storage' name = 'projects using Hashed Storage'
relation = Project.with_storage_feature(:repository) relation = Project.with_storage_feature(:repository)
root = Gitlab.config.repositories.storages['default'].legacy_disk_path
dry_run = !ENV['FORCE'].present?
helper.prune(name, relation) helper.prune(name, relation, dry_run: dry_run, root: root)
end end
desc 'Gitlab | Storage | Summary of project attachments using Legacy Storage' desc 'Gitlab | Storage | Summary of project attachments using Legacy Storage'
......
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