overman 0.0.1 → 0.88.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +54 -0
- data/bin/foreman-runner +41 -0
- data/bin/overman +7 -0
- data/data/example/Procfile +4 -0
- data/data/example/Procfile.without_colon +2 -0
- data/data/example/error +7 -0
- data/data/example/log/neverdie.log +4 -0
- data/data/example/spawnee +14 -0
- data/data/example/spawner +7 -0
- data/data/example/ticker +14 -0
- data/data/example/utf8 +11 -0
- data/data/export/bluepill/master.pill.erb +28 -0
- data/data/export/daemon/master.conf.erb +14 -0
- data/data/export/daemon/process.conf.erb +8 -0
- data/data/export/daemon/process_master.conf.erb +2 -0
- data/data/export/launchd/launchd.plist.erb +33 -0
- data/data/export/runit/log/run.erb +7 -0
- data/data/export/runit/run.erb +4 -0
- data/data/export/supervisord/app.conf.erb +31 -0
- data/data/export/systemd/master.target.erb +5 -0
- data/data/export/systemd/process.service.erb +21 -0
- data/data/export/upstart/master.conf.erb +2 -0
- data/data/export/upstart/process.conf.erb +15 -0
- data/data/export/upstart/process_master.conf.erb +2 -0
- data/lib/foreman/cli.rb +167 -0
- data/lib/foreman/distribution.rb +9 -0
- data/lib/foreman/engine/cli.rb +105 -0
- data/lib/foreman/engine.rb +494 -0
- data/lib/foreman/env.rb +29 -0
- data/lib/foreman/export/base.rb +170 -0
- data/lib/foreman/export/bluepill.rb +12 -0
- data/lib/foreman/export/daemon.rb +28 -0
- data/lib/foreman/export/inittab.rb +42 -0
- data/lib/foreman/export/launchd.rb +22 -0
- data/lib/foreman/export/runit.rb +34 -0
- data/lib/foreman/export/supervisord.rb +16 -0
- data/lib/foreman/export/systemd.rb +34 -0
- data/lib/foreman/export/upstart.rb +46 -0
- data/lib/foreman/export.rb +36 -0
- data/lib/foreman/helpers.rb +45 -0
- data/lib/foreman/process.rb +81 -0
- data/lib/foreman/procfile.rb +100 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/create_file.rb +103 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/create_link.rb +59 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/directory.rb +118 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/file_manipulation.rb +327 -0
- data/lib/foreman/vendor/thor/lib/thor/actions/inject_into_file.rb +103 -0
- data/lib/foreman/vendor/thor/lib/thor/actions.rb +318 -0
- data/lib/foreman/vendor/thor/lib/thor/base.rb +656 -0
- data/lib/foreman/vendor/thor/lib/thor/command.rb +133 -0
- data/lib/foreman/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +85 -0
- data/lib/foreman/vendor/thor/lib/thor/core_ext/io_binary_read.rb +12 -0
- data/lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb +129 -0
- data/lib/foreman/vendor/thor/lib/thor/error.rb +32 -0
- data/lib/foreman/vendor/thor/lib/thor/group.rb +281 -0
- data/lib/foreman/vendor/thor/lib/thor/invocation.rb +177 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
- data/lib/foreman/vendor/thor/lib/thor/line_editor.rb +17 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/argument.rb +70 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/arguments.rb +175 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/option.rb +146 -0
- data/lib/foreman/vendor/thor/lib/thor/parser/options.rb +220 -0
- data/lib/foreman/vendor/thor/lib/thor/parser.rb +4 -0
- data/lib/foreman/vendor/thor/lib/thor/rake_compat.rb +71 -0
- data/lib/foreman/vendor/thor/lib/thor/runner.rb +322 -0
- data/lib/foreman/vendor/thor/lib/thor/shell/basic.rb +436 -0
- data/lib/foreman/vendor/thor/lib/thor/shell/color.rb +149 -0
- data/lib/foreman/vendor/thor/lib/thor/shell/html.rb +126 -0
- data/lib/foreman/vendor/thor/lib/thor/shell.rb +81 -0
- data/lib/foreman/vendor/thor/lib/thor/util.rb +268 -0
- data/lib/foreman/vendor/thor/lib/thor/version.rb +3 -0
- data/lib/foreman/vendor/thor/lib/thor.rb +492 -0
- data/lib/foreman/version.rb +5 -0
- data/lib/foreman.rb +17 -0
- data/man/overman.1 +181 -0
- data/spec/foreman/cli_spec.rb +111 -0
- data/spec/foreman/engine_spec.rb +114 -0
- data/spec/foreman/export/base_spec.rb +19 -0
- data/spec/foreman/export/bluepill_spec.rb +37 -0
- data/spec/foreman/export/daemon_spec.rb +97 -0
- data/spec/foreman/export/inittab_spec.rb +40 -0
- data/spec/foreman/export/launchd_spec.rb +31 -0
- data/spec/foreman/export/runit_spec.rb +36 -0
- data/spec/foreman/export/supervisord_spec.rb +38 -0
- data/spec/foreman/export/systemd_spec.rb +155 -0
- data/spec/foreman/export/upstart_spec.rb +118 -0
- data/spec/foreman/export_spec.rb +24 -0
- data/spec/foreman/helpers_spec.rb +26 -0
- data/spec/foreman/process_spec.rb +70 -0
- data/spec/foreman/procfile_spec.rb +65 -0
- data/spec/foreman_spec.rb +16 -0
- data/spec/helper_spec.rb +19 -0
- data/spec/resources/Procfile +5 -0
- data/spec/resources/Procfile.bad +2 -0
- data/spec/resources/bin/echo +2 -0
- data/spec/resources/bin/env +2 -0
- data/spec/resources/bin/test +2 -0
- data/spec/resources/bin/utf8 +2 -0
- data/spec/resources/export/bluepill/app-concurrency.pill +49 -0
- data/spec/resources/export/bluepill/app.pill +81 -0
- data/spec/resources/export/daemon/app-alpha-1.conf +7 -0
- data/spec/resources/export/daemon/app-alpha-2.conf +7 -0
- data/spec/resources/export/daemon/app-alpha.conf +2 -0
- data/spec/resources/export/daemon/app-bravo-1.conf +7 -0
- data/spec/resources/export/daemon/app-bravo.conf +2 -0
- data/spec/resources/export/daemon/app.conf +14 -0
- data/spec/resources/export/inittab/inittab.concurrency +4 -0
- data/spec/resources/export/inittab/inittab.default +6 -0
- data/spec/resources/export/launchd/launchd-a.default +29 -0
- data/spec/resources/export/launchd/launchd-b.default +29 -0
- data/spec/resources/export/launchd/launchd-c.default +30 -0
- data/spec/resources/export/runit/app-alpha-1/log/run +7 -0
- data/spec/resources/export/runit/app-alpha-1/run +4 -0
- data/spec/resources/export/runit/app-alpha-2/log/run +7 -0
- data/spec/resources/export/runit/app-alpha-2/run +4 -0
- data/spec/resources/export/runit/app-bravo-1/log/run +7 -0
- data/spec/resources/export/runit/app-bravo-1/run +4 -0
- data/spec/resources/export/supervisord/app-alpha-1.conf +42 -0
- data/spec/resources/export/supervisord/app-alpha-2.conf +22 -0
- data/spec/resources/export/systemd/app-alpha.1.service +18 -0
- data/spec/resources/export/systemd/app-alpha.2.service +18 -0
- data/spec/resources/export/systemd/app-alpha.target +2 -0
- data/spec/resources/export/systemd/app-bravo.1.service +18 -0
- data/spec/resources/export/systemd/app-bravo.target +2 -0
- data/spec/resources/export/systemd/app.target +5 -0
- data/spec/resources/export/upstart/app-alpha-1.conf +11 -0
- data/spec/resources/export/upstart/app-alpha-2.conf +11 -0
- data/spec/resources/export/upstart/app-alpha.conf +2 -0
- data/spec/resources/export/upstart/app-bravo-1.conf +11 -0
- data/spec/resources/export/upstart/app-bravo.conf +2 -0
- data/spec/resources/export/upstart/app.conf +2 -0
- data/spec/spec_helper.rb +190 -0
- metadata +144 -16
- data/lib/overman/version.rb +0 -5
- data/lib/overman.rb +0 -1
@@ -0,0 +1,494 @@
|
|
1
|
+
require "foreman"
|
2
|
+
require "foreman/env"
|
3
|
+
require "foreman/process"
|
4
|
+
require "foreman/procfile"
|
5
|
+
require "tempfile"
|
6
|
+
require "fileutils"
|
7
|
+
require "thread"
|
8
|
+
|
9
|
+
class Foreman::Engine
|
10
|
+
|
11
|
+
# The signals that the engine cares about.
|
12
|
+
#
|
13
|
+
HANDLED_SIGNALS = [ :TERM, :INT, :HUP, :USR1, :USR2 ]
|
14
|
+
|
15
|
+
attr_reader :env
|
16
|
+
attr_reader :options
|
17
|
+
attr_reader :processes
|
18
|
+
|
19
|
+
# Create an +Engine+ for running processes
|
20
|
+
#
|
21
|
+
# @param [Hash] options
|
22
|
+
#
|
23
|
+
# @option options [String] :formation (all=1) The process formation to use
|
24
|
+
# @option options [Fixnum] :port (5000) The base port to assign to processes
|
25
|
+
# @option options [String] :root (Dir.pwd) The root directory from which to run processes
|
26
|
+
#
|
27
|
+
def initialize(options={})
|
28
|
+
@options = options.dup
|
29
|
+
|
30
|
+
@options[:formation] ||= "all=1"
|
31
|
+
@options[:timeout] ||= 5
|
32
|
+
|
33
|
+
@env = {}
|
34
|
+
@mutex = Mutex.new
|
35
|
+
@names = {}
|
36
|
+
@processes = []
|
37
|
+
@running = {}
|
38
|
+
@readers = {}
|
39
|
+
@shutdown = false
|
40
|
+
|
41
|
+
# Self-pipe for deferred signal-handling (ala djb: http://cr.yp.to/docs/selfpipe.html)
|
42
|
+
reader, writer = create_pipe
|
43
|
+
reader.close_on_exec = true if reader.respond_to?(:close_on_exec)
|
44
|
+
writer.close_on_exec = true if writer.respond_to?(:close_on_exec)
|
45
|
+
@selfpipe = { :reader => reader, :writer => writer }
|
46
|
+
|
47
|
+
# Set up a global signal queue
|
48
|
+
# http://blog.rubybestpractices.com/posts/ewong/016-Implementing-Signal-Handlers.html
|
49
|
+
Thread.main[:signal_queue] = []
|
50
|
+
end
|
51
|
+
|
52
|
+
# Start the processes registered to this +Engine+
|
53
|
+
#
|
54
|
+
def start
|
55
|
+
register_signal_handlers
|
56
|
+
startup
|
57
|
+
spawn_processes
|
58
|
+
watch_for_output
|
59
|
+
sleep 0.1
|
60
|
+
wait_for_shutdown_or_child_termination
|
61
|
+
shutdown
|
62
|
+
exit(@exitstatus) if @exitstatus
|
63
|
+
end
|
64
|
+
|
65
|
+
# Set up deferred signal handlers
|
66
|
+
#
|
67
|
+
def register_signal_handlers
|
68
|
+
HANDLED_SIGNALS.each do |sig|
|
69
|
+
if ::Signal.list.include? sig.to_s
|
70
|
+
trap(sig) { Thread.main[:signal_queue] << sig ; notice_signal }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Unregister deferred signal handlers
|
76
|
+
#
|
77
|
+
def restore_default_signal_handlers
|
78
|
+
HANDLED_SIGNALS.each do |sig|
|
79
|
+
trap(sig, :DEFAULT) if ::Signal.list.include? sig.to_s
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Wake the main thread up via the selfpipe when there's a signal
|
84
|
+
#
|
85
|
+
def notice_signal
|
86
|
+
@selfpipe[:writer].write_nonblock( '.' )
|
87
|
+
rescue Errno::EAGAIN
|
88
|
+
# Ignore writes that would block
|
89
|
+
rescue Errno::EINTR
|
90
|
+
# Retry if another signal arrived while writing
|
91
|
+
retry
|
92
|
+
end
|
93
|
+
|
94
|
+
# Invoke the real handler for signal +sig+. This shouldn't be called directly
|
95
|
+
# by signal handlers, as it might invoke code which isn't re-entrant.
|
96
|
+
#
|
97
|
+
# @param [Symbol] sig the name of the signal to be handled
|
98
|
+
#
|
99
|
+
def handle_signal(sig)
|
100
|
+
case sig
|
101
|
+
when :TERM
|
102
|
+
handle_term_signal
|
103
|
+
when :INT
|
104
|
+
handle_interrupt
|
105
|
+
when :HUP
|
106
|
+
handle_hangup
|
107
|
+
when *HANDLED_SIGNALS
|
108
|
+
handle_signal_forward(sig)
|
109
|
+
else
|
110
|
+
system "unhandled signal #{sig}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Handle a TERM signal
|
115
|
+
#
|
116
|
+
def handle_term_signal
|
117
|
+
system "SIGTERM received, starting shutdown"
|
118
|
+
@shutdown = true
|
119
|
+
end
|
120
|
+
|
121
|
+
# Handle an INT signal
|
122
|
+
#
|
123
|
+
def handle_interrupt
|
124
|
+
system "SIGINT received, starting shutdown"
|
125
|
+
@shutdown = true
|
126
|
+
end
|
127
|
+
|
128
|
+
# Handle a HUP signal
|
129
|
+
#
|
130
|
+
def handle_hangup
|
131
|
+
system "SIGHUP received, starting shutdown"
|
132
|
+
@shutdown = true
|
133
|
+
end
|
134
|
+
|
135
|
+
def handle_signal_forward(signal)
|
136
|
+
system "#{signal} received, forwarding it to children"
|
137
|
+
kill_children signal
|
138
|
+
end
|
139
|
+
|
140
|
+
# Register a process to be run by this +Engine+
|
141
|
+
#
|
142
|
+
# @param [String] name A name for this process
|
143
|
+
# @param [String] command The command to run
|
144
|
+
# @param [Hash] options
|
145
|
+
#
|
146
|
+
# @option options [Hash] :env A custom environment for this process
|
147
|
+
#
|
148
|
+
def register(name, command, options={})
|
149
|
+
options[:env] ||= env
|
150
|
+
options[:cwd] ||= File.dirname(command.split(" ").first)
|
151
|
+
process = Foreman::Process.new(command, options)
|
152
|
+
@names[process] = name
|
153
|
+
@processes << process
|
154
|
+
end
|
155
|
+
|
156
|
+
# Clear the processes registered to this +Engine+
|
157
|
+
#
|
158
|
+
def clear
|
159
|
+
@names = {}
|
160
|
+
@processes = []
|
161
|
+
end
|
162
|
+
|
163
|
+
# Register processes by reading a Procfile
|
164
|
+
#
|
165
|
+
# @param [String] filename A Procfile from which to read processes to register
|
166
|
+
#
|
167
|
+
def load_procfile(filename)
|
168
|
+
options[:root] ||= File.dirname(filename)
|
169
|
+
Foreman::Procfile.new(filename).entries do |name, command|
|
170
|
+
register name, command, :cwd => options[:root]
|
171
|
+
end
|
172
|
+
self
|
173
|
+
end
|
174
|
+
|
175
|
+
# Load a .env file into the +env+ for this +Engine+
|
176
|
+
#
|
177
|
+
# @param [String] filename A .env file to load into the environment
|
178
|
+
#
|
179
|
+
def load_env(filename)
|
180
|
+
Foreman::Env.new(filename).entries do |name, value|
|
181
|
+
@env[name] = value
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
# Send a signal to all processes started by this +Engine+
|
186
|
+
#
|
187
|
+
# @param [String] signal The signal to send to each process
|
188
|
+
#
|
189
|
+
def kill_children(signal="SIGTERM")
|
190
|
+
if Foreman.windows?
|
191
|
+
@running.each do |pid, (process, index)|
|
192
|
+
system "sending #{signal} to #{name_for(pid)} at pid #{pid}"
|
193
|
+
begin
|
194
|
+
Process.kill("-#{signal}", pid)
|
195
|
+
rescue Errno::ESRCH, Errno::EPERM
|
196
|
+
end
|
197
|
+
end
|
198
|
+
else
|
199
|
+
begin
|
200
|
+
pids = @running.keys.compact
|
201
|
+
Process.kill("-#{signal}", *pids) unless pids.empty?
|
202
|
+
rescue Errno::ESRCH, Errno::EPERM
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# Send a signal to the whole process group.
|
208
|
+
#
|
209
|
+
# @param [String] signal The signal to send
|
210
|
+
#
|
211
|
+
def killall(signal="SIGTERM")
|
212
|
+
if Foreman.windows?
|
213
|
+
kill_children(signal)
|
214
|
+
else
|
215
|
+
begin
|
216
|
+
Process.kill "-#{signal}", Process.pid
|
217
|
+
rescue Errno::ESRCH, Errno::EPERM
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Get the process formation
|
223
|
+
#
|
224
|
+
# @returns [Fixnum] The formation count for the specified process
|
225
|
+
#
|
226
|
+
def formation
|
227
|
+
@formation ||= parse_formation(options[:formation])
|
228
|
+
end
|
229
|
+
|
230
|
+
# List the available process names
|
231
|
+
#
|
232
|
+
# @returns [Array] A list of process names
|
233
|
+
#
|
234
|
+
def process_names
|
235
|
+
@processes.map { |p| @names[p] }
|
236
|
+
end
|
237
|
+
|
238
|
+
# Get the +Process+ for a specifid name
|
239
|
+
#
|
240
|
+
# @param [String] name The process name
|
241
|
+
#
|
242
|
+
# @returns [Foreman::Process] The +Process+ for the specified name
|
243
|
+
#
|
244
|
+
def process(name)
|
245
|
+
@names.invert[name]
|
246
|
+
end
|
247
|
+
|
248
|
+
# Yield each +Process+ in order
|
249
|
+
#
|
250
|
+
def each_process
|
251
|
+
process_names.each do |name|
|
252
|
+
yield name, process(name)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
# Get the root directory for this +Engine+
|
257
|
+
#
|
258
|
+
# @returns [String] The root directory
|
259
|
+
#
|
260
|
+
def root
|
261
|
+
File.expand_path(options[:root] || Dir.pwd)
|
262
|
+
end
|
263
|
+
|
264
|
+
# Get the port for a given process and offset
|
265
|
+
#
|
266
|
+
# @param [Foreman::Process] process A +Process+ associated with this engine
|
267
|
+
# @param [Fixnum] instance The instance of the process
|
268
|
+
#
|
269
|
+
# @returns [Fixnum] port The port to use for this instance of this process
|
270
|
+
#
|
271
|
+
def port_for(process, instance, base=nil)
|
272
|
+
if base
|
273
|
+
base + (@processes.index(process.process) * 100) + (instance - 1)
|
274
|
+
else
|
275
|
+
base_port + (@processes.index(process) * 100) + (instance - 1)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
# Get the base port for this foreman instance
|
280
|
+
#
|
281
|
+
# @returns [Fixnum] port The base port
|
282
|
+
#
|
283
|
+
def base_port
|
284
|
+
(options[:port] || env["PORT"] || ENV["PORT"] || 5000).to_i
|
285
|
+
end
|
286
|
+
|
287
|
+
# deprecated
|
288
|
+
def environment
|
289
|
+
env
|
290
|
+
end
|
291
|
+
|
292
|
+
private
|
293
|
+
|
294
|
+
### Engine API ######################################################
|
295
|
+
|
296
|
+
def startup
|
297
|
+
raise TypeError, "must use a subclass of Foreman::Engine"
|
298
|
+
end
|
299
|
+
|
300
|
+
def output(name, data)
|
301
|
+
raise TypeError, "must use a subclass of Foreman::Engine"
|
302
|
+
end
|
303
|
+
|
304
|
+
def shutdown
|
305
|
+
raise TypeError, "must use a subclass of Foreman::Engine"
|
306
|
+
end
|
307
|
+
|
308
|
+
## Helpers ##########################################################
|
309
|
+
|
310
|
+
def create_pipe
|
311
|
+
IO.method(:pipe).arity.zero? ? IO.pipe : IO.pipe("BINARY")
|
312
|
+
end
|
313
|
+
|
314
|
+
def name_for(pid)
|
315
|
+
process, index = @running[pid]
|
316
|
+
name_for_index(process, index)
|
317
|
+
end
|
318
|
+
|
319
|
+
def name_for_index(process, index)
|
320
|
+
[ @names[process], index.to_s ].compact.join(".")
|
321
|
+
end
|
322
|
+
|
323
|
+
def parse_formation(formation)
|
324
|
+
pairs = formation.to_s.gsub(/\s/, "").split(",")
|
325
|
+
|
326
|
+
pairs.inject(Hash.new(0)) do |ax, pair|
|
327
|
+
process, amount = pair.split("=")
|
328
|
+
process == "all" ? ax.default = amount.to_i : ax[process] = amount.to_i
|
329
|
+
ax
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
def output_with_mutex(name, message)
|
334
|
+
@mutex.synchronize do
|
335
|
+
output name, message
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
def system(message)
|
340
|
+
output_with_mutex "system", message
|
341
|
+
end
|
342
|
+
|
343
|
+
def termination_message_for(status)
|
344
|
+
if status.exited?
|
345
|
+
"exited with code #{status.exitstatus}"
|
346
|
+
elsif status.signaled?
|
347
|
+
"terminated by SIG#{Signal.list.invert[status.termsig]}"
|
348
|
+
else
|
349
|
+
"died a mysterious death"
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
def flush_reader(reader)
|
354
|
+
until reader.eof?
|
355
|
+
data = reader.gets
|
356
|
+
output_with_mutex name_for(@readers.key(reader)), data
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
## Engine ###########################################################
|
361
|
+
|
362
|
+
def spawn_processes
|
363
|
+
@processes.each do |process|
|
364
|
+
1.upto(formation[@names[process]]) do |n|
|
365
|
+
reader, writer = create_pipe
|
366
|
+
begin
|
367
|
+
pid = process.run(:output => writer, :env => {
|
368
|
+
"PORT" => port_for(process, n).to_s,
|
369
|
+
"PS" => name_for_index(process, n)
|
370
|
+
})
|
371
|
+
writer.puts "started with pid #{pid}"
|
372
|
+
rescue Errno::ENOENT
|
373
|
+
writer.puts "unknown command: #{process.command}"
|
374
|
+
end
|
375
|
+
@running[pid] = [process, n]
|
376
|
+
@readers[pid] = reader
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
def read_self_pipe
|
382
|
+
@selfpipe[:reader].read_nonblock(11)
|
383
|
+
rescue Errno::EAGAIN, Errno::EINTR, Errno::EBADF, Errno::EWOULDBLOCK
|
384
|
+
# ignore
|
385
|
+
end
|
386
|
+
|
387
|
+
def handle_signals
|
388
|
+
while sig = Thread.main[:signal_queue].shift
|
389
|
+
self.handle_signal(sig)
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
def handle_io(readers)
|
394
|
+
readers.each do |reader|
|
395
|
+
next if reader == @selfpipe[:reader]
|
396
|
+
|
397
|
+
if reader.eof?
|
398
|
+
@readers.delete_if { |key, value| value == reader }
|
399
|
+
else
|
400
|
+
data = reader.gets
|
401
|
+
output_with_mutex name_for(@readers.invert[reader]), data
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
def watch_for_output
|
407
|
+
Thread.new do
|
408
|
+
begin
|
409
|
+
loop do
|
410
|
+
io = IO.select([@selfpipe[:reader]] + @readers.values, nil, nil, 30)
|
411
|
+
read_self_pipe
|
412
|
+
handle_signals
|
413
|
+
handle_io(io ? io.first : [])
|
414
|
+
end
|
415
|
+
rescue Exception => ex
|
416
|
+
puts ex.message
|
417
|
+
puts ex.backtrace
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
def wait_for_shutdown_or_child_termination
|
423
|
+
loop do
|
424
|
+
# Stop if it is time to shut down (asked via a signal)
|
425
|
+
break if @shutdown
|
426
|
+
|
427
|
+
# Stop if any of the children died
|
428
|
+
break if check_for_termination
|
429
|
+
|
430
|
+
# Sleep for a moment and do not blow up if any signals are coming our way
|
431
|
+
begin
|
432
|
+
sleep(1)
|
433
|
+
rescue Exception
|
434
|
+
# noop
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
438
|
+
# Ok, we have exited from the main loop, time to shut down gracefully
|
439
|
+
terminate_gracefully
|
440
|
+
end
|
441
|
+
|
442
|
+
def check_for_termination
|
443
|
+
# Check if any of the children have died off
|
444
|
+
pid, status = begin
|
445
|
+
Process.wait2(-1, Process::WNOHANG)
|
446
|
+
rescue Errno::ECHILD
|
447
|
+
return nil
|
448
|
+
end
|
449
|
+
|
450
|
+
# record the exit status
|
451
|
+
@exitstatus ||= status.exitstatus if status
|
452
|
+
|
453
|
+
# If no childred have died, nothing to do here
|
454
|
+
return nil unless pid
|
455
|
+
|
456
|
+
# Log the information about the process that exited
|
457
|
+
output_with_mutex name_for(pid), termination_message_for(status)
|
458
|
+
|
459
|
+
# Delete it from the list of running processes and return its pid
|
460
|
+
@running.delete(pid)
|
461
|
+
return pid
|
462
|
+
end
|
463
|
+
|
464
|
+
def terminate_gracefully
|
465
|
+
restore_default_signal_handlers
|
466
|
+
|
467
|
+
# Tell all children to stop gracefully
|
468
|
+
if Foreman.windows?
|
469
|
+
system "sending SIGKILL to all processes"
|
470
|
+
kill_children "SIGKILL"
|
471
|
+
else
|
472
|
+
system "sending SIGTERM to all processes"
|
473
|
+
kill_children "SIGTERM"
|
474
|
+
end
|
475
|
+
|
476
|
+
# Wait for all children to stop or until the time comes to kill them all
|
477
|
+
start_time = Time.now
|
478
|
+
while Time.now - start_time <= options[:timeout]
|
479
|
+
return if @running.empty?
|
480
|
+
check_for_termination
|
481
|
+
|
482
|
+
# Sleep for a moment and do not blow up if more signals are coming our way
|
483
|
+
begin
|
484
|
+
sleep(0.1)
|
485
|
+
rescue Exception
|
486
|
+
# noop
|
487
|
+
end
|
488
|
+
end
|
489
|
+
|
490
|
+
# Ok, we have no other option than to kill all of our children
|
491
|
+
system "sending SIGKILL to all processes"
|
492
|
+
kill_children "SIGKILL"
|
493
|
+
end
|
494
|
+
end
|
data/lib/foreman/env.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "foreman"
|
2
|
+
|
3
|
+
class Foreman::Env
|
4
|
+
|
5
|
+
attr_reader :entries
|
6
|
+
|
7
|
+
def initialize(filename)
|
8
|
+
@entries = File.read(filename).gsub("\r\n","\n").split("\n").inject({}) do |ax, line|
|
9
|
+
if line =~ /\A([A-Za-z_0-9]+)=(.*)\z/
|
10
|
+
key = $1
|
11
|
+
case val = $2
|
12
|
+
# Remove single quotes
|
13
|
+
when /\A'(.*)'\z/ then ax[key] = $1
|
14
|
+
# Remove double quotes and unescape string preserving newline characters
|
15
|
+
when /\A"(.*)"\z/ then ax[key] = $1.gsub('\n', "\n").gsub(/\\(.)/, '\1')
|
16
|
+
else ax[key] = val
|
17
|
+
end
|
18
|
+
end
|
19
|
+
ax
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def entries
|
24
|
+
@entries.each do |key, value|
|
25
|
+
yield key, value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require "foreman/export"
|
2
|
+
require "pathname"
|
3
|
+
require "shellwords"
|
4
|
+
|
5
|
+
class Foreman::Export::Base
|
6
|
+
|
7
|
+
attr_reader :location
|
8
|
+
attr_reader :engine
|
9
|
+
attr_reader :options
|
10
|
+
attr_reader :formation
|
11
|
+
|
12
|
+
# deprecated
|
13
|
+
attr_reader :port
|
14
|
+
|
15
|
+
def initialize(location, engine, options={})
|
16
|
+
@location = location
|
17
|
+
@engine = engine
|
18
|
+
@options = options.dup
|
19
|
+
@formation = engine.formation
|
20
|
+
|
21
|
+
# deprecated
|
22
|
+
def port
|
23
|
+
Foreman::Export::Base.warn_deprecation!
|
24
|
+
engine.base_port
|
25
|
+
end
|
26
|
+
|
27
|
+
# deprecated
|
28
|
+
def template
|
29
|
+
Foreman::Export::Base.warn_deprecation!
|
30
|
+
options[:template]
|
31
|
+
end
|
32
|
+
|
33
|
+
# deprecated
|
34
|
+
def @engine.procfile
|
35
|
+
Foreman::Export::Base.warn_deprecation!
|
36
|
+
@processes.map do |process|
|
37
|
+
Struct.new(
|
38
|
+
:name => @names[process],
|
39
|
+
:process => process
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def export
|
46
|
+
error("Must specify a location") unless location
|
47
|
+
FileUtils.mkdir_p(location) rescue error("Could not create: #{location}")
|
48
|
+
chown user, log
|
49
|
+
chown user, run
|
50
|
+
end
|
51
|
+
|
52
|
+
def app
|
53
|
+
options[:app] || "app"
|
54
|
+
end
|
55
|
+
|
56
|
+
def log
|
57
|
+
options[:log] || "/var/log/#{app}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def run
|
61
|
+
options[:run] || "/var/run/#{app}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def user
|
65
|
+
options[:user] || app
|
66
|
+
end
|
67
|
+
|
68
|
+
private ######################################################################
|
69
|
+
|
70
|
+
def self.warn_deprecation!
|
71
|
+
@@deprecation_warned ||= false
|
72
|
+
return if @@deprecation_warned
|
73
|
+
puts "WARNING: Using deprecated exporter interface. Please update your exporter"
|
74
|
+
puts "the interface shown in the upstart exporter:"
|
75
|
+
puts
|
76
|
+
puts "https://github.com/ddollar/foreman/blob/main/lib/foreman/export/upstart.rb"
|
77
|
+
puts "https://github.com/ddollar/foreman/blob/main/data/export/upstart/process.conf.erb"
|
78
|
+
puts
|
79
|
+
@@deprecation_warned = true
|
80
|
+
end
|
81
|
+
|
82
|
+
def chown user, dir
|
83
|
+
FileUtils.chown user, nil, dir
|
84
|
+
rescue
|
85
|
+
error("Could not chown #{dir} to #{user}") unless File.writable?(dir) || ! File.exist?(dir)
|
86
|
+
end
|
87
|
+
|
88
|
+
def error(message)
|
89
|
+
raise Foreman::Export::Exception.new(message)
|
90
|
+
end
|
91
|
+
|
92
|
+
def say(message)
|
93
|
+
puts "[foreman export] %s" % message
|
94
|
+
end
|
95
|
+
|
96
|
+
def clean(filename)
|
97
|
+
return unless File.exist?(filename)
|
98
|
+
say "cleaning up: #{filename}"
|
99
|
+
FileUtils.rm(filename)
|
100
|
+
end
|
101
|
+
|
102
|
+
def clean_dir(dirname)
|
103
|
+
return unless File.exist?(dirname)
|
104
|
+
say "cleaning up directory: #{dirname}"
|
105
|
+
FileUtils.rm_r(dirname)
|
106
|
+
end
|
107
|
+
|
108
|
+
def shell_quote(value)
|
109
|
+
Shellwords.escape(value)
|
110
|
+
end
|
111
|
+
|
112
|
+
# deprecated
|
113
|
+
def old_export_template(exporter, file, template_root)
|
114
|
+
if template_root && File.exist?(file_path = File.join(template_root, file))
|
115
|
+
File.read(file_path)
|
116
|
+
elsif File.exist?(file_path = File.expand_path(File.join("~/.foreman/templates", file)))
|
117
|
+
File.read(file_path)
|
118
|
+
else
|
119
|
+
File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def export_template(name, file=nil, template_root=nil)
|
124
|
+
if file && template_root
|
125
|
+
old_export_template name, file, template_root
|
126
|
+
else
|
127
|
+
name_without_first = name.split("/")[1..-1].join("/")
|
128
|
+
matchers = []
|
129
|
+
matchers << File.join(options[:template], name_without_first) if options[:template]
|
130
|
+
matchers << File.expand_path("~/.foreman/templates/#{name}")
|
131
|
+
matchers << File.expand_path("../../../../data/export/#{name}", __FILE__)
|
132
|
+
File.read(matchers.detect { |m| File.exist?(m) })
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def write_template(name, target, binding)
|
137
|
+
compiled = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
|
138
|
+
ERB.new(export_template(name), trim_mode: '-').result(binding)
|
139
|
+
else
|
140
|
+
ERB.new(export_template(name), nil, '-').result(binding)
|
141
|
+
end
|
142
|
+
write_file target, compiled
|
143
|
+
end
|
144
|
+
|
145
|
+
def chmod(mode, file)
|
146
|
+
say "setting #{file} to mode #{mode}"
|
147
|
+
FileUtils.chmod mode, File.join(location, file)
|
148
|
+
end
|
149
|
+
|
150
|
+
def create_directory(dir)
|
151
|
+
say "creating: #{dir}"
|
152
|
+
FileUtils.mkdir_p(File.join(location, dir))
|
153
|
+
end
|
154
|
+
|
155
|
+
def create_symlink(link, target)
|
156
|
+
say "symlinking: #{link} -> #{target}"
|
157
|
+
FileUtils.symlink(target, File.join(location, link))
|
158
|
+
end
|
159
|
+
|
160
|
+
def write_file(filename, contents)
|
161
|
+
say "writing: #{filename}"
|
162
|
+
|
163
|
+
filename = File.join(location, filename) unless Pathname.new(filename).absolute?
|
164
|
+
|
165
|
+
File.open(filename, "w") do |file|
|
166
|
+
file.puts contents
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|