bluepill 0.2.9 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +111 -2
- data/README.md +16 -13
- data/bin/bluepill +21 -8
- data/bluepill.gemspec +7 -2
- data/lib/bluepill/application/lifecycle.rb +48 -35
- data/lib/bluepill/application/listener.rb +2 -4
- data/lib/bluepill/application/previous_daemon.rb +12 -14
- data/lib/bluepill/application.rb +36 -30
- data/lib/bluepill/base_directory.rb +18 -20
- data/lib/bluepill/check_registry.rb +104 -0
- data/lib/bluepill/clock.rb +1 -3
- data/lib/bluepill/condition_watch.rb +46 -54
- data/lib/bluepill/controller/directory.rb +1 -3
- data/lib/bluepill/controller.rb +35 -37
- data/lib/bluepill/daemonize.rb +52 -0
- data/lib/bluepill/dsl/app_proxy.rb +68 -64
- data/lib/bluepill/dsl/attributes.rb +135 -0
- data/lib/bluepill/dsl/process_factory/child_factory.rb +83 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +4 -26
- data/lib/bluepill/dsl/process_factory.rb +85 -84
- data/lib/bluepill/dsl/process_proxy.rb +80 -160
- data/lib/bluepill/dsl.rb +16 -15
- data/lib/bluepill/errors.rb +190 -0
- data/lib/bluepill/group.rb +14 -37
- data/lib/bluepill/logger.rb +65 -99
- data/lib/bluepill/process/attributes.rb +66 -32
- data/lib/bluepill/process/children.rb +6 -7
- data/lib/bluepill/process/pid.rb +43 -35
- data/lib/bluepill/process/state_machine.rb +15 -59
- data/lib/bluepill/process/stop_signals.rb +123 -0
- data/lib/bluepill/process/transition.rb +1 -3
- data/lib/bluepill/process/transition_callbacks.rb +2 -16
- data/lib/bluepill/process/triggers.rb +90 -0
- data/lib/bluepill/process/user_commands.rb +1 -1
- data/lib/bluepill/process/watches.rb +9 -23
- data/lib/bluepill/process.rb +55 -52
- data/lib/bluepill/process_commands/execution.rb +16 -24
- data/lib/bluepill/process_commands/hooks.rb +1 -31
- data/lib/bluepill/process_commands/stop_signals.rb +12 -17
- data/lib/bluepill/process_commands.rb +10 -8
- data/lib/bluepill/process_conditions/always_true.rb +2 -6
- data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
- data/lib/bluepill/process_conditions/file_time.rb +8 -9
- data/lib/bluepill/process_conditions/http.rb +29 -67
- data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
- data/lib/bluepill/process_conditions/process_condition.rb +29 -47
- data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
- data/lib/bluepill/process_conditions.rb +5 -14
- data/lib/bluepill/process_journal/cleanup.rb +5 -53
- data/lib/bluepill/process_journal/config.rb +73 -0
- data/lib/bluepill/process_journal/entries.rb +128 -0
- data/lib/bluepill/process_journal/journal.rb +69 -0
- data/lib/bluepill/process_journal/kind.rb +60 -0
- data/lib/bluepill/process_journal/lock.rb +9 -4
- data/lib/bluepill/process_journal.rb +13 -324
- data/lib/bluepill/process_statistics.rb +3 -7
- data/lib/bluepill/socket.rb +19 -17
- data/lib/bluepill/system/command_options.rb +136 -0
- data/lib/bluepill/system/command_result.rb +73 -0
- data/lib/bluepill/system/command_setup.rb +3 -4
- data/lib/bluepill/system/command_spawn.rb +244 -0
- data/lib/bluepill/system/commands.rb +27 -40
- data/lib/bluepill/system/daemon.rb +97 -15
- data/lib/bluepill/system/files.rb +35 -0
- data/lib/bluepill/system/ps.rb +20 -17
- data/lib/bluepill/system/ps_table.rb +7 -9
- data/lib/bluepill/system/usage.rb +175 -0
- data/lib/bluepill/system.rb +7 -192
- data/lib/bluepill/trigger.rb +18 -69
- data/lib/bluepill/triggers/flapping.rb +12 -8
- data/lib/bluepill/units.rb +7 -21
- data/lib/bluepill/version.rb +16 -26
- data/lib/bluepill.rb +1 -1
- data/sig/bluepill/application.rbs +11 -12
- data/sig/bluepill/attributes.rbs +48 -0
- data/sig/bluepill/checks.rbs +40 -40
- data/sig/bluepill/dsl.rbs +100 -59
- data/sig/bluepill/errors.rbs +48 -0
- data/sig/bluepill/logger.rbs +15 -31
- data/sig/bluepill/process.rbs +39 -305
- data/sig/bluepill/process_commands.rbs +235 -0
- data/sig/bluepill/stop_signals.rbs +21 -0
- data/sig/bluepill/system.rbs +191 -101
- data/sig/bluepill/version.rbs +17 -0
- data/sig/bluepill.rbs +2 -17
- data/sig/manifest.yaml +1 -0
- metadata +22 -8
- data/lib/bluepill/deprecation/custom_checks.rb +0 -193
- data/lib/bluepill/deprecation/dsl.rb +0 -141
- data/lib/bluepill/deprecation/options.rb +0 -140
- data/lib/bluepill/deprecation.rb +0 -92
- data/lib/bluepill/dsl/process_factory/children.rb +0 -99
- data/sig/bluepill/deprecation.rbs +0 -87
data/lib/bluepill/socket.rb
CHANGED
|
@@ -27,14 +27,12 @@ module Bluepill
|
|
|
27
27
|
# @return [Object] what the block returns
|
|
28
28
|
#
|
|
29
29
|
# @api private
|
|
30
|
-
def self.client(base_dir, name, &
|
|
31
|
-
UNIXSocket.open(socket_path(base_dir, name), &block)
|
|
32
|
-
end
|
|
30
|
+
def self.client(base_dir, name, &) = UNIXSocket.open(socket_path(base_dir, name), &)
|
|
33
31
|
|
|
34
32
|
# Send a command to an application's daemon, and return its answer
|
|
35
33
|
#
|
|
36
34
|
# It tries MAX_ATTEMPTS times when the daemon hangs up or takes longer
|
|
37
|
-
# than TIMEOUT
|
|
35
|
+
# than TIMEOUT.
|
|
38
36
|
#
|
|
39
37
|
# @param [String] base_dir the directory with the daemons' sockets
|
|
40
38
|
# @param [String] name the application's name
|
|
@@ -42,6 +40,8 @@ module Bluepill
|
|
|
42
40
|
#
|
|
43
41
|
# @return [Object] what the daemon answered, unmarshaled
|
|
44
42
|
#
|
|
43
|
+
# @raise [ConnectionError] if the last attempt fails too
|
|
44
|
+
#
|
|
45
45
|
# @api private
|
|
46
46
|
def self.client_command(base_dir, name, command)
|
|
47
47
|
res = nil
|
|
@@ -49,7 +49,7 @@ module Bluepill
|
|
|
49
49
|
res = send_command(base_dir, name, command)
|
|
50
50
|
break
|
|
51
51
|
rescue EOFError, Timeout::Error
|
|
52
|
-
|
|
52
|
+
retry_or_give_up(current_attempt)
|
|
53
53
|
end
|
|
54
54
|
res
|
|
55
55
|
end
|
|
@@ -75,28 +75,32 @@ module Bluepill
|
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
-
# Say that a client command tries again, or
|
|
78
|
+
# Say that a client command tries again, or give up after the last try
|
|
79
79
|
#
|
|
80
80
|
# @param [Integer] current_attempt the attempt that failed, from 0
|
|
81
81
|
#
|
|
82
82
|
# @return [void]
|
|
83
83
|
#
|
|
84
|
+
# @raise [ConnectionError] after the last attempt
|
|
85
|
+
#
|
|
84
86
|
# @api private
|
|
85
|
-
def self.
|
|
86
|
-
|
|
87
|
+
def self.retry_or_give_up(current_attempt)
|
|
88
|
+
raise ConnectionError, "Socket Timeout: Server may not be responding" if current_attempt.eql?(MAX_ATTEMPTS - 1)
|
|
89
|
+
|
|
87
90
|
puts "Retry #{current_attempt + 1} of #{MAX_ATTEMPTS}"
|
|
88
91
|
end
|
|
89
92
|
|
|
90
93
|
# Open the socket that an application's daemon listens on
|
|
91
94
|
#
|
|
92
|
-
# It replaces a socket file that no daemon answers on
|
|
93
|
-
# status 7 if a daemon already does.
|
|
95
|
+
# It replaces a socket file that no daemon answers on.
|
|
94
96
|
#
|
|
95
97
|
# @param [String] base_dir the directory with the daemons' sockets
|
|
96
98
|
# @param [String] name the application's name
|
|
97
99
|
#
|
|
98
100
|
# @return [UNIXServer]
|
|
99
101
|
#
|
|
102
|
+
# @raise [AlreadyRunningError] if a daemon already answers on it
|
|
103
|
+
#
|
|
100
104
|
# @api private
|
|
101
105
|
def self.server(base_dir, name)
|
|
102
106
|
# @type var path: String
|
|
@@ -108,13 +112,14 @@ module Bluepill
|
|
|
108
112
|
|
|
109
113
|
# Open a daemon's socket in place of a socket file that already exists
|
|
110
114
|
#
|
|
111
|
-
# It deletes the file and opens the socket if no daemon answers on it
|
|
112
|
-
# and exits with status 7 if a daemon does.
|
|
115
|
+
# It deletes the file and opens the socket if no daemon answers on it.
|
|
113
116
|
#
|
|
114
117
|
# @param [String] path the socket file
|
|
115
118
|
#
|
|
116
119
|
# @return [UNIXServer]
|
|
117
120
|
#
|
|
121
|
+
# @raise [AlreadyRunningError] if a daemon answers on it
|
|
122
|
+
#
|
|
118
123
|
# @api private
|
|
119
124
|
def self.replace_stale_socket(path)
|
|
120
125
|
# if sock file has been created, test to see if there is a server
|
|
@@ -123,8 +128,7 @@ module Bluepill
|
|
|
123
128
|
File.delete(path)
|
|
124
129
|
UNIXServer.open(path)
|
|
125
130
|
else
|
|
126
|
-
|
|
127
|
-
exit(7)
|
|
131
|
+
raise AlreadyRunningError, "Server is already running!"
|
|
128
132
|
end
|
|
129
133
|
|
|
130
134
|
# Return the path of an application's socket
|
|
@@ -135,8 +139,6 @@ module Bluepill
|
|
|
135
139
|
# @return [String]
|
|
136
140
|
#
|
|
137
141
|
# @api private
|
|
138
|
-
def self.socket_path(base_dir, name)
|
|
139
|
-
BaseDirectory.new(base_dir).socket_file(name)
|
|
140
|
-
end
|
|
142
|
+
def self.socket_path(base_dir, name) = BaseDirectory.new(base_dir).socket_file(name)
|
|
141
143
|
end
|
|
142
144
|
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bluepill
|
|
4
|
+
module System
|
|
5
|
+
# How a command runs: as which user and groups, in which directory,
|
|
6
|
+
# with which environment and standard streams, and for how long
|
|
7
|
+
#
|
|
8
|
+
# Every option is nil unless it's given. Process#command_options makes
|
|
9
|
+
# one from the process's attributes.
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
12
|
+
class CommandOptions
|
|
13
|
+
# Return the user the command runs as
|
|
14
|
+
#
|
|
15
|
+
# @return [String, nil] the user's name
|
|
16
|
+
#
|
|
17
|
+
# @api private
|
|
18
|
+
attr_reader :uid
|
|
19
|
+
|
|
20
|
+
# Return the group the command runs as
|
|
21
|
+
#
|
|
22
|
+
# @return [String, nil] the group's name
|
|
23
|
+
#
|
|
24
|
+
# @api private
|
|
25
|
+
attr_reader :gid
|
|
26
|
+
|
|
27
|
+
# Return the groups the command joins, besides the user's own
|
|
28
|
+
#
|
|
29
|
+
# @return [Array<String>, nil] the groups' names
|
|
30
|
+
#
|
|
31
|
+
# @api private
|
|
32
|
+
attr_reader :supplementary_groups
|
|
33
|
+
|
|
34
|
+
# Return the directory the command runs in
|
|
35
|
+
#
|
|
36
|
+
# @return [String, nil]
|
|
37
|
+
#
|
|
38
|
+
# @api private
|
|
39
|
+
attr_reader :working_dir
|
|
40
|
+
|
|
41
|
+
# Return the environment variables the command gets
|
|
42
|
+
#
|
|
43
|
+
# @return [Hash{#to_s => #to_s}] the variables, empty when there are
|
|
44
|
+
# none
|
|
45
|
+
#
|
|
46
|
+
# @api private
|
|
47
|
+
attr_reader :environment
|
|
48
|
+
|
|
49
|
+
# Return the file a daemon's standard input reads
|
|
50
|
+
#
|
|
51
|
+
# @return [String, nil]
|
|
52
|
+
#
|
|
53
|
+
# @api private
|
|
54
|
+
attr_reader :stdin
|
|
55
|
+
|
|
56
|
+
# Return the file a daemon's standard output is appended to
|
|
57
|
+
#
|
|
58
|
+
# @return [String, nil]
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
attr_reader :stdout
|
|
62
|
+
|
|
63
|
+
# Return the file a daemon's standard error is appended to
|
|
64
|
+
#
|
|
65
|
+
# @return [String, nil]
|
|
66
|
+
#
|
|
67
|
+
# @api private
|
|
68
|
+
attr_reader :stderr
|
|
69
|
+
|
|
70
|
+
# Return how long a command that runs until it finishes may run
|
|
71
|
+
#
|
|
72
|
+
# @return [Numeric, ActiveSupport::Duration, nil] the seconds, or nil
|
|
73
|
+
# to wait until it finishes
|
|
74
|
+
#
|
|
75
|
+
# @api private
|
|
76
|
+
attr_reader :timeout
|
|
77
|
+
|
|
78
|
+
# Describe how a command runs
|
|
79
|
+
#
|
|
80
|
+
# @param [String, nil] uid the user's name
|
|
81
|
+
# @param [String, nil] gid the group's name
|
|
82
|
+
# @param [Array<String>, nil] supplementary_groups more groups' names
|
|
83
|
+
# @param [String, nil] working_dir the directory to run in
|
|
84
|
+
# @param [Hash{#to_s => #to_s}, nil] environment the environment
|
|
85
|
+
# variables, or nil for none
|
|
86
|
+
# @param [String, nil] stdin a daemon's standard input
|
|
87
|
+
# @param [String, nil] stdout a daemon's standard output
|
|
88
|
+
# @param [String, nil] stderr a daemon's standard error
|
|
89
|
+
# @param [Numeric, ActiveSupport::Duration, nil] timeout the seconds a
|
|
90
|
+
# command that runs until it finishes may take
|
|
91
|
+
#
|
|
92
|
+
# @return [void]
|
|
93
|
+
#
|
|
94
|
+
# @api private
|
|
95
|
+
def initialize(uid: nil, gid: nil, supplementary_groups: nil, working_dir: nil, environment: nil, stdin: nil,
|
|
96
|
+
stdout: nil, stderr: nil, timeout: nil)
|
|
97
|
+
@uid = uid
|
|
98
|
+
@gid = gid
|
|
99
|
+
@supplementary_groups = supplementary_groups
|
|
100
|
+
@working_dir = working_dir
|
|
101
|
+
@environment = environment || {}
|
|
102
|
+
@stdin = stdin
|
|
103
|
+
@stdout = stdout
|
|
104
|
+
@stderr = stderr
|
|
105
|
+
@timeout = timeout
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Return the options for a hook command
|
|
109
|
+
#
|
|
110
|
+
# They're these, with more environment variables and a timeout.
|
|
111
|
+
#
|
|
112
|
+
# @param [Hash{String => String}] env more environment variables
|
|
113
|
+
# @param [Numeric, ActiveSupport::Duration] timeout the seconds the hook
|
|
114
|
+
# may take
|
|
115
|
+
#
|
|
116
|
+
# @return [CommandOptions]
|
|
117
|
+
#
|
|
118
|
+
# @api private
|
|
119
|
+
def for_hook(env, timeout)
|
|
120
|
+
self.class.new(**to_h, environment: environment.merge(env), timeout:)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Return the options as a Hash
|
|
124
|
+
#
|
|
125
|
+
# @return [Hash{Symbol => Object}] each option by name, nil when unset
|
|
126
|
+
#
|
|
127
|
+
# @api private
|
|
128
|
+
def to_h
|
|
129
|
+
{
|
|
130
|
+
uid:, gid:, supplementary_groups:, working_dir:,
|
|
131
|
+
environment:, stdin:, stdout:, stderr:, timeout:
|
|
132
|
+
}
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bluepill
|
|
4
|
+
module System
|
|
5
|
+
# What a command that ran until it finished printed, and how it exited
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
8
|
+
class CommandResult
|
|
9
|
+
# Make a result from the Hash that the command's child reports
|
|
10
|
+
#
|
|
11
|
+
# @param [Hash{Symbol => Object}] result its :exit_code, :stdout and
|
|
12
|
+
# :stderr
|
|
13
|
+
#
|
|
14
|
+
# @return [CommandResult]
|
|
15
|
+
#
|
|
16
|
+
# @api private
|
|
17
|
+
def self.from_h(result)
|
|
18
|
+
new(exit_code: result.fetch(:exit_code), stdout: result.fetch(:stdout), stderr: result.fetch(:stderr))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Return the command's exit status
|
|
22
|
+
#
|
|
23
|
+
# @return [Integer, nil] the status, or nil if a signal ended it
|
|
24
|
+
#
|
|
25
|
+
# @api private
|
|
26
|
+
attr_reader :exit_code
|
|
27
|
+
|
|
28
|
+
# Return what the command printed to its standard output
|
|
29
|
+
#
|
|
30
|
+
# @return [String]
|
|
31
|
+
#
|
|
32
|
+
# @api private
|
|
33
|
+
attr_reader :stdout
|
|
34
|
+
|
|
35
|
+
# Return what the command printed to its standard error
|
|
36
|
+
#
|
|
37
|
+
# @return [String]
|
|
38
|
+
#
|
|
39
|
+
# @api private
|
|
40
|
+
attr_reader :stderr
|
|
41
|
+
|
|
42
|
+
# Describe a command's result
|
|
43
|
+
#
|
|
44
|
+
# @param [Integer, nil] exit_code the exit status, or nil if a signal
|
|
45
|
+
# ended the command
|
|
46
|
+
# @param [String] stdout what it printed to its standard output
|
|
47
|
+
# @param [String] stderr what it printed to its standard error
|
|
48
|
+
#
|
|
49
|
+
# @return [void]
|
|
50
|
+
#
|
|
51
|
+
# @api private
|
|
52
|
+
def initialize(exit_code:, stdout: "", stderr: "")
|
|
53
|
+
@exit_code = exit_code
|
|
54
|
+
@stdout = stdout
|
|
55
|
+
@stderr = stderr
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Return whether the command exited with status 0
|
|
59
|
+
#
|
|
60
|
+
# @return [Boolean]
|
|
61
|
+
#
|
|
62
|
+
# @api private
|
|
63
|
+
def success? = exit_code&.zero? || false
|
|
64
|
+
|
|
65
|
+
# Return the result as a Hash, as the command's child reports it
|
|
66
|
+
#
|
|
67
|
+
# @return [Hash{Symbol => Object}] the :exit_code, :stdout and :stderr
|
|
68
|
+
#
|
|
69
|
+
# @api private
|
|
70
|
+
def to_h = {exit_code:, stdout:, stderr:}
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -14,17 +14,16 @@ module Bluepill
|
|
|
14
14
|
module CommandSetup
|
|
15
15
|
# Change to the command's working directory and set its environment
|
|
16
16
|
#
|
|
17
|
-
# @param [
|
|
17
|
+
# @param [CommandOptions] options its working_dir and environment
|
|
18
18
|
#
|
|
19
19
|
# @return [void]
|
|
20
20
|
#
|
|
21
21
|
# @api private
|
|
22
22
|
def setup_command_environment(options)
|
|
23
23
|
# Setting end PWD env emulates bash behavior when dealing with symlinks
|
|
24
|
-
working_dir = options
|
|
24
|
+
working_dir = options.working_dir
|
|
25
25
|
Dir.chdir(ENV["PWD"] = working_dir.to_s) if working_dir
|
|
26
|
-
environment
|
|
27
|
-
environment&.each { |key, value| ENV[key.to_s] = value.to_s }
|
|
26
|
+
options.environment.each { |key, value| ENV[key.to_s] = value.to_s }
|
|
28
27
|
end
|
|
29
28
|
|
|
30
29
|
# Switch to another user and groups, when bluepill runs as root
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shellwords"
|
|
4
|
+
require "bluepill/system/command_result"
|
|
5
|
+
|
|
6
|
+
module Bluepill
|
|
7
|
+
module System
|
|
8
|
+
# Start commands without forking bluepill, in a Ruby that can't fork,
|
|
9
|
+
# such as JRuby
|
|
10
|
+
#
|
|
11
|
+
# The command gets the working directory and environment that a forked
|
|
12
|
+
# one sets up. It can't switch to another user or groups, which a
|
|
13
|
+
# config that sets them fails to load for.
|
|
14
|
+
#
|
|
15
|
+
# Commands and Daemon include it, and System extends them, so these are
|
|
16
|
+
# System's module functions.
|
|
17
|
+
#
|
|
18
|
+
# @api private
|
|
19
|
+
module CommandSpawn
|
|
20
|
+
# The seconds between checks of whether a spawned command has exited
|
|
21
|
+
EXIT_POLL_INTERVAL = 0.05
|
|
22
|
+
|
|
23
|
+
# Start a command, without waiting for it to finish
|
|
24
|
+
#
|
|
25
|
+
# @param [String] cmd the command, split into words without a shell
|
|
26
|
+
# @param [CommandOptions] options its working_dir and environment
|
|
27
|
+
# @param [String, nil] chdir the directory to run in without a
|
|
28
|
+
# working_dir, or nil for bluepill's
|
|
29
|
+
# @param [Hash{Symbol => Object}] redirects more of Process.spawn's
|
|
30
|
+
# options, such as where its standard streams go
|
|
31
|
+
#
|
|
32
|
+
# @return [Integer] the command's pid
|
|
33
|
+
#
|
|
34
|
+
# @raise [SystemCallError] if it can't be run, say because it doesn't
|
|
35
|
+
# exist
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
38
|
+
def spawn_command(cmd, options, chdir: nil, **redirects)
|
|
39
|
+
dir = options.working_dir&.to_s || chdir
|
|
40
|
+
redirects[:chdir] = dir if dir
|
|
41
|
+
program, *args = Shellwords.shellwords(cmd)
|
|
42
|
+
::Process.spawn(spawn_environment(options), program.to_s, *args, **redirects)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Run a command until it finishes, and return what it printed
|
|
46
|
+
#
|
|
47
|
+
# It's execute_blocking, for a Ruby that can't fork. Two threads read
|
|
48
|
+
# what the command prints, so neither pipe fills up and stops it.
|
|
49
|
+
#
|
|
50
|
+
# @param [String] cmd the command, split into words without a shell
|
|
51
|
+
# @param [CommandOptions] options its working_dir, environment and
|
|
52
|
+
# timeout
|
|
53
|
+
#
|
|
54
|
+
# @return [CommandResult] what it printed, and its exit code, which is
|
|
55
|
+
# 1 if it couldn't be run
|
|
56
|
+
#
|
|
57
|
+
# @raise [Timeout::Error] if it ran longer than the timeout
|
|
58
|
+
#
|
|
59
|
+
# @api private
|
|
60
|
+
def spawn_blocking(cmd, options)
|
|
61
|
+
out_read, out_write = IO.pipe
|
|
62
|
+
err_read, err_write = IO.pipe
|
|
63
|
+
run_spawned(cmd, options, [out_read, err_read], [out_write, err_write])
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Spawn a command with its output on pipes, and wait for it to finish
|
|
67
|
+
#
|
|
68
|
+
# @param [String] cmd the command
|
|
69
|
+
# @param [CommandOptions] options as for spawn_blocking
|
|
70
|
+
# @param [Array(IO, IO)] readers the pipes' readers, for its standard
|
|
71
|
+
# output and standard error, which are closed after
|
|
72
|
+
# @param [Array(IO, IO)] writers the pipes' writers
|
|
73
|
+
#
|
|
74
|
+
# @return [CommandResult] what it printed, and its exit code, which is
|
|
75
|
+
# 1 if it couldn't be run
|
|
76
|
+
#
|
|
77
|
+
# @raise [Timeout::Error] if it ran longer than the timeout
|
|
78
|
+
#
|
|
79
|
+
# @api private
|
|
80
|
+
def run_spawned(cmd, options, readers, writers)
|
|
81
|
+
out, err = writers
|
|
82
|
+
pid = spawn_blocking_command(cmd, options, out, err)
|
|
83
|
+
wait_for_spawned(cmd, options.timeout, pid, readers)
|
|
84
|
+
rescue SystemCallError => e
|
|
85
|
+
CommandResult.new(exit_code: 1, stderr: "Could not run #{cmd}: #{e.message}\n")
|
|
86
|
+
ensure
|
|
87
|
+
readers.each(&:close)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Spawn a command that runs until it finishes, with its output on pipes
|
|
91
|
+
#
|
|
92
|
+
# With a timeout, it runs in a process group of its own, which is
|
|
93
|
+
# killed if it runs too long. The pipes' writers are closed here, so
|
|
94
|
+
# they end when the command closes its ends.
|
|
95
|
+
#
|
|
96
|
+
# @param [String] cmd the command
|
|
97
|
+
# @param [CommandOptions] options as for spawn_blocking
|
|
98
|
+
# @param [IO] out the pipe for its standard output
|
|
99
|
+
# @param [IO] err the pipe for its standard error
|
|
100
|
+
#
|
|
101
|
+
# @return [Integer] its pid
|
|
102
|
+
#
|
|
103
|
+
# @raise [SystemCallError] if it can't be run
|
|
104
|
+
#
|
|
105
|
+
# @api private
|
|
106
|
+
def spawn_blocking_command(cmd, options, out, err)
|
|
107
|
+
# pgroup: nil leaves it in bluepill's process group
|
|
108
|
+
spawn_command(cmd, options, in: File::NULL, out:, err:, pgroup: (true if options.timeout))
|
|
109
|
+
ensure
|
|
110
|
+
out.close
|
|
111
|
+
err.close
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Wait for a spawned command to finish, and collect what it printed
|
|
115
|
+
#
|
|
116
|
+
# @param [String] cmd the command, for the error on a timeout
|
|
117
|
+
# @param [Numeric, ActiveSupport::Duration, nil] timeout the seconds to
|
|
118
|
+
# wait for it, or nil to wait until it finishes
|
|
119
|
+
# @param [Integer] pid its pid
|
|
120
|
+
# @param [Array(IO, IO)] pipes where to read its standard output and
|
|
121
|
+
# standard error
|
|
122
|
+
#
|
|
123
|
+
# @return [CommandResult] what it printed, and its exit code (nil if a
|
|
124
|
+
# signal ended it)
|
|
125
|
+
#
|
|
126
|
+
# @raise [Timeout::Error] if it ran longer than the timeout
|
|
127
|
+
#
|
|
128
|
+
# @api private
|
|
129
|
+
def wait_for_spawned(cmd, timeout, pid, pipes)
|
|
130
|
+
readers = pipes.collect { |pipe| read_in_background(pipe) }
|
|
131
|
+
status = wait_for_exit(pid, timeout) || kill_spawned_after(pid, timeout, cmd)
|
|
132
|
+
stdout, stderr = readers.collect(&:value)
|
|
133
|
+
CommandResult.new(exit_code: status.exitstatus, stdout:, stderr:)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Wait for a spawned command to exit, for up to a timeout
|
|
137
|
+
#
|
|
138
|
+
# It polls, since a thread blocked waiting for a process would keep
|
|
139
|
+
# JRuby from exiting until the process does.
|
|
140
|
+
#
|
|
141
|
+
# @param [Integer] pid the command's pid
|
|
142
|
+
# @param [Numeric, ActiveSupport::Duration, nil] timeout the seconds to
|
|
143
|
+
# wait, or nil to wait until it exits
|
|
144
|
+
#
|
|
145
|
+
# @return [Process::Status, nil] how it exited, or nil if it's still
|
|
146
|
+
# running at the timeout
|
|
147
|
+
#
|
|
148
|
+
# @api private
|
|
149
|
+
def wait_for_exit(pid, timeout)
|
|
150
|
+
deadline = monotonic_now + timeout.to_f if timeout
|
|
151
|
+
loop do
|
|
152
|
+
_, status = ::Process.wait2(pid, ::Process::WNOHANG)
|
|
153
|
+
return status if status
|
|
154
|
+
return if deadline && monotonic_now >= deadline
|
|
155
|
+
|
|
156
|
+
Kernel.sleep(EXIT_POLL_INTERVAL)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Kill a spawned command's process group after its timeout, and reap it
|
|
161
|
+
#
|
|
162
|
+
# @param [Integer] pid the command's pid, which leads the group
|
|
163
|
+
# @param [Numeric, ActiveSupport::Duration] timeout the seconds it was given
|
|
164
|
+
# @param [String] cmd the command, for the error
|
|
165
|
+
#
|
|
166
|
+
# @return [void] it never returns
|
|
167
|
+
#
|
|
168
|
+
# @raise [Timeout::Error] always
|
|
169
|
+
#
|
|
170
|
+
# @api private
|
|
171
|
+
def kill_spawned_after(pid, timeout, cmd)
|
|
172
|
+
kill_group_after(pid, timeout, cmd)
|
|
173
|
+
ensure
|
|
174
|
+
begin
|
|
175
|
+
::Process.wait(pid)
|
|
176
|
+
rescue Errno::ECHILD
|
|
177
|
+
# It was collected already
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Kill a command's process group, once it has run past its timeout
|
|
182
|
+
#
|
|
183
|
+
# Forked commands are killed this way too.
|
|
184
|
+
#
|
|
185
|
+
# @param [Integer] child the pid of the group's leader, which runs the
|
|
186
|
+
# command
|
|
187
|
+
# @param [Numeric, ActiveSupport::Duration] timeout the seconds it was given
|
|
188
|
+
# @param [String] cmd the command, for the error
|
|
189
|
+
#
|
|
190
|
+
# @return [void] it never returns
|
|
191
|
+
#
|
|
192
|
+
# @raise [Timeout::Error] always
|
|
193
|
+
#
|
|
194
|
+
# @api private
|
|
195
|
+
def kill_group_after(child, timeout, cmd)
|
|
196
|
+
begin
|
|
197
|
+
::Process.kill("KILL", -child)
|
|
198
|
+
rescue Errno::ESRCH
|
|
199
|
+
nil # it finished just now
|
|
200
|
+
end
|
|
201
|
+
raise Timeout::Error, "#{cmd} took longer than #{timeout} seconds"
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Return the seconds on the monotonic clock, as a Float
|
|
205
|
+
#
|
|
206
|
+
# @return [Float]
|
|
207
|
+
#
|
|
208
|
+
# @api private
|
|
209
|
+
def monotonic_now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
210
|
+
|
|
211
|
+
# Read a pipe to its end in a thread
|
|
212
|
+
#
|
|
213
|
+
# @param [IO] reader the pipe
|
|
214
|
+
#
|
|
215
|
+
# @return [Thread] a thread whose value is what it read, or "" if the
|
|
216
|
+
# pipe was closed first, after a timeout
|
|
217
|
+
#
|
|
218
|
+
# @api private
|
|
219
|
+
def read_in_background(reader)
|
|
220
|
+
Thread.new do
|
|
221
|
+
reader.read
|
|
222
|
+
rescue IOError
|
|
223
|
+
""
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Return the environment variables a command gets, as Strings
|
|
228
|
+
#
|
|
229
|
+
# PWD is the working directory, as a shell sets it, unless the
|
|
230
|
+
# environment sets it too.
|
|
231
|
+
#
|
|
232
|
+
# @param [CommandOptions] options its working_dir and environment
|
|
233
|
+
#
|
|
234
|
+
# @return [Hash{String => String}]
|
|
235
|
+
#
|
|
236
|
+
# @api private
|
|
237
|
+
def spawn_environment(options)
|
|
238
|
+
working_dir = options.working_dir
|
|
239
|
+
env = options.environment.to_h { |key, value| [key.to_s, value.to_s] }
|
|
240
|
+
working_dir ? {"PWD" => working_dir.to_s}.merge(env) : env
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
end
|