ShellScriptExecutorImpl.java
12.4 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.jcr.api.SlingRepository
* org.apache.sling.settings.SlingSettingsService
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.monitoring.impl;
import com.adobe.granite.monitoring.impl.ScriptConfig;
import com.adobe.granite.monitoring.impl.ShellScriptExecutor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.Calendar;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={ShellScriptExecutor.class})
public class ShellScriptExecutorImpl
implements ShellScriptExecutor {
private static final String SERVICE_USER = "monitoringScripts";
private static final Logger log = LoggerFactory.getLogger(ShellScriptExecutorImpl.class);
public static final String[] SCRIPTS_PATHS = new String[]{"/libs/granite/monitoring/scripts"};
@Reference
private SlingRepository repository = null;
@Reference
private SlingSettingsService settings = null;
private File execDirectory;
@Activate
private void activate() {
try {
this.execDirectory = new File(this.settings.getSlingHomePath(), "monitoring").getCanonicalFile();
if (!this.execDirectory.exists()) {
this.execDirectory.mkdirs();
}
log.info("Scrip executor started. Exec directory is {}", (Object)this.execDirectory.getPath());
}
catch (IOException e) {
log.info("Scrip executor invalid exec directory", (Throwable)e);
this.execDirectory = null;
}
}
public ShellScriptExecutor.Result execute(ScriptConfig config) {
File scriptFile;
ShellScriptExecutor.Result r;
if (this.execDirectory == null) {
return null;
}
try {
scriptFile = new File(this.execDirectory, config.getScriptFilename()).getCanonicalFile();
}
catch (IOException e) {
log.error("Error sanitizing script file name.", (Throwable)e);
return null;
}
try {
String path = config.getScriptPath();
if (path != null && path.length() > 0) {
this.extractScript(path, scriptFile);
}
}
catch (IOException e) {
log.error("Unable to extract script '{}' to '{}'", new Object[]{config.getScriptPath(), scriptFile.getPath(), e});
}
ProcessExecutor ex = new ProcessExecutor(scriptFile);
Thread t = new Thread((Runnable)ex, "Process Executor for " + scriptFile.getName());
t.setDaemon(true);
t.start();
try {
t.join(5000);
}
catch (InterruptedException e) {
log.error("Error while waiting for process executor termination.", (Throwable)e);
}
if (t.isAlive()) {
log.warn("Script {} did not complete within 5 seconds. Aborting.", (Object)scriptFile.getPath());
ex.kill();
try {
t.join();
}
catch (InterruptedException e) {
log.error("Error while waiting for process executor termination.", (Throwable)e);
}
}
if ((r = ex.getResult()) != null && r.getExitCode() != 0) {
this.logOut(scriptFile.getName() + "-exit", String.valueOf(r.getExitCode()));
this.logOut(scriptFile.getName() + "-stdout", r.getStdout());
this.logOut(scriptFile.getName() + "-stderr", r.getStderr());
}
return r;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void extractScript(String relPath, File targetFile) throws IOException {
Session session = null;
try {
session = this.repository.loginService("monitoringScripts", null);
Node content = null;
for (String rootPath : SCRIPTS_PATHS) {
StringBuilder b = new StringBuilder(rootPath);
if (!relPath.startsWith("/")) {
b.append("/");
}
b.append(relPath).append("/").append("{http://www.jcp.org/jcr/1.0}content");
if (!session.nodeExists(b.toString()) || (content = session.getNode(b.toString())).getPath().startsWith(rootPath)) continue;
throw new IOException("Invalid absolute location " + content.getPath() + " of script " + relPath);
}
if (content == null) {
throw new FileNotFoundException(relPath);
}
Calendar cal = content.getProperty("{http://www.jcp.org/jcr/1.0}lastModified").getDate();
if (targetFile.isFile() && targetFile.lastModified() > cal.getTimeInMillis() - 1000) {
return;
}
Class len$ = this.getClass();
synchronized (len$) {
InputStream in = null;
FileOutputStream out = null;
Binary bin = null;
try {
bin = content.getProperty("{http://www.jcp.org/jcr/1.0}data").getBinary();
in = bin.getStream();
out = new FileOutputStream(targetFile);
IOUtils.copy((InputStream)in, (OutputStream)out);
}
finally {
if (bin != null) {
bin.dispose();
}
IOUtils.closeQuietly((InputStream)in);
IOUtils.closeQuietly((OutputStream)out);
}
targetFile.setLastModified(cal.getTimeInMillis());
this.makeScriptExecutable(targetFile);
}
}
catch (RepositoryException e) {
IOException ioe = new IOException(e.getMessage());
ioe.initCause((Throwable)e);
throw ioe;
}
finally {
if (session != null) {
session.logout();
}
}
}
private void makeScriptExecutable(File scriptFile) {
Method m = null;
boolean didSetBit = false;
try {
m = scriptFile.getClass().getDeclaredMethod("setExecutable", Boolean.TYPE);
}
catch (Exception e) {
log.debug("Can not set executable bit of {} to true because not supported by the current JDK!", (Object)scriptFile.getAbsolutePath());
}
if (m != null) {
try {
m.invoke(scriptFile, true);
didSetBit = true;
}
catch (Exception e) {
log.warn("Couldn't set executable bit of {} to true", (Object)scriptFile.getAbsolutePath(), (Object)e);
}
}
if (!didSetBit) {
try {
Process p = Runtime.getRuntime().exec(new String[]{"chmod", "+x", scriptFile.getPath()});
p.waitFor();
if (p.exitValue() != 0) {
this.logOut("chmod-stdout", IOUtils.toString((InputStream)p.getInputStream(), (String)"utf-8"));
this.logOut("chmod-stderr", IOUtils.toString((InputStream)p.getErrorStream(), (String)"utf-8"));
}
}
catch (Exception e) {
log.warn("Unable to make script {} executable: {}", (Object)scriptFile.getPath(), (Object)e.toString());
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void logOut(String name, String output) {
if (output == null) {
log.info("{}: (null)", (Object)name);
return;
}
try {
BufferedReader rdr = new BufferedReader(new StringReader(output));
try {
String line;
while ((line = rdr.readLine()) != null) {
log.info("{}: {}", (Object)name, (Object)line);
}
}
finally {
rdr.close();
}
}
catch (IOException e) {
// empty catch block
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindSettings(SlingSettingsService slingSettingsService) {
this.settings = slingSettingsService;
}
protected void unbindSettings(SlingSettingsService slingSettingsService) {
if (this.settings == slingSettingsService) {
this.settings = null;
}
}
private static class ResultImpl
implements ShellScriptExecutor.Result {
private final int exitCode;
private final String stdout;
private final String stderr;
private ResultImpl(int exitCode, String stdout, String stderr) {
this.exitCode = exitCode;
this.stdout = stdout;
this.stderr = stderr;
}
public int getExitCode() {
return this.exitCode;
}
public String getStdout() {
return this.stdout;
}
public String getStderr() {
return this.stderr;
}
}
private static class ProcessExecutor
implements Runnable {
private final File scriptFile;
private ShellScriptExecutor.Result result;
private Process process;
private ProcessExecutor(File scriptFile) {
this.scriptFile = scriptFile;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void run() {
InputStream stdout = null;
InputStream stderr = null;
OutputStream stdin = null;
try {
this.process = Runtime.getRuntime().exec(new String[]{this.scriptFile.getPath()}, null, this.scriptFile.getParentFile());
stdout = this.process.getInputStream();
stderr = this.process.getErrorStream();
stdin = this.process.getOutputStream();
this.process.waitFor();
String stdoutStr = null;
try {
stdoutStr = IOUtils.toString((InputStream)stdout, (String)"utf-8");
}
catch (IOException e) {
// empty catch block
}
String stderrStr = null;
try {
stderrStr = IOUtils.toString((InputStream)stderr, (String)"utf-8");
}
catch (IOException e) {
// empty catch block
}
this.result = new ResultImpl(this.process.exitValue(), stdoutStr, stderrStr);
this.process = null;
}
catch (Exception e) {
log.error("Error while executing script {}", (Object)this.scriptFile.getPath(), (Object)e);
}
finally {
IOUtils.closeQuietly((InputStream)stdout);
IOUtils.closeQuietly((OutputStream)stdin);
IOUtils.closeQuietly((InputStream)stderr);
}
}
public void kill() {
if (this.process != null) {
this.process.destroy();
}
}
public ShellScriptExecutor.Result getResult() {
return this.result;
}
}
}