forked from pierre/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.rb
291 lines (246 loc) · 9.19 KB
/
config.rb
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
105
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
require 'fileutils'
require 'logger'
require 'socket'
require 'galaxy/host'
module Galaxy
module Config
DEFAULT_HOST = ENV["GALAXY_HOST"] || "localhost"
DEFAULT_LOG = ENV["GALAXY_LOG"] || "SYSLOG"
DEFAULT_LOG_LEVEL = ENV["GALAXY_LOG_LEVEL"] || "INFO"
DEFAULT_MACHINE_FILE = ENV["GALAXY_MACHINE_FILE"] || ""
DEFAULT_AGENT_PID_FILE = ENV["GALAXY_AGENT_PID_FILE"] || "/tmp/galaxy-agent.pid"
DEFAULT_CONSOLE_PID_FILE = ENV["GALAXY_CONSOLE_PID_FILE"] || "/tmp/galaxy-agent.pid"
DEFAULT_PING_INTERVAL = 60
def read_config_file config_file
config_file = config_file || ENV['GALAXY_CONFIG']
unless config_file.nil? or config_file.empty?
msg = "Cannot find configuration file: #{config_file}"
unless File.exist?(config_file)
# Log exception to syslog
syslog_log msg
raise msg
end
end
config_files = [config_file, '/etc/galaxy.conf', '/usr/local/etc/galaxy.conf'].compact
config_files.each do |file|
begin
File.open file, "r" do |f|
return YAML.load(f.read)
end
rescue Errno::ENOENT
end
end
# Fall through to empty config hash
return {}
end
def set_host host_from_file
@host ||= @config.host || host_from_file || begin
Socket.gethostname rescue DEFAULT_HOST
end
end
def set_machine machine_from_file
@machine ||= @config.machine || machine_from_file
end
def set_pid_file pid_file_from_file
@pid_file ||= @config.pid_file || pid_file_from_file
end
def set_user user_from_file
@user ||= @config.user || user_from_file || nil
end
def set_log log_from_file
@log ||= @config.log || log_from_file || DEFAULT_LOG
begin
# Check if we can log to it
test_logger = Galaxy::Log::Glogger.new(@log)
# Make sure to reap file descriptors (except STDOUT/STDERR/SYSLOG)
test_logger.close unless @log == "STDOUT" or @log == "STDERR" or @log == "SYSLOG"
rescue
# Log exception to syslog
syslog_log $!
raise $!
end
return @log
end
def set_log_level log_level_from_file
@log_level ||= begin
log_level = @config.log_level || log_level_from_file || DEFAULT_LOG_LEVEL
case log_level
when "DEBUG"
Logger::DEBUG
when "INFO"
Logger::INFO
when "WARN"
Logger::WARN
when "ERROR"
Logger::ERROR
end
end
end
def guess key
val = self.send key
puts " --#{correct key} #{val}" if @config.verbose
val
end
def syslog_log e
Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.warning e }
end
module_function :read_config_file, :set_machine, :set_host, :set_pid_file,
:set_log, :set_log_level, :set_user, :guess
end
class AgentConfigurator
include Config
def initialize config
@config = config
@config_from_file = read_config_file(config.config_file)
end
def correct key
case key
when :deploy_dir
"deploy-to"
when :data_dir
"data-dir"
when :announce_interval
"announce-interval"
else
key
end
end
def configure
puts "startup configuration" if @config.verbose
{
:host => guess(:host),
:machine => guess(:machine),
:console => guess(:console),
:repository => guess(:repository),
:binaries => guess(:binaries),
:deploy_dir => guess(:deploy_dir),
:verbose => @config.verbose || false,
:data_dir => guess(:data_dir),
:log => guess(:log),
:log_level => guess(:log_level),
:pid_file => guess(:pid_file),
:user => guess(:user),
:announce_interval => guess(:announce_interval),
:event_listener => guess(:event_listener)
}
end
def log
set_log @config_from_file['galaxy.agent.log']
end
def log_level
set_log_level @config_from_file['galaxy.agent.log-level']
end
def pid_file
set_pid_file @config_from_file['galaxy.agent.pid-file'] ||
DEFAULT_AGENT_PID_FILE
end
def user
set_user @config_from_file['galaxy.agent.user']
end
def machine
set_machine @config_from_file['galaxy.agent.machine']
end
def host
set_host @config_from_file['galaxy.agent.host']
end
def console
@console ||= @config.console || @config_from_file['galaxy.agent.console']
end
def repository
@repository ||= @config.repository || @config_from_file['galaxy.agent.config-root']
end
def binaries
@binaries ||= @config.binaries || @config_from_file['galaxy.agent.binaries-root']
end
def deploy_dir
@deploy_dir ||= @config.deploy_dir || @config_from_file['galaxy.agent.deploy-dir'] || "#{HostUtils.avail_path}/galaxy-agent/deploy"
FileUtils.mkdir_p(@deploy_dir) unless File.exists? @deploy_dir
@deploy_dir
end
def data_dir
@data_dir ||= @config.data_dir || @config_from_file['galaxy.agent.data-dir'] || "#{HostUtils.avail_path}/galaxy-agent/data"
FileUtils.mkdir_p(@data_dir) unless File.exists? @data_dir
@data_dir
end
def announce_interval
@announce_interval ||= @config.announce_interval || @config_from_file['galaxy.agent.announce-interval'] || 60
@announce_interval = @announce_interval.to_i
end
def event_listener
@event_listener ||= @config.event_listener || @config_from_file['galaxy.agent.event_listener']
end
end
class ConsoleConfigurator
include Config
def initialize config
@config = config
@config_from_file = read_config_file(config.config_file)
end
def correct key
case key
when :data_dir
return :data
when :deploy_dir
"deploy-to"
when :ping_interval
"ping-interval"
else
key
end
end
def configure
puts "startup configuration" if @config.verbose
{
:environment => guess(:environment),
:verbose => @config.verbose || false,
:log => guess(:log),
:log_level => guess(:log_level),
:pid_file => guess(:pid_file),
:user => guess(:user),
:host => guess(:host),
:announcement_url => guess(:announcement_url),
:ping_interval => guess(:ping_interval),
:console_proxyied_url => guess(:console_proxyied_url),
:event_listener => guess(:event_listener)
}
end
def console_proxyied_url
return @config.console_proxyied_url
end
def log
set_log @config_from_file['galaxy.console.log']
end
def log_level
set_log_level @config_from_file['galaxy.console.log-level']
end
def pid_file
set_pid_file @config_from_file['galaxy.console.pid-file'] ||
DEFAULT_CONSOLE_PID_FILE
end
def user
set_user @config_from_file['galaxy.console.user']
end
def announcement_url
@announcement_url ||= @config.announcement_url || @config_from_file['galaxy.console.announcement-url'] || "http://#{`hostname`.strip}"
end
def host
set_host @config_from_file['galaxy.console.host']
end
def ping_interval
@ping_interval ||= @config.ping_interval || @config_from_file['galaxy.console.ping-interval'] || 60
@ping_interval = @ping_interval.to_i
end
def event_listener
@event_listener ||= @config.event_listener || @config_from_file['galaxy.console.event_listener']
end
def environment
@env ||= begin
if @config.environment
@config.environment
elsif @config_from_file['galaxy.console.environment']
@config_from_file['galaxy.console.environment']
end
end
end
end
end