Skip to content

Commit 288f066

Browse files
stephankrugggStephan Krug
andauthored
Add logs to script execution (#6874)
Co-authored-by: Stephan Krug <[email protected]>
1 parent 235e4fe commit 288f066

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

utils/src/main/java/com/cloud/utils/script/Script.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,20 @@ static String stackTraceAsString(Throwable throwable) {
205205
public String execute(OutputInterpreter interpreter) {
206206
String[] command = _command.toArray(new String[_command.size()]);
207207

208-
if (_logger.isDebugEnabled()) {
209-
_logger.debug("Executing: " + buildCommandLine(command).split(KeyStoreUtils.KS_FILENAME)[0]);
210-
}
208+
String commandLine = buildCommandLine(command);
209+
_logger.debug(String.format("Executing command [%s].", commandLine.split(KeyStoreUtils.KS_FILENAME)[0]));
211210

212211
try {
212+
_logger.trace(String.format("Creating process for command [%s].", commandLine));
213213
ProcessBuilder pb = new ProcessBuilder(command);
214214
pb.redirectErrorStream(true);
215215
if (_workDir != null)
216216
pb.directory(new File(_workDir));
217217

218+
_logger.trace(String.format("Starting process for command [%s].", commandLine));
218219
_process = pb.start();
219220
if (_process == null) {
220-
_logger.warn("Unable to execute: " + buildCommandLine(command));
221+
_logger.warn(String.format("Unable to execute command [%s] because no process was created.", commandLine));
221222
return "Unable to execute the command: " + command[0];
222223
}
223224

@@ -226,40 +227,48 @@ public String execute(OutputInterpreter interpreter) {
226227
_thread = Thread.currentThread();
227228
ScheduledFuture<String> future = null;
228229
if (_timeout > 0) {
230+
_logger.trace(String.format("Scheduling the execution of command [%s] with a timeout of [%s] milliseconds.", commandLine, _timeout));
229231
future = s_executors.schedule(this, _timeout, TimeUnit.MILLISECONDS);
230232
}
231233

234+
long processPid = _process.pid();
232235
Task task = null;
233236
if (interpreter != null && interpreter.drain()) {
237+
_logger.trace(String.format("Executing interpreting task of process [%s] for command [%s].", processPid, commandLine));
234238
task = new Task(interpreter, ir);
235239
s_executors.execute(task);
236240
}
237241

238242
while (true) {
239-
_logger.debug("Executing while with timeout : " + _timeout);
243+
_logger.trace(String.format("Attempting process [%s] execution for command [%s] with timeout [%s].", processPid, commandLine, _timeout));
240244
try {
241-
//process execution completed within timeout period
242245
if (_process.waitFor(_timeout, TimeUnit.MILLISECONDS)) {
243-
//process completed successfully
246+
_logger.trace(String.format("Process [%s] execution for command [%s] completed within timeout period [%s].", processPid, commandLine,
247+
_timeout));
244248
if (_process.exitValue() == 0) {
245-
_logger.debug("Execution is successful.");
249+
_logger.debug(String.format("Successfully executed process [%s] for command [%s].", processPid, commandLine));
246250
if (interpreter != null) {
247-
return interpreter.drain() ? task.getResult() : interpreter.interpret(ir);
251+
if (interpreter.drain()) {
252+
_logger.trace(String.format("Returning task result of process [%s] for command [%s].", processPid, commandLine));
253+
return task.getResult();
254+
}
255+
_logger.trace(String.format("Returning interpretation of process [%s] for command [%s].", processPid, commandLine));
256+
return interpreter.interpret(ir);
248257
} else {
249258
// null return exitValue apparently
259+
_logger.trace(String.format("Process [%s] for command [%s] exited with value [%s].", processPid, commandLine,
260+
_process.exitValue()));
250261
return String.valueOf(_process.exitValue());
251262
}
252-
} else { //process failed
263+
} else {
264+
_logger.warn(String.format("Execution of process [%s] for command [%s] failed.", processPid, commandLine));
253265
break;
254266
}
255-
} //timeout
267+
}
256268
} catch (InterruptedException e) {
257269
if (!_isTimeOut) {
258-
/*
259-
* This is not timeout, we are interrupted by others,
260-
* continue
261-
*/
262-
_logger.debug("We are interrupted but it's not a timeout, just continue");
270+
_logger.debug(String.format("Exception [%s] occurred; however, it was not a timeout. Therefore, proceeding with the execution of process [%s] for command "
271+
+ "[%s].", e.getMessage(), processPid, commandLine), e);
263272
continue;
264273
}
265274
} finally {
@@ -269,21 +278,21 @@ public String execute(OutputInterpreter interpreter) {
269278
Thread.interrupted();
270279
}
271280

272-
//timeout without completing the process
273281
TimedOutLogger log = new TimedOutLogger(_process);
274282
Task timedoutTask = new Task(log, ir);
275283

284+
_logger.trace(String.format("Running timed out task of process [%s] for command [%s].", processPid, commandLine));
276285
timedoutTask.run();
277286
if (!_passwordCommand) {
278-
_logger.warn("Timed out: " + buildCommandLine(command) + ". Output is: " + timedoutTask.getResult());
287+
_logger.warn(String.format("Process [%s] for command [%s] timed out. Output is [%s].", processPid, commandLine, timedoutTask.getResult()));
279288
} else {
280-
_logger.warn("Timed out: " + buildCommandLine(command));
289+
_logger.warn(String.format("Process [%s] for command [%s] timed out.", processPid, commandLine));
281290
}
282291

283292
return ERR_TIMEOUT;
284293
}
285294

286-
_logger.debug("Exit value is " + _process.exitValue());
295+
_logger.debug(String.format("Exit value of process [%s] for command [%s] is [%s].", processPid, commandLine, _process.exitValue()));
287296

288297
BufferedReader reader = new BufferedReader(new InputStreamReader(_process.getInputStream()), 128);
289298

@@ -294,18 +303,19 @@ public String execute(OutputInterpreter interpreter) {
294303
error = String.valueOf(_process.exitValue());
295304
}
296305

297-
if (_logger.isDebugEnabled()) {
298-
_logger.debug(error);
299-
}
306+
_logger.warn(String.format("Process [%s] for command [%s] encountered the error: [%s].", processPid, commandLine, error));
307+
300308
return error;
301309
} catch (SecurityException ex) {
302-
_logger.warn("Security Exception....not running as root?", ex);
310+
_logger.warn(String.format("Exception [%s] occurred. This may be due to an attempt of executing command [%s] as non root.", ex.getMessage(), commandLine),
311+
ex);
303312
return stackTraceAsString(ex);
304313
} catch (Exception ex) {
305-
_logger.warn("Exception: " + buildCommandLine(command), ex);
314+
_logger.warn(String.format("Exception [%s] occurred when attempting to run command [%s].", ex.getMessage(), commandLine), ex);
306315
return stackTraceAsString(ex);
307316
} finally {
308317
if (_process != null) {
318+
_logger.trace(String.format("Destroying process [%s] for command [%s].", _process.pid(), commandLine));
309319
IOUtils.closeQuietly(_process.getErrorStream());
310320
IOUtils.closeQuietly(_process.getOutputStream());
311321
IOUtils.closeQuietly(_process.getInputStream());

0 commit comments

Comments
 (0)