blob: 499436916134372e327b7dcc22f2a1d350cdbeab [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 Vachuska14eaba82015-08-31 16:13:07 -070038 private static final String NEGATE_CODE = "!";
39
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070040 private static final int FAIL = -1;
41
42 static String launcher = "stc-launcher ";
43
44 private final Step step;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070045 private final File logDir;
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070046 private String command;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070047
48 private Process process;
49 private StepProcessListener delegate;
50
51 /**
52 * Creates a process monitor.
53 *
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070054 * @param step step or group to be executed
55 * @param logDir directory where step process log should be stored
56 * @param delegate process lifecycle listener
57 * @param command actual command to execute
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070058 */
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070059 StepProcessor(Step step, File logDir, StepProcessListener delegate,
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070060 String command) {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070061 this.step = step;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070062 this.logDir = logDir;
63 this.delegate = delegate;
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070064 this.command = command;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070065 }
66
67 @Override
68 public void run() {
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070069 delegate.onStart(step, command);
Thomas Vachuska86439372015-06-05 09:21:32 -070070 int code = execute();
71 boolean ignoreCode = step.env() != null && step.env.equals(IGNORE_CODE);
Thomas Vachuska14eaba82015-08-31 16:13:07 -070072 boolean negateCode = step.env() != null && step.env.equals(NEGATE_CODE);
73 Status status = ignoreCode || code == 0 && !negateCode || code != 0 && negateCode ?
74 SUCCEEDED : FAILED;
Thomas Vachuska86439372015-06-05 09:21:32 -070075 delegate.onCompletion(step, status);
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070076 }
77
78 /**
79 * Executes the step process.
80 *
81 * @return exit code
82 */
83 private int execute() {
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070084 try (PrintWriter pw = new PrintWriter(logFile())) {
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -070085 process = Runtime.getRuntime().exec(command());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070086 processOutput(pw);
87
88 // Wait for the process to complete and get its exit code.
89 if (process.isAlive()) {
90 process.waitFor();
91 }
92 return process.exitValue();
93
94 } catch (IOException e) {
95 print("Unable to run step %s using command %s", step.name(), step.command());
96 } catch (InterruptedException e) {
97 print("Step %s interrupted", step.name());
98 }
99 return FAIL;
100 }
101
102 /**
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700103 * Returns ready-to-run command for the step.
104 *
105 * @return command to execute
106 */
107 private String command() {
108 return format("%s %s %s %s", launcher,
109 step.env() != null ? step.env() : "-",
110 step.cwd() != null ? step.cwd() : "-",
Thomas Vachuskae2de8ee2015-08-25 16:14:28 -0700111 command);
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700112 }
113
114 /**
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700115 * Captures output of the step process.
116 *
117 * @param pw print writer to send output to
118 * @throws IOException if unable to read output or write logs
119 */
120 private void processOutput(PrintWriter pw) throws IOException {
121 InputStream out = process.getInputStream();
122 BufferedReader br = new BufferedReader(new InputStreamReader(out));
123
124 // Slurp its combined stderr/stdout
125 String line;
126 while ((line = br.readLine()) != null) {
127 pw.println(line);
128 delegate.onOutput(step, line);
129 }
130 }
131
132 /**
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700133 * Returns the log file for the step output.
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700134 *
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700135 * @return log file
136 */
Thomas Vachuska4bfccd542015-05-30 00:35:25 -0700137 private File logFile() {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700138 return new File(logDir, step.name() + ".log");
139 }
140
141}