From 09011c85657b98c77b554ef2c5217b3c4478d6f3 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Mon, 3 Jun 2019 10:56:27 -0500 Subject: [PATCH] Moved Dor::CleanupService to dor-services-app --- config/config_defaults.yml | 4 - lib/dor-services.rb | 1 - lib/dor/services/cleanup_service.rb | 48 ------ lib/dor/static_config.rb | 2 - lib/dor/static_config/cleanup_config.rb | 27 ---- spec/services/cleanup_service_spec.rb | 203 ------------------------ 6 files changed, 285 deletions(-) delete mode 100644 lib/dor/services/cleanup_service.rb delete mode 100644 lib/dor/static_config/cleanup_config.rb delete mode 100644 spec/services/cleanup_service_spec.rb diff --git a/config/config_defaults.yml b/config/config_defaults.yml index 3e2b1deb..4bbd0755 100644 --- a/config/config_defaults.yml +++ b/config/config_defaults.yml @@ -1,8 +1,4 @@ --- -cleanup: - local_workspace_root: /dor/workspace - local_assembly_root: /dor/assembly - local_export_home: /dor/export ssl: cert_file: key_file: diff --git a/lib/dor-services.rb b/lib/dor-services.rb index 7342bd92..e827128f 100644 --- a/lib/dor-services.rb +++ b/lib/dor-services.rb @@ -107,7 +107,6 @@ def logger # Services autoload_under 'services' do - autoload :CleanupService autoload :CollectionService autoload :CreativeCommonsLicenseService autoload :EmbargoService diff --git a/lib/dor/services/cleanup_service.rb b/lib/dor/services/cleanup_service.rb deleted file mode 100644 index 628df2b6..00000000 --- a/lib/dor/services/cleanup_service.rb +++ /dev/null @@ -1,48 +0,0 @@ -# frozen_string_literal: true - -require 'pathname' - -module Dor - # Remove all traces of the object's data files from the workspace and export areas - class CleanupService - # @param [LyberCore::Robots::WorkItem] dor_item The DOR work item whose workspace should be cleaned up - # @return [void] Delete all workspace and export entities for the druid - def self.cleanup(dor_item) - druid = dor_item.respond_to?(:druid) ? dor_item.druid : dor_item.id - cleanup_by_druid druid - end - - def self.cleanup_by_druid(druid) - cleanup_workspace_content(druid, Config.cleanup.local_workspace_root) - cleanup_workspace_content(druid, Config.cleanup.local_assembly_root) - cleanup_export(druid) - end - - # @param [String] druid The identifier for the object whose data is to be removed - # @param [String] base The base directory to delete from - # @return [void] remove the object's data files from the workspace area - def self.cleanup_workspace_content(druid, base) - DruidTools::Druid.new(druid, base).prune! - end - private_class_method :cleanup_workspace_content - - # @param [String] druid The identifier for the object whose data is to be removed - # @return [void] remove copy of the data that was exported to preservation core - def self.cleanup_export(druid) - id = druid.split(':').last - bag_dir = File.join(Config.cleanup.local_export_home, id) - remove_branch(bag_dir) - tarfile = "#{bag_dir}.tar" - remove_branch(tarfile) - end - private_class_method :cleanup_export - - # @param [Pathname,String] pathname The full path of the branch to be removed - # @return [void] Remove the specified directory and all its children - def self.remove_branch(pathname) - pathname = Pathname(pathname) if pathname.instance_of? String - pathname.rmtree if pathname.exist? - end - private_class_method :remove_branch - end -end diff --git a/lib/dor/static_config.rb b/lib/dor/static_config.rb index 27b8f063..4be00be2 100644 --- a/lib/dor/static_config.rb +++ b/lib/dor/static_config.rb @@ -7,7 +7,6 @@ module Dor class StaticConfig extend ActiveSupport::Autoload eager_autoload do - autoload :CleanupConfig autoload :SslConfig autoload :FedoraConfig autoload :SolrConfig @@ -17,7 +16,6 @@ class StaticConfig end def initialize(hash) - @cleanup = CleanupConfig.new(hash.fetch(:cleanup)) @ssl = SslConfig.new(hash.fetch(:ssl)) @fedora = FedoraConfig.new(hash.fetch(:fedora)) @solr = SolrConfig.new(hash.fetch(:solr)) diff --git a/lib/dor/static_config/cleanup_config.rb b/lib/dor/static_config/cleanup_config.rb deleted file mode 100644 index e6db599c..00000000 --- a/lib/dor/static_config/cleanup_config.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module Dor - class StaticConfig - # Represents the configuration for the Dor::CleanupService - class CleanupConfig - def initialize(hash) - @local_workspace_root = hash.fetch(:local_workspace_root) - end - - def local_export_home(new_value = nil) - @local_export_home = new_value if new_value - @local_export_home - end - - def local_workspace_root(new_value = nil) - @local_workspace_root = new_value if new_value - @local_workspace_root - end - - def local_assembly_root(new_value = nil) - @local_assembly_root = new_value if new_value - @local_assembly_root - end - end - end -end diff --git a/spec/services/cleanup_service_spec.rb b/spec/services/cleanup_service_spec.rb deleted file mode 100644 index cf5005d3..00000000 --- a/spec/services/cleanup_service_spec.rb +++ /dev/null @@ -1,203 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'pathname' -require 'druid-tools' - -RSpec.describe Dor::CleanupService do - attr_reader :fixture_dir - - let(:fixtures) { Pathname(File.dirname(__FILE__)).join('../fixtures') } - let(:cleanup_config) do - instance_double( - Dor::StaticConfig::CleanupConfig, - local_workspace_root: fixtures.join('workspace').to_s, - local_export_home: fixtures.join('export').to_s, - local_assembly_root: fixtures.join('assembly').to_s - ) - end - - before do - allow(Dor::Config).to receive(:cleanup).and_return(cleanup_config) - - @druid = 'druid:aa123bb4567' - @workspace_root_pathname = Pathname(Dor::Config.cleanup.local_workspace_root) - @workitem_pathname = Pathname(DruidTools::Druid.new(@druid, @workspace_root_pathname.to_s).path) - @workitem_pathname.rmtree if @workitem_pathname.exist? - @export_pathname = Pathname(Dor::Config.cleanup.local_export_home) - @export_pathname.rmtree if @export_pathname.exist? - @bag_pathname = @export_pathname.join(@druid.split(':').last) - @tarfile_pathname = @export_pathname.join(@bag_pathname + '.tar') - - @workitem_pathname.join('content').mkpath - @workitem_pathname.join('temp').mkpath - @bag_pathname.mkpath - @tarfile_pathname.open('w') { |file| file.write("test tar\n") } - end - - after do - item_root_branch = @workspace_root_pathname.join('aa') - item_root_branch.rmtree if item_root_branch.exist? - @bag_pathname.rmtree if @bag_pathname.exist? - @tarfile_pathname.rmtree if @tarfile_pathname.exist? - end - - it 'can access configuration settings' do - cleanup = Dor::Config.cleanup - expect(cleanup.local_workspace_root).to eql fixtures.join('workspace').to_s - expect(cleanup.local_export_home).to eql fixtures.join('export').to_s - end - - it 'can find the fixtures workspace and export folders' do - expect(File).to be_directory(Dor::Config.cleanup.local_workspace_root) - expect(File).to be_directory(Dor::Config.cleanup.local_export_home) - end - - specify 'Dor::CleanupService.cleanup' do - expect(described_class).to receive(:cleanup_export).once.with(@druid) - mock_item = double('item') - expect(mock_item).to receive(:druid).and_return(@druid) - described_class.cleanup(mock_item) - end - - describe '.cleanup_export' do - it 'removes the files exported to preservation' do - expect(described_class).to receive(:remove_branch).once.with(fixtures.join('export/aa123bb4567').to_s) - expect(described_class).to receive(:remove_branch).once.with(fixtures.join('export/aa123bb4567.tar').to_s) - described_class.send(:cleanup_export, @druid) - end - end - - describe '.remove_branch' do - specify 'Dor::CleanupService.remove_branch non-existing branch' do - @bag_pathname.rmtree if @bag_pathname.exist? - expect(@bag_pathname).not_to receive(:rmtree) - described_class.send(:remove_branch, @bag_pathname) - end - - specify 'Dor::CleanupService.remove_branch existing branch' do - @bag_pathname.mkpath - expect(@bag_pathname).to exist - allow(@bag_pathname).to receive(:rmtree) - described_class.send(:remove_branch, @bag_pathname) - expect(@bag_pathname).to have_received(:rmtree) - end - end - - describe '#cleanup' do - it 'can do a complete cleanup' do - expect(@workitem_pathname.join('content')).to exist - expect(@bag_pathname).to exist - expect(@tarfile_pathname).to exist - mock_item = double('item') - expect(mock_item).to receive(:druid).and_return(@druid) - described_class.cleanup(mock_item) - expect(@workitem_pathname.parent.parent.parent.parent).not_to exist - expect(@bag_pathname).not_to exist - expect(@tarfile_pathname).not_to exist - end - end - - context 'with real files' do - let(:fixture_dir) { '/tmp/cleanup-spec' } - let(:workspace_dir) { File.join(fixture_dir, 'workspace') } - let(:export_dir) { File.join(fixture_dir, 'export') } - let(:assembly_dir) { File.join(fixture_dir, 'assembly') } - let(:stacks_dir) { File.join(fixture_dir, 'stacks') } - - let(:druid_1) { 'druid:cd456ef7890' } - let(:druid_2) { 'druid:cd456gh1234' } - - before do - cleanup = Dor::Config.cleanup - allow(cleanup).to receive_messages( - local_workspace_root: workspace_dir, - local_export_home: export_dir, - local_assembly_root: assembly_dir - ) - allow(Dor::Config.stacks).to receive(:local_stacks_root).and_return(stacks_dir) - - FileUtils.mkdir fixture_dir - FileUtils.mkdir workspace_dir - FileUtils.mkdir export_dir - FileUtils.mkdir assembly_dir - FileUtils.mkdir stacks_dir - end - - after do - FileUtils.rm_rf fixture_dir - end - - def create_tempfile(path) - File.open(File.join(path, 'tempfile'), 'w') do |tf1| - tf1.write 'junk' - end - end - - context '.cleanup' do - let(:item1) { double('item1') } - let(:item2) { double('item1') } - - before do - allow(item1).to receive(:druid) { druid_1 } - allow(item2).to receive(:druid) { druid_2 } - end - - it 'correctly prunes directories' do - dr1_wspace = DruidTools::Druid.new(druid_1, workspace_dir) - dr2_wspace = DruidTools::Druid.new(druid_2, workspace_dir) - dr1_assembly = DruidTools::Druid.new(druid_1, assembly_dir) - dr2_assembly = DruidTools::Druid.new(druid_2, assembly_dir) - - dr1_wspace.mkdir - dr2_wspace.mkdir - dr1_assembly.mkdir - dr2_assembly.mkdir - - # Add some 'content' - create_tempfile dr1_wspace.path - create_tempfile dr2_assembly.path - - # Setup the export content, remove 'druid:' prefix for bag and export/workspace dir - dr1 = druid_1.split(':').last - export_prefix = File.join(export_dir, dr1) - - # Create {export_dir}/druid1 - # {export_dir}/druid1/tempfile - # {export_dir}/druid1.tar - FileUtils.mkdir export_prefix - create_tempfile export_prefix - File.open(export_prefix + '.tar', 'w') { |f| f.write 'fake tar junk' } - - expect(File).to exist(dr1_wspace.path) - expect(File).to exist(dr1_assembly.path) - - # druid_1 cleaned up, including files - described_class.cleanup item1 - expect(File).not_to exist(dr1_wspace.path) - expect(File).not_to exist(dr1_assembly.path) - expect(File).not_to exist(export_prefix) - expect(File).not_to exist(export_prefix + '.tar') - - # But not druid_2 - expect(File).to exist(dr2_wspace.path) - expect(File).to exist(dr2_assembly.path) - - described_class.cleanup item2 - expect(File).not_to exist(dr2_wspace.path) - expect(File).not_to exist(dr2_assembly.path) - - # Empty common parent directories pruned - expect(File).not_to exist(File.join(workspace_dir, 'cd')) - end - - it 'cleans up without assembly content' do - dr1_wspace = DruidTools::Druid.new(druid_1, workspace_dir) - dr1_wspace.mkdir - - described_class.cleanup item1 - expect(File).not_to exist(dr1_wspace.path) - end - end - end -end