2016-07-01 14:45:24 +02:00
|
|
|
namespace :cloudstorage do
|
|
|
|
task init: :environment do
|
|
|
|
os_config = (YAML.load_file(Fog.credentials_path))['default']
|
|
|
|
@os = OpenStack::Connection.create(
|
|
|
|
{
|
|
|
|
username: os_config['openstack_username'],
|
|
|
|
api_key: os_config['openstack_api_key'],
|
|
|
|
auth_method: "password",
|
|
|
|
auth_url: "https://auth.cloud.ovh.net/v2.0/",
|
|
|
|
authtenant_name: os_config['openstack_tenant'],
|
|
|
|
service_type: "object-store",
|
|
|
|
region: os_config['openstack_region']
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@cont = @os.container(CarrierWave::Uploader::Base.fog_directory)
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Move local attestations on cloud storage'
|
|
|
|
task migrate: :environment do
|
|
|
|
puts 'Starting migration'
|
|
|
|
|
|
|
|
Rake::Task['cloudstorage:init'].invoke
|
|
|
|
|
2016-07-04 16:53:38 +02:00
|
|
|
error_count = 0
|
2016-07-01 14:45:24 +02:00
|
|
|
[Cerfa, PieceJustificative, Procedure].each { |c|
|
|
|
|
c.all.each { |entry|
|
2016-07-05 16:09:03 +02:00
|
|
|
content = (c == Procedure) ? entry.logo : entry.content
|
2016-07-01 14:45:24 +02:00
|
|
|
unless content.current_path.nil? || File.exist?(File.dirname(content.current_path) + '/uploaded')
|
|
|
|
secure_token = SecureRandom.uuid
|
2016-07-04 18:00:15 +02:00
|
|
|
filename = "#{entry.class.to_s.underscore}-#{secure_token}#{File.extname(content.current_path)}"
|
2016-07-01 14:45:24 +02:00
|
|
|
puts "Uploading #{content.current_path}"
|
2016-07-04 16:53:38 +02:00
|
|
|
begin
|
2016-07-04 18:00:15 +02:00
|
|
|
@cont.create_object(filename, {}, File.open(content.current_path))
|
2016-07-05 16:09:03 +02:00
|
|
|
|
|
|
|
File.open(File.dirname(content.current_path) + '/uploaded', "w+") { |f| f.write(File.basename(content.current_path)) }
|
|
|
|
File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "w+") { |f| f.write(filename) }
|
|
|
|
File.open(File.dirname(content.current_path) + '/secure_token_cloudstorage', "w+") { |f| f.write(secure_token) }
|
|
|
|
|
2016-07-04 16:53:38 +02:00
|
|
|
entry.update_column(c == Procedure ? :logo : :content, filename)
|
2016-07-04 18:41:09 +02:00
|
|
|
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
|
2016-07-04 16:53:38 +02:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
puts "ERROR: #{content.current_path} does not exist!"
|
2016-07-05 16:09:03 +02:00
|
|
|
File.open('upload_errors.report', "a+") { |f| f.write(content.current_path) }
|
2016-07-04 16:53:38 +02:00
|
|
|
error_count += 1
|
|
|
|
end
|
|
|
|
else
|
2016-07-05 16:09:03 +02:00
|
|
|
if !content.current_path.nil? && File.exist?(File.dirname(content.current_path) + '/uploaded')
|
|
|
|
filename = File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read
|
|
|
|
secure_token = File.open(File.dirname(content.current_path) + '/secure_token_cloudstorage', "r").read
|
|
|
|
|
|
|
|
entry.update_column(c == Procedure ? :logo : :content, filename)
|
|
|
|
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
|
|
|
|
|
|
|
|
puts "RESTORE IN DATABASE: #{filename} "
|
|
|
|
elsif !content.current_path.nil?
|
|
|
|
puts "Skipping #{content.current_path}"
|
|
|
|
end
|
2016-07-01 14:45:24 +02:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 16:53:38 +02:00
|
|
|
puts "There were #{error_count} errors while uploading files. See upload_errors.report file for details."
|
2016-07-01 14:45:24 +02:00
|
|
|
puts 'Enf of migration'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Clear documents in tenant and revert file entries in database'
|
|
|
|
task :revert do
|
|
|
|
Rake::Task['cloudstorage:init'].invoke
|
|
|
|
|
|
|
|
[Cerfa, PieceJustificative, Procedure].each { |c|
|
|
|
|
c.all.each { |entry|
|
2016-07-05 16:09:03 +02:00
|
|
|
content = (c == Procedure) ? entry.logo : entry.content
|
2016-07-01 14:45:24 +02:00
|
|
|
unless content.current_path.nil?
|
|
|
|
if File.exist?(File.dirname(content.current_path) + '/uploaded')
|
|
|
|
previous_filename = File.read(File.dirname(content.current_path) + '/uploaded')
|
2016-07-05 16:09:03 +02:00
|
|
|
|
2016-07-04 17:47:34 +02:00
|
|
|
entry.update_column(c == Procedure ? :logo : :content, previous_filename)
|
2016-07-04 18:41:09 +02:00
|
|
|
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, nil)
|
2016-07-05 16:09:03 +02:00
|
|
|
|
2016-07-01 14:45:24 +02:00
|
|
|
puts "restoring #{content.current_path} db data to #{previous_filename}"
|
2016-07-05 16:09:03 +02:00
|
|
|
|
|
|
|
@cont.delete_object(File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read)
|
|
|
|
|
2016-07-01 14:45:24 +02:00
|
|
|
FileUtils.rm(File.dirname(content.current_path) + '/uploaded')
|
2016-07-05 16:09:03 +02:00
|
|
|
FileUtils.rm(File.dirname(content.current_path) + '/filename_cloudstorage')
|
|
|
|
FileUtils.rm(File.dirname(content.current_path) + '/secure_token_cloudstorage')
|
2016-07-01 14:45:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Clear old documents in tenant'
|
|
|
|
task :clear do
|
|
|
|
Rake::Task['cloudstorage:init'].invoke
|
|
|
|
|
|
|
|
@cont.objects.each { |object|
|
|
|
|
puts "Removing #{object}"
|
|
|
|
@cont.delete_object(object)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
task :clear_old_objects do
|
|
|
|
Rake::Task['cloudstorage:init'].invoke
|
|
|
|
|
|
|
|
@cont.objects_detail.each { |object, details|
|
|
|
|
last_modified = DateTime.parse(details[:last_modified])
|
2016-07-05 16:09:03 +02:00
|
|
|
@cont.delete_object(object) unless last_modified.utc > (Time.now - 2.year).utc
|
2016-07-01 14:45:24 +02:00
|
|
|
}
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|