blob: 1dcff28b6464ecc0694d36e36e39c504488429d4 [file] [log] [blame]
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.stc;
17
Thomas Vachuska86439372015-06-05 09:21:32 -070018import org.onlab.stc.Coordinator.Status;
19
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070020import java.io.BufferedReader;
21import java.io.File;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.PrintWriter;
26
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070027import static java.lang.String.format;
Thomas Vachuska86439372015-06-05 09:21:32 -070028import static org.onlab.stc.Coordinator.Status.FAILED;
29import static org.onlab.stc.Coordinator.Status.SUCCEEDED;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070030import static org.onlab.stc.Coordinator.print;
31
32/**
33 * Manages execution of the specified step or a group.
34 */
35class StepProcessor implements Runnable {
36
Thomas Vachuska86439372015-06-05 09:21:32 -070037 private static final String IGNORE_CODE = "~";
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070038 private static final int FAIL = -1;
39
40 static String launcher = "stc-launcher ";
41
42 private final Step step;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070043 private final File logDir;
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070044 private String command;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070045
46 private Process process;
47 private StepProcessListener delegate;
48
49 /**
50 * Creates a process monitor.
51 *
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070052 * @param step step or group to be executed
53 * @param logDir directory where step process log should be stored
54 * @param delegate process lifecycle listener
55 * @param command actual command to execute
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070056 */
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070057 StepProcessor(Step step, File logDir, StepProcessListener delegate,
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070058 String command) {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070059 this.step = step;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070060 this.logDir = logDir;
61 this.delegate = delegate;
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070062 this.command = command;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070063 }
64
65 @Override
66 public void run() {
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070067 delegate.onStart(step, command);
Thomas Vachuska86439372015-06-05 09:21:32 -070068 int code = execute();
69 boolean ignoreCode = step.env() != null && step.env.equals(IGNORE_CODE);
70 Status status = ignoreCode || code == 0 ? SUCCEEDED : FAILED;
71 delegate.onCompletion(step, status);
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070072 }
73
74 /**
75 * Executes the step process.
76 *
77 * @return exit code
78 */
79 private int execute() {
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070080 try (PrintWriter pw = new PrintWriter(logFile())) {
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070081 process = Runtime.getRuntime().exec(command());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070082 processOutput(pw);
83
84 // Wait for the process to complete and get its exit code.
85 if (process.isAlive()) {
86 process.waitFor();
87 }
88 return process.exitValue();
89
90 } catch (IOException e) {
91 print("Unable to run step %s using command %s", step.name(), step.command());
92 } catch (InterruptedException e) {
93 print("Step %s interrupted", step.name());
94 }
95 return FAIL;
96 }
97
98 /**
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070099 * Returns ready-to-run command for the step.
100 *
101 * @return command to execute
102 */
103 private String command() {
104 return format("%s %s %s %s", launcher,
105 step.env() != null ? step.env() : "-",
106 step.cwd() != null ? step.cwd() : "-",
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -0700107 command);
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700108 }
109
110 /**
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700111 * Captures output of the step process.
112 *
113 * @param pw print writer to send output to
114 * @throws IOException if unable to read output or write logs
115 */
116 private void processOutput(PrintWriter pw) throws IOException {
117 InputStream out = process.getInputStream();
118 BufferedReader br = new BufferedReader(new InputStreamReader(out));
119
120 // Slurp its combined stderr/stdout
121 String line;
122 while ((line = br.readLine()) != null) {
123 pw.println(line);
124 delegate.onOutput(step, line);
125 }
126 }
127
128 /**
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700129 * Returns the log file for the step output.
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700130 *
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700131 * @return log file
132 */
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700133 private File logFile() {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700134 return new File(logDir, step.name() + ".log");
135 }
136
137}