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
|
require 'yaml'
module Tapicero
module Config
extend self
attr_accessor :users_db_name
attr_accessor :db_prefix
attr_accessor :couch_connection
attr_accessor :security
attr_accessor :seq_file
attr_accessor :log_file
attr_accessor :log_level
def self.load(base_dir, *configs)
loaded = configs.collect do |file_path|
file = find_file(base_dir, file_path)
load_config(file)
end
init_logger
log_loaded_configs(loaded.compact)
end
def couch_host(conf = nil)
conf ||= couch_connection
userinfo = [conf[:username], conf[:password]].compact.join(':')
userinfo += '@' unless userinfo.empty?
"#{conf[:protocol]}://#{userinfo}#{conf[:host]}:#{conf[:port]}"
end
def couch_host_without_password
couch_host couch_connection.merge({:password => nil})
end
private
def init_logger
if log_file
require 'logger'
Tapicero.logger = Logger.new(log_file)
else
require 'syslog/logger'
Tapicero.logger = Syslog::Logger.new('tapicero')
end
Tapicero.logger.level = Logger.const_get(log_level.upcase)
end
def load_config(file_path)
return unless file_path
load_settings YAML.load(File.read(file_path))
return file_path
rescue NoMethodError => exc
init_logger
Tapicero.logger.fatal "Error in file #{file_path}"
Tapicero.logger.fatal exc
exit(1)
end
def load_settings(hash)
return unless hash
hash.each do |key, value|
apply_setting(key, value)
end
end
def apply_setting(key, value)
if value.is_a? Hash
value = symbolize_keys(value)
end
self.send("#{key}=", value)
rescue NoMethodError => exc
STDERR.puts "'#{key}' is not a valid option"
raise exc
end
def self.symbolize_keys(hsh)
newhsh = {}
hsh.keys.each do |key|
newhsh[key.to_sym] = hsh[key]
end
newhsh
end
def self.find_file(base_dir, file_path)
return nil unless file_path
if defined? CWD
return File.expand_path(file_path, CWD) if File.exists?(File.expand_path(file_path, CWD))
end
return File.expand_path(file_path, base_dir) if File.exists?(File.expand_path(file_path, base_dir))
return nil
end
def log_loaded_configs(files)
files.each do |file|
Tapicero.logger.info "Loaded config from #{file} ."
end
end
end
end
|