Class: GGConfig
- Inherits:
-
Object
show all
- Includes:
- Logging
- Defined in:
- lib/gg_config/gg_config.rb
Instance Method Summary
collapse
Methods included from Logging
create_logger, #logger, logger, set_log_conf, #set_log_file, #set_log_level
Constructor Details
#initialize(options = {:conf_dir => "/conf", :conf_file => "gaddy_*.gcf"}) ⇒ GGConfig
Returns a new instance of GGConfig.
27
28
29
30
|
# File 'lib/gg_config/gg_config.rb', line 27
def initialize(options = {:conf_dir => "/conf", :conf_file => "gaddy_*.gcf"} )
@conf_file = options[:conf_file]
@conf_dir = File.expand_path(options[:conf_dir])
end
|
Instance Method Details
#config ⇒ Object
79
80
81
|
# File 'lib/gg_config/gg_config.rb', line 79
def config
@config_content ||= read_config_file
end
|
#config=(new_config) ⇒ Object
83
84
85
|
# File 'lib/gg_config/gg_config.rb', line 83
def config= (new_config)
@config_content = new_config.symbolize_keys!
end
|
#config_file_name ⇒ Object
63
64
65
|
# File 'lib/gg_config/gg_config.rb', line 63
def config_file_name
Dir.glob(File.join(@conf_dir, @conf_file))[0]
end
|
#device_id ⇒ Object
33
34
35
|
# File 'lib/gg_config/gg_config.rb', line 33
def device_id
config[:device_id]
end
|
#get_config_for(config_type, conf_name, extra_conf_file) ⇒ Object
Generic method to get the initial config for a config_type, could be device_name or user_name, will also try to find all kind of configs files with device_name or user_name
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/gg_config/gg_config.rb', line 106
def get_config_for(config_type, conf_name, )
config_value = nil
begin
if config
logger.debug "Config exist and is #{config} looking for config type #{config_type}"
config_value = config[config_type]
end
rescue Exception => e
logger.info "Could not find a gaddygaddy config file, message is #{e.message}"
end
unless config_value
files = Dir.glob(File.join(@conf_dir, 'config.*'))
files << File.join(@conf_dir, ) if File.exist? File.join(@conf_dir, )
files.each do |conf_file_name|
logger.debug "The file #{conf_file_name} has file size #{File.size(conf_file_name)}"
if File.size(conf_file_name).to_i < 1000
conf_file = File.open(conf_file_name,"r")
lines = conf_file.readlines
if lines.size < 4
lines.each do |line|
if line.index("=")
args = line.split("=")
if args.size == 2
config_value = args[1].strip if args[0].strip == conf_name || args[0].strip == "config_value"
else
logger.info "The file #{conf_file_name} has too many = in the line #{line}"
end
else
config_value = line.strip
end
end
else
logger.info "The file #{conf_file_name} is not a config file as it's too many lines"
end
else
logger.info "Will not check file #{conf_file_name} as it is to big"
end
end
end
unless config_value
raise JNoGaddyGaddyConfigFile.new({:message => "Could not found any file with config in the config dir #{@conf_dir}",
:topic => TPC_NO_CONFIG_FILE,
:extra_info => {:conf_dir => @conf_dir}
})
end
config_value
end
|
#get_device_name ⇒ Object
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/gg_config/gg_config.rb', line 37
def get_device_name
device_name = get_config_for(:device_name,"host","device")
unless valid_host_name?(device_name)
raise JException.new({:message => "The host name #{device_name} is not a valid host name",
:status => ERR_NO_VALID_HOSTNAME,
:extra_info => {:host_name => device_name}
})
end
device_name
end
|
#read_config_file ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/gg_config/gg_config.rb', line 67
def read_config_file
begin
config_file = File.open(config_file_name)
file_content = config_file.read
config = JSON.parse(file_content)
logger.debug "Have read config file #{config_file_name} and found content #{config.to_s}"
rescue Exception => e
raise JNoGaddyGaddyConfigFile.new({:message => e.message})
end
config.symbolize_keys!
end
|
#save ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/gg_config/gg_config.rb', line 91
def save
unless valid_config?
raise JCouldNotSaveConfigFileException.new({:message => "Missing configuration information, could not save config file for #{config.inspect}"})
end
begin
config_file = File.open(config_file_name,"w")
config_file.write config.to_json.to_s
rescue Exception => e
raise JCouldNotSaveConfigFileException.new({:message => e.message})
end
end
|
#token ⇒ Object
48
49
50
|
# File 'lib/gg_config/gg_config.rb', line 48
def token
config[:token]
end
|
#user_email ⇒ Object
52
53
54
|
# File 'lib/gg_config/gg_config.rb', line 52
def user_email
get_config_for(:user_email,"user","user")
end
|
#user_id_salt ⇒ Object
56
57
58
|
# File 'lib/gg_config/gg_config.rb', line 56
def user_id_salt
get_config_for(:user_id_salt,"user","user_id_salt")
end
|
#valid_config? ⇒ Boolean
158
159
160
|
# File 'lib/gg_config/gg_config.rb', line 158
def valid_config?
config[:user_id_salt] && config[:token] && config[:device_name]
end
|
#valid_host_name?(host_name) ⇒ Boolean
87
88
89
|
# File 'lib/gg_config/gg_config.rb', line 87
def valid_host_name?(host_name)
host_name.size <= 63 and not (host_name.rindex('-', 0) or host_name.index('-', -1) or host_name.scan(/[^a-z\d-]/i).any?)
end
|