Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onlab.stc; |
| 17 | |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 18 | import org.onlab.stc.Coordinator.Status; |
| 19 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 20 | import java.io.BufferedReader; |
| 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.io.InputStreamReader; |
| 25 | import java.io.PrintWriter; |
| 26 | |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 27 | import static java.lang.String.format; |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 28 | import static org.onlab.stc.Coordinator.Status.FAILED; |
| 29 | import static org.onlab.stc.Coordinator.Status.SUCCEEDED; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 30 | import static org.onlab.stc.Coordinator.print; |
| 31 | |
| 32 | /** |
| 33 | * Manages execution of the specified step or a group. |
| 34 | */ |
| 35 | class StepProcessor implements Runnable { |
| 36 | |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 37 | private static final String IGNORE_CODE = "~"; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 38 | private static final int FAIL = -1; |
| 39 | |
| 40 | static String launcher = "stc-launcher "; |
| 41 | |
| 42 | private final Step step; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 43 | private final File logDir; |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 44 | private String command; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 45 | |
| 46 | private Process process; |
| 47 | private StepProcessListener delegate; |
| 48 | |
| 49 | /** |
| 50 | * Creates a process monitor. |
| 51 | * |
Thomas Vachuska | e2de8ee | 2015-08-25 16:14:28 -0700 | [diff] [blame^] | 52 | * @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 Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 56 | */ |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 57 | StepProcessor(Step step, File logDir, StepProcessListener delegate, |
Thomas Vachuska | e2de8ee | 2015-08-25 16:14:28 -0700 | [diff] [blame^] | 58 | String command) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 59 | this.step = step; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 60 | this.logDir = logDir; |
| 61 | this.delegate = delegate; |
Thomas Vachuska | e2de8ee | 2015-08-25 16:14:28 -0700 | [diff] [blame^] | 62 | this.command = command; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void run() { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 67 | delegate.onStart(step, command); |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 68 | 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 Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Executes the step process. |
| 76 | * |
| 77 | * @return exit code |
| 78 | */ |
| 79 | private int execute() { |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 80 | try (PrintWriter pw = new PrintWriter(logFile())) { |
Thomas Vachuska | e2de8ee | 2015-08-25 16:14:28 -0700 | [diff] [blame^] | 81 | process = Runtime.getRuntime().exec(command()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 82 | 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 Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 99 | * 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 Vachuska | e2de8ee | 2015-08-25 16:14:28 -0700 | [diff] [blame^] | 107 | command); |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 111 | * 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 Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 129 | * Returns the log file for the step output. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 130 | * |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 131 | * @return log file |
| 132 | */ |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 133 | private File logFile() { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 134 | return new File(logDir, step.name() + ".log"); |
| 135 | } |
| 136 | |
| 137 | } |