-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.rb
More file actions
77 lines (62 loc) · 1.89 KB
/
controller.rb
File metadata and controls
77 lines (62 loc) · 1.89 KB
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
require 'yaml'
require 'json'
require 'erb'
require './logger'
require './database'
module Gaudi
class Controller
attr_reader :user_options
def initialize(user_options:)
@db_config = YAML.load_file(File.join(__dir__, 'config.yml'))
@databases = []
@user_options = user_options
prepare_db_config
initialize_databases
end
def call
current_action = @user_options[:action]
self.send current_action.to_sym
end
private
def install
@databases.each(&:install)
end
def report
logs = @databases.map(&:audit_log)
template = File.read(File.join(__dir__, 'template.erb'))
Dir.mkdir './reports' unless Dir.exist?('./reports')
report_path = "./reports/#{Time.now.to_i}.html"
File.open(report_path, 'w') do |io|
io << ERB.new(template).result_with_hash({data: logs})
end
Gaudi.logger.debug(self.class){ "Report generated to #{report_path}" }
end
def uninstall
@databases.each(&:uninstall)
end
def prepare_db_config
# Символизируем ключи и заполняем немспейс в хостах
@db_config.each do |config|
config.transform_keys!(&:to_sym)
config[:name] = config[:name].downcase
end
# Оставляем только БД, переданные в параметрах
if @user_options[:databases]
@db_config = @db_config.select do |config|
@user_options[:databases].include?(config[:name])
end
end
Gaudi.logger.debug(self.class){ "Config applied" }
end
# Создаем инстансы БД
def initialize_databases
@db_config.each do |config|
@databases.push Database.new(
db_config: config,
controller: self
)
end
Gaudi.logger.debug(self.class){ "DBs initialized" }
end
end
end