Fix rake tasks
This commit is contained in:
parent
6612e56da2
commit
42359eff15
4 changed files with 0 additions and 0 deletions
0
lib/tasks/.keep
Normal file
0
lib/tasks/.keep
Normal file
114
lib/tasks/cloud_storage.rake
Normal file
114
lib/tasks/cloud_storage.rake
Normal file
|
@ -0,0 +1,114 @@
|
|||
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
|
||||
|
||||
error_count = 0
|
||||
[Cerfa, PieceJustificative, Procedure].each { |c|
|
||||
c.all.each { |entry|
|
||||
content = (c == Procedure) ? entry.logo : entry.content
|
||||
unless content.current_path.nil? || File.exist?(File.dirname(content.current_path) + '/uploaded')
|
||||
secure_token = SecureRandom.uuid
|
||||
filename = "#{entry.class.to_s.underscore}-#{secure_token}#{File.extname(content.current_path)}"
|
||||
puts "Uploading #{content.current_path}"
|
||||
begin
|
||||
@cont.create_object(filename, {}, File.open(content.current_path))
|
||||
|
||||
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) }
|
||||
|
||||
entry.update_column(c == Procedure ? :logo : :content, filename)
|
||||
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
|
||||
rescue Errno::ENOENT
|
||||
puts "ERROR: #{content.current_path} does not exist!"
|
||||
File.open('upload_errors.report', "a+") { |f| f.write(content.current_path) }
|
||||
error_count += 1
|
||||
end
|
||||
else
|
||||
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
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
puts "There were #{error_count} errors while uploading files. See upload_errors.report file for details."
|
||||
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|
|
||||
content = (c == Procedure) ? entry.logo : entry.content
|
||||
unless content.current_path.nil?
|
||||
if File.exist?(File.dirname(content.current_path) + '/uploaded')
|
||||
previous_filename = File.read(File.dirname(content.current_path) + '/uploaded')
|
||||
|
||||
entry.update_column(c == Procedure ? :logo : :content, previous_filename)
|
||||
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, nil)
|
||||
|
||||
puts "restoring #{content.current_path} db data to #{previous_filename}"
|
||||
|
||||
@cont.delete_object(File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read)
|
||||
|
||||
FileUtils.rm(File.dirname(content.current_path) + '/uploaded')
|
||||
FileUtils.rm(File.dirname(content.current_path) + '/filename_cloudstorage')
|
||||
FileUtils.rm(File.dirname(content.current_path) + '/secure_token_cloudstorage')
|
||||
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])
|
||||
@cont.delete_object(object) unless last_modified.utc > (Time.now - 2.year).utc
|
||||
}
|
||||
end
|
||||
|
||||
end
|
65
lib/tasks/dev.rake
Normal file
65
lib/tasks/dev.rake
Normal file
|
@ -0,0 +1,65 @@
|
|||
namespace :dev do
|
||||
desc 'Initialise dev environment'
|
||||
task :init do
|
||||
puts 'start initialisation'
|
||||
Rake::Task['dev:generate_token_file'].invoke
|
||||
Rake::Task['dev:generate_franceconnect_file'].invoke
|
||||
Rake::Task['dev:generate_fog_credentials_file'].invoke
|
||||
Rake::Task['dev:generate_features_file'].invoke
|
||||
|
||||
puts 'end initialisation'
|
||||
end
|
||||
|
||||
task :generate_token_file do
|
||||
puts 'creating token.rb file'
|
||||
res = `rake secret`.gsub("\n", '')
|
||||
file = File.new('config/initializers/token.rb', 'w+')
|
||||
comment = <<EOF
|
||||
EOF
|
||||
file.write(comment)
|
||||
file.write("TPS::Application.config.SIADETOKEN = '#{res}'")
|
||||
file.close
|
||||
end
|
||||
|
||||
task :generate_franceconnect_file do
|
||||
file = File.new('config/france_connect.yml', 'w+')
|
||||
comment = <<EOF
|
||||
particulier_identifier: plop
|
||||
particulier_secret: plip
|
||||
|
||||
particulier_redirect_uri: 'http://localhost:3000/france_connect/particulier/callback'
|
||||
|
||||
particulier_authorization_endpoint: 'https://fcp.integ01.dev-franceconnect.fr/api/v1/authorize'
|
||||
particulier_token_endpoint: 'https://fcp.integ01.dev-franceconnect.fr/api/v1/token'
|
||||
particulier_userinfo_endpoint: 'https://fcp.integ01.dev-franceconnect.fr/api/v1/userinfo'
|
||||
particulier_logout_endpoint: 'https://fcp.integ01.dev-franceconnect.fr/api/v1/logout'
|
||||
EOF
|
||||
file.write(comment)
|
||||
file.close
|
||||
end
|
||||
|
||||
task :generate_fog_credentials_file do
|
||||
puts 'creating fog_credentials.test.yml file'
|
||||
content = <<EOF
|
||||
default:
|
||||
openstack_tenant: "ovh_fake_tenant_name"
|
||||
openstack_api_key: "ovh_fake_password"
|
||||
openstack_username: "ovh_fake_username"
|
||||
openstack_auth_url: "https://auth.cloud.ovh.net/v2.0/tokens"
|
||||
openstack_region: "SBG1"
|
||||
EOF
|
||||
file = File.new("config/fog_credentials.test.yml", "w+")
|
||||
file.write(content)
|
||||
file.close
|
||||
end
|
||||
|
||||
task :generate_features_file do
|
||||
puts 'creating features.yml file'
|
||||
content = <<EOF
|
||||
remote_storage: true
|
||||
EOF
|
||||
file = File.new("config/initializers/features.yml", "w+")
|
||||
file.write(content)
|
||||
file.close
|
||||
end
|
||||
end
|
118
lib/tasks/opensimplif_import.rake
Normal file
118
lib/tasks/opensimplif_import.rake
Normal file
|
@ -0,0 +1,118 @@
|
|||
require 'csv'
|
||||
require 'json'
|
||||
|
||||
namespace :opensimplif_import do
|
||||
task :import_all => :environment do
|
||||
puts 'start opensimplif'
|
||||
|
||||
Rake::Task['opensimplif_import:import_proposition'].invoke
|
||||
Rake::Task['opensimplif_import:import_piste'].invoke
|
||||
Rake::Task['opensimplif_import:import_mesure'].invoke
|
||||
|
||||
puts 'end import opensimplif'
|
||||
end
|
||||
|
||||
task :import_proposition do
|
||||
file_path = "lib/tasks/161102_OS_Inputs_test_propositions.csv"
|
||||
procedure_id = 35
|
||||
|
||||
matching = [
|
||||
{id: 44, key: 'Intitulé de la proposition'},
|
||||
{id: 43, key: 'Champ concerné'},
|
||||
{id: 45, key: 'Champ ministériel chef de file'},
|
||||
{id: 59, key: 'Date de la proposition'},
|
||||
{id: 60, key: 'Moment de vie'},
|
||||
{id: 61, key: 'Source'},
|
||||
{id: 48, key: 'Description de la proposition'}
|
||||
]
|
||||
|
||||
puts 'start propositions'
|
||||
import file_path, procedure_id, matching
|
||||
puts 'done propositions'
|
||||
end
|
||||
|
||||
task :import_piste do
|
||||
file_path = "lib/tasks/161102_OS_Inputs_test_pistes.csv"
|
||||
procedure_id = 36
|
||||
|
||||
matching = [
|
||||
{id: 81, key: 'Intitulé de la piste *'},
|
||||
{id: 82, key: 'Usager concerné *'},
|
||||
{id: 83, key: 'Champ ministériel chef de file *'},
|
||||
{id: 84, key: 'Champ ministériel contributeur'},
|
||||
{id: 85, key: 'Date de saisine'},
|
||||
{id: 66, key: 'Moment de vie'},
|
||||
{id: 80, key: 'Source de la piste'},
|
||||
{id: 70, key: 'Description de la piste '},
|
||||
{id: 68, key: 'Objectifs / bénéfices attendus'},
|
||||
{id: 65, key: 'Description détaillée des démarches impactées par la piste'},
|
||||
{id: 69, key: 'Levier de mise en oeuvre'},
|
||||
{id: 67, key: 'Précision sur le levier de meo'},
|
||||
{id: 64, key: 'Calendrier de mise en oeuvre'}
|
||||
]
|
||||
|
||||
puts 'start piste'
|
||||
import file_path, procedure_id, matching
|
||||
puts 'done pistes'
|
||||
end
|
||||
|
||||
task :import_mesure do
|
||||
file_path = "lib/tasks/161102_OS_Inputs_test_mesures.csv"
|
||||
procedure_id = 37
|
||||
|
||||
matching = [
|
||||
{id: 107, key: 'Intitulé projet / mesure'},
|
||||
{id: 104, key: 'Champ concerné'},
|
||||
{id: 105, key: 'Champ ministériel chef de file'},
|
||||
{id: 112, key: 'Direction chef de file'},
|
||||
{id: 106, key: 'Champ ministériel contributeur'},
|
||||
{id: 113, key: 'Direction contributrice'},
|
||||
{id: 92, key: 'Moment de vie'},
|
||||
{id: 109, key: 'Date d\'annonce'},
|
||||
{id: 114, key: 'N° de la mesure'},
|
||||
{id: 115, key: 'Responsable ministère'},
|
||||
{id: 116, key: 'Responsable SGMAP'},
|
||||
{id: 89, key: 'Actions réalisées'},
|
||||
{id: 95, key: 'Etapes nécessaires à l\'atteinte de la cible et alertes'},
|
||||
{id: 102, key: 'Alertes'},
|
||||
{id: 101, key: 'Échéance initiale'},
|
||||
{id: 96, key: 'Échéance prévisionnelle / réelle'},
|
||||
{id: 94, key: 'Appréciation avancement'},
|
||||
{id: 91, key: 'Etat d\'avancement LOLF'},
|
||||
{id: 111, key: '§ de com'}
|
||||
]
|
||||
|
||||
puts 'start mesures'
|
||||
import file_path, procedure_id, matching
|
||||
puts 'done mesures'
|
||||
end
|
||||
|
||||
def self.import file_path, procedure_id, matching
|
||||
user = User.find_or_create_by(email: 'import@opensimplif.modernisation.fr')
|
||||
|
||||
unless user.valid?
|
||||
user.password = 'TPSpassword2016'
|
||||
user.save
|
||||
end
|
||||
|
||||
file ||= CSV.open(file_path, :col_sep => ";", :headers => true).map { |x| x.to_h }.to_json
|
||||
file = JSON.parse(file)
|
||||
|
||||
procedure = Procedure.find(procedure_id)
|
||||
|
||||
user.dossiers.where(procedure_id: procedure.id).destroy_all
|
||||
|
||||
file.each do |proposition|
|
||||
dossier = Dossier.create procedure: procedure, user: user, state: :initiated
|
||||
|
||||
dossier.champs.each do |champ|
|
||||
matching.each do |match|
|
||||
if match[:id] == champ.type_de_champ.id
|
||||
champ.update_column :value, proposition[match[:key]]
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue