Commit 87cc5ef2 authored by George Koltsov's avatar George Koltsov

Clean up shared/tmp folder after Import/Export

- After Project/Group Import/Export is complete
  (success or failure) ensure shared/tmp folder
  is cleaned up so that there are no intermediate
  files left
parent 4bba2799
...@@ -22,7 +22,7 @@ module Groups ...@@ -22,7 +22,7 @@ module Groups
save! save!
ensure ensure
cleanup remove_base_tmp_dir
end end
private private
...@@ -81,8 +81,8 @@ module Groups ...@@ -81,8 +81,8 @@ module Groups
Gitlab::ImportExport::Saver.new(exportable: @group, shared: @shared) Gitlab::ImportExport::Saver.new(exportable: @group, shared: @shared)
end end
def cleanup def remove_base_tmp_dir
FileUtils.rm_rf(shared.archive_path) if shared&.archive_path FileUtils.rm_rf(shared.base_path) if shared&.base_path
end end
def notify_error! def notify_error!
......
...@@ -26,6 +26,7 @@ module Groups ...@@ -26,6 +26,7 @@ module Groups
end end
ensure ensure
remove_base_tmp_dir
remove_import_file remove_import_file
end end
...@@ -102,6 +103,10 @@ module Groups ...@@ -102,6 +103,10 @@ module Groups
raise Gitlab::ImportExport::Error.new(@shared.errors.to_sentence) raise Gitlab::ImportExport::Error.new(@shared.errors.to_sentence)
end end
def remove_base_tmp_dir
FileUtils.rm_rf(@shared.base_path)
end
end end
end end
end end
---
title: Clean up shared/tmp folder after Import/Export
merge_request: 32326
author:
type: fixed
...@@ -26,6 +26,7 @@ module Gitlab ...@@ -26,6 +26,7 @@ module Gitlab
rescue => e rescue => e
raise Projects::ImportService::Error.new(e.message) raise Projects::ImportService::Error.new(e.message)
ensure ensure
remove_base_tmp_dir
remove_import_file remove_import_file
end end
...@@ -148,6 +149,10 @@ module Gitlab ...@@ -148,6 +149,10 @@ module Gitlab
::Project.find_by_full_path("#{project.namespace.full_path}/#{original_path}") ::Project.find_by_full_path("#{project.namespace.full_path}/#{original_path}")
end end
end end
def remove_base_tmp_dir
FileUtils.rm_rf(@shared.base_path)
end
end end
end end
end end
...@@ -16,8 +16,6 @@ module Gitlab ...@@ -16,8 +16,6 @@ module Gitlab
def save def save
if compress_and_save if compress_and_save
remove_export_path
Gitlab::Export::Logger.info( Gitlab::Export::Logger.info(
message: 'Export archive saved', message: 'Export archive saved',
exportable_class: @exportable.class.to_s, exportable_class: @exportable.class.to_s,
...@@ -33,8 +31,7 @@ module Gitlab ...@@ -33,8 +31,7 @@ module Gitlab
@shared.error(e) @shared.error(e)
false false
ensure ensure
remove_archive remove_base_tmp_dir
remove_export_path
end end
private private
...@@ -43,12 +40,8 @@ module Gitlab ...@@ -43,12 +40,8 @@ module Gitlab
tar_czf(archive: archive_file, dir: @shared.export_path) tar_czf(archive: archive_file, dir: @shared.export_path)
end end
def remove_export_path def remove_base_tmp_dir
FileUtils.rm_rf(@shared.export_path) FileUtils.rm_rf(@shared.base_path)
end
def remove_archive
FileUtils.rm_rf(@shared.archive_path)
end end
def archive_file def archive_file
......
...@@ -18,6 +18,7 @@ describe Gitlab::ImportExport::Importer do ...@@ -18,6 +18,7 @@ describe Gitlab::ImportExport::Importer do
FileUtils.mkdir_p(shared.export_path) FileUtils.mkdir_p(shared.export_path)
ImportExportUpload.create(project: project, import_file: import_file) ImportExportUpload.create(project: project, import_file: import_file)
allow(FileUtils).to receive(:rm_rf).and_call_original
end end
after do after do
...@@ -78,6 +79,13 @@ describe Gitlab::ImportExport::Importer do ...@@ -78,6 +79,13 @@ describe Gitlab::ImportExport::Importer do
expect(project.import_export_upload.import_file&.file).to be_nil expect(project.import_export_upload.import_file&.file).to be_nil
end end
it 'removes tmp files' do
importer.execute
expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
expect(Dir.exist?(shared.base_path)).to eq(false)
end
it 'sets the correct visibility_level when visibility level is a string' do it 'sets the correct visibility_level when visibility level is a string' do
project.create_or_update_import_data( project.create_or_update_import_data(
data: { override_params: { visibility_level: Gitlab::VisibilityLevel::PRIVATE.to_s } } data: { override_params: { visibility_level: Gitlab::VisibilityLevel::PRIVATE.to_s } }
......
...@@ -5,18 +5,21 @@ require 'fileutils' ...@@ -5,18 +5,21 @@ require 'fileutils'
describe Gitlab::ImportExport::Saver do describe Gitlab::ImportExport::Saver do
let!(:project) { create(:project, :public, name: 'project') } let!(:project) { create(:project, :public, name: 'project') }
let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" } let(:base_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
let(:export_path) { "#{base_path}/project_tree_saver_spec/export" }
let(:shared) { project.import_export_shared } let(:shared) { project.import_export_shared }
subject { described_class.new(exportable: project, shared: shared) } subject { described_class.new(exportable: project, shared: shared) }
before do before do
allow(shared).to receive(:base_path).and_return(base_path)
allow_next_instance_of(Gitlab::ImportExport) do |instance| allow_next_instance_of(Gitlab::ImportExport) do |instance|
allow(instance).to receive(:storage_path).and_return(export_path) allow(instance).to receive(:storage_path).and_return(export_path)
end end
FileUtils.mkdir_p(shared.export_path) FileUtils.mkdir_p(shared.export_path)
FileUtils.touch("#{shared.export_path}/tmp.bundle") FileUtils.touch("#{shared.export_path}/tmp.bundle")
allow(FileUtils).to receive(:rm_rf).and_call_original
end end
after do after do
...@@ -31,4 +34,11 @@ describe Gitlab::ImportExport::Saver do ...@@ -31,4 +34,11 @@ describe Gitlab::ImportExport::Saver do
expect(ImportExportUpload.find_by(project: project).export_file.url) expect(ImportExportUpload.find_by(project: project).export_file.url)
.to match(%r[\/uploads\/-\/system\/import_export_upload\/export_file.*]) .to match(%r[\/uploads\/-\/system\/import_export_upload\/export_file.*])
end end
it 'removes tmp files' do
subject.save
expect(FileUtils).to have_received(:rm_rf).with(base_path)
expect(Dir.exist?(base_path)).to eq(false)
end
end end
...@@ -134,7 +134,7 @@ describe Groups::ImportExport::ExportService do ...@@ -134,7 +134,7 @@ describe Groups::ImportExport::ExportService do
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error) expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
expect(group.import_export_upload).to be_nil expect(group.import_export_upload).to be_nil
expect(File.exist?(shared.archive_path)).to eq(false) expect(Dir.exist?(shared.base_path)).to eq(false)
end end
it 'notifies the user about failed group export' do it 'notifies the user about failed group export' do
...@@ -159,7 +159,7 @@ describe Groups::ImportExport::ExportService do ...@@ -159,7 +159,7 @@ describe Groups::ImportExport::ExportService do
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error) expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
expect(group.import_export_upload).to be_nil expect(group.import_export_upload).to be_nil
expect(File.exist?(shared.archive_path)).to eq(false) expect(Dir.exist?(shared.base_path)).to eq(false)
end end
it 'notifies logger' do it 'notifies logger' do
......
...@@ -91,6 +91,7 @@ describe Groups::ImportExport::ImportService do ...@@ -91,6 +91,7 @@ describe Groups::ImportExport::ImportService do
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger) allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
allow(import_logger).to receive(:error) allow(import_logger).to receive(:error)
allow(import_logger).to receive(:info) allow(import_logger).to receive(:info)
allow(FileUtils).to receive(:rm_rf).and_call_original
end end
context 'when user has correct permissions' do context 'when user has correct permissions' do
...@@ -104,6 +105,16 @@ describe Groups::ImportExport::ImportService do ...@@ -104,6 +105,16 @@ describe Groups::ImportExport::ImportService do
expect(group.import_export_upload.import_file.file).to be_nil expect(group.import_export_upload.import_file.file).to be_nil
end end
it 'removes tmp files' do
shared = Gitlab::ImportExport::Shared.new(group)
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
subject
expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
expect(Dir.exist?(shared.base_path)).to eq(false)
end
it 'logs the import success' do it 'logs the import success' do
expect(import_logger).to receive(:info).with( expect(import_logger).to receive(:info).with(
group_id: group.id, group_id: group.id,
...@@ -191,6 +202,7 @@ describe Groups::ImportExport::ImportService do ...@@ -191,6 +202,7 @@ describe Groups::ImportExport::ImportService do
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger) allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
allow(import_logger).to receive(:error) allow(import_logger).to receive(:error)
allow(import_logger).to receive(:info) allow(import_logger).to receive(:info)
allow(FileUtils).to receive(:rm_rf).and_call_original
end end
context 'when user has correct permissions' do context 'when user has correct permissions' do
...@@ -204,6 +216,16 @@ describe Groups::ImportExport::ImportService do ...@@ -204,6 +216,16 @@ describe Groups::ImportExport::ImportService do
expect(group.import_export_upload.import_file.file).to be_nil expect(group.import_export_upload.import_file.file).to be_nil
end end
it 'removes tmp files' do
shared = Gitlab::ImportExport::Shared.new(group)
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
subject
expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
expect(Dir.exist?(shared.base_path)).to eq(false)
end
it 'logs the import success' do it 'logs the import success' do
expect(import_logger).to receive(:info).with( expect(import_logger).to receive(:info).with(
group_id: group.id, group_id: group.id,
......
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