This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from heroku/heroku-buildpack-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_helpers.rb
298 lines (260 loc) · 8.02 KB
/
shell_helpers.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
292
293
294
295
296
297
298
require "shellwords"
class BuildpackError < StandardError
end
class NoShellEscape < String
def shellescape
self
end
end
module LanguagePack
module ShellHelpers
@@user_env_hash = {}
@@warnings = []
@@deprecations = []
def warnings
@@warnings
end
def deprecations
@@deprecations
end
def mcount(key, value = 1)
private_log("count", key => value)
end
def mmeasure(key, value)
private_log("measure", key => value)
end
def munique(key, value)
private_log("unique", key => value)
end
def self.user_env_hash
@@user_env_hash
end
def user_env_hash
@@user_env_hash
end
def env(var)
ENV[var] || user_env_hash[var]
end
def self.blacklist?(key)
%w(PATH GEM_PATH GEM_HOME GIT_DIR JRUBY_OPTS JAVA_OPTS JAVA_TOOL_OPTIONS RUBYOPT).include?(key)
end
def self.initialize_env(path)
env_dir = Pathname.new("#{path}")
if env_dir.exist? && env_dir.directory?
env_dir.each_child do |file|
key = file.basename.to_s
value = file.read.strip
user_env_hash[key] = value unless blacklist?(key)
end
end
end
# Should only be used once in the top level script
# for example $ bin/support/ruby_compile
def self.display_error_and_exit(e)
Kernel.puts "\e[1m\e[31m" # Bold Red
Kernel.puts " !"
e.message.split("\n").each do |line|
Kernel.puts " ! #{line.strip}"
end
Kernel.puts " !\e[0m"
if e.is_a?(BuildpackError)
exit 1
else
raise e
end
end
# run a shell command (deferring to #run), and raise an error if it fails
# @param [String] command to be run
# @return [String] result of #run
# @option options [Error] :error_class Class of error to raise, defaults to Standard Error
# @option options [Integer] :max_attempts Number of times to attempt command before raising
def run!(command, options = {})
max_attempts = options[:max_attempts] || 1
error_class = options[:error_class] || StandardError
max_attempts.times do |attempt_number|
result = run(command, options)
if $?.success?
return result
end
if attempt_number == max_attempts - 1
raise error_class, "Command: '#{command}' failed unexpectedly:\n#{result}"
else
puts "Command: '#{command}' failed on attempt #{attempt_number + 1} of #{max_attempts}."
end
end
end
# doesn't do any special piping. stderr won't be redirected.
# @param [String] command to be run
# @return [String] output of stdout
def run_no_pipe(command, options = {})
run(command, options.merge({:out => ""}))
end
# run a shell command and pipe stderr to stdout
# @param [String] command
# @option options [String] :out the IO redirect of the command
# @option options [Hash] :env explicit environment to run command in
# @option options [Boolean] :user_env whether or not a user's environment variables will be loaded
def run(command, options = {})
%x{ #{command_options_to_string(command, options)} }
end
# run a shell command and pipe stderr to /dev/null
# @param [String] command to be run
# @return [String] output of stdout
def run_stdout(command, options = {})
options[:out] ||= '2>/dev/null'
run(command, options)
end
def command_options_to_string(command, options)
options[:env] ||= {}
options[:out] ||= "2>&1"
options[:env] = user_env_hash.merge(options[:env]) if options[:user_env]
env = options[:env].map {|key, value| "#{key.shellescape}=#{value.shellescape}" }.join(" ")
"/usr/bin/env #{env} bash -c #{command.shellescape} #{options[:out]} "
end
# Class for running process spawn with a timeout
#
# Example:
#
# spawn = ProcessSpawn.new("echo 'hello'")
# spawn.success? # => true
# spawn.timeout? # => false
#
# If you want to get the output of the process, you will need to pass
# in a file:
#
# spawn = ProcessSpawn.new("echo 'hello'", file: "hello.txt")
# spawn.success? # => true
# spawn.output # => "hello"
#
# The main benefit of using a file is that even if the command fails
# or times out, there will still be partial results in the output file.
#
# To add a timeout, instantiate pass in a hash with a `timeout` key
# in seconds:
#
# spawn = ProcessSpawn.new("sleep 10", timeout: 4)
# spawn.success? # => false
# spawn.timeout? # => true
#
class ProcessSpawn
include ShellHelpers
def initialize(command, options = {})
@timeout_value = options.delete(:timeout)
@file = options.delete(:file)
if @file
raise "Cannot specify :file, and :out" if options[:out]
@file = Pathname.new(@file)
@file.dirname.mkpath
options[:out] = ">> #{@file} 2>&1"
end
@command = command_options_to_string(command, options)
@did_time_out = false
@success = false
end
def output
raise "no file name given" if @file.nil?
exec_once
@file.read.encode('UTF-8', invalid: :replace)
end
def timeout?
exec_once
@did_time_out
end
def success?
exec_once
@success
end
private
def exec_once
return if @exec_once
@exec_once = true
wait_with_timeout
true
end
def wait_with_timeout
pid = Process.spawn(@command)
Timeout.timeout(@timeout_value) do
Process.wait(pid)
@success = $?.success?
end
rescue Timeout::Error
Process.kill("SIGKILL", pid)
@did_time_out = true
@success = false
end
end
# run a shell command and stream the output
# @param [String] command to be run
def pipe(command, options = {})
output = options[:buffer] || ""
silent = options[:silent]
# self.puts uses the method defined in this file
output_object = options[:output_object] || self
IO.popen(command_options_to_string(command, options)) do |io|
until io.eof?
buffer = io.gets
output << buffer
output_object.puts(buffer) unless silent
end
end
output
end
# display a topic message
# (denoted by ----->)
# @param [String] topic message to be displayed
def topic(message)
Kernel.puts "-----> #{message}"
$stdout.flush
end
# display a message in line
# (indented by 6 spaces)
# @param [String] message to be displayed
def puts(message)
message.each_line do |line|
if line.end_with?("\n".freeze)
print " #{line}"
else
print " #{line}\n"
end
end
$stdout.flush
rescue ArgumentError => e
error_message = e.message
raise e if error_message !~ /invalid byte sequence/
mcount "fail.invalid_utf8"
error_message << "\n Invalid string: #{message}"
raise e, error_message
end
def warn(message, options = {})
if options.key?(:inline) ? options[:inline] : false
Kernel.puts ""
Kernel.puts "\e[1m\e[33m###### WARNING:\e[0m" # Bold yellow
Kernel.puts ""
puts message
Kernel.puts ""
end
warnings << message
end
def error(message)
raise BuildpackError, message
end
def deprecate(message)
deprecations << message
end
def noshellescape(string)
NoShellEscape.new(string)
end
private
def private_log(name, key_value_hash)
File.open(ENV["BUILDPACK_LOG_FILE"] || "/dev/null", "a+") do |f|
key_value_hash.each do |key, value|
metric = String.new("#{name}#")
metric << "#{ENV["BPLOG_PREFIX"]}"
metric << "." unless metric.end_with?('.')
metric << "#{key}=#{value}"
f.puts metric
end
end
end
end
end