-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rakefile
executable file
·104 lines (87 loc) · 3.16 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'fiedl/log'
# This is needed for `rake db:migrate` et cetera:
#
Wingolfsplattform::Application.load_tasks
# This is a hack to fix "Don't know how to build task 'test:prepare'":
# https://github.com/rspec/rspec-rails/issues/936#issuecomment-36129887
#
task 'test:prepare' do
# This task does not do anything.
end
task :tests do
sh "rspec spec/models spec/features"
end
task test: :tests
task default: :tests
export_path = File.join Rails.root, "exports"
task :export => [
:export_info,
:export_aktivitates,
:encrypt_export_folders
]
task :export_info do
log.head "Export der Mitgliederdaten"
log.info "Ziel: #{export_path}"
end
task :export_aktivitates => :environment do
log.section "Exportiere Mitgliederdaten der Aktivitates"
Aktivitas.active.all.each do |aktivitas|
domain = aktivitas.corporation.subdomain || raise('no domain present')
aktivitas_export_folder = File.join export_path, domain
print "#{domain.blue} ..."
FileUtils.mkdir_p aktivitas_export_folder
aktivitas.members.each do |member|
member.backup_profile
FileUtils.cp member.latest_backup_file, aktivitas_export_folder
end
log.success "ok"
end
end
task :encrypt_export_folders do
log.section "Verschlüsselung der Export-Ordner"
log.info "Basisverzeichnis: #{export_path}"
passwords = {}
directories = Dir.glob(File.join(export_path, "*")).select { |f| File.directory?(f) }
directories.each do |directory|
domain = File.basename directory
encryption_password = `pwgen 48`.strip
passwords[domain] = encryption_password
destination_zip_file = "#{domain}.7z"
print "#{destination_zip_file.blue} ... "
`cd #{export_path} && 7z a #{destination_zip_file} #{domain} -t7z -ms=on -mhe=on -p#{encryption_password}`
print encryption_password
log.success " ok"
end
log.section "Passwörter"
passwords_file = File.join(export_path, "encryption_passwords.json")
log.info "Passwort-Sammlung zur Entschlüsselung: #{passwords_file}"
File.write passwords_file, passwords.to_json
end
task :extract_w_nummern_liste_from_backups => [:environment] do
log.head "Liste von W-Nummern erstellen"
export_file = File.join export_path, "w-nummern.csv"
log.info "Export-Datei: #{export_file}"
destination_array = []
User.all.each do |user|
if user.w_nummer.present? && user.w_nummer >= "W65709"
destination_array << {
w_nummer: user.w_nummer,
last_name: (user.latest_backup["last_name"] if user.latest_backup),
first_name: (user.latest_backup["first_name"] if user.latest_backup),
date_of_birth: (user.latest_backup["profile_fields"].detect { |pf| pf["key"] == "date_of_birth" }.try(:[], "value") if user.latest_backup),
id: user.id
}
end
end
destination_array.sort_by! { |row| row[:w_nummer] }
CSV.open export_file, "wb" do |csv|
csv << destination_array.first.keys
destination_array.each do |row|
csv << row.values
end
end
end