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
|
# File 'lib/itamae/backend.rb', line 45
def run_command(commands, options = {})
options = {error: true}.merge(options)
if commands.is_a?(Array)
command = commands.map do |cmd|
Shellwords.escape(cmd)
end.join(' ')
else
command = commands
end
cwd = options[:cwd]
if cwd
command = "cd #{Shellwords.escape(cwd)} && #{command}"
end
user = options[:user]
if user
command = "sudo -u #{Shellwords.escape(user)} -- /bin/sh -c #{Shellwords.escape(command)}"
end
Logger.debug "Executing `#{command}`..."
result = Specinfra::Runner.run_command(command)
exit_status = result.exit_status
Logger.formatter.indent do
if exit_status == 0 || !options[:error]
method = :debug
message = "exited with #{exit_status}"
else
method = :error
message = "Command `#{command}` failed. (exit status: #{exit_status})"
end
Logger.public_send(method, message)
{"stdout" => result.stdout, "stderr" => result.stderr}.each_pair do |name, value|
next unless value && value != ''
if value.bytesize > 1024 * 1024
Logger.public_send(method, "#{name} is suppressed because it's too large")
next
end
value.each_line do |line|
case line.encoding
when Encoding::UTF_8
line = line.tr("\u0000-\u001f\u007f\u2028",'')
end
Logger.public_send(method, "#{name} | #{line}")
end
end
end
if options[:error] && exit_status != 0
raise CommandExecutionError
end
result
end
|