Use full dir listing instead of per subdir
This commit is contained in:
@@ -35,7 +35,7 @@ class Migrator
|
|||||||
logger.info "Starting migration for '#{username}'"
|
logger.info "Starting migration for '#{username}'"
|
||||||
set_container_migration_state("in_progress")
|
set_container_migration_state("in_progress")
|
||||||
begin
|
begin
|
||||||
work_on_dir("", "")
|
copy_all_documents
|
||||||
rescue Exception => ex
|
rescue Exception => ex
|
||||||
logger.error "Error migrating documents for '#{username}': #{ex}"
|
logger.error "Error migrating documents for '#{username}': #{ex}"
|
||||||
set_container_migration_state("not_started")
|
set_container_migration_state("not_started")
|
||||||
@@ -48,8 +48,8 @@ class Migrator
|
|||||||
logger.info "Finished migration for '#{username}'"
|
logger.info "Finished migration for '#{username}'"
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_dir?(name)
|
def is_document?(name)
|
||||||
name[-1] == "/"
|
name[-1] != "/"
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_container_migration_state(type)
|
def set_container_migration_state(type)
|
||||||
@@ -60,31 +60,26 @@ class Migrator
|
|||||||
redis.hdel("rs:container_migration", username) unless dry_run
|
redis.hdel("rs:container_migration", username) unless dry_run
|
||||||
end
|
end
|
||||||
|
|
||||||
def work_on_dir(directory, parent_directory)
|
def copy_all_documents
|
||||||
full_directory = "#{parent_directory}#{directory}"
|
logger.debug "Retrieving object listing"
|
||||||
logger.debug "Retrieving listing for '#{full_directory}'"
|
|
||||||
|
|
||||||
listing = get_directory_listing_from_swift("#{full_directory}")
|
listing = get_directory_listing_from_swift
|
||||||
|
|
||||||
logger.debug "Listing for '#{full_directory}': #{listing}"
|
logger.debug "Full listing: #{listing}"
|
||||||
|
|
||||||
if listing
|
if listing
|
||||||
listing.split("\n").each do |item|
|
listing.split("\n").each do |item|
|
||||||
if is_dir? item
|
if is_document? item
|
||||||
# get dir listing and repeat
|
copy_document(item)
|
||||||
work_on_dir(item, "#{parent_directory}") unless item == full_directory
|
|
||||||
else
|
|
||||||
copy_document("#{parent_directory}", item)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_document(directory, document)
|
def copy_document(document_path)
|
||||||
old_document_url = "#{url_for_directory(@username, directory)}/#{escape(document)}"
|
old_document_url = "#{container_url_for(@username)}/#{escape(document_path)}"
|
||||||
|
|
||||||
full_path = [directory, document].reject(&:empty?).join('/')
|
new_document_path = "rs:documents:#{environment.to_s.downcase}/#{@username}/#{escape(document_path)}"
|
||||||
new_document_path = "rs:documents:#{environment.to_s.downcase}/#{@username}/#{escape(full_path)}"
|
|
||||||
|
|
||||||
logger.debug "Copying document from #{old_document_url} to #{new_document_path}"
|
logger.debug "Copying document from #{old_document_url} to #{new_document_path}"
|
||||||
do_copy_request(old_document_url, new_document_path) unless dry_run
|
do_copy_request(old_document_url, new_document_path) unless dry_run
|
||||||
@@ -94,32 +89,16 @@ class Migrator
|
|||||||
@redis ||= Redis.new(@settings["redis"].symbolize_keys)
|
@redis ||= Redis.new(@settings["redis"].symbolize_keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_directory_listing_from_swift(directory)
|
def get_directory_listing_from_swift
|
||||||
is_root_listing = directory.empty?
|
get_response = do_get_request("#{container_url_for(@username)}/?prefix=")
|
||||||
|
|
||||||
get_response = nil
|
|
||||||
|
|
||||||
if is_root_listing
|
|
||||||
get_response = do_get_request("#{container_url_for(@username)}/?delimiter=/&prefix=")
|
|
||||||
else
|
|
||||||
get_response = do_get_request("#{container_url_for(@username)}/?delimiter=/&prefix=#{escape(directory)}")
|
|
||||||
end
|
|
||||||
|
|
||||||
get_response.body
|
get_response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_head_request(url, &block)
|
|
||||||
RestClient.head(url, default_headers, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def do_get_request(url, &block)
|
def do_get_request(url, &block)
|
||||||
RestClient.get(url, default_headers, &block)
|
RestClient.get(url, default_headers, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_put_request(url, data, content_type)
|
|
||||||
RestClient.put(url, data, default_headers.merge({content_type: content_type}))
|
|
||||||
end
|
|
||||||
|
|
||||||
def do_copy_request(url, destination_path)
|
def do_copy_request(url, destination_path)
|
||||||
RestClient::Request.execute(
|
RestClient::Request.execute(
|
||||||
method: :copy,
|
method: :copy,
|
||||||
@@ -132,14 +111,6 @@ class Migrator
|
|||||||
{"x-auth-token" => @swift_token}
|
{"x-auth-token" => @swift_token}
|
||||||
end
|
end
|
||||||
|
|
||||||
def url_for_directory(user, directory)
|
|
||||||
if directory.empty?
|
|
||||||
container_url_for(user)
|
|
||||||
else
|
|
||||||
"#{container_url_for(user)}/#{escape(directory)}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def container_url_for(user)
|
def container_url_for(user)
|
||||||
"#{base_url}/#{container_for(user)}"
|
"#{base_url}/#{container_for(user)}"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user